Search in sources :

Example 1 with ThingAsset

use of org.openremote.model.asset.impl.ThingAsset 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)

Example 2 with ThingAsset

use of org.openremote.model.asset.impl.ThingAsset in project openremote by openremote.

the class AbstractVelbusProtocol method startAssetImport.

/* ProtocolAssetImport */
@Override
public Future<Void> startAssetImport(byte[] fileData, Consumer<AssetTreeNode[]> assetConsumer) {
    return executorService.submit(() -> {
        Document xmlDoc;
        try {
            String xmlStr = new String(fileData, StandardCharsets.UTF_8);
            LOG.info("Parsing VELBUS project file");
            xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xmlStr)));
        } catch (Exception e) {
            LOG.log(Level.WARNING, "Failed to convert VELBUS project file into XML", e);
            return;
        }
        xmlDoc.getDocumentElement().normalize();
        NodeList modules = xmlDoc.getElementsByTagName("Module");
        LOG.info("Found " + modules.getLength() + " module(s)");
        List<Asset<?>> devices = new ArrayList<>(modules.getLength());
        for (int i = 0; i < modules.getLength(); i++) {
            Element module = (Element) modules.item(i);
            // TODO: Process memory map and add
            Optional<VelbusDeviceType> deviceType = EnumUtil.enumFromString(VelbusDeviceType.class, module.getAttribute("type").replaceAll("-", ""));
            if (!deviceType.isPresent()) {
                LOG.info("Module device type '" + module.getAttribute("type") + "' is not supported so ignoring");
                continue;
            }
            String[] addresses = module.getAttribute("address").split(",");
            int baseAddress = Integer.parseInt(addresses[0], 16);
            String build = module.getAttribute("build");
            String serial = module.getAttribute("serial");
            String name = module.getElementsByTagName("Caption").item(0).getTextContent();
            name = isNullOrEmpty(name) ? deviceType.toString() : name;
            // TODO: Use device specific asset types
            Asset<?> device = new ThingAsset(name);
            device.addAttributes(new Attribute<>("build", ValueType.TEXT, build).addMeta(new MetaItem<>(MetaItemType.LABEL, "Build"), new MetaItem<>(MetaItemType.READ_ONLY, true)), new Attribute<>("serialNumber", ValueType.TEXT, serial).addMeta(new MetaItem<>(MetaItemType.LABEL, "Serial No"), new MetaItem<>(MetaItemType.READ_ONLY, true)));
            device.addAttributes(deviceType.flatMap(type -> Optional.ofNullable(type.getFeatureProcessors()).map(processors -> Arrays.stream(processors).flatMap(processor -> processor.getPropertyDescriptors(type).stream().map(descriptor -> {
                VelbusAgentLink agentLink = new VelbusAgentLink(agent.getId(), baseAddress, descriptor.getLinkName());
                Attribute<?> attribute = new Attribute<>(descriptor.getName(), descriptor.getAttributeValueDescriptor()).addMeta(new MetaItem<>(AGENT_LINK, agentLink), new MetaItem<>(MetaItemType.LABEL, descriptor.getDisplayName()));
                if (descriptor.isReadOnly()) {
                    attribute.addMeta(new MetaItem<>(MetaItemType.READ_ONLY, true));
                }
                return attribute;
            })).toArray(Attribute<?>[]::new))).orElse(new Attribute<?>[0]));
            devices.add(device);
        }
        assetConsumer.accept(devices.stream().map(AssetTreeNode::new).toArray(AssetTreeNode[]::new));
    }, null);
}
Also used : java.util(java.util) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) ThingAsset(org.openremote.model.asset.impl.ThingAsset) VelbusDeviceType(org.openremote.agent.protocol.velbus.device.VelbusDeviceType) Level(java.util.logging.Level) AGENT_LINK(org.openremote.model.value.MetaItemType.AGENT_LINK) Future(java.util.concurrent.Future) Document(org.w3c.dom.Document) SyslogCategory(org.openremote.model.syslog.SyslogCategory) IOClient(org.openremote.agent.protocol.io.IOClient) InputSource(org.xml.sax.InputSource) AssetTreeNode(org.openremote.model.asset.AssetTreeNode) NodeList(org.w3c.dom.NodeList) ValueType(org.openremote.model.value.ValueType) Asset(org.openremote.model.asset.Asset) MetaItemType(org.openremote.model.value.MetaItemType) EnumUtil(org.openremote.model.util.EnumUtil) Logger(java.util.logging.Logger) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) StandardCharsets(java.nio.charset.StandardCharsets) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) Consumer(java.util.function.Consumer) ProtocolAssetImport(org.openremote.model.protocol.ProtocolAssetImport) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) org.openremote.model.attribute(org.openremote.model.attribute) AgentLink.getOrThrowAgentLinkProperty(org.openremote.model.asset.agent.AgentLink.getOrThrowAgentLinkProperty) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) VelbusDeviceType(org.openremote.agent.protocol.velbus.device.VelbusDeviceType) Document(org.w3c.dom.Document) AssetTreeNode(org.openremote.model.asset.AssetTreeNode) StringReader(java.io.StringReader) ThingAsset(org.openremote.model.asset.impl.ThingAsset) Asset(org.openremote.model.asset.Asset) ThingAsset(org.openremote.model.asset.impl.ThingAsset)

Example 3 with ThingAsset

use of org.openremote.model.asset.impl.ThingAsset in project openremote by openremote.

the class MockProtocol method startAssetDiscovery.

@Override
public Future<Void> startAssetDiscovery(Consumer<AssetTreeNode[]> assetConsumer) {
    return container.getExecutorService().submit(() -> {
        // Simulate discovery init delay
        Thread.sleep(2000);
        // Discover a few assets
        assetConsumer.accept(new AssetTreeNode[] { new AssetTreeNode(new ThingAsset("MockAsset1").addAttributes(new Attribute<>("mock1", ValueType.TEXT, "dummy1"), new Attribute<>("mock2", ValueType.POSITIVE_INTEGER, 1234))), new AssetTreeNode(new ThingAsset("MockAsset2").addAttributes(new Attribute<>("mock1", ValueType.TEXT, "dummy2"), new Attribute<>("mock2", ValueType.POSITIVE_INTEGER, 1234))) });
        // Simulate a delay
        Thread.sleep(1000);
        // Discover a few assets
        assetConsumer.accept(new AssetTreeNode[] { new AssetTreeNode(new ThingAsset("MockAsset3").addAttributes(new Attribute<>("mock1", ValueType.TEXT, "dummy3"), new Attribute<>("mock2", ValueType.POSITIVE_INTEGER, 1234))), new AssetTreeNode(new ThingAsset("MockAsset3").addAttributes(new Attribute<>("mock1", ValueType.TEXT, "dummy3"), new Attribute<>("mock2", ValueType.POSITIVE_INTEGER, 1234))) });
        return null;
    });
}
Also used : AssetTreeNode(org.openremote.model.asset.AssetTreeNode) ThingAsset(org.openremote.model.asset.impl.ThingAsset)

Example 4 with ThingAsset

use of org.openremote.model.asset.impl.ThingAsset in project openremote by openremote.

the class KNXProtocol method createAsset.

protected void createAsset(StateDP datapoint, boolean isStatusGA, Map<String, Asset<?>> createdAssets) {
    String name = datapoint.getName().substring(0, datapoint.getName().length() - 3);
    String assetName = name.replaceAll(" -.*-", "");
    Asset<?> asset;
    if (createdAssets.containsKey(assetName)) {
        asset = createdAssets.get(assetName);
    } else {
        asset = new ThingAsset(assetName);
    }
    String attrName = assetName.replaceAll(" ", "");
    ValueDescriptor<?> type = TypeMapper.toAttributeType(datapoint);
    KNXAgentLink agentLink = new KNXAgentLink(agent.getId(), datapoint.getDPT(), !isStatusGA ? datapoint.getMainAddress().toString() : null, isStatusGA ? datapoint.getMainAddress().toString() : null);
    Attribute<?> attr = asset.getAttributes().get(attrName).orElse(new Attribute<>(attrName, type).addMeta(new MetaItem<>(MetaItemType.LABEL, name), new MetaItem<>(AGENT_LINK, agentLink)));
    asset.getAttributes().addOrReplace(attr);
    createdAssets.put(assetName, asset);
}
Also used : ThingAsset(org.openremote.model.asset.impl.ThingAsset)

Aggregations

ThingAsset (org.openremote.model.asset.impl.ThingAsset)4 AssetTreeNode (org.openremote.model.asset.AssetTreeNode)3 java.util (java.util)2 Consumer (java.util.function.Consumer)2 Level (java.util.logging.Level)2 Asset (org.openremote.model.asset.Asset)2 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)2 MetaItemType (org.openremote.model.value.MetaItemType)2 AGENT_LINK (org.openremote.model.value.MetaItemType.AGENT_LINK)2 StringReader (java.io.StringReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Logger (java.util.logging.Logger)1 Collectors.toList (java.util.stream.Collectors.toList)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)1 IOClient (org.openremote.agent.protocol.io.IOClient)1 VelbusDeviceType (org.openremote.agent.protocol.velbus.device.VelbusDeviceType)1