Search in sources :

Example 1 with ZWaveInclusionEvent

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

the class ZWaveConfiguration method ZWaveIncomingEvent.

/**
     * Event handler method for incoming Z-Wave events.
     *
     * @param event
     *            the incoming Z-Wave event.
     */
@Override
public void ZWaveIncomingEvent(ZWaveEvent event) {
    if (event instanceof ZWaveConfigurationParameterEvent) {
        // Write the node to disk
        ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
        nodeSerializer.SerializeNode(zController.getNode(event.getNodeId()));
        // We've received an updated configuration parameter
        // See if this is something in our 'pending' list and remove it
        PendingCfg.Remove(ZWaveCommandClass.CommandClass.CONFIGURATION.getKey(), event.getNodeId(), ((ZWaveConfigurationParameterEvent) event).getParameter().getIndex());
        return;
    }
    if (event instanceof ZWaveAssociationEvent) {
        // Write the node to disk
        ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
        nodeSerializer.SerializeNode(zController.getNode(event.getNodeId()));
        // See if this is something in our 'pending' list and remove it
        for (ZWaveNode node : zController.getNodes()) {
            PendingCfg.Remove(ZWaveCommandClass.CommandClass.ASSOCIATION.getKey(), event.getNodeId(), ((ZWaveAssociationEvent) event).getGroup(), node.getNodeId());
        }
        return;
    }
    if (event instanceof ZWaveWakeUpCommandClass.ZWaveWakeUpEvent) {
        // We only care about the wake-up report
        if (((ZWaveWakeUpCommandClass.ZWaveWakeUpEvent) event).getEvent() != ZWaveWakeUpCommandClass.WAKE_UP_INTERVAL_REPORT) {
            return;
        }
        // Write the node to disk
        ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
        nodeSerializer.SerializeNode(zController.getNode(event.getNodeId()));
        // Remove this node from the pending list
        PendingCfg.Remove(ZWaveCommandClass.CommandClass.WAKE_UP.getKey(), event.getNodeId());
        return;
    }
    if (event instanceof ZWaveSwitchAllCommandClass.ZWaveSwitchAllModeEvent) {
        ZWaveSwitchAllCommandClass.ZWaveSwitchAllModeEvent e = (ZWaveSwitchAllCommandClass.ZWaveSwitchAllModeEvent) event;
        // Write the node to disk
        ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
        nodeSerializer.SerializeNode(zController.getNode(event.getNodeId()));
        // Remove this node from the pending list
        PendingCfg.Remove(ZWaveCommandClass.CommandClass.SWITCH_ALL.getKey(), e.getNodeId());
        return;
    }
    if (event instanceof ZWaveInclusionEvent) {
        handleInclusionEvent((ZWaveInclusionEvent) event);
    }
}
Also used : ZWaveNodeSerializer(org.openhab.binding.zwave.internal.protocol.initialization.ZWaveNodeSerializer) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveInclusionEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent) ZWaveSwitchAllCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass) ZWaveConfigurationParameterEvent(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass.ZWaveConfigurationParameterEvent) ZWaveAssociationEvent(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass.ZWaveAssociationEvent) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)

Example 2 with ZWaveInclusionEvent

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

the class ApplicationUpdateMessageClass method handleRequest.

@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    int nodeId;
    boolean result = true;
    UpdateState updateState = UpdateState.getUpdateState(incomingMessage.getMessagePayloadByte(0));
    switch(updateState) {
        case NODE_INFO_RECEIVED:
            // We've received a NIF, and this contains the node ID.
            nodeId = incomingMessage.getMessagePayloadByte(1);
            logger.debug("NODE {}: Application update request. Node information received.", nodeId);
            int length = incomingMessage.getMessagePayloadByte(2);
            ZWaveNode node = zController.getNode(nodeId);
            if (node == null) {
                logger.debug("NODE {}: Application update request. Node not known!", nodeId);
                // This allows everyone to be notified.
                if (nodeId > 0 && nodeId <= 232) {
                    zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeDone, incomingMessage.getMessagePayloadByte(2)));
                }
                break;
            }
            node.resetResendCount();
            // Remember that we've received this so we can continue initialisation
            node.setApplicationUpdateReceived(true);
            // If we're finished initialisation, then we can treat this like a HAIL
            if (node.getNodeInitializationStage() == ZWaveNodeInitStage.DONE) {
                // If this node supports associations, then assume this should be handled through that mechanism
                if (node.getCommandClass(CommandClass.ASSOCIATION) == null) {
                    // If we receive an Application Update Request and the node is already
                    // fully initialised we assume this is a request to the controller to
                    // re-get the current node values
                    logger.debug("NODE {}: Application update request. Requesting node state.", nodeId);
                    zController.pollNode(node);
                }
            } else {
                List<Integer> nif = new ArrayList<Integer>();
                for (int i = 6; i < length + 3; i++) {
                    int data = incomingMessage.getMessagePayloadByte(i);
                    if (data == 0xef) {
                        // TODO: Implement control command classes
                        break;
                    }
                    logger.trace(String.format("NODE %d: Command class 0x%02X is supported.", nodeId, data));
                    // See if the command class already exists on the node
                    CommandClass commandClass = CommandClass.getCommandClass(data);
                    if (node.getCommandClass(commandClass) == null) {
                        // add it
                        ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(data, node, zController);
                        if (zwaveCommandClass != null) {
                            logger.trace(String.format("NODE %d: Application update request. Adding Command class %s.", nodeId, commandClass));
                            node.addCommandClass(zwaveCommandClass);
                        }
                    }
                }
                node.updateNIF(nif);
            }
            // Notify we received an info frame
            zController.notifyEventListeners(new ZWaveNodeInfoEvent(nodeId));
            // Treat the node information frame as a wakeup
            ZWaveWakeUpCommandClass wakeUp = (ZWaveWakeUpCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.WAKE_UP);
            if (wakeUp != null) {
                wakeUp.setAwake(true);
            }
            break;
        case NODE_INFO_REQ_FAILED:
            // Make sure we can correlate the request before we use the nodeId
            if (lastSentMessage.getMessageClass() != SerialMessageClass.RequestNodeInfo) {
                logger.warn("Got ApplicationUpdateMessage without request, ignoring. Last message was {}.", lastSentMessage.getMessageClass());
                return false;
            }
            // The failed message doesn't contain the node number, so use the info from the request.
            nodeId = lastSentMessage.getMessageNode();
            logger.debug("NODE {}: Application update request. Node Info Request Failed.", nodeId);
            // Handle retries
            if (--lastSentMessage.attempts >= 0) {
                logger.error("NODE {}: Got Node Info Request Failed. Requeueing", nodeId);
                zController.enqueue(lastSentMessage);
            } else {
                logger.warn("NODE {}: Node Info Request Failed 3x. Discarding message: {}", nodeId, lastSentMessage.toString());
            }
            // Transaction is not successful
            incomingMessage.setTransactionCanceled();
            result = false;
            break;
        default:
            logger.warn("TODO: Implement Application Update Request Handling of {} ({}).", updateState.getLabel(), updateState.getKey());
    }
    // Check if this completes the transaction
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return result;
}
Also used : ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveInclusionEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent) ArrayList(java.util.ArrayList) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNodeInfoEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeInfoEvent) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)

Example 3 with ZWaveInclusionEvent

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

the class AddNodeMessageClass method handleRequest.

@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    switch(incomingMessage.getMessagePayloadByte(1)) {
        case ADD_NODE_STATUS_LEARN_READY:
            logger.debug("Add Node: Learn ready.");
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeStart));
            break;
        case ADD_NODE_STATUS_NODE_FOUND:
            logger.debug("Add Node: New node found.");
            break;
        case ADD_NODE_STATUS_ADDING_SLAVE:
            logger.debug("NODE {}: Adding slave.", incomingMessage.getMessagePayloadByte(2));
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeSlaveFound, incomingMessage.getMessagePayloadByte(2)));
            break;
        case ADD_NODE_STATUS_ADDING_CONTROLLER:
            logger.debug("NODE {}: Adding controller.", incomingMessage.getMessagePayloadByte(2));
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeControllerFound, incomingMessage.getMessagePayloadByte(2)));
            break;
        case ADD_NODE_STATUS_PROTOCOL_DONE:
            logger.debug("Add Node: Protocol done.");
            break;
        case ADD_NODE_STATUS_DONE:
            logger.debug("Add Node: Done.");
            // If the node ID is 0, ignore!
            if (incomingMessage.getMessagePayloadByte(2) > 0 && incomingMessage.getMessagePayloadByte(2) <= 232) {
                zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeDone, incomingMessage.getMessagePayloadByte(2)));
            }
            break;
        case ADD_NODE_STATUS_FAILED:
            logger.debug("Add Node: Failed.");
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeFail));
            break;
        default:
            logger.debug("Unknown request ({}).", incomingMessage.getMessagePayloadByte(1));
            break;
    }
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return transactionComplete;
}
Also used : ZWaveInclusionEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent)

Example 4 with ZWaveInclusionEvent

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

the class RemoveNodeMessageClass method handleRequest.

@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    switch(incomingMessage.getMessagePayloadByte(1)) {
        case REMOVE_NODE_STATUS_LEARN_READY:
            logger.debug("Remove Node: Learn ready.");
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.ExcludeStart));
            break;
        case REMOVE_NODE_STATUS_NODE_FOUND:
            logger.debug("Remove Node: Node found for removal.");
            break;
        case REMOVE_NODE_STATUS_REMOVING_SLAVE:
            logger.debug("NODE {}: Removing slave.", incomingMessage.getMessagePayloadByte(2));
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.ExcludeSlaveFound, incomingMessage.getMessagePayloadByte(2)));
            break;
        case REMOVE_NODE_STATUS_REMOVING_CONTROLLER:
            logger.debug("NODE {}: Removing controller.", incomingMessage.getMessagePayloadByte(2));
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.ExcludeControllerFound, incomingMessage.getMessagePayloadByte(2)));
            break;
        case REMOVE_NODE_STATUS_DONE:
            logger.debug("NODE {}: Removed from network.", incomingMessage.getMessagePayloadByte(2));
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.ExcludeDone, incomingMessage.getMessagePayloadByte(2)));
            break;
        case REMOVE_NODE_STATUS_FAILED:
            logger.debug("Remove Node: Failed.");
            zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.ExcludeFail));
            break;
        default:
            logger.debug("Remove Node: Unknown request ({}).", incomingMessage.getMessagePayloadByte(1));
            break;
    }
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return transactionComplete;
}
Also used : ZWaveInclusionEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent)

Example 5 with ZWaveInclusionEvent

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

the class ZWaveController method notifyEventListeners.

/**
     * Notify our own event listeners of a Z-Wave event.
     *
     * @param event the event to send.
     */
public void notifyEventListeners(ZWaveEvent event) {
    logger.debug("NODE {}: Notifying event listeners: {}", event.getNodeId(), event.getClass().getSimpleName());
    ArrayList<ZWaveEventListener> copy = new ArrayList<ZWaveEventListener>(this.zwaveEventListeners);
    for (ZWaveEventListener listener : copy) {
        listener.ZWaveIncomingEvent(event);
    }
    // We also need to handle the inclusion internally within the controller
    if (event instanceof ZWaveInclusionEvent) {
        ZWaveInclusionEvent incEvent = (ZWaveInclusionEvent) event;
        switch(incEvent.getEvent()) {
            case // TODO: DB any potential negative consequences by changing from IncludeDone to
            IncludeSlaveFound:
                // IncludeSlaveFound?
                // Trigger by IncludeSlaveFound since security based devices need the initial exchange to take place
                // immediately or they will time out
                logger.debug("NODE {}: Including node.", incEvent.getNodeId());
                // First make sure this isn't an existing node
                if (getNode(incEvent.getNodeId()) != null) {
                    logger.debug("NODE {}: Newly included node already exists - not initialising.", incEvent.getNodeId());
                    break;
                }
                this.lastIncludeSlaveFoundEvent = incEvent;
                // Initialise the new node
                addNode(incEvent.getNodeId());
                break;
            case ExcludeDone:
                logger.debug("NODE {}: Excluding node.", incEvent.getNodeId());
                // Remove the node from the controller
                if (getNode(incEvent.getNodeId()) == null) {
                    logger.debug("NODE {}: Excluding node that doesn't exist.", incEvent.getNodeId());
                    break;
                }
                this.zwaveNodes.remove(incEvent.getNodeId());
                // Remove the XML file
                ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
                nodeSerializer.DeleteNode(event.getNodeId());
                break;
            default:
                break;
        }
    } else if (event instanceof ZWaveNetworkEvent) {
        ZWaveNetworkEvent networkEvent = (ZWaveNetworkEvent) event;
        switch(networkEvent.getEvent()) {
            case DeleteNode:
                if (getNode(networkEvent.getNodeId()) == null) {
                    logger.debug("NODE {}: Deleting a node that doesn't exist.", networkEvent.getNodeId());
                    break;
                }
                this.zwaveNodes.remove(networkEvent.getNodeId());
                // Remove the XML file
                ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
                nodeSerializer.DeleteNode(event.getNodeId());
                break;
            default:
                break;
        }
    } else if (event instanceof ZWaveNodeStatusEvent) {
        ZWaveNodeStatusEvent statusEvent = (ZWaveNodeStatusEvent) event;
        logger.debug("NODE {}: Node Status event - Node is {}", statusEvent.getNodeId(), statusEvent.getState());
        // Get the node
        ZWaveNode node = getNode(event.getNodeId());
        if (node == null) {
            logger.error("NODE {}: Node is unknown!", statusEvent.getNodeId());
            return;
        }
        // Handle node state changes
        switch(statusEvent.getState()) {
            case DEAD:
                break;
            case FAILED:
                break;
            case ALIVE:
                break;
        }
    }
}
Also used : ZWaveNodeStatusEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeStatusEvent) ZWaveNetworkEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveNetworkEvent) ZWaveNodeSerializer(org.openhab.binding.zwave.internal.protocol.initialization.ZWaveNodeSerializer) ZWaveInclusionEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent) ArrayList(java.util.ArrayList)

Aggregations

ZWaveInclusionEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent)6 ArrayList (java.util.ArrayList)2 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)2 ZWaveWakeUpCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)2 ZWaveNodeSerializer (org.openhab.binding.zwave.internal.protocol.initialization.ZWaveNodeSerializer)2 ZWaveAssociationEvent (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass.ZWaveAssociationEvent)1 ZWaveCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)1 CommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass)1 ZWaveConfigurationParameterEvent (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass.ZWaveConfigurationParameterEvent)1 ZWaveSwitchAllCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass)1 ZWaveNetworkEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNetworkEvent)1 ZWaveNodeInfoEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeInfoEvent)1 ZWaveNodeStatusEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeStatusEvent)1