Search in sources :

Example 41 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 42 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 43 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 44 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)

Example 45 with OnOffType

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

the class IntegraStateBindingConfig method convertCommand.

/**
     * {@inheritDoc}
     */
@Override
public SatelCommand convertCommand(Command command, IntegraType integraType, String userCode) {
    if (command instanceof OnOffType && this.objectNumbers.length == 1) {
        boolean switchOn = ((OnOffType) command == OnOffType.ON);
        boolean force_arm = hasOptionEnabled(Options.FORCE_ARM);
        switch(this.stateType.getObjectType()) {
            case OUTPUT:
                byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16);
                boolean invertState = hasOptionEnabled(Options.INVERT_STATE);
                return new ControlObjectCommand(switchOn ^ invertState ? OutputControl.ON : OutputControl.OFF, outputs, userCode);
            case DOORS:
                // for doors you can also control outputs of type 101, but we do not support this feature
                // anyway we need to send list of outputs, so we have 'dummy' array for this purpose
                byte[] doors = getObjectBitset(8);
                byte[] dummy = new byte[(integraType == IntegraType.I256_PLUS) ? 32 : 16];
                if (switchOn) {
                    return new ControlObjectCommand(DoorsControl.OPEN, ArrayUtils.addAll(dummy, doors), userCode);
                } else {
                    return null;
                }
            case ZONE:
                byte[] zones = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16);
                switch((ZoneState) this.stateType) {
                    case BYPASS:
                        return new ControlObjectCommand(switchOn ? ZoneControl.BYPASS : ZoneControl.UNBYPASS, zones, userCode);
                    case ISOLATE:
                        if (switchOn) {
                            return new ControlObjectCommand(ZoneControl.ISOLATE, zones, userCode);
                        } else {
                            return null;
                        }
                    default:
                        // do nothing for other types of state
                        break;
                }
                break;
            case PARTITION:
                byte[] partitions = getObjectBitset(4);
                switch((PartitionState) this.stateType) {
                    // clear alarms on OFF command
                    case ALARM:
                    case ALARM_MEMORY:
                    case FIRE_ALARM:
                    case FIRE_ALARM_MEMORY:
                    case VERIFIED_ALARMS:
                    case WARNING_ALARMS:
                        if (switchOn) {
                            return null;
                        } else {
                            return new ControlObjectCommand(PartitionControl.CLEAR_ALARM, partitions, userCode);
                        }
                    // arm or disarm, depending on command
                    case ARMED:
                    case REALLY_ARMED:
                        return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_0 : PartitionControl.ARM_MODE_0) : PartitionControl.DISARM, partitions, userCode);
                    case ARMED_MODE_1:
                        return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_1 : PartitionControl.ARM_MODE_1) : PartitionControl.DISARM, partitions, userCode);
                    case ARMED_MODE_2:
                        return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_2 : PartitionControl.ARM_MODE_2) : PartitionControl.DISARM, partitions, userCode);
                    case ARMED_MODE_3:
                        return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_3 : PartitionControl.ARM_MODE_3) : PartitionControl.DISARM, partitions, userCode);
                    default:
                        // do nothing for other types of state
                        break;
                }
        }
    } else if (this.stateType.getObjectType() == ObjectType.OUTPUT && this.objectNumbers.length == 2) {
        // roller shutter support
        if (command == UpDownType.UP) {
            byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16, 0);
            return new ControlObjectCommand(OutputControl.ON, outputs, userCode);
        } else if (command == UpDownType.DOWN) {
            byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16, 1);
            return new ControlObjectCommand(OutputControl.ON, outputs, userCode);
        } else if (command == StopMoveType.STOP) {
            byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16);
            return new ControlObjectCommand(OutputControl.OFF, outputs, userCode);
        }
    }
    return null;
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) ZoneState(org.openhab.binding.satel.internal.types.ZoneState) PartitionState(org.openhab.binding.satel.internal.types.PartitionState) ControlObjectCommand(org.openhab.binding.satel.command.ControlObjectCommand)

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