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