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