Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added protocol config options to Simulator Protocol

Rich Turner76c3fa4ed2fc
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java
index 1cba97fda..7a2f83d43 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java
@@ -22,25 +22,67 @@ package org.openremote.agent3.protocol.simulator;
import elemental.json.JsonValue;
import org.openremote.agent3.protocol.AbstractProtocol;
import org.openremote.agent3.protocol.simulator.element.*;
-import org.openremote.model.AttributeRef;
-import org.openremote.model.AttributeState;
-import org.openremote.model.AttributeEvent;
-import org.openremote.model.MetaItem;
+import org.openremote.model.*;
+import org.openremote.model.asset.ProtocolConfiguration;
import org.openremote.model.asset.ThingAttribute;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.asset.AssetMeta.RANGE_MAX;
import static org.openremote.model.asset.AssetMeta.RANGE_MIN;
+// TODO: Should putState be allowed or should users be forced to go through the AssetProcessingService processing chain
public class SimulatorProtocol extends AbstractProtocol {
+ /**
+ * Controls how and when sensor update is called after an actuator write.
+ */
+ public enum Mode {
+ // Send to actuator values are written through to sensor immediately
+ WRITE_THROUGH_IMMEDIATE,
+
+ // Send to actuator values are written through to sensor after configured delay
+ WRITE_THROUGH_DELAYED,
+
+ // Producer of send to actuator will have to manually update the sensor by calling updateSensor
+ MANUAL
+ }
+
+ /**
+ * Stores protocol config parameters.
+ */
+ public static class Instance {
+ protected Mode mode;
+ protected int delayMilliseconds;
+
+ public Instance(Mode mode, int delayMilliseconds) {
+ this.mode = mode;
+ this.delayMilliseconds = delayMilliseconds;
+ }
+
+ public Mode getMode() {
+ return mode;
+ }
+
+ public int getDelayMilliseconds() {
+ return delayMilliseconds;
+ }
+ }
+
+ protected static final Map<AttributeRef, SimulatorElement> elements = new HashMap<>();
+
+ private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private static final Logger LOG = Logger.getLogger(org.openremote.agent3.protocol.simulator.SimulatorProtocol.class.getName());
+ public static final int DEFAULT_WRITE_DELAY = 1000;
+
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":simulator";
/**
@@ -49,11 +91,19 @@ public class SimulatorProtocol extends AbstractProtocol {
public static final String SIMULATOR_ELEMENT = PROTOCOL_NAME + ":element";
/**
- * Optional (defaults to false) meta item, whether actuator writes should immediately be reflected as sensor reads.
+ * Optional (defaults to {@link Mode#WRITE_THROUGH_IMMEDIATE}) determines how sensor updates occur after actuator write.
+ */
+ public static final String CONFIG_MODE = PROTOCOL_NAME + ":mode";
+
+ /**
+ * Optional (defaults to {@link #DEFAULT_WRITE_DELAY}) used in {@link Mode#WRITE_THROUGH_DELAYED} mode to control
+ * delay between actuator write and sensor update
*/
- public static final String SIMULATOR_REFLECT_ACTUATOR_WRITES = PROTOCOL_NAME + ":reflectActuatorWrites";
+ public static final String CONFIG_WRITE_DELAY_MILLISECONDS = PROTOCOL_NAME + ":delayMilliseconds";
- static final protected Map<AttributeRef, SimulatorElement> elements = new HashMap<>();
+ static final protected Map<String, Instance> instances = new HashMap<>();
+
+ static final protected Map<AttributeRef, String> attributeInstanceMap = new HashMap<>();
@Override
public String getProtocolName() {
@@ -63,12 +113,34 @@ public class SimulatorProtocol extends AbstractProtocol {
@Override
protected void onAttributeAdded(ThingAttribute attribute) {
String elementType = attribute.firstMetaItemOrThrow(SIMULATOR_ELEMENT).getValueAsString();
+ ProtocolConfiguration config = attribute.getProtocolConfiguration();
+ String configName = config.getName();
+
+ Instance instance = instances.get(configName);
+
+ if (instance == null) {
+ MetaItem configModeMeta = config.firstMetaItem(CONFIG_MODE);
+ MetaItem configDelayMeta = config.firstMetaItem(CONFIG_WRITE_DELAY_MILLISECONDS);
+
+ Mode mode = Mode.WRITE_THROUGH_IMMEDIATE;
+ if (configModeMeta != null) {
+ try {
+ mode = Mode.valueOf(configModeMeta.getValueAsString());
+ } catch (Exception e) {
+ LOG.fine("Invalid Mode value '" + configModeMeta.getValueAsString() + "' provided");
+ }
+ }
+
+ int writeDelay = DEFAULT_WRITE_DELAY;
+ if (configDelayMeta != null) {
+ writeDelay = configDelayMeta.getValueAsInteger();
+ }
- boolean reflectActuatorWrites =
- attribute.hasMetaItem(SIMULATOR_REFLECT_ACTUATOR_WRITES)
- && attribute.firstMetaItem(SIMULATOR_REFLECT_ACTUATOR_WRITES).isValueTrue();
+ instance = new Instance(mode, writeDelay);
+ instances.put(configName, instance);
+ }
- SimulatorElement element = createElement(elementType, reflectActuatorWrites, attribute);
+ SimulatorElement element = createElement(elementType, attribute);
try {
element.setState(attribute.getValue());
} catch (IllegalArgumentException ex) {
@@ -77,6 +149,7 @@ public class SimulatorProtocol extends AbstractProtocol {
LOG.info("Putting element '" + element + "' for: " + attribute);
elements.put(attribute.getAttributeRef(), element);
+ attributeInstanceMap.put(attribute.getAttributeRef(), configName);
}
@Override
@@ -87,57 +160,112 @@ public class SimulatorProtocol extends AbstractProtocol {
@Override
protected void onAttributeRemoved(ThingAttribute attribute) {
elements.remove(attribute.getAttributeRef());
+ attributeInstanceMap.remove(attribute.getAttributeRef());
}
@Override
protected void sendToActuator(AttributeEvent event) {
- putState(event.getAttributeState(), true);
+ putState(event.getAttributeState());
+ }
+
+ /**
+ * Call this to simulate an immediate sensor update (it uses the last value supplied from send to actuator).
+ */
+ public void updateSensor(String entityId, String attributeName) {
+ updateSensor(new AttributeRef(entityId, attributeName));
}
/**
- * Call this to simulate a sensor read.
+ * Call this to simulate a sensor update after the specified delay (it uses the last value supplied from send to actuator).
+ */
+ public void updateSensor(String entityId, String attributeName, int updateSensorDelayMilliseconds) {
+ updateSensor(new AttributeRef(entityId, attributeName), updateSensorDelayMilliseconds);
+ }
+
+ /**
+ * Call this to simulate an immediate sensor update (it uses the last value supplied from send to actuator).
+ */
+ public void updateSensor(AttributeRef attributeRef) {
+ updateSensor(attributeRef, 0);
+ }
+
+ /**
+ * Call this to simulate a sensor update after the specified delay (it uses the last value supplied from send to actuator).
+ */
+ public void updateSensor(AttributeRef attributeRef, int updateSensorDelayMilliseconds) {
+ JsonValue value = getState(attributeRef);
+ final AttributeState state = new AttributeState(attributeRef, value);
+
+ if (updateSensorDelayMilliseconds <= 0) {
+ onSensorUpdate(state);
+ } else {
+ executorService.schedule(() -> onSensorUpdate(state), updateSensorDelayMilliseconds, TimeUnit.MILLISECONDS);
+ }
+ }
+
+ /**
+ * Call this to simulate a send to actuator.
*/
public void putState(String entityId, String attributeName, JsonValue value) {
- putState(new AttributeState(new AttributeRef(entityId, attributeName), value), true);
+ putState(new AttributeState(new AttributeRef(entityId, attributeName), value));
}
/**
- * Call this to simulate a sensor read.
+ * Call this to simulate a send to actuator.
*/
public void putState(AttributeRef attributeRef, JsonValue value) {
- putState(new AttributeState(attributeRef, value), true);
+ putState(new AttributeState(attributeRef, value));
}
/**
- * Call this to simulate a sensor read or actuator write.
- *
- * @param simulateSensorRead <code>true</code> if an {@link AttributeEvent} sensor update should be produced
+ * Call this to simulate a send to actuator.
*/
- public void putState(AttributeState attributeState, boolean simulateSensorRead) {
+ public void putState(AttributeEvent event) {
+ putState(event.getAttributeState());
+ }
+
+ /**
+ * Call this to simulate a send to actuator.
+ */
+ public void putState(AttributeState attributeState) {
+ AttributeRef attributeRef = attributeState.getAttributeRef();
+ String instanceName = attributeInstanceMap.get(attributeRef);
+
+ if (instanceName == null) {
+ throw new IllegalArgumentException("Attribute is not referenced by an instance:" + attributeRef);
+ }
+
+ Instance instance = instances.get(instanceName);
+
+ if (instance == null) {
+ throw new IllegalArgumentException("No instance found by name '" + instanceName + "'");
+ }
+
synchronized (elements) {
LOG.info("Put simulator state: " + attributeState);
-
- AttributeRef attributeRef = attributeState.getAttributeRef();
SimulatorElement element = elements.get(attributeRef);
- if (element == null)
+ if (element == null) {
throw new IllegalArgumentException("No simulated element for: " + attributeRef);
+ }
element.setState(attributeState.getValue());
+ }
- if (simulateSensorRead) {
- LOG.info("Propagating state change as sensor read: " + element);
- onSensorUpdate(attributeState);
- } else if (element.isReflectActuatorWrites()) {
- LOG.info("Reflecting actuator write as sensor read: " + element);
- onSensorUpdate(attributeState);
- }
+ if (instance.getMode() != Mode.MANUAL) {
+ updateSensor(attributeRef, instance.getMode() == Mode.WRITE_THROUGH_IMMEDIATE ? 0 : instance.getDelayMilliseconds());
}
}
+ /**
+ * Call this to get the current value of an attribute.
+ */
public JsonValue getState(String entityId, String attributeName) {
return getState(new AttributeRef(entityId, attributeName));
}
+ /**
+ * Call this to get the current value of an attribute.
+ */
public JsonValue getState(AttributeRef attributeRef) {
synchronized (elements) {
SimulatorElement element = elements.get(attributeRef);
@@ -147,22 +275,22 @@ public class SimulatorProtocol extends AbstractProtocol {
}
}
- protected SimulatorElement createElement(String elementType, boolean reflectActuatorWrites, ThingAttribute attribute) {
+ protected SimulatorElement createElement(String elementType, ThingAttribute attribute) {
switch (elementType.toLowerCase(Locale.ROOT)) {
case SwitchSimulatorElement.ELEMENT_NAME:
- return new SwitchSimulatorElement(reflectActuatorWrites);
+ return new SwitchSimulatorElement();
case IntegerSimulatorElement.ELEMENT_NAME_INTEGER:
- return new IntegerSimulatorElement(reflectActuatorWrites);
+ return new IntegerSimulatorElement();
case DecimalSimulatorElement.ELEMENT_NAME:
- return new DecimalSimulatorElement(reflectActuatorWrites);
+ return new DecimalSimulatorElement();
case IntegerSimulatorElement.ELEMENT_NAME_RANGE:
MetaItem minItem = attribute.firstMetaItem(RANGE_MIN);
MetaItem maxItem = attribute.firstMetaItem(RANGE_MAX);
double min = minItem != null ? minItem.getValueAsInteger() : 0;
double max = maxItem != null ? maxItem.getValueAsInteger() : 100;
- return new IntegerSimulatorElement(reflectActuatorWrites, min, max);
+ return new IntegerSimulatorElement(min, max);
case ColorSimulatorElement.ELEMENT_NAME:
- return new ColorSimulatorElement(reflectActuatorWrites);
+ return new ColorSimulatorElement();
default:
throw new UnsupportedOperationException("Can't simulate element '" + elementType + "': " + attribute);
}
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/ColorSimulatorElement.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/ColorSimulatorElement.java
index e7424ea95..f958c3c9e 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/ColorSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/ColorSimulatorElement.java
@@ -25,7 +25,7 @@ public class ColorSimulatorElement extends SimulatorElement {
public static final String ELEMENT_NAME = "color";
- public ColorSimulatorElement(boolean reflectActuatorWrites) {
- super(AttributeType.INTEGER_ARRAY, reflectActuatorWrites);
+ public ColorSimulatorElement() {
+ super(AttributeType.INTEGER_ARRAY);
}
}
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/DecimalSimulatorElement.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/DecimalSimulatorElement.java
index 71a967795..4b737e3c8 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/DecimalSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/DecimalSimulatorElement.java
@@ -25,7 +25,7 @@ public class DecimalSimulatorElement extends SimulatorElement {
public static final String ELEMENT_NAME = "decimal";
- public DecimalSimulatorElement(boolean reflectActuatorWrites) {
- super(AttributeType.DECIMAL, reflectActuatorWrites);
+ public DecimalSimulatorElement() {
+ super(AttributeType.DECIMAL);
}
}
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/IntegerSimulatorElement.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/IntegerSimulatorElement.java
index 677f9731d..41e9656e6 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/IntegerSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/IntegerSimulatorElement.java
@@ -30,12 +30,12 @@ public class IntegerSimulatorElement extends SimulatorElement {
final protected Double min;
final protected Double max;
- public IntegerSimulatorElement(boolean reflectActuatorWrites) {
- this(reflectActuatorWrites, null, null);
+ public IntegerSimulatorElement() {
+ this(null, null);
}
- public IntegerSimulatorElement(boolean reflectActuatorWrites, Double min, Double max) {
- super(AttributeType.INTEGER, reflectActuatorWrites);
+ public IntegerSimulatorElement(Double min, Double max) {
+ super(AttributeType.INTEGER);
this.min = min;
this.max = max;
}
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SimulatorElement.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SimulatorElement.java
index c51569927..65c92c4ff 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SimulatorElement.java
@@ -27,13 +27,11 @@ import org.openremote.model.AttributeType;
public abstract class SimulatorElement<T> {
final protected AttributeType expectedType;
- final protected boolean reflectActuatorWrites;
protected JsonValue state = Json.createNull();
- public SimulatorElement(AttributeType expectedType, boolean reflectActuatorWrites) {
+ public SimulatorElement(AttributeType expectedType) {
this.expectedType = expectedType;
- this.reflectActuatorWrites = reflectActuatorWrites;
}
public JsonValue getState() {
@@ -54,11 +52,6 @@ public abstract class SimulatorElement<T> {
}
}
-
- public boolean isReflectActuatorWrites() {
- return reflectActuatorWrites;
- }
-
protected boolean isValid(JsonValue value) {
return value == null || value.getType() == JsonType.NULL || value.getType() == expectedType.getJsonType();
}
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SwitchSimulatorElement.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SwitchSimulatorElement.java
index f459e5d4e..6928a18bc 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SwitchSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/element/SwitchSimulatorElement.java
@@ -25,7 +25,7 @@ public class SwitchSimulatorElement extends SimulatorElement {
public static final String ELEMENT_NAME = "switch";
- public SwitchSimulatorElement(boolean reflectActuatorWrites) {
- super(AttributeType.BOOLEAN, reflectActuatorWrites);
+ public SwitchSimulatorElement() {
+ super(AttributeType.BOOLEAN);
}
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
index d68a2e553..be44f366b 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
@@ -143,6 +143,17 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
AgentAttributes agentAttributes = new AgentAttributes();
agentAttributes.setEnabled(false);
ProtocolConfiguration protocolConfigSimulator123 = new ProtocolConfiguration(demoAgentProtocolConfigName, SimulatorProtocol.PROTOCOL_NAME);
+ protocolConfigSimulator123.setMeta(new Meta()
+ .add(new MetaItem(
+ SimulatorProtocol.CONFIG_MODE,
+ Json.create(SimulatorProtocol.Mode.WRITE_THROUGH_DELAYED.toString())
+ )
+ )
+ .add(new MetaItem(
+ SimulatorProtocol.CONFIG_WRITE_DELAY_MILLISECONDS,
+ Json.create(500)
+ )
+ ));
agentAttributes.put(protocolConfigSimulator123);
agent.setAttributes(agentAttributes.getJsonObject());
agent = assetStorageService.merge(agent);
@@ -190,7 +201,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(IntegerSimulatorElement.ELEMENT_NAME_RANGE)
))
.add(new MetaItem(
- SimulatorProtocol.SIMULATOR_REFLECT_ACTUATOR_WRITES, Json.create(true)
+ SimulatorProtocol.CONFIG_MODE, Json.create(true)
))
),
new Attribute("light1Color", INTEGER_ARRAY, new ColorRGB(88, 123, 88).asJsonValue())