Search in sources :

Example 1 with ZWaveIndicatorCommandClass

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

the class ZWaveIndicatorConverter method handleEvent.

/**
     * {@inheritDoc}
     *
     * Process Z-Wave Indicator Report events. Apply any pending changes (see receiveCommand) and if
     * indicator is changed in any way issue an INDICATOR_SET message to update the node's indicator value
     */
@Override
public void handleEvent(ZWaveCommandClassValueEvent event, Item item, Map<String, String> arguments) {
    ZWaveStateConverter<?, ?> converter = this.getStateConverter(item, event.getValue());
    if (converter == null) {
        logger.warn("NODE {}: No converter found for item = {}, ignoring event.", event.getNodeId(), item.getName());
        return;
    }
    if (!arguments.containsKey("bit")) {
        logger.error("NODE {}: You must specify an Indicator bit for item = {}, ignoring event.", event.getNodeId(), item.getName());
        return;
    }
    int bit = Integer.parseInt(arguments.get("bit"));
    ZWaveIndicatorCommandClassChangeEvent e = (ZWaveIndicatorCommandClassChangeEvent) event;
    // Get INDICATOR Command Class to check if node supports it and later to generate the INDICATOR_SET message
    // to the node to set the new indicator value
    ZWaveNode node = this.getController().getNode(event.getNodeId());
    ZWaveIndicatorCommandClass commandClass = (ZWaveIndicatorCommandClass) node.getCommandClass(CommandClass.INDICATOR);
    if (commandClass == null) {
        logger.warn("NODE {}: Indicator command Class not supported for Item = {}, ignoring event.", event.getNodeId(), item.getName());
        return;
    }
    // Assume no changes and get the actual indicator value just received from the node
    boolean madeChanges = false;
    int indicator = (Integer) e.getValue();
    logger.warn(String.format("NODE %d: Processing Indicator Event for Item %s indicator = 0x%02X, bit = %d.", event.getNodeId(), item.getName(), indicator, bit));
    // Check if the bound bit is ON (1) or OFF (0)
    int isOn = (((byte) indicator) >> bit) & 0x01;
    // Check for pending changes from previous receiveCommand() method calls
    if (pending.containsKey(item.getName())) {
        if (pending.get(item.getName()).containsKey(bit)) {
            logger.warn(String.format("NODE %d: There are pending changes to Item %s bit = %d", event.getNodeId(), item.getName(), bit));
            // check if there are any pending set to ON for the bound bit, and if the current bit value is OFF
            if (pending.get(item.getName()).get(bit) == PENDING_ON && isOn == OFF) {
                // Set bit to ON in current indicator value
                byte buttonMask = (byte) (0x01 << bit);
                indicator = (byte) (indicator | buttonMask);
                HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
                h.put(bit, NO_PENDING_CHANGES);
                pending.put(item.getName(), h);
                madeChanges = true;
                logger.warn(String.format("NODE %d: Item %s indicator = 0x%02X, Set bit = %d to ON - DONE", event.getNodeId(), item.getName(), indicator, bit));
            }
            if (pending.get(item.getName()).get(bit) == PENDING_OFF && isOn == ON) {
                // Set bit to OFF in current indicator value
                byte buttonMask = (byte) ~(0x01 << bit);
                indicator = (byte) (indicator & buttonMask);
                HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
                h.put(bit, NO_PENDING_CHANGES);
                pending.put(item.getName(), h);
                madeChanges = true;
                logger.warn(String.format("NODE %d: Item %s indicator = 0x%02X, Set bit = %d to OFF - DONE", event.getNodeId(), item.getName(), indicator, bit));
            }
            // Check if changes were made send an INDICATOR_SET message to update the node's indicator value
            if (madeChanges) {
                // recalculate because we made changes to Indicator
                isOn = (((byte) indicator) >> bit) & 0x01;
                // Create an Indicator SET message and send it to the node
                SerialMessage serialMessage = node.encapsulate(commandClass.setValueMessage(indicator), commandClass, 0);
                if (serialMessage == null) {
                    logger.warn("NODE {}: Generating message failed for command class = {}, endpoint = {}", node.getNodeId(), commandClass.getCommandClass().getLabel());
                    return;
                }
                this.getController().sendData(serialMessage);
                logger.warn(String.format("NODE %d: Item %s indicator = 0x%02X, Send pending operations to node", event.getNodeId(), item.getName(), indicator, bit));
            }
        } else {
            logger.warn(String.format("NODE %d: No pending indicator changes to Item %s bit = %d", event.getNodeId(), item.getName(), bit));
        }
    } else {
        logger.warn(String.format("NODE %d: No pending Indicator changes to Item %s bit = %d", event.getNodeId(), item.getName(), bit));
    }
    if (isOn == ON) {
        this.getEventPublisher().postUpdate(item.getName(), converter.convertFromValueToState(0xFF));
    } else {
        this.getEventPublisher().postUpdate(item.getName(), converter.convertFromValueToState(0x00));
    }
}
Also used : ZWaveIndicatorCommandClassChangeEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveIndicatorCommandClassChangeEvent) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) HashMap(java.util.HashMap) ZWaveIndicatorCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveIndicatorCommandClass) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Aggregations

HashMap (java.util.HashMap)1 SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)1 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)1 ZWaveIndicatorCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveIndicatorCommandClass)1 ZWaveIndicatorCommandClassChangeEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveIndicatorCommandClassChangeEvent)1