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()]);
}
Aggregations