Search in sources :

Example 1 with MultiLevelPercentCommandConverter

use of org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter in project openhab1-addons by openhab.

the class ZWaveMultiLevelSwitchConverter method receiveCommand.

/**
     * {@inheritDoc}
     */
@Override
public void receiveCommand(Item item, Command command, ZWaveNode node, ZWaveMultiLevelSwitchCommandClass commandClass, int endpointId, Map<String, String> arguments) {
    SerialMessage serialMessage = null;
    String restoreLastValue = null;
    if (command instanceof StopMoveType && (StopMoveType) command == StopMoveType.STOP) {
        // special handling for the STOP command
        serialMessage = commandClass.stopLevelChangeMessage();
    } else {
        ZWaveCommandConverter<?, ?> converter = null;
        if (command instanceof OnOffType) {
            restoreLastValue = arguments.get("restore_last_value");
            if ("true".equalsIgnoreCase(restoreLastValue)) {
                converter = this.restoreValueOnOffConverter;
            } else {
                converter = this.normalOnOffConverter;
            }
        } else {
            converter = this.getCommandConverter(command.getClass());
        }
        if (converter == null) {
            logger.warn("NODE {}: No converter found for item = {}, endpoint = {}, ignoring command.", node.getNodeId(), item.getName(), endpointId);
            return;
        }
        // Allow inversion of roller shutter UP/DOWN
        if (converter instanceof MultiLevelUpDownCommandConverter) {
            logger.debug("Multilevel Switch MultiLevelUpDownCommandConverter");
            if ("true".equalsIgnoreCase(arguments.get("invert_state"))) {
                logger.trace("Multilevel Switch MultiLevelUpDownCommandConverter - invert");
                if (command == UpDownType.UP) {
                    command = UpDownType.DOWN;
                } else {
                    command = UpDownType.UP;
                }
                logger.trace("Multilevel Switch MultiLevelUpDownCommandConverter - inverted: {}", command);
            }
        }
        // Allow inversion of roller shutter PERCENT value
        if (converter instanceof MultiLevelPercentCommandConverter) {
            logger.debug("Multilevel Switch MultiLevelPercentCommandConverter");
            if ("true".equalsIgnoreCase(arguments.get("invert_percent"))) {
                logger.trace("Multilevel Switch MultiLevelPercentCommandConverter - invert");
                command = new PercentType(100 - ((DecimalType) command).intValue());
                logger.trace("Multilevel Switch MultiLevelPercentCommandConverter - inverted: {}", command);
            }
        }
        Integer value = (Integer) converter.convertFromCommandToValue(item, command);
        logger.trace("NODE {}: Converted command '{}' to value {} for item = {}, endpoint = {}.", node.getNodeId(), command.toString(), value, item.getName(), endpointId);
        serialMessage = commandClass.setValueMessage(value);
    }
    // encapsulate the message in case this is a multi-instance node
    serialMessage = node.encapsulate(serialMessage, commandClass, endpointId);
    if (serialMessage == null) {
        logger.warn("Generating message failed for command class = {}, node = {}, endpoint = {}", commandClass.getCommandClass().getLabel(), node.getNodeId(), endpointId);
        return;
    }
    this.getController().sendData(serialMessage);
    // update the bus in case of normal dimming. schedule refresh in case of restore to last value dimming.
    if (!"true".equalsIgnoreCase(restoreLastValue) && command instanceof OnOffType && (OnOffType) command == OnOffType.ON) {
        executeRefresh(node, commandClass, endpointId, arguments);
    } else if (command instanceof State) {
        this.getEventPublisher().postUpdate(item.getName(), (State) command);
    }
}
Also used : MultiLevelUpDownCommandConverter(org.openhab.binding.zwave.internal.converter.command.MultiLevelUpDownCommandConverter) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage) PercentType(org.openhab.core.library.types.PercentType) StopMoveType(org.openhab.core.library.types.StopMoveType) MultiLevelPercentCommandConverter(org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter)

Example 2 with MultiLevelPercentCommandConverter

use of org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter in project openhab1-addons by openhab.

the class FibaroFGRM222Converter method receiveCommand.

@Override
void receiveCommand(final Item item, final Command command, final ZWaveNode node, final FibaroFGRM222CommandClass commandClass, final int endpointId, final Map<String, String> arguments) {
    logger.debug("NODE {}: receiveCommand()", node.getNodeId());
    Command internalCommand = command;
    SerialMessage serialMessage = null;
    if (internalCommand instanceof StopMoveType && (StopMoveType) internalCommand == StopMoveType.STOP) {
        // special handling for the STOP command
        serialMessage = commandClass.stopLevelChangeMessage(arguments.get("type"));
    } else {
        ZWaveCommandConverter<?, ?> converter = this.getCommandConverter(command.getClass());
        if (converter == null) {
            logger.warn("NODE {}: No converter found for item = {}, endpoint = {}, ignoring command.", node.getNodeId(), item.getName(), endpointId);
            return;
        }
        if (converter instanceof MultiLevelPercentCommandConverter) {
            internalCommand = new PercentType(100 - ((DecimalType) command).intValue());
        }
        Integer value = (Integer) converter.convertFromCommandToValue(item, internalCommand);
        if (value == 0) {
            value = 1;
        }
        logger.trace("NODE {}: Converted command '{}' to value {} for item = {}, endpoint = {}.", node.getNodeId(), internalCommand.toString(), value, item.getName(), endpointId);
        serialMessage = commandClass.setValueMessage(value, arguments.get("type"));
    }
    // encapsulate the message in case this is a multi-instance node
    serialMessage = node.encapsulate(serialMessage, commandClass, endpointId);
    if (serialMessage == null) {
        logger.warn("NODE {}: Generating message failed for command class = {}, node = {}, endpoint = {}", node.getNodeId(), commandClass.getCommandClass().getLabel(), endpointId);
        return;
    }
    this.getController().sendData(serialMessage);
    if (command instanceof State) {
        this.getEventPublisher().postUpdate(item.getName(), (State) command);
    }
}
Also used : Command(org.openhab.core.types.Command) State(org.openhab.core.types.State) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage) PercentType(org.openhab.core.library.types.PercentType) StopMoveType(org.openhab.core.library.types.StopMoveType) MultiLevelPercentCommandConverter(org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter)

Aggregations

MultiLevelPercentCommandConverter (org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter)2 SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)2 PercentType (org.openhab.core.library.types.PercentType)2 StopMoveType (org.openhab.core.library.types.StopMoveType)2 State (org.openhab.core.types.State)2 MultiLevelUpDownCommandConverter (org.openhab.binding.zwave.internal.converter.command.MultiLevelUpDownCommandConverter)1 OnOffType (org.openhab.core.library.types.OnOffType)1 Command (org.openhab.core.types.Command)1