Search in sources :

Example 41 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage 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 42 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveClockConverter method receiveCommand.

/**
     * {@inheritDoc}
     */
@Override
public void receiveCommand(Item item, Command command, ZWaveNode node, ZWaveClockCommandClass commandClass, int endpointId, Map<String, String> arguments) {
    // It's not an ON command from a button switch, do not set
    if (command != OnOffType.ON) {
        return;
    }
    // get the set message - will return null if not supported
    SerialMessage serialMessage = node.encapsulate(commandClass.getSetMessage(Calendar.getInstance()), commandClass, endpointId);
    if (serialMessage == null) {
        logger.warn("NODE {}: Meter reset not supported for item = {}, endpoint = {}, ignoring event.", node.getNodeId(), item.getName(), endpointId);
        return;
    }
    // send set message
    getController().sendData(serialMessage);
    // poll the device
    for (SerialMessage serialGetMessage : commandClass.getDynamicValues(true)) {
        getController().sendData(node.encapsulate(serialGetMessage, commandClass, endpointId));
    }
}
Also used : SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Example 43 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveConverterHandler method executeRefresh.

/**
     * Execute refresh method. This method is called every time a binding item
     * is refreshed and the corresponding node should be sent a message.
     *
     * @param provider
     *            the {@link ZWaveBindingProvider} that provides the item
     * @param itemName
     *            the name of the item to poll.
     * @param forceRefresh
     *            indicates that a polling refresh should be forced.
     */
@SuppressWarnings("unchecked")
public void executeRefresh(ZWaveBindingProvider provider, String itemName, boolean forceRefresh) {
    ZWaveBindingConfig bindingConfiguration = provider.getZwaveBindingConfig(itemName);
    ZWaveCommandClass commandClass;
    String commandClassName = bindingConfiguration.getArguments().get("command");
    // this binding is configured not to poll.
    if (!forceRefresh && bindingConfiguration.getRefreshInterval() != null && 0 == bindingConfiguration.getRefreshInterval()) {
        return;
    }
    ZWaveNode node = this.controller.getNode(bindingConfiguration.getNodeId());
    // ignore nodes that are not initialized.
    if (node == null) {
        return;
    }
    if (commandClassName != null) {
        // this is a report item, handle it with the report info converter.
        if (commandClassName.equalsIgnoreCase("info")) {
            infoConverter.executeRefresh(provider.getItem(itemName), node, bindingConfiguration.getEndpoint(), bindingConfiguration.getArguments());
            return;
        }
        // ignore nodes that are not initialized or dead.
        if (node.getNodeState() != ZWaveNodeState.ALIVE || node.isInitializationComplete() == false) {
            return;
        }
        commandClass = node.resolveCommandClass(CommandClass.getCommandClass(commandClassName), bindingConfiguration.getEndpoint());
        if (commandClass == null) {
            logger.warn("No command class found for item = {}, command class name = {}, ignoring execute refresh.", itemName, commandClassName);
            return;
        }
    } else {
        commandClass = resolveConverter(provider.getItem(itemName), node, bindingConfiguration.getEndpoint());
    }
    if (commandClass == null) {
        logger.warn("No converter found for item = {}, ignoring execute refresh.", itemName);
        return;
    }
    ZWaveCommandClassConverter<ZWaveCommandClass> converter = (ZWaveCommandClassConverter<ZWaveCommandClass>) getConverter(commandClass.getCommandClass());
    if (converter == null) {
        logger.warn("No converter found for item = {}, ignoring execute refresh.", itemName);
        return;
    }
    if (bindingConfiguration.getRefreshInterval() == null) {
        bindingConfiguration.setRefreshInterval(converter.getRefreshInterval());
        // this binding is configured not to poll.
        if (!forceRefresh && 0 == bindingConfiguration.getRefreshInterval()) {
            return;
        }
    }
    // not enough time has passed to refresh the item.
    if (!forceRefresh && bindingConfiguration.getLastRefreshed() != null && (bindingConfiguration.getLastRefreshed().getTime() + (bindingConfiguration.getRefreshInterval() * 1000) > Calendar.getInstance().getTimeInMillis())) {
        return;
    }
    bindingConfiguration.setLastRefreshed(Calendar.getInstance().getTime());
    SerialMessage serialMessage = converter.executeRefresh(node, commandClass, bindingConfiguration.getEndpoint(), bindingConfiguration.getArguments());
    if (serialMessage == null) {
        logger.warn("NODE {}: Generating message failed for command class = {}", node.getNodeId(), commandClass.getCommandClass().getLabel());
        return;
    }
    // This is a poll - treat it as a low priority!
    serialMessage.setPriority(SerialMessagePriority.Poll);
    // Queue the message
    this.controller.sendData(serialMessage);
}
Also used : ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveBindingConfig(org.openhab.binding.zwave.ZWaveBindingConfig) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Example 44 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage 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)

Example 45 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveBasicConverter method receiveCommand.

/**
     * {@inheritDoc}
     */
@Override
public void receiveCommand(Item item, Command command, ZWaveNode node, ZWaveBasicCommandClass commandClass, int endpointId, Map<String, String> arguments) {
    ZWaveCommandConverter<?, ?> converter = this.getCommandConverter(command.getClass());
    if (converter == null) {
        logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring command.", item.getName(), node.getNodeId(), endpointId);
        return;
    }
    SerialMessage serialMessage = node.encapsulate(commandClass.setValueMessage((Integer) converter.convertFromCommandToValue(item, command)), 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);
    if (command instanceof State) {
        this.getEventPublisher().postUpdate(item.getName(), (State) command);
    }
}
Also used : State(org.openhab.core.types.State) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Aggregations

SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)125 State (org.openhab.core.types.State)12 ZWaveEndpoint (org.openhab.binding.zwave.internal.protocol.ZWaveEndpoint)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 HashMap (java.util.HashMap)5 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)5 ZWaveWakeUpCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ConfigurationParameter (org.openhab.binding.zwave.internal.protocol.ConfigurationParameter)3 SecurityEncapsulatedSerialMessage (org.openhab.binding.zwave.internal.protocol.SecurityEncapsulatedSerialMessage)3 ZWaveCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ZWaveDbConfigurationParameter (org.openhab.binding.zwave.internal.config.ZWaveDbConfigurationParameter)2 ZWaveProductDatabase (org.openhab.binding.zwave.internal.config.ZWaveProductDatabase)2 MultiLevelPercentCommandConverter (org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter)2 ZWaveAssociationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass)2 ZWaveConfigurationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass)2 ZWaveNetworkEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNetworkEvent)2