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
Velbus concurrency improvements
Rich Turner78b4c90b377b
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.javaindex be4395988..b5c5ef075 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java@@ -69,7 +69,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implementspublic static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractVelbusProtocol.class);protected final Map<String, VelbusNetwork> networkMap = new HashMap<>();protected final Map<AttributeRef, Pair<VelbusNetwork, Consumer<ConnectionStatus>>> networkConfigurationMap = new HashMap<>();- protected final Map<AttributeRef, Consumer<DevicePropertyValue>> attributePropertyValueConsumers = new HashMap<>();+ protected final Map<AttributeRef, Consumer<DevicePropertyValue<?>>> attributePropertyValueConsumers = new HashMap<>();protected static final String VERSION = "1.0";protected static final List<MetaItemDescriptorImpl> META_ITEM_DESCRIPTORS = Collections.singletonList(new MetaItemDescriptorImpl(@@ -205,7 +205,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implementsValueType valueType = attribute.getType().orElse(AttributeValueType.STRING).getValueType();LOG.fine("Linking attribute to device '" +deviceAddress + "' and property '" + property + "': " + attributeRef);- Consumer<DevicePropertyValue> propertyValueConsumer = propertyValue ->+ Consumer<DevicePropertyValue<?>> propertyValueConsumer = propertyValue ->updateLinkedAttribute(new AttributeState(attributeRef, propertyValue != null ? propertyValue.toValue(valueType) : null));attributePropertyValueConsumers.put(attributeRef, propertyValueConsumer);@@ -229,7 +229,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implementsString property = getVelbusDevicePropertyLink(attribute);AttributeRef attributeRef = attribute.getReferenceOrThrow();- Consumer<DevicePropertyValue> propertyValueConsumer = attributePropertyValueConsumers.remove(attributeRef);+ Consumer<DevicePropertyValue<?>> propertyValueConsumer = attributePropertyValueConsumers.remove(attributeRef);velbusNetwork.removePropertyValueConsumer(deviceAddress, property, propertyValueConsumer);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.javaindex 4e70517e0..9153a2e89 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java@@ -77,12 +77,14 @@ public class VelbusNetwork {}public synchronized void sendPackets(VelbusPacket... packets) {- if (getConnectionStatus() == ConnectionStatus.CONNECTED) {- messageQueue.addAll(Arrays.asList(packets));+ if (getConnectionStatus() != ConnectionStatus.CONNECTED) {+ return;+ }- if (queueProcessingTask == null) {- startSendingPackets();- }+ messageQueue.addAll(Arrays.asList(packets));++ if (queueProcessingTask == null) {+ startSendingPackets();}}@@ -133,7 +135,9 @@ public class VelbusNetwork {if (status == ConnectionStatus.CONNECTED) {// Don't process stale messages- messageQueue.clear();+ synchronized (messageQueue) {+ messageQueue.clear();+ }// Initialise the devicesfor (int i=0; i<devices.length; i++) {@@ -149,8 +153,14 @@ public class VelbusNetwork {}}// Clear out sub device registrations- for (int i=0; i<subAddressDevices.length; i++) {- subAddressDevices[i] = null;+ Arrays.fill(subAddressDevices, null);++ synchronized (messageQueue) {+ messageQueue.clear();+ if (queueProcessingTask != null) {+ queueProcessingTask.cancel(false);+ queueProcessingTask = null;+ }}}}@@ -179,28 +189,29 @@ public class VelbusNetwork {}}- public void addPropertyValueConsumer(int deviceAddress, String property, Consumer<DevicePropertyValue> propertyValueConsumer) {+ public void addPropertyValueConsumer(int deviceAddress, String property, Consumer<DevicePropertyValue<?>> propertyValueConsumer) {if (deviceAddress < 1 || deviceAddress > 254) {LOG.warning("Invalid device address: " + deviceAddress);return;}VelbusDevice device = getDevice(deviceAddress);+ boolean deviceExists = device != null;- if (device == null) {+ if (!deviceExists) {// Device hasn't been created yet so create it- device = new VelbusDevice(deviceAddress, this);+ device = new VelbusDevice(deviceAddress, this);devices[deviceAddress-1] = device;-- if (getConnectionStatus() == ConnectionStatus.CONNECTED) {- device.initialise();- }}device.addPropertyValueConsumer(property, propertyValueConsumer);++ if (!deviceExists && getConnectionStatus() == ConnectionStatus.CONNECTED) {+ device.initialise();+ }}- public void removePropertyValueConsumer(int deviceAddress, String property, Consumer<DevicePropertyValue> propertyValueConsumer) {+ public void removePropertyValueConsumer(int deviceAddress, String property, Consumer<DevicePropertyValue<?>> propertyValueConsumer) {if (deviceAddress < 1 || deviceAddress > 254) {LOG.warning("Invalid device address: " + deviceAddress);return;@@ -219,7 +230,6 @@ public class VelbusNetwork {if (device != null) {device.removeAllPropertyValueConsumers();}- //devices[i] = null;}}@@ -257,11 +267,7 @@ public class VelbusNetwork {return devices[address-1];}- protected synchronized void startSendingPackets() {- if (queueProcessingTask != null) {- return;- }-+ protected void startSendingPackets() {queueProcessingTask = getExecutorService().scheduleWithFixedDelay(this::doSendPacket,0,@@ -270,18 +276,22 @@ public class VelbusNetwork {);}- protected void doSendPacket() {- if (getConnectionStatus() == ConnectionStatus.CONNECTED) {- VelbusPacket packet = messageQueue.poll();- if (packet != null) {- VelbusPacket.OutboundCommand command = VelbusPacket.OutboundCommand.fromCode(packet.getCommand());- LOG.finest("Sending packet " + command + " : " + packet);- client.sendMessage(packet);- } else {- queueProcessingTask.cancel(false);- queueProcessingTask = null;- }+ protected synchronized void doSendPacket() {+ if (getConnectionStatus() != ConnectionStatus.CONNECTED) {+ return;+ }++ VelbusPacket packet = messageQueue.poll();++ if (packet == null) {+ queueProcessingTask.cancel(false);+ queueProcessingTask = null;+ return;}++ VelbusPacket.OutboundCommand command = VelbusPacket.OutboundCommand.fromCode(packet.getCommand());+ LOG.finest("Sending packet " + command + " : " + packet);+ client.sendMessage(packet);}public ScheduledFuture scheduleTask(Runnable runnable, int delayMillis) {@@ -289,7 +299,7 @@ public class VelbusNetwork {scheduledTasks.removeIf(Future::isDone);if (getConnectionStatus() == ConnectionStatus.CONNECTED) {- ScheduledFuture future = getExecutorService().schedule(runnable, delayMillis, TimeUnit.MILLISECONDS);+ ScheduledFuture<?> future = getExecutorService().schedule(runnable, delayMillis, TimeUnit.MILLISECONDS);scheduledTasks.add(future);return future;}diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.javaindex 5b05a708c..1fefe99ec 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.java@@ -2,35 +2,32 @@ package org.openremote.agent.protocol.velbus.device;import org.openremote.agent.protocol.velbus.VelbusNetwork;import org.openremote.agent.protocol.velbus.VelbusPacket;+import org.openremote.model.util.TextUtil;import org.openremote.model.value.Value;import java.util.*;+import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.function.Consumer;-import java.util.stream.Collectors;import static org.openremote.agent.protocol.velbus.AbstractVelbusProtocol.LOG;public class VelbusDevice {public static final int MAX_INITIALISATION_ATTEMPTS = 5;- public static final int INITIALISATION_TIMEOUT_SECONDS = 10;+ public static int INITIALISATION_TIMEOUT_MILLISECONDS = 10000;protected int baseAddress;protected int[] subAddresses = new int[4]; // Max of 4 sub-addresses- protected Map<String, DevicePropertyValue> devicePropertyCache = new HashMap<>();- protected final Map<String, List<Consumer<DevicePropertyValue>>> propertyValueConsumers = new HashMap<>();- //protected Map<AttributeRef, AssetAttribute> linkedAttribute = new HashMap<>();- //protected Map<String, List<AssetAttribute>> propertyAttributeMap = new HashMap<>();+ protected final Map<String, DevicePropertyValue<?>> devicePropertyCache = new HashMap<>();+ protected final Map<String, List<Consumer<DevicePropertyValue<?>>>> propertyValueConsumers = new ConcurrentHashMap<>();protected VelbusNetwork velbusNetwork;protected FeatureProcessor[] featureProcessors;protected boolean initialised;protected boolean initialisationFailed;protected int initialisationAttempts;protected VelbusDeviceType deviceType;- protected Future initialisationTask;- protected final Object initialisationLock = new Object();-+ protected Future<?> initialisationTask;public VelbusDevice(int baseAddress, VelbusNetwork velbusNetwork) {this.baseAddress = baseAddress;@@ -67,7 +64,7 @@ public class VelbusDevice {return -1;}- private synchronized void setDeviceType(VelbusDeviceType deviceType) {+ private void setDeviceType(VelbusDeviceType deviceType) {this.deviceType = deviceType;featureProcessors = deviceType.getFeatureProcessors();}@@ -79,44 +76,42 @@ public class VelbusDevice {}}- public synchronized void reset() {+ public void reset() {cancelInitialisationTask(true);devicePropertyCache.clear();initialised = false;initialisationAttempts = 0;deviceType = null;-- for (int i=0; i<subAddresses.length; i++) {- subAddresses[i] = 0;- }+ Arrays.fill(subAddresses, 0);}/*** Attempt initialisation of this device*/public void initialise() {- synchronized (initialisationLock) {+ synchronized (this) {if (isInitialised()) {return;}- if (initialisationFailed) {- LOG.finest("Device initialisation already failed");- return;- }-if (initialisationTask != null) {LOG.finest("Initialisation already in progress");return;}+ if (initialisationFailed) {+ initialisationAttempts = 0;+ initialisationFailed = false;+ LOG.finest("Re-attempting device initialisation");+ }+LOG.info("Initialisation starting: " + getBaseAddress());- initialisationTask = velbusNetwork.getExecutorService().scheduleWithFixedDelay(this::doInitialisation, 0, INITIALISATION_TIMEOUT_SECONDS, TimeUnit.SECONDS);+ initialisationTask = velbusNetwork.getExecutorService().scheduleWithFixedDelay(this::doInitialisation, 0, INITIALISATION_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);}}private void doInitialisation() {- synchronized (initialisationLock) {+ synchronized (this) {if (initialisationAttempts >= MAX_INITIALISATION_ATTEMPTS) {LOG.fine("Initialisation failed - Device has reached maximum initialisation attempts: " + getBaseAddress());initialisationFailed = true;@@ -143,75 +138,72 @@ public class VelbusDevice {* Indicates that the device is now initialised and if so it updates the flag and performs post initialisation tasks*/private void onInitialised() {- synchronized (initialisationLock) {+ synchronized (this) {if (isInitialised()) {return;}cancelInitialisationTask(true);-LOG.info("Device initialised: " + getBaseAddress());this.initialised = true;+ }- // Send the status request packets for this device- if (isInitialisedAndValid() && featureProcessors != null) {-- List<VelbusPacket> statusPackets = Arrays.stream(featureProcessors)- .flatMap(processor -> processor.getStatusRequestPackets(this).stream())- .distinct()- .collect(Collectors.toList());+ // Send the status request packets for this device+ if (isInitialisedAndValid() && featureProcessors != null) {- LOG.fine("Sending module status request packets");- velbusNetwork.sendPackets(statusPackets.toArray(new VelbusPacket[statusPackets.size()]));- }+ LOG.fine("Sending module status request packets");+ velbusNetwork.sendPackets(Arrays.stream(featureProcessors)+ .flatMap(processor -> processor.getStatusRequestPackets(this).stream())+ .distinct().toArray(VelbusPacket[]::new));}}- public void addPropertyValueConsumer(String property, Consumer<DevicePropertyValue> propertyValueConsumer) {+ public void addPropertyValueConsumer(String property, Consumer<DevicePropertyValue<?>> propertyValueConsumer) {if (property.isEmpty()) {return;}- synchronized (propertyValueConsumers) {- propertyValueConsumers- .computeIfAbsent(property, p -> new ArrayList<>())- .add(propertyValueConsumer);+ List<Consumer<DevicePropertyValue<?>>> consumers = propertyValueConsumers+ .computeIfAbsent(property, p -> new ArrayList<>());- // Push the current value of the property to the consumer- propertyValueConsumer.accept(getPropertyValue(property));+ //noinspection SynchronizationOnLocalVariableOrMethodParameter+ synchronized (consumers) {+ consumers.add(propertyValueConsumer);}++ // Push the current value of the property to the consumer+ propertyValueConsumer.accept(getPropertyValue(property));}- public void removePropertyValueConsumer(String property, Consumer<DevicePropertyValue> propertyValueConsumer) {+ public void removePropertyValueConsumer(String property, Consumer<DevicePropertyValue<?>> propertyValueConsumer) {if (property.isEmpty()) {return;}- synchronized (propertyValueConsumers) {- propertyValueConsumers.computeIfPresent(property, (prop, consumers) -> {- consumers.remove(propertyValueConsumer);- return consumers;- });- }+ propertyValueConsumers.computeIfPresent(property, (prop, consumers) -> {+ //noinspection SynchronizationOnLocalVariableOrMethodParameter+ synchronized (consumers) {+ consumers.remove(propertyValueConsumer);+ }+ return consumers;+ });}public void removeAllPropertyValueConsumers() {- synchronized (propertyValueConsumers) {- propertyValueConsumers.forEach((prop, consumers) -> {- consumers.clear();- });+ propertyValueConsumers.forEach((prop, consumers) -> {+ consumers.clear();+ });- propertyValueConsumers.clear();- }+ propertyValueConsumers.clear();}- public synchronized void writeProperty(String property, Value value) {+ public void writeProperty(String property, Value value) {if (!isInitialisedAndValid()) {LOG.fine("Ignoring property write as device is not initialised and/or it is invalid");return;}- if (property.isEmpty()) {+ if (TextUtil.isNullOrEmpty(property)) {return;}@@ -219,14 +211,14 @@ public class VelbusDevice {for (FeatureProcessor processor : featureProcessors) {List<VelbusPacket> packets = processor.getPropertyWritePackets(this, property, value);if (packets != null) {- velbusNetwork.sendPackets(packets.toArray(new VelbusPacket[packets.size()]));+ velbusNetwork.sendPackets(packets.toArray(new VelbusPacket[0]));break;}}}}- public synchronized void processReceivedPacket(VelbusPacket velbusPacket) {+ public void processReceivedPacket(VelbusPacket velbusPacket) {VelbusPacket.InboundCommand packetCommand = VelbusPacket.InboundCommand.fromCode(velbusPacket.getCommand());switch (packetCommand) {@@ -285,24 +277,26 @@ public class VelbusDevice {}}- synchronized void setProperty(String property, DevicePropertyValue value) {+ void setProperty(String property, DevicePropertyValue<?> value) {property = property.toUpperCase();- devicePropertyCache.put(property, value);+ synchronized (devicePropertyCache) {+ devicePropertyCache.put(property, value);+ }// Notify linked consumers- synchronized (propertyValueConsumers) {- propertyValueConsumers.computeIfPresent(property, (prop, consumers) -> {+ propertyValueConsumers.computeIfPresent(property, (prop, consumers) -> {+ synchronized (consumers) {consumers.forEach(consumer -> consumer.accept(value));- return consumers;- });- }+ }+ return consumers;+ });}protected DevicePropertyValue getPropertyValue(String propertyName) {return devicePropertyCache.get(propertyName);}- protected synchronized boolean hasPropertyValue(String propertyName) {+ protected boolean hasPropertyValue(String propertyName) {return devicePropertyCache.containsKey(propertyName);}diff --git a/test/src/main/groovy/org/openremote/test/MockVelbusClient.java b/test/src/main/groovy/org/openremote/test/MockVelbusClient.javaindex 2e0cfe10d..d59fa8f41 100644--- a/test/src/main/groovy/org/openremote/test/MockVelbusClient.java+++ b/test/src/main/groovy/org/openremote/test/MockVelbusClient.java@@ -78,17 +78,23 @@ public class MockVelbusClient implements IoClient<VelbusPacket> {@Overridepublic void addMessageConsumer(Consumer<VelbusPacket> messageConsumer) {- messageConsumers.add(messageConsumer);+ synchronized (messageConsumers) {+ messageConsumers.add(messageConsumer);+ }}@Overridepublic void removeMessageConsumer(Consumer<VelbusPacket> messageConsumer) {- messageConsumers.remove(messageConsumer);+ synchronized (messageConsumers) {+ messageConsumers.remove(messageConsumer);+ }}@Overridepublic void removeAllMessageConsumers() {- messageConsumers.clear();+ synchronized (messageConsumers) {+ messageConsumers.clear();+ }}@Overridediff --git a/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusBasicTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusBasicTest.groovyindex ae18fa6b7..de286b884 100644--- a/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusBasicTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusBasicTest.groovy@@ -32,7 +32,6 @@ import spock.lang.Sharedimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions-import static org.openremote.container.util.MapAccess.getIntegerimport static spock.util.matcher.HamcrestMatchers.closeToclass VelbusBasicTest extends Specification {@@ -43,7 +42,7 @@ class VelbusBasicTest extends Specification {def static MockVelbusClient messageProcessor = new MockVelbusClient()@Shared- def static PollingConditions conditions = new PollingConditions(timeout: 15, delay: 0.2)+ def static PollingConditions conditions = new PollingConditions(timeout: 10, delay: 0.2)static loadDevicePackets(MockVelbusClient messageProcessor) {messageProcessor.mockPackets = [@@ -801,10 +800,12 @@ class VelbusBasicTest extends Specification {}static writeDelayMillis = VelbusNetwork.DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS+ static timeoutMillis = VelbusDevice.INITIALISATION_TIMEOUT_MILLISECONDSdef setupSpec() {- VelbusNetwork.DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS = 1; // Minimal delay for simulated network-+ // Minimal delays for simulated network+ VelbusNetwork.DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS = 10+ VelbusDevice.INITIALISATION_TIMEOUT_MILLISECONDS = 10// Uncomment and configure the below lines to communicate with an actual VELBUS network// def client = new VelbusSocketMessageProcessor("192.168.0.65", 6000, protocolExecutorService);// VelbusNetwork.DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS = 100;@@ -819,6 +820,7 @@ class VelbusBasicTest extends Specification {def cleanupSpec() {VelbusNetwork.DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS = writeDelayMillis+ VelbusDevice.INITIALISATION_TIMEOUT_MILLISECONDS = timeoutMillisif (network != null) {network.close()}@@ -838,8 +840,8 @@ class VelbusBasicTest extends Specification {Thread.sleep(20)counter++}- network.removeAllDevices()network.disconnect()+ network.removeAllDevices()}def "Input Button Processor Test"() {@@ -851,7 +853,7 @@ class VelbusBasicTest extends Specification {when: "A device property value consumer is registered for device address 2 (VMB4AN)"DevicePropertyValue ioAlarm1;network.addPropertyValueConsumer(2, "CH1", {- newValue -> ioAlarm1 = newValue;+ newValue -> ioAlarm1 = newValue})device = network.getDevice(2)