Search in sources :

Example 11 with ZWaveNode

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

the class GetRoutingInfoMessageClass method handleResponse.

@Override
public boolean handleResponse(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    int nodeId = lastSentMessage.getMessagePayloadByte(0);
    logger.debug("NODE {}: Got NodeRoutingInfo request.", nodeId);
    // Get the node
    ZWaveNode node = zController.getNode(nodeId);
    if (node == null) {
        logger.error("NODE {}: Routing information for unknown node", nodeId);
        incomingMessage.setTransactionCanceled();
        return false;
    }
    node.clearNeighbors();
    boolean hasNeighbors = false;
    for (int by = 0; by < NODE_BYTES; by++) {
        for (int bi = 0; bi < 8; bi++) {
            if ((incomingMessage.getMessagePayloadByte(by) & (0x01 << bi)) != 0) {
                hasNeighbors = true;
                // Add the node to the neighbor list
                node.addNeighbor((by << 3) + bi + 1);
            }
        }
    }
    if (!hasNeighbors) {
        logger.debug("NODE {}: No neighbors reported", nodeId);
    } else {
        String neighbors = "Neighbor nodes:";
        for (Integer neighborNode : node.getNeighbors()) {
            neighbors += " " + neighborNode;
        }
        logger.debug("NODE {}: {}", nodeId, neighbors);
    }
    zController.notifyEventListeners(new ZWaveNetworkEvent(ZWaveNetworkEvent.Type.NodeRoutingInfo, nodeId, ZWaveNetworkEvent.State.Success));
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return true;
}
Also used : ZWaveNetworkEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveNetworkEvent) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode)

Example 12 with ZWaveNode

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

the class ZWaveNodeSerializer method DeserializeNode.

/**
     * Deserializes an XML tree of a {@link ZWaveNode}
     *
     * @param nodeId
     *            the number of the node to deserialize
     * @return returns the Node or null in case Serialization failed.
     */
public ZWaveNode DeserializeNode(int nodeId) {
    synchronized (stream) {
        File file = new File(this.folderName, String.format("node%d.xml", nodeId));
        BufferedReader reader = null;
        logger.debug("NODE {}: Serializing from file {}", nodeId, file.getPath());
        if (!file.exists()) {
            logger.debug("NODE {}: Error serializing from file: file does not exist.", nodeId);
            return null;
        }
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            return (ZWaveNode) stream.fromXML(reader);
        } catch (IOException e) {
            logger.error("NODE {}: Error serializing from file: {}", nodeId, e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 13 with ZWaveNode

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

the class ApplicationCommandMessageClass method handleRequest.

@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    logger.trace("Handle Message Application Command Request");
    int nodeId = incomingMessage.getMessagePayloadByte(1);
    ZWaveNode node = zController.getNode(nodeId);
    if (node == null) {
        logger.warn("NODE {}: Not initialized yet, ignoring message.", nodeId);
        return false;
    }
    logger.debug("NODE {}: Application Command Request ({}:{})", nodeId, node.getNodeState().toString(), node.getNodeInitializationStage().toString());
    // If the node is DEAD, but we've just received a message from it, then it's not dead!
    if (node.isDead()) {
        node.setNodeState(ZWaveNodeState.ALIVE);
    }
    node.resetResendCount();
    node.incrementReceiveCount();
    int commandClassCode = incomingMessage.getMessagePayloadByte(3);
    ZWaveCommandClass zwaveCommandClass = resolveZWaveCommandClass(node, commandClassCode, zController);
    if (zwaveCommandClass == null) {
        // Error message was logged in resolveZWaveCommandClass
        return false;
    }
    final int commandByte = incomingMessage.getMessagePayloadByte(4);
    if (zwaveCommandClass instanceof ZWaveSecurityCommandClass && (ZWaveSecurityCommandClass.bytesAreEqual(ZWaveSecurityCommandClass.SECURITY_MESSAGE_ENCAP, commandByte) || ZWaveSecurityCommandClass.bytesAreEqual(ZWaveSecurityCommandClass.SECURITY_MESSAGE_ENCAP_NONCE_GET, commandByte))) {
        boolean isEncapNonceGet = ZWaveSecurityCommandClass.bytesAreEqual(ZWaveSecurityCommandClass.SECURITY_MESSAGE_ENCAP_NONCE_GET, commandByte);
        // Intercept security encapsulated messages here and decrypt them.
        ZWaveSecurityCommandClass zwaveSecurityCommandClass = (ZWaveSecurityCommandClass) zwaveCommandClass;
        logger.trace("NODE {}: Preparing to decrypt security encapsulated message, messagePayload={}", nodeId, SerialMessage.bb2hex(incomingMessage.getMessagePayload()));
        int toDecryptLength = incomingMessage.getMessageBuffer().length - 9;
        byte[] toDecrypt = new byte[toDecryptLength];
        System.arraycopy(incomingMessage.getMessageBuffer(), 8, toDecrypt, 0, toDecryptLength);
        byte[] decryptedBytes = zwaveSecurityCommandClass.decryptMessage(toDecrypt, 0);
        if (decryptedBytes == null) {
            logger.error("NODE {}: Failed to decrypt message out of {} .", nodeId, incomingMessage);
        } else {
            // call handleApplicationCommandRequest with the decrypted message. Note that we do NOT set
            // incomingMessage as that needs to be processed below with the original security encapsulated message
            final SerialMessage decryptedMessage = new SerialMessage(incomingMessage.getMessageClass(), incomingMessage.getMessageType(), incomingMessage.getExpectedReply(), incomingMessage.getPriority());
            decryptedMessage.setMessagePayload(decryptedBytes);
            // Get the new command class with the decrypted contents
            zwaveCommandClass = resolveZWaveCommandClass(node, decryptedBytes[1], zController);
            // Use a flag bc we need to handle isEncapNonceGet either way
            boolean failed = false;
            if (zwaveCommandClass == null) {
                // Error message was logged in resolveZWaveCommandClass
                failed = true;
            } else {
                // Note that we do not call node.doesMessageRequireSecurityEncapsulation since it was encapsulated.
                // Messages that are not required to be are allowed to be, just not the other way around
                logger.debug("NODE {}: After decrypt, found Command Class {}, passing to handleApplicationCommandRequest", nodeId, zwaveCommandClass.getCommandClass().getLabel());
                zwaveCommandClass.handleApplicationCommandRequest(decryptedMessage, 2, 0);
            }
            if (isEncapNonceGet) {
                // the device also needs another nonce; send it regardless of the success/failure of decryption
                zwaveSecurityCommandClass.sendNonceReport();
            }
            if (failed) {
                return false;
            }
        }
    } else {
        // Message does not require decryption
        if (node.doesMessageRequireSecurityEncapsulation(incomingMessage)) {
            // Should have been security encapsulation but wasn't!
            logger.error("NODE {}: Command Class {} {} was required to be security encapsulation but it wasn't!  Dropping message.", nodeId, zwaveCommandClass.getCommandClass().getKey(), zwaveCommandClass.getCommandClass().getLabel());
        // do not call zwaveCommandClass.handleApplicationCommandRequest();
        } else {
            logger.trace("NODE {}: Found Command Class {}, passing to handleApplicationCommandRequest", nodeId, zwaveCommandClass.getCommandClass().getLabel());
            zwaveCommandClass.handleApplicationCommandRequest(incomingMessage, 4, 0);
        }
    }
    if (node.getNodeId() == lastSentMessage.getMessageNode()) {
        checkTransactionComplete(lastSentMessage, incomingMessage);
    } else {
        logger.debug("NODE {}: Transaction not completed: node address inconsistent.  lastSent={}, incoming={}", lastSentMessage.getMessageNode(), lastSentMessage.getMessageNode(), incomingMessage.getMessageNode());
    }
    return true;
}
Also used : ZWaveSecurityCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSecurityCommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Example 14 with ZWaveNode

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

the class IdentifyNodeMessageClass method handleResponse.

@Override
public boolean handleResponse(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    logger.trace("Handle Message Get Node ProtocolInfo Response");
    // Check that this request is consistent with the response
    if (lastSentMessage.getMessageClass() != SerialMessageClass.IdentifyNode) {
        logger.warn("Got IdentifyNodeMessage without request, ignoring. Last message was {}.", lastSentMessage.getMessageClass());
        return false;
    }
    int nodeId = lastSentMessage.getMessagePayloadByte(0);
    logger.debug("NODE {}: ProtocolInfo", nodeId);
    ZWaveNode node = zController.getNode(nodeId);
    boolean listening = (incomingMessage.getMessagePayloadByte(0) & 0x80) != 0 ? true : false;
    boolean routing = (incomingMessage.getMessagePayloadByte(0) & 0x40) != 0 ? true : false;
    int version = (incomingMessage.getMessagePayloadByte(0) & 0x07) + 1;
    boolean frequentlyListening = (incomingMessage.getMessagePayloadByte(1) & 0x60) != 0 ? true : false;
    boolean beaming = ((incomingMessage.getMessagePayloadByte(1) & 0x10) != 0);
    boolean security = ((incomingMessage.getMessagePayloadByte(1) & 0x01) != 0);
    int maxBaudRate = 9600;
    if ((incomingMessage.getMessagePayloadByte(0) & 0x38) == 0x10) {
        maxBaudRate = 40000;
    }
    logger.debug("NODE {}: Listening = {}", nodeId, listening);
    logger.debug("NODE {}: Routing = {}", nodeId, routing);
    logger.debug("NODE {}: Beaming = {}", nodeId, beaming);
    logger.debug("NODE {}: Version = {}", nodeId, version);
    logger.debug("NODE {}: FLIRS = {}", nodeId, frequentlyListening);
    logger.debug("NODE {}: Security = {}", nodeId, security);
    logger.debug("NODE {}: Max Baud = {}", nodeId, maxBaudRate);
    node.setListening(listening);
    node.setRouting(routing);
    node.setVersion(version);
    node.setFrequentlyListening(frequentlyListening);
    node.setSecurity(security);
    node.setBeaming(beaming);
    node.setMaxBaud(maxBaudRate);
    Basic basic = Basic.getBasic(incomingMessage.getMessagePayloadByte(3));
    if (basic == null) {
        logger.error(String.format("NODE %d: Basic device class 0x%02x not found", nodeId, incomingMessage.getMessagePayloadByte(3)));
        return false;
    }
    logger.debug("NODE {}: Basic = {}", nodeId, basic.getLabel());
    Generic generic = Generic.getGeneric(incomingMessage.getMessagePayloadByte(4));
    if (generic == null) {
        logger.error(String.format("NODE %d: Generic device class 0x%02x not found", nodeId, incomingMessage.getMessagePayloadByte(4)));
        return false;
    }
    logger.debug("NODE {}: Generic = {}", nodeId, generic.getLabel());
    Specific specific = Specific.getSpecific(generic, incomingMessage.getMessagePayloadByte(5));
    if (specific == null) {
        logger.error(String.format("NODE %d: Specific device class 0x%02x not found", nodeId, incomingMessage.getMessagePayloadByte(5)));
        return false;
    }
    logger.debug("NODE {}: Specific = {}", nodeId, specific.getLabel());
    ZWaveDeviceClass deviceClass = node.getDeviceClass();
    deviceClass.setBasicDeviceClass(basic);
    deviceClass.setGenericDeviceClass(generic);
    deviceClass.setSpecificDeviceClass(specific);
    // Add mandatory command classes as specified by its generic device class.
    for (CommandClass commandClass : generic.getMandatoryCommandClasses()) {
        ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(commandClass.getKey(), node, zController);
        if (zwaveCommandClass != null) {
            zController.getNode(nodeId).addCommandClass(zwaveCommandClass);
        }
    }
    // Add mandatory command classes as specified by its specific device class.
    for (CommandClass commandClass : specific.getMandatoryCommandClasses()) {
        ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(commandClass.getKey(), node, zController);
        if (zwaveCommandClass != null) {
            node.addCommandClass(zwaveCommandClass);
        }
    }
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return true;
}
Also used : Basic(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Basic) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) Generic(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Generic) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) Specific(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Specific) ZWaveDeviceClass(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass)

Example 15 with ZWaveNode

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

the class IsFailedNodeMessageClass method handleResponse.

@Override
public boolean handleResponse(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    int nodeId = lastSentMessage.getMessagePayloadByte(0);
    ZWaveNode node = zController.getNode(nodeId);
    if (node == null) {
        logger.error("NODE {}: Failed node message for unknown node", nodeId);
        incomingMessage.setTransactionCanceled();
        return false;
    }
    if (incomingMessage.getMessagePayloadByte(0) != 0x00) {
        logger.warn("NODE {}: Is currently marked as failed by the controller!", nodeId);
        node.setNodeState(ZWaveNodeState.FAILED);
    } else {
        logger.debug("NODE {}: Is currently marked as healthy by the controller", nodeId);
    }
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return true;
}
Also used : ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode)

Aggregations

ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)23 ZWaveWakeUpCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)8 ZWaveCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)6 SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)5 ZWaveBindingConfig (org.openhab.binding.zwave.ZWaveBindingConfig)4 ZWaveAssociationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass)4 ZWaveNodeSerializer (org.openhab.binding.zwave.internal.protocol.initialization.ZWaveNodeSerializer)4 ArrayList (java.util.ArrayList)3 ZWaveConfigurationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass)3 ZWaveSwitchAllCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass)3 HashMap (java.util.HashMap)2 List (java.util.List)2 ConfigurationParameter (org.openhab.binding.zwave.internal.protocol.ConfigurationParameter)2 ZWaveDeviceClass (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass)2 CommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass)2 ZWaveNoOperationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveNoOperationCommandClass)2 ZWaveInclusionEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent)2 ZWaveNetworkEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNetworkEvent)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1