100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Z-Wave bugfix
Rainer Hitzb6c0a107e66d
diff --git a/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveNetwork.java b/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveNetwork.javaindex 1203ae5f0..eb051112d 100644--- a/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveNetwork.java+++ b/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveNetwork.java@@ -28,6 +28,7 @@ import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.attribute.MetaItem;import org.openremote.model.value.MetaItemType;import org.openremote.model.value.Values;+import org.openremote.model.value.impl.ColourRGB;import org.openremote.protocol.zwave.ConnectionException;import org.openremote.protocol.zwave.model.Controller;import org.openremote.protocol.zwave.model.ZWEndPoint;@@ -81,7 +82,7 @@ public class ZWaveNetwork {ioClient.addConnectionStatusConsumer(this::onConnectionStatusChanged);controller = new Controller(NettyConnectionManager.create(configuration, ioClient));-+try {controller.connect();} catch (ConfigurationException | ConnectionException e) {@@ -96,7 +97,7 @@ public class ZWaveNetwork {if (controller == null) {return;}-+try {controller.disconnect();} catch (ConnectionException e) {@@ -178,7 +179,15 @@ public class ZWaveNetwork {zwValue = Values.getBoolean(value).map(BooleanValue::new).orElse(null);break;case ARRAY:-+ if (value instanceof ColourRGB) {+ ArrayValue zwArray = new ArrayValue();+ zwArray.add(new NumberValue(((ColourRGB)value).getR()));+ zwArray.add(new NumberValue(((ColourRGB)value).getG()));+ zwArray.add(new NumberValue(((ColourRGB)value).getB()));+ zwValue = zwArray;+ }+ break;+ /*zwValue = Values.getValue(value, Object[].class).map(arrValue -> {ArrayValue zwArray = new ArrayValue();@@ -211,6 +220,7 @@ public class ZWaveNetwork {);return zwArray;}).orElse(null);+ */}return zwValue;@@ -233,7 +243,7 @@ public class ZWaveNetwork {}}-+private Channel findChannel(int nodeId, int endpointNumber, String channelName) {Channel channel = null;if (controller != null) {@@ -396,7 +406,7 @@ public class ZWaveNetwork {return new AssetTreeNode(device);}-+private static List<Attribute<?>> createNodeInfoAttributes(String agentId, ZWaveNode node) {List<Attribute<?>> attributes = new ArrayList<>();diff --git a/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.javaindex 61c239b70..37f68e84a 100644--- a/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.java@@ -29,8 +29,12 @@ import org.openremote.model.attribute.AttributeRef;import org.openremote.model.attribute.AttributeState;import org.openremote.model.protocol.ProtocolAssetDiscovery;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.value.impl.ColourRGB;import org.openremote.protocol.zwave.model.commandclasses.channel.value.Value;+import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;import java.util.Map;import java.util.concurrent.CompletableFuture;import java.util.concurrent.Future;@@ -53,7 +57,7 @@ public class ZWaveProtocol extends AbstractProtocol<ZWaveAgent, ZWaveAgent.ZWave// Protected Instance Fields --------------------------------------------------------------------protected ZWaveNetwork network;- protected Map<AttributeRef, Consumer<Value>> sensorValueConsumerMap;+ protected Map<AttributeRef, Consumer<Value>> sensorValueConsumerMap = new HashMap<>();public ZWaveProtocol(ZWaveAgent agent) {super(agent);@@ -67,39 +71,42 @@ public class ZWaveProtocol extends AbstractProtocol<ZWaveAgent, ZWaveAgent.ZWave}@Override- public String getProtocolInstanceUri() {+ public synchronized String getProtocolInstanceUri() {return network != null && network.ioClient != null ? network.ioClient.getClientUri() : "";}// Implements AbstractProtocol ------------------------------------------------------------------@Override- protected void doStart(Container container) throws Exception {+ protected synchronized void doStart(Container container) throws Exception {String serialPort = agent.getSerialPort().orElseThrow(() -> new IllegalStateException("Invalid serial port property"));- ZWaveNetwork network = new ZWaveNetwork(serialPort, executorService);+ network = new ZWaveNetwork(serialPort, executorService);network.addConnectionStatusConsumer(this::setConnectionStatus);network.connect();}@Override- protected void doStop(Container container) throws Exception {+ protected synchronized void doStop(Container container) throws Exception {if (network != null) {network.removeConnectionStatusConsumer(this::setConnectionStatus);network.disconnect();+ network = null;}}@Overrideprotected synchronized void doLinkAttribute(String assetId, Attribute<?> attribute, ZWaveAgent.ZWaveAgentLink agentLink) {-+ if (network == null) {+ return;+ }int nodeId = agentLink.getDeviceNodeId().orElse(0);int endpoint = agentLink.getDeviceEndpoint().orElse(0);String linkName = agentLink.getDeviceValue().orElse("");AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());- // TODO: Value must be compatible with the value type of the attribute...for non primitives the object types must match+ Class<?> clazz = (attribute == null ? null : attribute.getType().getType());Consumer<Value> sensorValueConsumer = value ->- updateLinkedAttribute(new AttributeState(attributeRef, value));+ updateLinkedAttribute(new AttributeState(attributeRef, toAttributeValue(value, clazz)));sensorValueConsumerMap.put(attributeRef, sensorValueConsumer);network.addSensorValueConsumer(nodeId, endpoint, linkName, sensorValueConsumer);@@ -107,18 +114,22 @@ public class ZWaveProtocol extends AbstractProtocol<ZWaveAgent, ZWaveAgent.ZWave@Overrideprotected synchronized void doUnlinkAttribute(String assetId, Attribute<?> attribute, ZWaveAgent.ZWaveAgentLink agentLink) {+ if (network == null) {+ return;+ }AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());Consumer<Value> sensorValueConsumer = sensorValueConsumerMap.remove(attributeRef);network.removeSensorValueConsumer(sensorValueConsumer);}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, ZWaveAgent.ZWaveAgentLink agentLink, AttributeEvent event, Object processedValue) {-+ protected synchronized void doLinkedAttributeWrite(Attribute<?> attribute, ZWaveAgent.ZWaveAgentLink agentLink, AttributeEvent event, Object processedValue) {+ if (network == null) {+ return;+ }int nodeId = getOrThrowAgentLinkProperty(agentLink.getDeviceNodeId(), "device node ID");int endpoint = getOrThrowAgentLinkProperty(agentLink.getDeviceEndpoint(), "device endpoint");String linkName = getOrThrowAgentLinkProperty(agentLink.getDeviceValue(), "device property");-network.writeChannel(nodeId, endpoint, linkName, processedValue);}@@ -126,15 +137,13 @@ public class ZWaveProtocol extends AbstractProtocol<ZWaveAgent, ZWaveAgent.ZWave// Implements ProtocolAssetDiscovery ------------------------------------------------@Override- public Future<Void> startAssetDiscovery(Consumer<AssetTreeNode[]> assetConsumer) {-+ public synchronized Future<Void> startAssetDiscovery(Consumer<AssetTreeNode[]> assetConsumer) {if (network == null || network.getConnectionStatus() != ConnectionStatus.CONNECTED) {LOG.info("Network not connected so cannot perform discovery");return CompletableFuture.completedFuture(null);}return executorService.submit(() -> {-try {AssetTreeNode[] assetTreeNodes = network.discoverDevices(agent);assetConsumer.accept(assetTreeNodes);@@ -143,4 +152,41 @@ public class ZWaveProtocol extends AbstractProtocol<ZWaveAgent, ZWaveAgent.ZWave}}, null);}+++ // Private Instance Methods -------------------------------------------------------------------++ private Object toAttributeValue(org.openremote.protocol.zwave.model.commandclasses.channel.value.Value value, Class<?> clazz) {+ if (value == null || clazz == null) {+ return null;+ }+ Object retValue = null;+ if (clazz == String.class) {+ retValue = value.getString();+ } else if (clazz == Boolean.class) {+ retValue = value.getBoolean();+ } else if (clazz == Double.class) {+ retValue = value.getNumber();+ } else if (clazz == Integer.class) {+ retValue = value.getInteger();+ } else if (clazz == ColourRGB.class && value instanceof org.openremote.protocol.zwave.model.commandclasses.channel.value.ArrayValue) {+ org.openremote.protocol.zwave.model.commandclasses.channel.value.ArrayValue zwArray = (org.openremote.protocol.zwave.model.commandclasses.channel.value.ArrayValue) value;+ if (zwArray.length() >= 3) {+ List<Object> values = new ArrayList<>(zwArray.length());+ for (int i = 0; i < zwArray.length(); i++) {+ values.add(toAttributeValue(zwArray.get(i), Integer.class));+ }+ if (values.stream().anyMatch(val -> val == null)) {+ return null;+ }+ int offset = (zwArray.length() == 3 ? 0 : 1); // RGB : ARGB+ retValue = new ColourRGB(+ (Integer) values.get(0 + offset), (Integer) values.get(1 + offset), (Integer) values.get(2 + offset)+ );+ }+ } else {+ LOG.warning("Couldn't update Z-Wave sensor value because of unexpected attribute type: " + clazz);+ }+ return retValue;+ }}diff --git a/docker-compose.yml b/docker-compose.ymlindex d973346e7..e0f0316d4 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -46,3 +46,6 @@ services:environment:DEV_MODE: ${DEV_MODE:-false}SETUP_ADMIN_PASSWORD: ${SETUP_ADMIN_PASSWORD:-secret}+# devices:+# - /dev/ttyACM0:/dev/ttyS0+diff --git a/manager/Dockerfile b/manager/Dockerfileindex 2e2adc2d0..9fc1964c6 100644--- a/manager/Dockerfile+++ b/manager/Dockerfile@@ -85,6 +85,7 @@ HEALTHCHECK --interval=3s --timeout=3s --start-period=30s --retries=120 CMD curlRUN mkdir -p /opt/appRUN mkdir -p /deployment/manager/extensions+RUN mkdir -p /zwaveWORKDIR /opt/app