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
More refactoring
Christian Bauer6b39095db2b9
diff --git a/controller/src/main/java/org/openremote/controller/ControllerService.java b/controller/src/main/java/org/openremote/controller/ControllerService.javaindex ea90df1b3..7e4951e04 100644--- a/controller/src/main/java/org/openremote/controller/ControllerService.java+++ b/controller/src/main/java/org/openremote/controller/ControllerService.java@@ -2,7 +2,7 @@ package org.openremote.controller;import org.openremote.container.Container;import org.openremote.container.ContainerService;-import org.openremote.controller.command.CommandFactory;+import org.openremote.controller.command.CommandBuilder;import org.openremote.controller.deploy.DeploymentDefinition;import org.openremote.controller.deploy.xml.ControllerDOMParser;import org.openremote.controller.event.EventProcessor;@@ -19,7 +19,7 @@ public class ControllerService implements ContainerService {final protected ControllerDOMParser controllerDOMParser = new ControllerDOMParser();final protected InputStream deploymentXml;- final protected CommandFactory commandFactory;+ final protected CommandBuilder commandBuilder;final protected EventProcessor[] eventProcessors;protected DeploymentDefinition deploymentDefinition;@@ -27,9 +27,9 @@ public class ControllerService implements ContainerService {protected CommandFacade commandFacade;protected DataContext dataContext;- public ControllerService(InputStream deploymentXml, CommandFactory commandFactory, EventProcessor[] eventProcessors) {+ public ControllerService(InputStream deploymentXml, CommandBuilder commandBuilder, EventProcessor[] eventProcessors) {this.deploymentXml = deploymentXml;- this.commandFactory = commandFactory;+ this.commandBuilder = commandBuilder;this.eventProcessors = eventProcessors;}@@ -40,7 +40,7 @@ public class ControllerService implements ContainerService {@Overridepublic void configure(Container container) throws Exception {- deployment = new Deployment(deploymentDefinition, commandFactory);+ deployment = new Deployment(deploymentDefinition, commandBuilder);commandFacade = new CommandFacade(deployment);diff --git a/controller/src/main/java/org/openremote/controller/command/CommandFactory.java b/controller/src/main/java/org/openremote/controller/command/CommandBuilder.javasimilarity index 95%rename from controller/src/main/java/org/openremote/controller/command/CommandFactory.javarename to controller/src/main/java/org/openremote/controller/command/CommandBuilder.javaindex 8e0af64bd..d82a8c654 100644--- a/controller/src/main/java/org/openremote/controller/command/CommandFactory.java+++ b/controller/src/main/java/org/openremote/controller/command/CommandBuilder.java@@ -7,7 +7,7 @@ import org.openremote.controller.deploy.CommandDefinition;/*** Build protocol- and device-specific command implementation.*/-public class CommandFactory {+public class CommandBuilder {public Command build(CommandDefinition commandDefinition) {diff --git a/controller/src/main/java/org/openremote/controller/context/DataContext.java b/controller/src/main/java/org/openremote/controller/context/DataContext.javaindex 8d922a94b..5edc551a1 100644--- a/controller/src/main/java/org/openremote/controller/context/DataContext.java+++ b/controller/src/main/java/org/openremote/controller/context/DataContext.java@@ -6,8 +6,7 @@ import org.openremote.controller.event.EventProcessorChain;import org.openremote.controller.model.Deployment;import org.openremote.controller.model.Sensor;-import java.util.Iterator;-import java.util.Map;+`import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.logging.Level;import java.util.logging.Logger;@@ -20,7 +19,7 @@ public class DataContext {final protected EventProcessorChain eventProcessorChain;final protected Map<Integer, Sensor> sensors = new ConcurrentHashMap<>();- final protected Map<Integer, Event> sensorState = new ConcurrentHashMap<>();+ final protected StateStorage stateStorage = new InMemoryStateStorage();/*** Used to indicate if the data context is in the middle of a shut down process -- this@@ -58,7 +57,7 @@ public class DataContext {LOG.log(Level.SEVERE, "Failed to stop sensor: " + sensor, t);}}- sensorState.clear();+ stateStorage.clear();sensors.clear();} finally {isShutdownInProcess = false;@@ -76,7 +75,7 @@ public class DataContext {}// Initial state- sensorState.put(sensor.getSensorDefinition().getSensorID(), new Sensor.UnknownEvent(sensor));+ stateStorage.put(new SensorState(new Sensor.UnknownEvent(sensor)));sensor.start(this);LOG.info("Registered and started sensor: " + sensor);@@ -98,28 +97,17 @@ public class DataContext {return;}- int sourceID = event.getSourceID();- if (sensorState.get(sourceID) == null) {- LOG.fine("Inserted: " + event);- sensorState.put(sourceID, event);- } else {- Event previousState = sensorState.get(sourceID);- if (previousState.isEqual(event)) {- return;- }- LOG.fine("Updated: " + event);- sensorState.put(sourceID, event);- }+ stateStorage.put(new SensorState(event));LOG.fine("<== Updating status complete for event: " + event);// TODO: Trigger notification of client that stuff has changed? Put it in a message broker/queue/topic?}public String queryValue(int sensorID) {- if (!sensorState.containsKey(sensorID)) {+ if (!stateStorage.contains(sensorID)) {LOG.info("Requested sensor id '" + sensorID + "' was not found. Defaulting to: " + Sensor.UNKNOWN_STATUS);return Sensor.UNKNOWN_STATUS;}- return sensorState.get(sensorID).serialize();+ return stateStorage.get(sensorID).getEvent().serialize();}public String queryValue(String sensorName) {@@ -127,15 +115,11 @@ public class DataContext {}public Event queryEvent(int sensorID) {- return sensorState.get(sensorID);+ return stateStorage.get(sensorID).getEvent();}public Event queryEvent(String sensorName) {Integer sensorID = deployment.getSensorID(sensorName);return sensorID != null ? queryEvent(sensorID) : null;}-- public Iterator<Event> getFullSnapshot() {- return sensorState.values().iterator();- }}diff --git a/controller/src/main/java/org/openremote/controller/context/InMemoryStateStorage.java b/controller/src/main/java/org/openremote/controller/context/InMemoryStateStorage.javanew file mode 100644index 000000000..4dd7b1e7a--- /dev/null+++ b/controller/src/main/java/org/openremote/controller/context/InMemoryStateStorage.java@@ -0,0 +1,46 @@+package org.openremote.controller.context;++import org.openremote.controller.event.Event;++import java.util.HashMap;+import java.util.Map;+import java.util.logging.Logger;++public class InMemoryStateStorage implements StateStorage {++ private static final Logger LOG = Logger.getLogger(InMemoryStateStorage.class.getName());++ final protected Map<Integer, SensorState> sensorStates = new HashMap<>();++ @Override+ synchronized public void clear() {+ sensorStates.clear();+ }++ @Override+ synchronized public void put(SensorState sensorState) {+ Event event = sensorState.getEvent();+ int sourceID = event.getSourceID();+ if (sensorStates.get(sourceID) == null) {+ LOG.fine("Inserted: " + event);+ sensorStates.put(sourceID, new SensorState(event));+ } else {+ SensorState previousState = sensorStates.get(sourceID);+ if (previousState.getEvent().isEqual(event)) {+ return;+ }+ LOG.fine("Updated: " + event);+ sensorStates.put(sourceID, new SensorState(event));+ }+ }++ @Override+ synchronized public boolean contains(int sensorID) {+ return sensorStates.containsKey(sensorID);+ }++ @Override+ public SensorState get(int sensorID) {+ return sensorStates.get(sensorID);+ }+}diff --git a/controller/src/main/java/org/openremote/controller/context/SensorState.java b/controller/src/main/java/org/openremote/controller/context/SensorState.javanew file mode 100644index 000000000..e9b1ea84c--- /dev/null+++ b/controller/src/main/java/org/openremote/controller/context/SensorState.java@@ -0,0 +1,29 @@+package org.openremote.controller.context;++import org.openremote.controller.event.Event;++public class SensorState {++ final protected long timestamp = System.currentTimeMillis();+ final protected Event event;++ public SensorState(Event event) {+ this.event = event;+ }++ public Event getEvent() {+ return event;+ }++ public boolean isNewerThan(long timestamp) {+ return this.timestamp < timestamp;+ }++ @Override+ public String toString() {+ return "SensorState{" ++ "timestamp=" + timestamp ++ ", event=" + event ++ '}';+ }+}diff --git a/controller/src/main/java/org/openremote/controller/context/StateStorage.java b/controller/src/main/java/org/openremote/controller/context/StateStorage.javanew file mode 100644index 000000000..1d8112657--- /dev/null+++ b/controller/src/main/java/org/openremote/controller/context/StateStorage.java@@ -0,0 +1,18 @@+package org.openremote.controller.context;++public interface StateStorage {++ void clear();++ void put(SensorState sensorState);++ boolean contains(int sensorID);++ SensorState get(int sensorID);++ /* TODO For offset publishing/polling+ Iterator<SensorState> getSince(int timestamp);++ Iterator<SensorState> getSince(int timestamp, int sensorID);+ */+}diff --git a/controller/src/main/java/org/openremote/controller/deploy/CommandDefinition.java b/controller/src/main/java/org/openremote/controller/deploy/CommandDefinition.javaindex 1a0250775..e5e8e9099 100644--- a/controller/src/main/java/org/openremote/controller/deploy/CommandDefinition.java+++ b/controller/src/main/java/org/openremote/controller/deploy/CommandDefinition.java@@ -8,62 +8,24 @@ import java.util.Map;*/public class CommandDefinition {- /**- * Command property name that contains a logical command name that can be used by scripts,- * rules, REST API calls, etc.- */public final static String NAME_PROPERTY = "name";-- /**- * Default value returned by {@link #getName()} if no name has been set in command's properties.- */public final static String NAME_PROPERTY_DEFAULT_VALUE = "<No Name>";-- /**- * Command property that contains the name of the related device.- */public final static String DEVICE_NAME_PROPERTY = "urn:openremote:device-command:device-name";-- /**- * Command property that contains the ID of the related device.- */public final static String DEVICE_ID_PROPERTY = "urn:openremote:device-command:device-id";- /**- * Command's protocol identifier, such as 'knx', 'x10', 'onewire', etc. This identifier is- * used to determine which protocol builder plugin is used to construct executable commands.- */final protected String protocolType;-- /**- * A unique command identifier. This corresponds to the 'id' attribute in controller XML- * definition's {@code <command>} element.- */final protected int commandID;+ final protected Map<String, String> properties; // Note: properties are not case-sensitive, we convert to lowercase!/**- * List of generic command properties. The correspond to {@code <property>} elements nested- * within {@code <command>} element in controller's XML definition.- * <p>- * An arbitrary list of properties is allowed. Specific property names (such as- * {@link #NAME_PROPERTY}) may have a special meaning.- * <p>- * NOTE: property *names* are *not* case sensitive. All property names are converted to lower- * case.- */- final protected Map<String, String> properties;-- /**- * Constructs a command data model.- *- * @param commandID This represents the unique identifier of the command element in controller's- * XML definition (corresponding to 'id' attribute of {@code <command>} element)- * @param protocolType Protocol type identifier for this command. The protocol type identifier is used- * with the command factory to identify which plugin (Java bean implementation) is- * used to construct command's protocol specific execution model.- * @param properties Command's properties. Arbitrary list of properties that can be used to configure- * command instances. Certain property values such as {@link #NAME_PROPERTY}- * may have special meaning.+ * @param commandID This represents the unique identifier of the command element in controller's+ * XML definition (corresponding to 'id' attribute of {@code <command>} element)+ * @param protocolType Protocol type identifier for this command. The protocol type identifier is used+ * with the command factory to identify which plugin (Java bean implementation) is+ * used to construct command's protocol specific execution model.+ * @param properties Command's properties. Arbitrary list of properties that can be used to configure+ * command instances. Certain property values such as {@link #NAME_PROPERTY}+ * may have special meaning.*/public CommandDefinition(int commandID, String protocolType, Map<String, String> properties) {if (protocolType == null)@@ -79,18 +41,32 @@ public class CommandDefinition {throw new IllegalStateException("Command definition must have device name: " + this);}- /**- * Returns the unique identifier of the command.- */+ public String getProtocolType() {+ return protocolType;+ }+public int getCommandID() {return commandID;}/**- * Returns a command's name property if present.+ * Returns a command property value.*- * @return commands name, or a default {@link #NAME_PROPERTY_DEFAULT_VALUE} string if- * name property is not present+ * @param name Name of the command property to return. Note that command property names are+ * not case-sensitive. All names are converted to lower case characters.+ * @return Command property value, or empty string if not found.+ */+ public String getProperty(String name) {+ return getProperty(name, "");+ }++ public String getProperty(String name, String defaultValue) {+ String value = properties.get(name.trim().toLowerCase(Locale.ROOT));+ return (value == null) ? defaultValue : value;+ }++ /**+ * @return commands name, or a default {@link #NAME_PROPERTY_DEFAULT_VALUE} string if name property is not present*/public String getName() {String name = getProperty(NAME_PROPERTY);@@ -112,32 +88,6 @@ public class CommandDefinition {}}- /**- * Returns the protocol type of the command that corresponds to 'protocol' attribute of- * {@code <command>} element in controller's XML definition.- *- * @return the protocol type- */- public String getProtocolType() {- return protocolType;- }-- /**- * Returns a command property value.- *- * @param name Name of the command property to return. Note that command property names are- * not case-sensitive. All names are converted to lower case characters.- * @return Command property value, or empty string if not found.- */- public String getProperty(String name) {- return getProperty(name, "");- }-- public String getProperty(String name, String defaultValue) {- String value = properties.get(name.trim().toLowerCase(Locale.ROOT));- return (value == null) ? defaultValue : value;- }-@Overridepublic boolean equals(Object o) {if (this == o) return true;diff --git a/controller/src/main/java/org/openremote/controller/deploy/SensorDefinition.java b/controller/src/main/java/org/openremote/controller/deploy/SensorDefinition.javaindex 54dea12a2..637bd9243 100644--- a/controller/src/main/java/org/openremote/controller/deploy/SensorDefinition.java+++ b/controller/src/main/java/org/openremote/controller/deploy/SensorDefinition.java@@ -15,8 +15,6 @@ public class SensorDefinition {final protected Map<String, String> properties;/**- * Constructs a new sensor definition with a given name, ID, and a set of sensor properties.- ** @param name Human readable name of the sensor. Used with event processors, logging, etc.* @param sensorID A unique sensor ID. Must be unique per controller deployment.* @param type A sensor type.diff --git a/controller/src/main/java/org/openremote/controller/model/Deployment.java b/controller/src/main/java/org/openremote/controller/model/Deployment.javaindex 4993e3d0c..de76c5d1c 100644--- a/controller/src/main/java/org/openremote/controller/model/Deployment.java+++ b/controller/src/main/java/org/openremote/controller/model/Deployment.java@@ -1,7 +1,7 @@package org.openremote.controller.model;import org.openremote.controller.command.Command;-import org.openremote.controller.command.CommandFactory;+import org.openremote.controller.command.CommandBuilder;import org.openremote.controller.command.EventProducerCommand;import org.openremote.controller.deploy.CommandDefinition;import org.openremote.controller.deploy.DeploymentDefinition;@@ -12,12 +12,12 @@ import java.util.Map;public class Deployment {- final protected CommandFactory commandFactory;+ final protected CommandBuilder commandBuilder;final protected Map<String, Device> devices = new HashMap<>();final protected Map<Integer, Sensor> sensors = new HashMap<>();- public Deployment(DeploymentDefinition deploymentDefinition, CommandFactory commandFactory) {- this.commandFactory = commandFactory;+ public Deployment(DeploymentDefinition deploymentDefinition, CommandBuilder commandBuilder) {+ this.commandBuilder = commandBuilder;for (SensorDefinition sensorDefinition : deploymentDefinition.getSensorDefinitions()) {Sensor sensor = buildSensor(sensorDefinition);@@ -38,8 +38,8 @@ public class Deployment {}}- public CommandFactory getCommandFactory() {- return commandFactory;+ public CommandBuilder getCommandBuilder() {+ return commandBuilder;}public CommandDefinition getCommandDefinition(int commandID) {@@ -103,13 +103,13 @@ public class Deployment {protected Sensor buildSensor(SensorDefinition sensorDefinition) {Sensor sensor;- Command definedCommand = commandFactory.build(sensorDefinition.getCommandDefinition());+ Command command = commandBuilder.build(sensorDefinition.getCommandDefinition());EventProducerCommand sensorCommand;- if (definedCommand instanceof EventProducerCommand) {- sensorCommand = (EventProducerCommand) definedCommand;+ if (command instanceof EventProducerCommand) {+ sensorCommand = (EventProducerCommand) command;} else {throw new IllegalStateException(- "Sensor must reference event producer command, not '" + definedCommand + "': " + sensorDefinition+ "Sensor must reference EventProducerCommand, not '" + command + "': " + sensorDefinition);}diff --git a/controller/src/main/java/org/openremote/controller/rules/CommandFacade.java b/controller/src/main/java/org/openremote/controller/rules/CommandFacade.javaindex b1d40c636..c6e02899e 100644--- a/controller/src/main/java/org/openremote/controller/rules/CommandFacade.java+++ b/controller/src/main/java/org/openremote/controller/rules/CommandFacade.java@@ -10,7 +10,8 @@ import java.util.logging.Level;import java.util.logging.Logger;/**- * Can be used by {@link EventProcessor}s to trigger {@link ExecutableCommand}s (e.g. in rules).+ * Can be used directly and/or by {@link EventProcessor}s to trigger+ * {@link ExecutableCommand}s (e.g. in rules or from simple client call).*/public class CommandFacade {@@ -26,8 +27,8 @@ public class CommandFacade {command(commandName, null);}- public void command(String commandName, int value) {- command(commandName, Integer.toString(value));+ public void command(String commandName, int arg) {+ command(commandName, Integer.toString(arg));}public void command(String commandName, String arg) {@@ -55,10 +56,10 @@ public class CommandFacade {public void execute(CommandDefinition commandDefinition, String arg) {Command command;try {- command = deployment.getCommandFactory().build(commandDefinition);+ command = deployment.getCommandBuilder().build(commandDefinition);if (command == null) {LOG.log(Level.WARNING,- "No command was produced (can the protocol build an ExecutableCommand?): " + commandDefinition+ "No command was produced (does the protocol have an ExecutableCommand?): " + commandDefinition);} else if (command instanceof ExecutableCommand) {LOG.fine("Executing command '" + command + "' with: " + arg);diff --git a/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy b/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovyindex 2497324eb..ea5d99c4a 100644--- a/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy+++ b/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy@@ -34,7 +34,7 @@ class ClimateControlTest extends Specification implements ContainerTrait {and: "the started controller server"- def testCommandFactory = new TestCommandFactory();+ def testCommandFactory = new TestCommandBuilder();def controllerService = new ControllerService(controllerDeploymentXml,testCommandFactory,diff --git a/controller/src/test/groovy/org/openremote/test/rules/MixedWithErrorsTest.groovy b/controller/src/test/groovy/org/openremote/test/rules/MixedWithErrorsTest.groovyindex f0f2055c4..d236a185c 100644--- a/controller/src/test/groovy/org/openremote/test/rules/MixedWithErrorsTest.groovy+++ b/controller/src/test/groovy/org/openremote/test/rules/MixedWithErrorsTest.groovy@@ -47,7 +47,7 @@ class MixedWithErrorsTest extends Specification implements ContainerTrait {def grabProcessor = new EventGrabProcessor()and: "the started controller server"- def testCommandFactory = new TestCommandFactory();+ def testCommandFactory = new TestCommandBuilder();def controllerService = new ControllerService(controllerDeploymentXml,testCommandFactory,diff --git a/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy b/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovyindex f01d54e94..4445e74f4 100644--- a/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy+++ b/controller/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy@@ -40,7 +40,7 @@ class SimpleRuleTest extends Specification implements ContainerTrait {and: "the started controller server"def controllerService = new ControllerService(controllerDeploymentXml,- new TestCommandFactory(),+ new TestCommandBuilder(),ruleEngineProcessor,grabProcessor)diff --git a/controller/src/test/groovy/org/openremote/test/rules/TestCommandFactory.java b/controller/src/test/groovy/org/openremote/test/rules/TestCommandBuilder.javasimilarity index 92%rename from controller/src/test/groovy/org/openremote/test/rules/TestCommandFactory.javarename to controller/src/test/groovy/org/openremote/test/rules/TestCommandBuilder.javaindex 7bc01b57e..ef5459f89 100644--- a/controller/src/test/groovy/org/openremote/test/rules/TestCommandFactory.java+++ b/controller/src/test/groovy/org/openremote/test/rules/TestCommandBuilder.java@@ -1,16 +1,16 @@package org.openremote.test.rules;import org.openremote.controller.command.Command;-import org.openremote.controller.command.CommandFactory;+import org.openremote.controller.command.CommandBuilder;import org.openremote.controller.command.ExecutableCommand;import org.openremote.controller.command.PullCommand;import org.openremote.controller.deploy.CommandDefinition;import java.util.logging.Logger;-public class TestCommandFactory extends CommandFactory {+public class TestCommandBuilder extends CommandBuilder {- private static final Logger LOG = Logger.getLogger(TestCommandFactory.class.getName());+ private static final Logger LOG = Logger.getLogger(TestCommandBuilder.class.getName());public String lastExecutionArgument;diff --git a/controller/src/test/groovy/org/openremote/test/rules/VacationTest.groovy b/controller/src/test/groovy/org/openremote/test/rules/VacationTest.groovyindex 90fc83f58..4662974f9 100644--- a/controller/src/test/groovy/org/openremote/test/rules/VacationTest.groovy+++ b/controller/src/test/groovy/org/openremote/test/rules/VacationTest.groovy@@ -37,7 +37,7 @@ class VacationTest extends Specification implements ContainerTrait {def grabProcessor = new EventGrabProcessor();and: "the started controller server"- def testCommandFactory = new TestCommandFactory();+ def testCommandFactory = new TestCommandBuilder();def controllerService = new ControllerService(controllerDeploymentXml,testCommandFactory,@@ -88,6 +88,12 @@ class VacationTest extends Specification implements ContainerTrait {then: "the temperature change should be executed"testCommandFactory.lastExecutionArgument == "15"+ when: "we manually set the temperature"+ controllerService.getCommandFacade().command("temp", 19)++ then: "the temperature change should be executed"+ testCommandFactory.lastExecutionArgument == "19"+cleanup: "the server should be stopped"stopContainer(container)}