Search in sources :

Example 1 with ProtocolAssetImport

use of org.openremote.model.protocol.ProtocolAssetImport 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)

Aggregations

StringReader (java.io.StringReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 Future (java.util.concurrent.Future)1 Consumer (java.util.function.Consumer)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)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 Container (org.openremote.model.Container)1 Asset (org.openremote.model.asset.Asset)1 AssetTreeNode (org.openremote.model.asset.AssetTreeNode)1 AgentLink.getOrThrowAgentLinkProperty (org.openremote.model.asset.agent.AgentLink.getOrThrowAgentLinkProperty)1 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)1 ThingAsset (org.openremote.model.asset.impl.ThingAsset)1 org.openremote.model.attribute (org.openremote.model.attribute)1 ProtocolAssetImport (org.openremote.model.protocol.ProtocolAssetImport)1 SyslogCategory (org.openremote.model.syslog.SyslogCategory)1