Search in sources :

Example 36 with OnOffType

use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.

the class CULIntertechnoBinding method internalReceiveCommand.

/**
     *
     * @{inheritDoc
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    IntertechnoBindingConfig config = null;
    for (CULIntertechnoBindingProvider provider : providers) {
        config = provider.getConfigForItemName(itemName);
        if (config != null) {
            break;
        }
    }
    if (config != null && culHandlerLifecycle.isCulReady() && command instanceof OnOffType) {
        OnOffType type = (OnOffType) command;
        String commandValue = null;
        switch(type) {
            case ON:
                commandValue = config.getCommandValueON();
                break;
            case OFF:
                commandValue = config.getCommandValueOFF();
                break;
        }
        if (commandValue != null) {
            try {
                culHandlerLifecycle.getCul().send("is" + config.getAddress() + commandValue);
            } catch (CULCommunicationException e) {
                logger.error("Can't write to CUL", e);
            }
        } else {
            logger.error("Can't determine value to send for command " + command.toString());
        }
    }
}
Also used : CULIntertechnoBindingProvider(org.openhab.binding.intertechno.CULIntertechnoBindingProvider) OnOffType(org.openhab.core.library.types.OnOffType) IntertechnoBindingConfig(org.openhab.binding.intertechno.IntertechnoBindingConfig) CULCommunicationException(org.openhab.io.transport.cul.CULCommunicationException)

Example 37 with OnOffType

use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.

the class HueBinding method computeCommandForItemOnBridge.

/**
     * Checks whether the command is for one of the configured Hue bulbs. If
     * this is the case, the command is translated to the corresponding action
     * which is then sent to the given bulb.
     *
     * @param command
     *            The command from the openHAB bus.
     * @param itemName
     *            The name of the targeted item.
     * @param bridge
     *            The Hue bridge the Hue bulb is connected to
     */
private void computeCommandForItemOnBridge(Command command, String itemName, HueBridge bridge) {
    HueBindingConfig deviceConfig = getConfigForItemName(itemName);
    if (deviceConfig == null) {
        return;
    }
    HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
    if (bulb == null) {
        bulb = new HueBulb(bridge, deviceConfig.getDeviceId());
        bulbCache.put(deviceConfig.getDeviceId(), bulb);
    }
    if (command instanceof OnOffType) {
        bulb.switchOn(OnOffType.ON.equals(command));
    }
    if (command instanceof HSBType) {
        HSBType hsbCommand = (HSBType) command;
        DecimalType hue = hsbCommand.getHue();
        PercentType sat = hsbCommand.getSaturation();
        PercentType bri = hsbCommand.getBrightness();
        bulb.colorizeByHSB(hue.doubleValue() / 360, sat.doubleValue() / 100, bri.doubleValue() / 100);
    }
    if (deviceConfig.getType().equals(BindingType.brightness) || deviceConfig.getType().equals(BindingType.rgb)) {
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            int resultingValue = bulb.increaseBrightness(deviceConfig.getStepSize());
            eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            int resultingValue = bulb.decreaseBrightness(deviceConfig.getStepSize());
            eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
        } else if ((command instanceof PercentType) && !(command instanceof HSBType)) {
            bulb.setBrightness((int) Math.round((double) HueBulb.MAX_BRIGHTNESS / (double) 100 * ((PercentType) command).intValue()));
        }
    }
    if (deviceConfig.getType().equals(BindingType.colorTemperature)) {
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            bulb.increaseColorTemperature(deviceConfig.getStepSize());
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            bulb.decreaseColorTemperature(deviceConfig.getStepSize());
        } else if (command instanceof PercentType) {
            bulb.setColorTemperature((int) Math.round((((double) 346 / (double) 100) * ((PercentType) command).intValue()) + 154));
        }
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) HueBulb(org.openhab.binding.hue.internal.hardware.HueBulb) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 38 with OnOffType

use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.

the class MpdBinding method determinePlayStateChange.

private void determinePlayStateChange(String playerId) {
    MPD daemon = findMPDInstance(playerId);
    if (daemon == null) {
        // we give that player another chance -> try to reconnect
        reconnect(playerId);
    }
    if (daemon != null) {
        try {
            MPDPlayer player = daemon.getMPDPlayer();
            // get the song object here
            PlayerStatus ps = player.getStatus();
            PlayerStatus curPs = playerStatusCache.get(playerId);
            if (curPs != null) {
                if (ps != curPs) {
                    logger.debug("Play state of player '{}' changed", playerId);
                    playerStatusCache.put(playerId, ps);
                    PlayerCommandTypeMapping reportTo;
                    OnOffType reportStatus;
                    // trigger song update
                    if (ps.equals(PlayerStatus.STATUS_PAUSED) || ps.equals(PlayerStatus.STATUS_STOPPED)) {
                        // stopped
                        reportTo = PlayerCommandTypeMapping.STOP;
                        reportStatus = OnOffType.OFF;
                    } else {
                        // playing
                        reportTo = PlayerCommandTypeMapping.PLAY;
                        reportStatus = OnOffType.ON;
                    }
                    broadcastPlayerStateChange(playerId, reportTo, reportStatus);
                } else {
                // nothing, same state
                }
            } else {
                playerStatusCache.put(playerId, ps);
            }
        } catch (MPDPlayerException pe) {
            logger.error("Error while updating player status for player '{}': {}", playerId, pe.getMessage());
        } catch (Exception e) {
            logger.warn("Failed to communicate with player '{}'", playerId);
        }
    } else {
        logger.warn("Player '{}' was not found or is not connected", playerId);
    }
}
Also used : MPD(org.bff.javampd.MPD) OnOffType(org.openhab.core.library.types.OnOffType) PlayerStatus(org.bff.javampd.MPDPlayer.PlayerStatus) MPDPlayerException(org.bff.javampd.exception.MPDPlayerException) MPDPlayer(org.bff.javampd.MPDPlayer) ConfigurationException(org.osgi.service.cm.ConfigurationException) MPDConnectionException(org.bff.javampd.exception.MPDConnectionException) MPDResponseException(org.bff.javampd.exception.MPDResponseException) JobExecutionException(org.quartz.JobExecutionException) MPDPlayerException(org.bff.javampd.exception.MPDPlayerException) SchedulerException(org.quartz.SchedulerException) UnknownHostException(java.net.UnknownHostException)

Example 39 with OnOffType

use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.

the class PLCLogoBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    // the code being executed when a command was sent on the openHAB
    // event bus goes here. This method is only called if one of the
    // BindingProviders provide a binding for the given 'itemName'.
    // Note itemname is the item name not the controller name/instance!
    //
    super.internalReceiveCommand(itemName, command);
    logger.debug("internalReceiveCommand() is called!");
    for (PLCLogoBindingProvider provider : providers) {
        if (!provider.providesBindingFor(itemName)) {
            continue;
        }
        PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
        if (!controllers.containsKey(config.getController())) {
            logger.warn("Invalid write requested for controller {}", config.getController());
            continue;
        }
        PLCLogoConfig controller = controllers.get(config.getController());
        PLCLogoMemoryConfig wr = config.getWR();
        int address = -1;
        try {
            address = wr.getAddress(controller.getModel());
        } catch (BindingConfigParseException exception) {
            logger.error("Invalid address for block {} on {}", wr.getBlockName(), controller);
            continue;
        }
        int bit = -1;
        try {
            bit = wr.getBit(controller.getModel());
        } catch (BindingConfigParseException exception) {
            logger.error("Invalid bit for block {} on {}", wr.getBlockName(), controller);
            continue;
        }
        if (!wr.isInRange(controller.getModel())) {
            logger.warn("Invalid write request for block {} at address {}", wr.getBlockName(), address);
            continue;
        }
        // Send command to the LOGO! controller memory
        S7Client LogoS7Client = controller.getS7Client();
        if (LogoS7Client == null) {
            logger.debug("No S7client for controller {} found", config.getController());
            continue;
        }
        final byte[] buffer = new byte[2];
        int size = wr.isDigital() ? 1 : 2;
        lock.lock();
        int result = LogoS7Client.ReadArea(S7.S7AreaDB, 1, address, size, buffer);
        logger.debug("Read word from logo memory: at {} {} bytes, result = {}", address, size, result);
        if (result == 0) {
            Item item = config.getItem();
            if (item instanceof NumberItem && !wr.isDigital()) {
                if (command instanceof DecimalType) {
                    int oldValue = S7.GetShortAt(buffer, 0);
                    int newValue = ((DecimalType) command).intValue();
                    S7.SetWordAt(buffer, 0, newValue);
                    logger.debug("Changed word at {} from {} to {}", address, oldValue, newValue);
                    result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
                    logger.debug("Wrote to memory at {} two bytes: [{}, {}]", address, buffer[0], buffer[1]);
                }
            } else if (item instanceof SwitchItem && wr.isDigital()) {
                if (command instanceof OnOffType) {
                    boolean oldValue = S7.GetBitAt(buffer, 0, bit) ? true : false;
                    boolean newValue = command == OnOffType.ON ? true : false;
                    S7.SetBitAt(buffer, 0, bit, newValue);
                    logger.debug("Changed bit {}.{} from {} to {}", address, bit, oldValue, newValue);
                    result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
                    logger.debug("Wrote to memory at {} one byte: [{}]", address, buffer[0]);
                }
            }
            // If nothing was written and read was ok, nothing will happen here
            if (result != 0) {
                logger.warn("Failed to write memory: {}. Reconnecting...", S7Client.ErrorText(result));
                ReconnectLogo(LogoS7Client);
            }
        } else {
            logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
            ReconnectLogo(LogoS7Client);
        }
        lock.unlock();
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) OnOffType(org.openhab.core.library.types.OnOffType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DecimalType(org.openhab.core.library.types.DecimalType) PLCLogoBindingProvider(org.openhab.binding.plclogo.PLCLogoBindingProvider) PLCLogoBindingConfig(org.openhab.binding.plclogo.PLCLogoBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem) S7Client(Moka7.S7Client)

Example 40 with OnOffType

use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.

the class OpenSprinklerBinding method internalReceiveCommand.

/**
     * @{inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    if (command instanceof OnOffType) {
        final OnOffType switchCommand = (OnOffType) command;
        final OpenSprinklerBindingProvider bindingProvider = findFirstMatchingBindingProvider(itemName, command);
        final int station = bindingProvider.getStationNumber(itemName);
        if (station < 0 || station >= numberOfStations) {
            logger.warn("Station " + station + " is not in the valid [" + 0 + ".." + numberOfStations + "] range");
            return;
        }
        switch(switchCommand) {
            case ON:
                openSprinkler.openStation(station);
                break;
            case OFF:
                openSprinkler.closeStation(station);
                break;
        }
        return;
    }
    if (command instanceof OpenClosedType) {
        // abort processing
        return;
    }
    logger.debug("Provided command " + command + " is not of type 'OnOffType' or 'OpenClosedType'");
}
Also used : OpenSprinklerBindingProvider(org.openhab.binding.opensprinkler.OpenSprinklerBindingProvider) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType)

Aggregations

OnOffType (org.openhab.core.library.types.OnOffType)50 DecimalType (org.openhab.core.library.types.DecimalType)22 PercentType (org.openhab.core.library.types.PercentType)15 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)12 OpenClosedType (org.openhab.core.library.types.OpenClosedType)11 StringType (org.openhab.core.library.types.StringType)11 UpDownType (org.openhab.core.library.types.UpDownType)9 IOException (java.io.IOException)8 State (org.openhab.core.types.State)7 StopMoveType (org.openhab.core.library.types.StopMoveType)6 DateTimeType (org.openhab.core.library.types.DateTimeType)5 HSBType (org.openhab.core.library.types.HSBType)5 UnknownHostException (java.net.UnknownHostException)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 RFXComException (org.openhab.binding.rfxcom.internal.RFXComException)3 Item (org.openhab.core.items.Item)3 SwitchItem (org.openhab.core.library.items.SwitchItem)3 Color (java.awt.Color)2 DatagramPacket (java.net.DatagramPacket)2 SocketTimeoutException (java.net.SocketTimeoutException)2