use of org.openremote.model.value.MetaItemType.AGENT_LINK in project openremote by openremote.
the class AgentService method startAgent.
protected void startAgent(Agent<?, ?, ?> agent) {
withLock(getClass().getSimpleName() + "::startAgent", () -> {
Protocol<?> protocol = null;
try {
protocol = agent.getProtocolInstance();
protocolInstanceMap.put(agent.getId(), protocol);
LOG.fine("Starting protocol instance: " + protocol);
protocol.start(container);
LOG.fine("Started protocol instance:" + protocol);
LOG.finer("Linking attributes to protocol instance: " + protocol);
// Get all assets that have attributes with agent link meta for this agent
List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().attributes(new AttributePredicate().meta(new NameValuePredicate(AGENT_LINK, new StringPredicate(agent.getId()), false, new NameValuePredicate.Path("id")))));
LOG.finer("Found '" + assets.size() + "' asset(s) with attributes linked to this protocol instance: " + protocol);
assets.forEach(asset -> getGroupedAgentLinkAttributes(asset.getAttributes().stream(), assetAttribute -> assetAttribute.getMetaValue(AGENT_LINK).map(agentLink -> agentLink.getId().equals(agent.getId())).orElse(false)).forEach((agnt, attributes) -> linkAttributes(agnt, asset.getId(), attributes)));
} catch (Exception e) {
if (protocol != null) {
try {
protocol.stop(container);
} catch (Exception ignored) {
}
}
protocolInstanceMap.remove(agent.getId());
LOG.log(Level.SEVERE, "Failed to start protocol instance for agent: " + agent, e);
sendAttributeEvent(new AttributeEvent(agent.getId(), Agent.STATUS.getName(), ConnectionStatus.ERROR));
}
});
}
use of org.openremote.model.value.MetaItemType.AGENT_LINK 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