Search in sources :

Example 1 with ZWEndPoint

use of org.openremote.protocol.zwave.model.ZWEndPoint in project openremote by openremote.

the class ZWaveNetwork method discoverDevices.

public synchronized AssetTreeNode[] discoverDevices(ZWaveAgent agent) {
    if (controller == null) {
        return new AssetTreeNode[0];
    }
    // Filter nodes
    List<ZWaveNode> nodes = controller.getNodes().stream().filter(node -> node.getState() == INITIALIZATION_FINISHED).filter(node -> {
        // Do not add devices that have already been discovered
        List<ZWCommandClass> cmdClasses = new ArrayList<>(node.getSupportedCommandClasses());
        for (ZWEndPoint curEndpoint : node.getEndPoints()) {
            cmdClasses.addAll(curEndpoint.getCmdClasses());
        }
        return cmdClasses.stream().map(ZWCommandClass::getChannels).flatMap(List<Channel>::stream).noneMatch(channel -> consumerLinkMap.values().stream().anyMatch(link -> link.getChannel() == channel));
    }).collect(toList());
    List<AssetTreeNode> assetNodes = nodes.stream().map(node -> {
        // Root device
        AssetTreeNode deviceNode = createDeviceNode(agent.getId(), node.getSupportedCommandClasses(), node.getGenericDeviceClassID().getDisplayName(node.getSpecificDeviceClassID()));
        // Device Info
        Asset<?> deviceInfoAsset = new ThingAsset("Info");
        deviceInfoAsset.getAttributes().addAll(createNodeInfoAttributes(agent.getId(), node));
        deviceNode.addChild(new AssetTreeNode(deviceInfoAsset));
        for (ZWEndPoint curEndpoint : node.getEndPoints()) {
            String subDeviceName = curEndpoint.getGenericDeviceClassID().getDisplayName(curEndpoint.getSpecificDeviceClassID()) + " - " + curEndpoint.getEndPointNumber();
            AssetTreeNode subDeviceNode = createDeviceNode(agent.getId(), curEndpoint.getCmdClasses(), subDeviceName);
            deviceNode.addChild(subDeviceNode);
        }
        // Parameters
        List<ZWParameterItem> parameters = node.getParameters();
        if (parameters.size() > 0) {
            AssetTreeNode parameterListNode = new AssetTreeNode(new ThingAsset("Parameters"));
            deviceNode.addChild(parameterListNode);
            List<AssetTreeNode> parameterNodes = parameters.stream().filter(parameter -> parameter.getChannels().size() > 0).map(parameter -> {
                int number = parameter.getNumber();
                String parameterLabel = number + " : " + parameter.getDisplayName();
                String description = parameter.getDescription();
                Asset<?> parameterAsset = new ThingAsset(parameterLabel);
                AssetTreeNode parameterNode = new AssetTreeNode(parameterAsset);
                List<Attribute<?>> attributes = parameter.getChannels().stream().map(channel -> {
                    Attribute<?> attribute = TypeMapper.createAttribute(channel.getName(), channel.getChannelType());
                    addAttributeChannelMetaItems(agent.getId(), attribute, channel);
                    addValidRangeMeta(attribute, parameter);
                    return attribute;
                }).collect(toList());
                if (description != null && description.length() > 0) {
                    Attribute<String> descriptionAttribute = new Attribute<>("description", org.openremote.model.value.ValueType.TEXT, description);
                    descriptionAttribute.addMeta(new MetaItem<>(MetaItemType.LABEL, "Description"), new MetaItem<>(MetaItemType.READ_ONLY, true));
                    attributes.add(descriptionAttribute);
                }
                parameterAsset.getAttributes().addAll(attributes);
                return parameterNode;
            }).collect(toList());
            parameterNodes.forEach(parameterListNode::addChild);
        }
        return deviceNode;
    }).collect(toList());
    // Z-Wave Controller
    Asset<?> networkManagementAsset = new ThingAsset("Z-Wave Controller");
    List<Attribute<?>> attributes = controller.getSystemCommandManager().getChannels().stream().filter(channel -> consumerLinkMap.values().stream().noneMatch(link -> link.getChannel() == channel)).map(channel -> {
        Attribute<?> attribute = TypeMapper.createAttribute(channel.getName(), channel.getChannelType());
        addAttributeChannelMetaItems(agent.getId(), attribute, channel);
        return attribute;
    }).collect(toList());
    if (attributes.size() > 0) {
        networkManagementAsset.getAttributes().addAll(attributes);
        assetNodes.add(new AssetTreeNode(networkManagementAsset));
    }
    return assetNodes.toArray(new AssetTreeNode[0]);
}
Also used : ZWParameterItem(org.openremote.protocol.zwave.model.commandclasses.ZWParameterItem) org.openremote.protocol.zwave.model.commandclasses.channel.value(org.openremote.protocol.zwave.model.commandclasses.channel.value) java.util(java.util) ConnectionException(org.openremote.protocol.zwave.ConnectionException) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) ZWParameterByte(org.openremote.protocol.zwave.model.commandclasses.ZWParameterByte) ThingAsset(org.openremote.model.asset.impl.ThingAsset) ValueUtil(org.openremote.model.util.ValueUtil) ZWaveNode(org.openremote.protocol.zwave.model.ZWaveNode) Level(java.util.logging.Level) AGENT_LINK(org.openremote.model.value.MetaItemType.AGENT_LINK) Attribute(org.openremote.model.attribute.Attribute) ZWCommandClassID(org.openremote.protocol.zwave.model.commandclasses.ZWCommandClassID) ZWManufacturerID(org.openremote.protocol.zwave.model.ZWManufacturerID) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ZWParameterShort(org.openremote.protocol.zwave.model.commandclasses.ZWParameterShort) ConfigurationException(org.openremote.controller.exception.ConfigurationException) AssetTreeNode(org.openremote.model.asset.AssetTreeNode) ZWavePortConfiguration(org.openremote.protocol.zwave.port.ZWavePortConfiguration) INITIALIZATION_FINISHED(org.openremote.protocol.zwave.model.ZWNodeInitializerListener.NodeInitState.INITIALIZATION_FINISHED) Asset(org.openremote.model.asset.Asset) MetaItemType(org.openremote.model.value.MetaItemType) ColourRGB(org.openremote.model.value.impl.ColourRGB) MetaItem(org.openremote.model.attribute.MetaItem) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Controller(org.openremote.protocol.zwave.model.Controller) Channel(org.openremote.protocol.zwave.model.commandclasses.channel.Channel) ZWCommandClass(org.openremote.protocol.zwave.model.commandclasses.ZWCommandClass) ZWEndPoint(org.openremote.protocol.zwave.model.ZWEndPoint) ZWParameterInt(org.openremote.protocol.zwave.model.commandclasses.ZWParameterInt) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ZWEndPoint(org.openremote.protocol.zwave.model.ZWEndPoint) Attribute(org.openremote.model.attribute.Attribute) ZWEndPoint(org.openremote.protocol.zwave.model.ZWEndPoint) AssetTreeNode(org.openremote.model.asset.AssetTreeNode) ZWaveNode(org.openremote.protocol.zwave.model.ZWaveNode) ThingAsset(org.openremote.model.asset.impl.ThingAsset) Asset(org.openremote.model.asset.Asset) Collectors.toList(java.util.stream.Collectors.toList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThingAsset(org.openremote.model.asset.impl.ThingAsset)

Aggregations

java.util (java.util)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Consumer (java.util.function.Consumer)1 Level (java.util.logging.Level)1 Collectors.toList (java.util.stream.Collectors.toList)1 ConfigurationException (org.openremote.controller.exception.ConfigurationException)1 Asset (org.openremote.model.asset.Asset)1 AssetTreeNode (org.openremote.model.asset.AssetTreeNode)1 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)1 ThingAsset (org.openremote.model.asset.impl.ThingAsset)1 Attribute (org.openremote.model.attribute.Attribute)1 MetaItem (org.openremote.model.attribute.MetaItem)1 ValueUtil (org.openremote.model.util.ValueUtil)1 MetaItemType (org.openremote.model.value.MetaItemType)1 AGENT_LINK (org.openremote.model.value.MetaItemType.AGENT_LINK)1 ColourRGB (org.openremote.model.value.impl.ColourRGB)1 ConnectionException (org.openremote.protocol.zwave.ConnectionException)1 Controller (org.openremote.protocol.zwave.model.Controller)1 ZWEndPoint (org.openremote.protocol.zwave.model.ZWEndPoint)1