Search in sources :

Example 1 with VelbusDeviceType

use of org.openremote.agent.protocol.velbus.device.VelbusDeviceType in project openremote by openremote.

the class AbstractVelbusProtocol method discoverLinkedAssetAttributes.

@Override
public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
    Document xmlDoc;
    try {
        String xmlStr = fileInfo.isBinary() ? new String(CodecUtil.decodeBase64(fileInfo.getContents()), "UTF8") : fileInfo.getContents();
        LOG.info("Parsing VELBUS project file: " + fileInfo.getName());
        xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xmlStr)));
    } catch (Exception e) {
        throw new IllegalStateException("Failed to convert file into XML", e);
    }
    xmlDoc.getDocumentElement().normalize();
    NodeList modules = xmlDoc.getElementsByTagName("Module");
    LOG.info("Found " + modules.getLength() + " module(s)");
    List<Asset> devices = new ArrayList<>(modules.getLength());
    MetaItem agentLink = AgentLink.asAgentLinkMetaItem(protocolConfiguration.getReferenceOrThrow());
    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(",");
        Integer 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;
        Asset device = new Asset(name, AssetType.THING);
        device.setAttributes(new AssetAttribute("build", AttributeType.STRING, Values.create(build)).setMeta(new MetaItem(AssetMeta.LABEL, Values.create("Build")), new MetaItem(AssetMeta.READ_ONLY, Values.create(true))), new AssetAttribute("serialNumber", AttributeType.STRING, Values.create(serial)).setMeta(new MetaItem(AssetMeta.LABEL, Values.create("Serial No")), new MetaItem(AssetMeta.READ_ONLY, Values.create(true))));
        getLinkedAttributeDescriptors(deviceType.get(), baseAddress).forEach(descriptor -> {
            AssetAttribute attribute = new AssetAttribute(descriptor.getName(), descriptor.getAttributeType()).setMeta(agentLink, new MetaItem(AssetMeta.LABEL, Values.create(descriptor.getDisplayName()))).addMeta(descriptor.getMetaItems());
            if (descriptor.isReadOnly()) {
                attribute.addMeta(new MetaItem(AssetMeta.READ_ONLY, Values.create(true)));
            } else if (descriptor.isExecutable()) {
                attribute.addMeta(new MetaItem(AssetMeta.EXECUTABLE, Values.create(true)));
            }
            device.addAttributes(attribute);
        });
        devices.add(device);
    }
    return devices.toArray(new Asset[devices.size()]);
}
Also used : 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) StringReader(java.io.StringReader) AssetAttribute(org.openremote.model.asset.AssetAttribute) Asset(org.openremote.model.asset.Asset)

Aggregations

StringReader (java.io.StringReader)1 VelbusDeviceType (org.openremote.agent.protocol.velbus.device.VelbusDeviceType)1 Asset (org.openremote.model.asset.Asset)1 AssetAttribute (org.openremote.model.asset.AssetAttribute)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1 InputSource (org.xml.sax.InputSource)1