Search in sources :

Example 1 with ZWaveSwitchAllCommandClass

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

the class ZWaveConfiguration method doSet.

@Override
public void doSet(String domain, String value) {
    logger.debug("doSet domain '{}' to '{}'", domain, value);
    String[] splitDomain = domain.split("/");
    // If the controller isn't ready, then ignore any requests
    if (zController.isConnected() == false) {
        logger.debug("Controller not ready - Ignoring request to '{}'", domain);
        return;
    }
    // There must be at least 2 components to the domain
    if (splitDomain.length < 2) {
        logger.error("Error malformed domain in doSet '{}'", domain);
        return;
    }
    if (splitDomain[0].equals("nodes")) {
        int nodeId = Integer.parseInt(splitDomain[1].substring(4));
        ZWaveNode node = zController.getNode(nodeId);
        if (node == null) {
            logger.error("Error finding node in doSet '{}'", domain);
            return;
        }
        // TODO: Should we check that the node is finished initialising
        ZWaveProductDatabase database = new ZWaveProductDatabase();
        if (database.FindProduct(node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion()) == false) {
            logger.error("NODE {}: Error in doSet - no database found", nodeId);
            return;
        }
        if (splitDomain.length == 3) {
            if (splitDomain[2].equals("Name")) {
                node.setName(value);
            }
            if (splitDomain[2].equals("Location")) {
                node.setLocation(value);
            }
            if (splitDomain[2].equals("SwitchAll")) {
                ZWaveSwitchAllCommandClass switchAllCommandClass = (ZWaveSwitchAllCommandClass) node.getCommandClass(CommandClass.SWITCH_ALL);
                if (switchAllCommandClass == null) {
                    logger.error("NODE {}: Error getting switchAllCommandClass in doSet", nodeId);
                    return;
                }
                // Set the switch all mode
                int mode = Integer.parseInt(value);
                PendingCfg.Add(ZWaveSwitchAllCommandClass.CommandClass.SWITCH_ALL.getKey(), node.getNodeId(), 0, mode);
                SerialMessage msg = switchAllCommandClass.setValueMessage(mode);
                this.zController.sendData(msg);
                // And request a read-back
                this.zController.sendData(switchAllCommandClass.getValueMessage());
            }
            // Write the node to disk
            ZWaveNodeSerializer nodeSerializer = new ZWaveNodeSerializer();
            nodeSerializer.SerializeNode(node);
        } else if (splitDomain.length == 4) {
            if (splitDomain[2].equals("parameters")) {
                ZWaveConfigurationCommandClass configurationCommandClass = (ZWaveConfigurationCommandClass) node.getCommandClass(CommandClass.CONFIGURATION);
                if (configurationCommandClass == null) {
                    logger.error("NODE {}: Error getting configurationCommandClass in doSet", nodeId);
                    return;
                }
                int paramIndex = Integer.parseInt(splitDomain[3].substring(13));
                List<ZWaveDbConfigurationParameter> configList = database.getProductConfigParameters();
                // Get the size
                int size = 1;
                for (ZWaveDbConfigurationParameter parameter : configList) {
                    if (parameter.Index == paramIndex) {
                        size = parameter.Size;
                        break;
                    }
                }
                logger.debug("Set parameter index '{}' to '{}'", paramIndex, value);
                PendingCfg.Add(ZWaveCommandClass.CommandClass.CONFIGURATION.getKey(), nodeId, paramIndex, Integer.valueOf(value));
                ConfigurationParameter configurationParameter = new ConfigurationParameter(paramIndex, Integer.valueOf(value), size);
                // Set the parameter
                this.zController.sendData(configurationCommandClass.setConfigMessage(configurationParameter));
                // And request a read-back
                this.zController.sendData(configurationCommandClass.getConfigMessage(paramIndex));
            }
            if (splitDomain[2].equals("wakeup")) {
                ZWaveWakeUpCommandClass wakeupCommandClass = (ZWaveWakeUpCommandClass) node.getCommandClass(CommandClass.WAKE_UP);
                if (wakeupCommandClass == null) {
                    logger.error("NODE {}: Error getting wakeupCommandClass in doSet", nodeId);
                    return;
                }
                logger.debug("NODE {}: Set wakeup interval to '{}'", nodeId, value);
                // Add this as a pending transaction
                PendingCfg.Add(ZWaveCommandClass.CommandClass.WAKE_UP.getKey(), node.getNodeId(), Integer.parseInt(value));
                // Set the wake-up interval
                this.zController.sendData(wakeupCommandClass.setInterval(Integer.parseInt(value)));
                // And request a read-back
                this.zController.sendData(wakeupCommandClass.getIntervalMessage());
            }
            if (splitDomain[2].equals("controller")) {
                if (splitDomain[3].equals("Type")) {
                    ZWaveDeviceType type = ZWaveDeviceType.fromString(value);
                    logger.error("NODE {}: Setting controller type to {}", nodeId, type.toString());
                // ZW_EnableSUC and ZW_SetSUCNodeID
                }
            }
        } else if (splitDomain.length == 5) {
            if (splitDomain[2].equals("associations")) {
                ZWaveAssociationCommandClass associationCommandClass = (ZWaveAssociationCommandClass) node.getCommandClass(CommandClass.ASSOCIATION);
                if (associationCommandClass == null) {
                    logger.error("NODE {}: Error getting associationCommandClass in doSet", nodeId);
                    return;
                }
                int assocId = Integer.parseInt(splitDomain[3].substring(11));
                int assocArg = Integer.parseInt(splitDomain[4].substring(4));
                if (value.equalsIgnoreCase("true")) {
                    PendingCfg.Add(ZWaveCommandClass.CommandClass.ASSOCIATION.getKey(), nodeId, assocId, assocArg, 1);
                    logger.debug("Add association index '{}' to '{}'", assocId, assocArg);
                    this.zController.sendData(associationCommandClass.setAssociationMessage(assocId, assocArg));
                } else {
                    PendingCfg.Add(ZWaveCommandClass.CommandClass.ASSOCIATION.getKey(), nodeId, assocId, assocArg, 0);
                    logger.debug("Remove association index '{}' to '{}'", assocId, assocArg);
                    this.zController.sendData(associationCommandClass.removeAssociationMessage(assocId, assocArg));
                }
                // Request an update to the group
                this.zController.sendData(associationCommandClass.getAssociationMessage(assocId));
                // So, let's start a network heal - just for this node right now
                if (networkMonitor != null) {
                    networkMonitor.startNodeHeal(nodeId);
                }
            }
        }
    }
}
Also used : ZWaveDeviceType(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceType) ConfigurationParameter(org.openhab.binding.zwave.internal.protocol.ConfigurationParameter) ZWaveSwitchAllCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass) ZWaveAssociationCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass) ZWaveNodeSerializer(org.openhab.binding.zwave.internal.protocol.initialization.ZWaveNodeSerializer) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveConfigurationCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ZWaveSwitchAllCommandClass

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

the class ZWaveController method allOn.

/**
     * Send All On message to all devices that support the Switch All command class
     */
public void allOn() {
    Enumeration<Integer> nodeIds = this.zwaveNodes.keys();
    while (nodeIds.hasMoreElements()) {
        Integer nodeId = nodeIds.nextElement();
        ZWaveNode node = this.getNode(nodeId);
        ZWaveSwitchAllCommandClass switchAllCommandClass = (ZWaveSwitchAllCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.SWITCH_ALL);
        if (switchAllCommandClass != null) {
            logger.debug("NODE {}: Supports Switch All Command Class Sending AllOn Message", nodeId);
            switchAllCommandClass.setNode(node);
            switchAllCommandClass.setController(this);
            this.enqueue(switchAllCommandClass.allOnMessage());
            continue;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZWaveSwitchAllCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass)

Example 3 with ZWaveSwitchAllCommandClass

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

the class ZWaveController method allOff.

/**
     * Send All Off message to all devices that support the Switch All command class
     */
public void allOff() {
    Enumeration<Integer> nodeIds = this.zwaveNodes.keys();
    while (nodeIds.hasMoreElements()) {
        Integer nodeId = nodeIds.nextElement();
        ZWaveNode node = this.getNode(nodeId);
        ZWaveSwitchAllCommandClass switchAllCommandClass = (ZWaveSwitchAllCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.SWITCH_ALL);
        if (switchAllCommandClass != null) {
            logger.debug("NODE {}: Supports Switch All Command Class Sending AllOff Message", nodeId);
            switchAllCommandClass.setNode(node);
            switchAllCommandClass.setController(this);
            this.enqueue(switchAllCommandClass.allOffMessage());
            continue;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZWaveSwitchAllCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass)

Example 4 with ZWaveSwitchAllCommandClass

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

the class ZWaveConfiguration method getConfiguration.

@Override
public List<OpenHABConfigurationRecord> getConfiguration(String domain) {
    // We only deal with top level domains here!
    if (domain.endsWith("/") == false) {
        logger.error("Malformed domain request in getConfiguration '{}'", domain);
        return null;
    }
    List<OpenHABConfigurationRecord> records = new ArrayList<OpenHABConfigurationRecord>();
    OpenHABConfigurationRecord record;
    if (domain.equals("status/")) {
        return null;
    }
    if (domain.startsWith("products/")) {
        ZWaveProductDatabase database = new ZWaveProductDatabase();
        String[] splitDomain = domain.split("/");
        switch(splitDomain.length) {
            case 1:
                // Just list the manufacturers
                for (ZWaveDbManufacturer manufacturer : database.GetManufacturers()) {
                    record = new OpenHABConfigurationRecord(domain + manufacturer.Id.toString() + "/", manufacturer.Name);
                    records.add(record);
                }
                break;
            case 2:
                // Get products list
                if (database.FindManufacturer(Integer.parseInt(splitDomain[1])) == false) {
                    break;
                }
                record = new OpenHABConfigurationRecord(domain, "ManufacturerID", "Manufacturer ID", true);
                record.value = Integer.toHexString(database.getManufacturerId());
                records.add(record);
                for (ZWaveDbProduct product : database.GetProducts()) {
                    record = new OpenHABConfigurationRecord(domain + product.Reference.get(0).Type + "/" + product.Reference.get(0).Id + "/", product.Model);
                    record.value = database.getLabel(product.Label);
                    records.add(record);
                }
                break;
            case 4:
                // Get product
                if (database.FindProduct(Integer.parseInt(splitDomain[1]), Integer.parseInt(splitDomain[2]), Integer.parseInt(splitDomain[3]), MAX_VERSION) == false) {
                    break;
                }
                if (database.doesProductImplementCommandClass(ZWaveCommandClass.CommandClass.CONFIGURATION.getKey()) == true) {
                    record = new OpenHABConfigurationRecord(domain + "parameters/", "Configuration Parameters");
                    records.add(record);
                }
                if (database.doesProductImplementCommandClass(ZWaveCommandClass.CommandClass.ASSOCIATION.getKey()) == true) {
                    record = new OpenHABConfigurationRecord(domain + "associations/", "Association Groups");
                    records.add(record);
                }
                record = new OpenHABConfigurationRecord(domain + "classes/", "Command Classes");
                records.add(record);
                break;
            case 5:
                // Get product
                if (database.FindProduct(Integer.parseInt(splitDomain[1]), Integer.parseInt(splitDomain[2]), Integer.parseInt(splitDomain[3]), MAX_VERSION) == false) {
                    break;
                }
                if (splitDomain[4].equals("parameters")) {
                    List<ZWaveDbConfigurationParameter> configList = database.getProductConfigParameters();
                    // Loop through the parameters and add to the records...
                    for (ZWaveDbConfigurationParameter parameter : configList) {
                        record = new OpenHABConfigurationRecord(domain, "configuration" + parameter.Index, database.getLabel(parameter.Label), true);
                        if (parameter != null) {
                            record.value = parameter.Default;
                        }
                        // Add the data type
                        try {
                            record.type = OpenHABConfigurationRecord.TYPE.valueOf(parameter.Type.toUpperCase());
                        } catch (IllegalArgumentException e) {
                            logger.error("Error with parameter type for {} - Set {} - assuming LONG", parameter.Label.toString(), parameter.Type);
                            record.type = OpenHABConfigurationRecord.TYPE.LONG;
                        }
                        if (parameter.Item != null) {
                            for (ZWaveDbConfigurationListItem item : parameter.Item) {
                                record.addValue(Integer.toString(item.Value), database.getLabel(item.Label));
                            }
                        }
                        // Add the description
                        record.description = database.getLabel(parameter.Help);
                        records.add(record);
                    }
                }
                if (splitDomain[4].equals("associations")) {
                    List<ZWaveDbAssociationGroup> groupList = database.getProductAssociationGroups();
                    if (groupList != null) {
                        // records...
                        for (ZWaveDbAssociationGroup group : groupList) {
                            record = new OpenHABConfigurationRecord(domain, "association" + group.Index, database.getLabel(group.Label), true);
                            // Add the description
                            record.description = database.getLabel(group.Help);
                            records.add(record);
                        }
                    }
                }
                if (splitDomain[4].equals("classes")) {
                    List<ZWaveDbCommandClass> classList = database.getProductCommandClasses();
                    if (classList != null) {
                        // records...
                        for (ZWaveDbCommandClass iClass : classList) {
                            // Make sure the command class exists!
                            if (ZWaveCommandClass.CommandClass.getCommandClass(iClass.Id) == null) {
                                continue;
                            }
                            record = new OpenHABConfigurationRecord(domain, "class" + iClass.Id, ZWaveCommandClass.CommandClass.getCommandClass(iClass.Id).getLabel(), true);
                            if (ZWaveCommandClass.CommandClass.getCommandClass(iClass.Id).getCommandClassClass() == null) {
                                record.state = OpenHABConfigurationRecord.STATE.WARNING;
                            }
                            records.add(record);
                        }
                    }
                }
                break;
        }
        return records;
    }
    // All domains after here must have an initialised ZWave network
    if (zController == null) {
        logger.error("Controller not initialised in call to getConfiguration");
        return null;
    }
    if (domain.equals("nodes/")) {
        ZWaveProductDatabase database = new ZWaveProductDatabase();
        // Return the list of nodes
        for (ZWaveNode node : zController.getNodes()) {
            if (node.getName() == null || node.getName().isEmpty()) {
                record = new OpenHABConfigurationRecord("nodes/" + "node" + node.getNodeId() + "/", "Node " + node.getNodeId());
            } else {
                record = new OpenHABConfigurationRecord("nodes/" + "node" + node.getNodeId() + "/", node.getNodeId() + ": " + node.getName());
            }
            // If we can't find the product, then try and find just the manufacturer
            if (node.getManufacturer() == Integer.MAX_VALUE) {
            // The device isn't know, print nothing!
            } else if (database.FindProduct(node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion()) == false) {
                if (database.FindManufacturer(node.getManufacturer()) == false) {
                    record.value = "Manufacturer:" + node.getManufacturer() + " [ID:" + Integer.toHexString(node.getDeviceId()) + ",Type:" + Integer.toHexString(node.getDeviceType()) + "]";
                } else {
                    record.value = database.getManufacturerName() + " [ID:" + Integer.toHexString(node.getDeviceId()) + ",Type:" + Integer.toHexString(node.getDeviceType()) + "]";
                }
                logger.debug("NODE {}: No database entry: {}", node.getNodeId(), record.value);
            } else {
                if (node.getLocation() == null || node.getLocation().isEmpty()) {
                    record.value = database.getProductName();
                } else {
                    record.value = database.getProductName() + ": " + node.getLocation();
                }
            }
            // Set the state
            boolean canDelete = false;
            switch(node.getNodeState()) {
                case DEAD:
                    record.state = OpenHABConfigurationRecord.STATE.ERROR;
                    break;
                case FAILED:
                    record.state = OpenHABConfigurationRecord.STATE.ERROR;
                    canDelete = true;
                    break;
                case ALIVE:
                    Date lastDead = node.getDeadTime();
                    Long timeSinceLastDead = Long.MAX_VALUE;
                    if (lastDead != null) {
                        timeSinceLastDead = System.currentTimeMillis() - lastDead.getTime();
                    }
                    if (node.getNodeInitializationStage() != ZWaveNodeInitStage.DONE) {
                        record.state = OpenHABConfigurationRecord.STATE.INITIALIZING;
                    } else if (node.getDeadCount() > 0 && timeSinceLastDead < 86400000) {
                        record.state = OpenHABConfigurationRecord.STATE.WARNING;
                    } else if (node.getSendCount() > 20 && (node.getRetryCount() * 100 / node.getSendCount()) > 5) {
                        record.state = OpenHABConfigurationRecord.STATE.WARNING;
                    } else {
                        record.state = OpenHABConfigurationRecord.STATE.OK;
                    }
                    break;
            }
            // Add the action buttons
            record.addAction("Heal", "Heal Node");
            record.addAction("Initialise", "Reinitialise Node");
            // Add the delete button if the node is not "operational"
            if (canDelete) {
                record.addAction("Delete", "Delete Node");
            }
            records.add(record);
        }
        return records;
    }
    if (domain.startsWith("nodes/node")) {
        String nodeNumber = domain.substring(10);
        int next = nodeNumber.indexOf('/');
        String arg = null;
        if (next != -1) {
            arg = nodeNumber.substring(next + 1);
            nodeNumber = nodeNumber.substring(0, next);
        }
        int nodeId = Integer.parseInt(nodeNumber);
        // Return the detailed configuration for this node
        ZWaveNode node = zController.getNode(nodeId);
        if (node == null) {
            return null;
        }
        // Open the product database
        ZWaveProductDatabase database = new ZWaveProductDatabase();
        // Process the request
        if (arg.equals("")) {
            record = new OpenHABConfigurationRecord(domain, "Name", "Name", false);
            record.value = node.getName();
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Location", "Location", false);
            record.value = node.getLocation();
            records.add(record);
            ZWaveSwitchAllCommandClass switchAllCommandClass = (ZWaveSwitchAllCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.SWITCH_ALL);
            if (node.getCommandClass(CommandClass.SWITCH_ALL) != null) {
                record = new OpenHABConfigurationRecord(domain, "SwitchAll", "Switch All", false);
                record.type = OpenHABConfigurationRecord.TYPE.LIST;
                record.addValue("0", "Exclude from All On and All Off groups");
                record.addValue("1", "Include in All On group");
                record.addValue("2", "Include in All Off group");
                record.addValue("255", "Include in All On and All Off groups");
                record.value = String.valueOf(switchAllCommandClass.getMode());
                records.add(record);
            }
            // Get the configuration command class for this node if it's supported
            if (node.getCommandClass(CommandClass.CONFIGURATION) != null) {
                record = new OpenHABConfigurationRecord(domain + "parameters/", "Configuration Parameters");
                record.addAction("Refresh", "Refresh");
                records.add(record);
            }
            if (node.getCommandClass(CommandClass.ASSOCIATION) != null) {
                record = new OpenHABConfigurationRecord(domain + "associations/", "Association Groups");
                record.addAction("Refresh", "Refresh");
                records.add(record);
            }
            if (node.getCommandClass(CommandClass.WAKE_UP) != null) {
                record = new OpenHABConfigurationRecord(domain + "wakeup/", "Wakeup Period");
                record.addAction("Refresh", "Refresh");
                records.add(record);
            }
            // Is this a controller
            if (nodeId == zController.getOwnNodeId()) {
                record = new OpenHABConfigurationRecord(domain + "controller/", "Controller");
                records.add(record);
            }
            record = new OpenHABConfigurationRecord(domain + "neighbors/", "Neighbors");
            record.addAction("Refresh", "Refresh");
            records.add(record);
            record = new OpenHABConfigurationRecord(domain + "status/", "Status");
            records.add(record);
            record = new OpenHABConfigurationRecord(domain + "info/", "Information");
            records.add(record);
        } else if (arg.equals("switch_all/")) {
        /*
                 * ZWaveSwitchAllCommandClass switchAllCommandClass =
                 * (ZWaveSwitchAllCommandClass)node.getCommandClass(ZWaveCommandClass.CommandClass.SWITCH_ALL);
                 * if (switchAllCommandClass != null) {
                 * logger.debug("NODE {}: Supports Switch All Command Class Adding Switch All Configuration ",
                 * (Object)nodeId);
                 * record = new OpenHABConfigurationRecord(domain, "Mode", "Mode", true);
                 * record.type = OpenHABConfigurationRecord.TYPE.LIST;
                 * record.addValue("0x00", "Exclude from All On and All Off groups");
                 * record.addValue("0x01", "Include in All On group");
                 * record.addValue("0x02", "Include in All Off group");
                 * record.addValue("0xFF", "Include in All On and All Off groups");
                 * records.add(record);
                 * }
                 */
        } else if (arg.equals("info/")) {
            record = new OpenHABConfigurationRecord(domain, "NodeID", "Node ID", true);
            record.value = Integer.toString(node.getNodeId());
            records.add(record);
            if (node.getManufacturer() != Integer.MAX_VALUE) {
                if (database.FindManufacturer(node.getManufacturer()) == true) {
                    record = new OpenHABConfigurationRecord(domain, "Manufacturer", "Manufacturer", true);
                    record.value = database.getManufacturerName();
                    records.add(record);
                }
                if (database.FindProduct(node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion()) == true) {
                    record = new OpenHABConfigurationRecord(domain, "Product", "Product", true);
                    record.value = database.getProductName();
                    records.add(record);
                }
            }
            record = new OpenHABConfigurationRecord(domain, "ManufacturerID", "Manufacturer ID", true);
            if (node.getDeviceId() == Integer.MAX_VALUE) {
                record.value = "UNKNOWN";
            } else {
                record.value = Integer.toHexString(node.getManufacturer());
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "DeviceID", "Device ID", true);
            if (node.getDeviceId() == Integer.MAX_VALUE) {
                record.value = "UNKNOWN";
            } else {
                record.value = Integer.toHexString(node.getDeviceId());
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "DeviceType", "Device Type", true);
            if (node.getDeviceId() == Integer.MAX_VALUE) {
                record.value = "UNKNOWN";
            } else {
                record.value = Integer.toHexString(node.getDeviceType());
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Version", "Version", true);
            record.value = Integer.toString(node.getVersion());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Listening", "Listening", true);
            record.value = Boolean.toString(node.isListening());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Routing", "Routing", true);
            record.value = Boolean.toString(node.isRouting());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Power", "Power", true);
            ZWaveBatteryCommandClass batteryCommandClass = (ZWaveBatteryCommandClass) node.getCommandClass(CommandClass.BATTERY);
            if (batteryCommandClass != null) {
                if (batteryCommandClass.getBatteryLevel() == null) {
                    record.value = "BATTERY " + "UNKNOWN";
                } else {
                    record.value = "BATTERY " + batteryCommandClass.getBatteryLevel() + "%";
                }
                // If the node is reporting low battery, mark it up...
                if (batteryCommandClass.getBatteryLow() == true) {
                    record.value += " LOW";
                }
            } else if (node.getNodeInitializationStage().getStage() <= ZWaveNodeInitStage.DETAILS.getStage()) {
                // If we haven't passed the DETAILS stage, then we don't know the source
                record.value = "UNKNOWN";
            } else {
                record.value = "MAINS";
            }
            records.add(record);
            ZWaveVersionCommandClass versionCommandClass = (ZWaveVersionCommandClass) node.getCommandClass(CommandClass.VERSION);
            if (versionCommandClass != null) {
                record = new OpenHABConfigurationRecord(domain, "LibType", "Library Type", true);
                if (versionCommandClass.getLibraryType() == null) {
                    record.value = "Unknown";
                } else {
                    record.value = versionCommandClass.getLibraryType().getLabel();
                }
                records.add(record);
                record = new OpenHABConfigurationRecord(domain, "ProtocolVersion", "Protocol Version", true);
                if (versionCommandClass.getProtocolVersion() == null) {
                    record.value = "Unknown";
                } else {
                    record.value = versionCommandClass.getProtocolVersion();
                }
                records.add(record);
                record = new OpenHABConfigurationRecord(domain, "AppVersion", "Application Version", true);
                if (versionCommandClass.getApplicationVersion() == null) {
                    record.value = "Unknown";
                } else {
                    record.value = versionCommandClass.getApplicationVersion();
                }
                records.add(record);
            }
            ZWaveDeviceClass devClass = node.getDeviceClass();
            if (devClass != null) {
                record = new OpenHABConfigurationRecord(domain, "BasicClass", "Basic Device Class", true);
                record.value = devClass.getBasicDeviceClass().toString();
                records.add(record);
                record = new OpenHABConfigurationRecord(domain, "GenericClass", "Generic Device Class", true);
                record.value = devClass.getGenericDeviceClass().toString();
                records.add(record);
                record = new OpenHABConfigurationRecord(domain, "SpecificClass", "Specific Device Class", true);
                record.value = devClass.getSpecificDeviceClass().toString();
                records.add(record);
            }
        } else if (arg.equals("status/")) {
            record = new OpenHABConfigurationRecord(domain, "LastSent", "Last Packet Sent", true);
            if (node.getLastSent() == null) {
                record.value = "NEVER";
            } else {
                record.value = df.format(node.getLastSent());
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "LastReceived", "Last Packet Received", true);
            if (node.getLastReceived() == null) {
                record.value = "NEVER";
            } else {
                record.value = df.format(node.getLastReceived());
            }
            records.add(record);
            if (networkMonitor != null) {
                record = new OpenHABConfigurationRecord(domain, "LastHeal", "Heal Status", true);
                if (node.getHealState() == null) {
                    record.value = networkMonitor.getNodeState(nodeId);
                } else {
                    record.value = node.getHealState();
                }
                records.add(record);
            }
            record = new OpenHABConfigurationRecord(domain, "NodeStage", "Node Stage", true);
            record.value = node.getNodeState() + " " + node.getNodeInitializationStage() + " " + df.format(node.getQueryStageTimeStamp());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Packets", "Packet Statistics", true);
            record.value = node.getRetryCount() + " / " + node.getSendCount();
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Dead", "Dead", true);
            if (node.getDeadCount() == 0) {
                record.value = Boolean.toString(node.isDead());
            } else {
                record.value = Boolean.toString(node.isDead()) + " [" + node.getDeadCount() + " previous - last @ " + df.format(node.getDeadTime()) + "]";
            }
            records.add(record);
        } else if (arg.equals("parameters/")) {
            if (database.FindProduct(node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion()) != false) {
                List<ZWaveDbConfigurationParameter> configList = database.getProductConfigParameters();
                if (configList == null) {
                    return records;
                }
                // Get the configuration command class for this node if it's supported
                ZWaveConfigurationCommandClass configurationCommandClass = (ZWaveConfigurationCommandClass) node.getCommandClass(CommandClass.CONFIGURATION);
                if (configurationCommandClass == null) {
                    return null;
                }
                // Loop through the parameters and add to the records...
                for (ZWaveDbConfigurationParameter parameter : configList) {
                    record = new OpenHABConfigurationRecord(domain, "configuration" + parameter.Index, parameter.Index + ": " + database.getLabel(parameter.Label), false);
                    ConfigurationParameter configurationParameter = configurationCommandClass.getParameter(parameter.Index);
                    // This is the only way we can be sure of its real value
                    if (configurationParameter != null && configurationParameter.getWriteOnly() == false) {
                        record.value = Integer.toString(configurationParameter.getValue());
                    }
                    // If the value is in our PENDING list, then use that instead
                    Integer pendingValue = PendingCfg.Get(ZWaveCommandClass.CommandClass.CONFIGURATION.getKey(), nodeId, parameter.Index);
                    if (pendingValue != null) {
                        record.value = Integer.toString(pendingValue);
                        record.state = OpenHABConfigurationRecord.STATE.PENDING;
                    }
                    try {
                        record.type = OpenHABConfigurationRecord.TYPE.valueOf(parameter.Type.toUpperCase());
                    } catch (IllegalArgumentException e) {
                        record.type = OpenHABConfigurationRecord.TYPE.LONG;
                    }
                    if (parameter.Item != null) {
                        for (ZWaveDbConfigurationListItem item : parameter.Item) {
                            record.addValue(Integer.toString(item.Value), database.getLabel(item.Label));
                        }
                    }
                    // Add any limits
                    record.minimum = parameter.Minimum;
                    record.maximum = parameter.Maximum;
                    // Add the description
                    record.description = database.getLabel(parameter.Help);
                    records.add(record);
                }
            }
        } else if (arg.equals("associations/")) {
            if (database.FindProduct(node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion()) != false) {
                List<ZWaveDbAssociationGroup> groupList = database.getProductAssociationGroups();
                if (groupList == null) {
                    return records;
                }
                // Get the association command class for this node if it's supported
                ZWaveAssociationCommandClass associationCommandClass = (ZWaveAssociationCommandClass) node.getCommandClass(CommandClass.ASSOCIATION);
                if (associationCommandClass == null) {
                    return null;
                }
                // records...
                for (ZWaveDbAssociationGroup group : groupList) {
                    record = new OpenHABConfigurationRecord(domain, "association" + group.Index + "/", database.getLabel(group.Label), true);
                    // Add the description
                    record.description = database.getLabel(group.Help);
                    // For the 'value', describe how many devices are set and the maximum allowed
                    int memberCnt = 0;
                    List<Integer> members = associationCommandClass.getGroupMembers(group.Index);
                    if (members != null) {
                        memberCnt = members.size();
                    }
                    record.value = memberCnt + " of " + group.Maximum + " group members";
                    // Add the action for refresh
                    record.addAction("Refresh", "Refresh");
                    records.add(record);
                }
            }
        } else if (arg.startsWith("associations/association")) {
            if (database.FindProduct(node.getManufacturer(), node.getDeviceType(), node.getDeviceId(), node.getApplicationVersion()) != false) {
                String groupString = arg.substring(24);
                int nextDelimiter = groupString.indexOf('/');
                // String arg = null;
                if (nextDelimiter != -1) {
                    // arg = nodeNumber.substring(nextDelimiter + 1);
                    groupString = groupString.substring(0, nextDelimiter);
                }
                int groupId = Integer.parseInt(groupString);
                // Get the requested group so we have access to the
                // attributes
                List<ZWaveDbAssociationGroup> groupList = database.getProductAssociationGroups();
                if (groupList == null) {
                    return null;
                }
                ZWaveDbAssociationGroup group = null;
                for (int cnt = 0; cnt < groupList.size(); cnt++) {
                    if (groupList.get(cnt).Index == groupId) {
                        group = groupList.get(cnt);
                        break;
                    }
                }
                // Return if the group wasn't found
                if (group == null) {
                    return null;
                }
                // Get the group members
                ZWaveAssociationCommandClass associationCommandClass = (ZWaveAssociationCommandClass) node.getCommandClass(CommandClass.ASSOCIATION);
                // First we build a list of all nodes, starting with the group members
                // This ensures that old nodes included in a group, but not now part
                // of the network will still be listed, and can be removed.
                List<Integer> nodes = new ArrayList<Integer>();
                nodes.addAll(associationCommandClass.getGroupMembers(groupId));
                for (ZWaveNode nodeList : zController.getNodes()) {
                    // Don't allow an association with itself
                    if (nodeList.getNodeId() == node.getNodeId()) {
                        continue;
                    }
                    if (!nodes.contains(nodeList.getNodeId())) {
                        nodes.add(nodeList.getNodeId());
                    }
                }
                // Let's sort them!
                Collections.sort(nodes);
                // Now we loop through the node list and create an entry for each node.
                List<Integer> members = associationCommandClass.getGroupMembers(groupId);
                for (int i = 0; i < nodes.size(); i++) {
                    int nodeNum = nodes.get(i);
                    ZWaveNode nodeList = zController.getNode(nodeNum);
                    // Add the member
                    if (nodeList == null || nodeList.getName() == null || nodeList.getName().isEmpty()) {
                        record = new OpenHABConfigurationRecord(domain, "node" + nodeNum, "Node " + nodeNum, false);
                    } else {
                        record = new OpenHABConfigurationRecord(domain, "node" + nodeNum, nodeList.getName(), false);
                    }
                    record.type = OpenHABConfigurationRecord.TYPE.LIST;
                    record.addValue("true", "Member");
                    record.addValue("false", "Non-Member");
                    if (members != null && members.contains(nodeNum)) {
                        record.value = "true";
                    } else {
                        record.value = "false";
                    }
                    // If the value is in our PENDING list, then use that instead
                    Integer pendingValue = PendingCfg.Get(ZWaveCommandClass.CommandClass.ASSOCIATION.getKey(), nodeId, groupId, nodeNum);
                    if (pendingValue != null) {
                        if (pendingValue == 1) {
                            record.value = "true";
                        } else {
                            record.value = "false";
                        }
                        record.state = OpenHABConfigurationRecord.STATE.PENDING;
                    }
                    records.add(record);
                }
            }
        } else if (arg.equals("wakeup/")) {
            ZWaveWakeUpCommandClass wakeupCommandClass = (ZWaveWakeUpCommandClass) node.getCommandClass(CommandClass.WAKE_UP);
            if (wakeupCommandClass == null) {
                logger.debug("NODE {}: wakeupCommandClass not supported", nodeId);
                return null;
            }
            // Display the wakeup parameters.
            // Note that only the interval is writable.
            record = new OpenHABConfigurationRecord(domain, "Interval", "Wakeup Interval", false);
            record.minimum = wakeupCommandClass.getMinInterval();
            record.maximum = wakeupCommandClass.getMaxInterval();
            record.value = Integer.toString(wakeupCommandClass.getInterval());
            // If the value is in our PENDING list, then use that instead
            Integer pendingValue = PendingCfg.Get(ZWaveCommandClass.CommandClass.WAKE_UP.getKey(), nodeId);
            if (pendingValue != null) {
                record.value = Integer.toString(pendingValue);
                record.state = OpenHABConfigurationRecord.STATE.PENDING;
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "LastWake", "Last Wakeup", true);
            if (wakeupCommandClass.getLastWakeup() == null) {
                record.value = "NEVER";
            } else {
                record.value = df.format(wakeupCommandClass.getLastWakeup());
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Target", "Target Node", true);
            record.value = Integer.toString(wakeupCommandClass.getTargetNodeId());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Minimum", "Minimum Interval", true);
            record.value = Integer.toString(wakeupCommandClass.getMinInterval());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Maximum", "Maximum Interval", true);
            record.value = Integer.toString(wakeupCommandClass.getMaxInterval());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Default", "Default Interval", true);
            record.value = Integer.toString(wakeupCommandClass.getDefaultInterval());
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "Step", "Interval Step", true);
            record.value = Integer.toString(wakeupCommandClass.getIntervalStep());
            records.add(record);
        } else if (arg.equals("neighbors/")) {
            // Check that we have the neighbor list for this node
            if (node.getNeighbors() == null) {
                return null;
            }
            for (Integer neighbor : node.getNeighbors()) {
                ZWaveNode nodeNeighbor = zController.getNode(neighbor);
                String neighborName;
                if (nodeNeighbor == null) {
                    neighborName = "Node " + neighbor + " (UNKNOWN)";
                } else if (nodeNeighbor.getName() == null || nodeNeighbor.getName().isEmpty()) {
                    neighborName = "Node " + neighbor;
                } else {
                    neighborName = nodeNeighbor.getName();
                }
                // Create the record
                record = new OpenHABConfigurationRecord(domain, "node" + neighbor, neighborName, false);
                record.readonly = true;
                // If this node isn't known, mark it as an error
                if (nodeNeighbor == null) {
                    record.state = OpenHABConfigurationRecord.STATE.ERROR;
                }
                records.add(record);
            }
        } else if (arg.equals("controller/")) {
            // Create the record
            record = new OpenHABConfigurationRecord(domain, "Type", "Controller Type", true);
            record.type = OpenHABConfigurationRecord.TYPE.LIST;
            record.value = zController.getControllerType().getLabel();
            record.addValue(ZWaveDeviceType.PRIMARY.toString(), ZWaveDeviceType.PRIMARY.getLabel());
            record.addValue(ZWaveDeviceType.SECONDARY.toString(), ZWaveDeviceType.SECONDARY.getLabel());
            record.addValue(ZWaveDeviceType.SUC.toString(), ZWaveDeviceType.SUC.getLabel());
            // Set the read-only if this isn't a controller!
            switch(zController.getControllerType()) {
                case SUC:
                case PRIMARY:
                case SECONDARY:
                    record.readonly = true;
                    break;
                default:
                    record.readonly = true;
                    break;
            }
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "APIVersion", "API Version", true);
            record.value = zController.getSerialAPIVersion();
            records.add(record);
            record = new OpenHABConfigurationRecord(domain, "ZWaveVersion", "ZWave Version", true);
            record.value = zController.getZWaveVersion();
            records.add(record);
        }
        return records;
    }
    return null;
}
Also used : ZWaveVersionCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveVersionCommandClass) ArrayList(java.util.ArrayList) ConfigurationParameter(org.openhab.binding.zwave.internal.protocol.ConfigurationParameter) ZWaveSwitchAllCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass) ZWaveDeviceClass(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass) ZWaveAssociationCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass) ZWaveConfigurationCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass) ArrayList(java.util.ArrayList) List(java.util.List) ZWaveBatteryCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveBatteryCommandClass) Date(java.util.Date) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode)

Aggregations

ZWaveSwitchAllCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSwitchAllCommandClass)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConfigurationParameter (org.openhab.binding.zwave.internal.protocol.ConfigurationParameter)2 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)2 ZWaveAssociationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass)2 ZWaveConfigurationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass)2 ZWaveWakeUpCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)2 Date (java.util.Date)1 SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)1 ZWaveDeviceClass (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass)1 ZWaveDeviceType (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceType)1 ZWaveBatteryCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveBatteryCommandClass)1 ZWaveVersionCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveVersionCommandClass)1 ZWaveNodeSerializer (org.openhab.binding.zwave.internal.protocol.initialization.ZWaveNodeSerializer)1