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
Updated Asset Processing
Rich Turner723bd365dc75
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.javaindex 2dcece9ba..30265c3cb 100644--- a/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java+++ b/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java@@ -24,6 +24,7 @@ import org.apache.camel.ProducerTemplate;import org.apache.camel.builder.RouteBuilder;import org.openremote.container.message.MessageBrokerSetupService;import org.openremote.model.AttributeEvent;+import org.openremote.model.AttributeState;import org.openremote.model.asset.ThingAttribute;import org.openremote.container.Container;import org.openremote.container.message.MessageBrokerContext;@@ -128,8 +129,12 @@ public abstract class AbstractProtocol implements Protocol {}}- protected void onSensorUpdate(AttributeEvent event) {- producerTemplate.sendBody(SENSOR_TOPIC, event);+ protected void onSensorUpdate(AttributeState state, long timestamp) {+ producerTemplate.sendBody(SENSOR_TOPIC, new AttributeEvent(state, this.getClass(), timestamp));+ }++ protected void onSensorUpdate(AttributeState state) {+ onSensorUpdate(state, System.currentTimeMillis());}@Overridediff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.javaindex b7e172797..6bf13598d 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@@ -126,10 +126,10 @@ public class SimulatorProtocol extends AbstractProtocol {if (simulateSensorRead) {LOG.info("Propagating state change as sensor read: " + element);- onSensorUpdate(new AttributeEvent(attributeState));+ onSensorUpdate(attributeState);} else if (element.isReflectActuatorWrites()) {LOG.info("Reflecting actuator write as sensor read: " + element);- onSensorUpdate(new AttributeEvent(attributeState));+ onSensorUpdate(attributeState);}}}diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.javaindex 369def13e..b5c867814 100644--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java@@ -20,21 +20,26 @@package org.openremote.manager.server.agent;import org.apache.camel.builder.RouteBuilder;+import org.openremote.agent3.protocol.AbstractProtocol;import org.openremote.agent3.protocol.Protocol;import org.openremote.container.Container;import org.openremote.container.ContainerService;+import org.openremote.container.message.MessageBrokerService;import org.openremote.container.message.MessageBrokerSetupService;import org.openremote.container.persistence.PersistenceEvent;import org.openremote.manager.server.asset.AssetStorageService;+import org.openremote.manager.server.asset.AssetUpdate;import org.openremote.manager.server.datapoint.AssetDatapointService;import org.openremote.model.asset.*;import java.util.Collection;import java.util.List;import java.util.Map;+import java.util.function.Consumer;import java.util.logging.Level;import java.util.logging.Logger;+import static org.openremote.agent3.protocol.Protocol.ACTUATOR_TOPIC;import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;import static org.openremote.manager.server.asset.AssetPredicates.isPersistenceEventForAssetType;import static org.openremote.manager.server.asset.AssetPredicates.isPersistenceEventForEntityType;@@ -44,20 +49,21 @@ import static org.openremote.model.asset.AssetType.THING;/*** Finds all {@link AssetType#AGENT} and {@link AssetType#THING} assets and starts the protocols for them.*/-public class AgentService extends RouteBuilder implements ContainerService {+public class AgentService extends RouteBuilder implements ContainerService, Consumer<AssetUpdate> {private static final Logger LOG = Logger.getLogger(AgentService.class.getName());protected Container container;protected AssetStorageService assetStorageService;protected AssetDatapointService assetDatapointService;+ protected MessageBrokerService messageBrokerService;@Overridepublic void init(Container container) throws Exception {this.container = container;assetStorageService = container.getService(AssetStorageService.class);assetDatapointService = container.getService(AssetDatapointService.class);-+ messageBrokerService = container.getService(MessageBrokerService.class);container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);}@@ -170,6 +176,58 @@ public class AgentService extends RouteBuilder implements ContainerService {}}+ /**+ * If this update is not for an asset of type THING or it has been initiated by+ * a protocol then we ignore it.+ *+ * Otherwise we push the update to the protocol to handle and prevent any further+ * processing of this event by the processing chain, the protocol should raise+ * sensor updates as required (i.e. the protocol is responsible for synchronising state)+ *+ * @param assetUpdate+ */+ @Override+ public void accept(AssetUpdate assetUpdate) {+ // Check that asset is a THING+ if (assetUpdate.getAsset().getWellKnownType() != THING) {+ LOG.fine("Ignoring asset update as asset is not a THING:" + assetUpdate);+ return;+ }++ // If update was initiated by a protocol ignore it+ if (AbstractProtocol.class.isAssignableFrom(assetUpdate.getSender())) {+ LOG.fine("Ignoring asset update as it came from a protocol:" + assetUpdate);+ return;+ }++ boolean hasAgentLink = ThingAttribute.getAgentLink(assetUpdate.getAttribute()) != null;++ if (hasAgentLink) {+ // Check attribute is linked to an actual agent+ ThingAttributes thingAttributes = new ThingAttributes(assetUpdate.getAsset());+ ThingAttribute thingAttribute = thingAttributes.getLinkedAttribute(+ assetStorageService.getAgentLinkResolver(), assetUpdate.getAttribute().getName()+ );++ if (thingAttribute == null) {+ LOG.warning("Cannot process asset update as agent link is invalid:" + assetUpdate);+ assetUpdate.setStatus(AssetUpdate.Status.ERROR);+ return;+ }+ } else {+ // This is just a non protocol attribute so allow the processing to continue+ LOG.fine("Ignoring asset update as it is not for an attribute linked to an agent:" + assetUpdate);+ return;+ }++ // Send it to the protocol actuator+ LOG.fine("Processing asset update: " + assetUpdate);+ // Its' a send to actuator - push the update to the protocol+ // TODO See class Javadoc, should we have a forceUpdate flag?+ messageBrokerService.getProducerTemplate().sendBody(ACTUATOR_TOPIC, assetUpdate.getNewState());+ assetUpdate.setStatus(AssetUpdate.Status.HANDLED);+ }+public String toString() {return getClass().getSimpleName() + "{" +'}';diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.javaindex 13e854137..b176abfed 100644--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java@@ -26,17 +26,20 @@ import org.openremote.container.ContainerService;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.message.MessageBrokerSetupService;import org.openremote.container.persistence.PersistenceEvent;+import org.openremote.manager.server.agent.AgentService;import org.openremote.manager.server.agent.ThingAttributes;import org.openremote.manager.server.datapoint.AssetDatapointService;import org.openremote.manager.server.rules.RulesService;import org.openremote.model.*;import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetType;import org.openremote.model.asset.ThingAttribute;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;+import java.util.function.Consumer;import static org.openremote.agent3.protocol.Protocol.ACTUATOR_TOPIC;import static org.openremote.agent3.protocol.Protocol.SENSOR_TOPIC;@@ -50,6 +53,7 @@ import static org.openremote.model.asset.AssetType.THING;* The regular processor chain is:* <ul>* <li>{@link RulesService}</li>+ * <li>{@link AgentService}</li>* <li>{@link AssetStorageService}</li>* <li>{@link AssetDatapointService}</li>* </ul>@@ -61,7 +65,7 @@ import static org.openremote.model.asset.AssetType.THING;* send to its {@link Protocol#ACTUATOR_TOPIC}. Any other created or updated attribute will be processed.* TODO This is not implemented* <p>- * Accepts client update calls on {@link #processClientUpdate(AttributeEvent)}. An update event for+ * Accepts client update calls on {@link #updateAttributeValue(AttributeEvent)}. An update event for* a {@link ThingAttribute} will be send to its {@link Protocol#ACTUATOR_TOPIC}. An update event for* other attributes will be processed.* <p>@@ -111,6 +115,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerprivate static final Logger LOG = Logger.getLogger(AssetProcessingService.class.getName());protected RulesService rulesService;+ protected AgentService agentService;protected AssetStorageService assetStorageService;protected AssetDatapointService assetDatapointService;protected MessageBrokerService messageBrokerService;@@ -120,11 +125,13 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer@Overridepublic void init(Container container) throws Exception {rulesService = container.getService(RulesService.class);+ agentService = container.getService(AgentService.class);assetStorageService = container.getService(AssetStorageService.class);assetDatapointService = container.getService(AssetDatapointService.class);messageBrokerService = container.getService(MessageBrokerService.class);processors.add(rulesService);+ processors.add(agentService);processors.add(assetStorageService);processors.add(assetDatapointService);@@ -161,8 +168,27 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer});}- public void processClientUpdate(AttributeEvent attributeEvent) {-+ /**+ * This is the entry point for any attribute value change event in the entire system. This deals+ * with single attribute value changes and pushes them through the attribute event processing chain+ * where each consumer is given the opportunity to consume the event or allow it progress to the+ * next consumer {@link AssetUpdate.Status}.+ *+ * NOTE: An attribute value can be changed during Asset CRUD but this does not come through+ * this route but is handled separately see {@link AssetResourceImpl}. Any attribute values+ * assigned during Asset CRUD can be thought of as the attributes initial value and is subject+ * to change by the following actors (depending on attribute meta etc.) all actors use this+ * entry point to initiate an attribute value change:+ *+ * Rules+ * Protocol+ * User+ *+ * @param attributeEvent+ */+ public void updateAttributeValue(AttributeEvent attributeEvent) {++ // Check this event relates to a valid assetServerAsset asset = assetStorageService.find(attributeEvent.getEntityId());if (asset == null) {@@ -170,43 +196,35 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerreturn;}- switch (asset.getWellKnownType()) {- case THING:- // Send it to the protocol actuator after checking if it's a thing asset and linked attribute- LOG.fine("Processing client " + attributeEvent + " for thing: " + asset);-- ThingAttributes thingAttributes = new ThingAttributes(asset);- ThingAttribute thingAttribute = thingAttributes.getLinkedAttribute(- assetStorageService.getAgentLinkResolver(), attributeEvent.getAttributeName()- );-- // TODO: This is broken a thing should be able to have attributes not linked to a protocol- if (thingAttribute == null) {- throw new RuntimeException("Ignoring " + attributeEvent + ", thing attribute not available or linked to agent: " + asset);- }-- // TODO See class Javadoc, should we have a forceUpdate flag?- // processUpdate(asset, thingAttribute, attributeEvent, false);-- // Push it to protocol actuator- messageBrokerService.getProducerTemplate().sendBody(ACTUATOR_TOPIC, attributeEvent);+ // Prevent editing of individual agent attributes+ if (asset.getWellKnownType() == AssetType.AGENT) {+ throw new RuntimeException("Agent attributes can not be updated individually, update the whole asset instead");+ }- break;+ // Pass attribute event through the processing chain+ LOG.fine("Processing client " + attributeEvent + " for: " + asset);+ Attributes attributes = new Attributes(asset.getAttributes());+ Attribute attribute = attributes.get(attributeEvent.getAttributeName());- case AGENT:- // TODO We don't want to allow agent attribute updates?! Not sure why but it seems like a good idea...- throw new RuntimeException("Agent attributes can not be updated individually, update the whole asset instead");+ if (attribute == null) {+ LOG.warning("Ignoring " + attributeEvent + ", attribute doesn't exist on asset: " + asset);+ return;+ }- default:- // Perform regular asset attribute update- LOG.fine("Processing client " + attributeEvent + " for: " + asset);- Attributes attributes = new Attributes(asset.getAttributes());- Attribute attribute = attributes.get(attributeEvent.getAttributeName());- processUpdate(asset, attribute, attributeEvent, false); // Don't ignore readOnly flag- break;+ // Prevent editing of read only attributes+ if (attribute.isReadOnly()) {+ LOG.warning("Ignoring " + attributeEvent + ", attribute is read-only in: " + asset);+ return;}++ processUpdate(asset, attribute, attributeEvent);}+ /**+ * We get here if a protocol pushes a sensor update message.+ *+ * @param attributeEvent+ */protected void processSensorUpdate(AttributeEvent attributeEvent) {// Must reference a thing assetServerAsset thing = assetStorageService.find(attributeEvent.getEntityId());@@ -226,26 +244,20 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerassetStorageService.getAgentLinkResolver(), attributeEvent.getAttributeName());- processUpdate(thing, thingAttribute, attributeEvent, true);- }-- protected void processUpdate(ServerAsset asset,- Attribute attribute,- AttributeEvent attributeEvent,- boolean ignoreReadOnly) {-- // Check the asset has this attribute (might also not be present if a Thing's attribute is unlinked)- if (attribute == null) {- LOG.warning("Ignoring " + attributeEvent + ", attribute not available: " + asset);+ if (thingAttribute == null) {+ LOG.warning("Processing sensor update failed attribute not linked to an agent: " + attributeEvent);return;}- // Check attribute isn't readonly- if (!ignoreReadOnly && attribute.isReadOnly()) {- LOG.warning("Ignoring " + attributeEvent + ", attribute is read-only in: " + asset);- return;- }+ // Protocols can write to readonly attributes (i.e. sensor attributes)+ // So no need to check readonly flag++ processUpdate(thing, thingAttribute, attributeEvent);+ }+ protected void processUpdate(ServerAsset asset,+ Attribute attribute,+ AttributeEvent attributeEvent) {// Ensure timestamp of event is not in the future as that would essentially block access to// the attribute until after that time (maybe that is desirable behaviour)// Allow a leniency of 1sdiff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.javaindex 82f234369..aadeab6d4 100644--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java@@ -27,6 +27,7 @@ import org.openremote.manager.shared.asset.AssetResource;import org.openremote.manager.shared.http.RequestParams;import org.openremote.model.Attribute;import org.openremote.model.AttributeEvent;+import org.openremote.model.AttributeRef;import org.openremote.model.Attributes;import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetInfo;@@ -234,7 +235,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour// Process updatetry {JsonValue value = Json.instance().parse(rawJson);- assetProcessingService.processClientUpdate(new AttributeEvent(assetId, attributeName, value));+ assetProcessingService.updateAttributeValue(new AttributeEvent(new AttributeRef(assetId, attributeName), value, this.getClass()));} catch (RuntimeException ex) {throw new IllegalStateException("Error updating attribute: " + attributeName, ex);}diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.javaindex 131c1a117..9826da56b 100644--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java@@ -40,6 +40,7 @@ import java.util.Arrays;import java.util.List;import java.util.logging.Logger;import java.util.regex.Pattern;+import java.util.function.Consumer;import static org.openremote.model.asset.AssetType.AGENT;@@ -51,6 +52,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpdaprotected PersistenceService persistenceService;protected ManagerIdentityService managerIdentityService;+ protected AssetProcessingService assetProcessingService;final protected Function<AttributeRef, ProtocolConfiguration> agentLinkResolver = agentLink -> {// Resolve the agent and the protocol configuration@@ -67,6 +69,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpdapublic void init(Container container) throws Exception {persistenceService = container.getService(PersistenceService.class);managerIdentityService = container.getService(ManagerIdentityService.class);+ assetProcessingService = container.getService(AssetProcessingService.class);container.getService(WebService.class).getApiSingletons().add(new AssetResourceImpl(@@ -303,6 +306,11 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda});}+ // Here for convenience - a single service to handle Asset/AssetAttribute events+ public void updateAttributeValue(AttributeEvent attributeEvent) {+ assetProcessingService.updateAttributeValue(attributeEvent);+ }+@Overridepublic void accept(AssetUpdate assetUpdate) {String assetId = assetUpdate.getAsset().getId();diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetUpdate.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetUpdate.javaindex 535ab7382..cdc557256 100644--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetUpdate.java+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetUpdate.java@@ -98,6 +98,10 @@ public class AssetUpdate {return newState;}+ public Class<?> getSender() {+ return newState.getSender();+ }+public AttributeEvent getOldState() {return oldState;}diff --git a/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java b/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.javaindex 770093f5c..91e070a36 100644--- a/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java@@ -5,11 +5,11 @@ import org.openremote.container.ContainerService;import org.openremote.container.persistence.PersistenceService;import org.openremote.manager.server.asset.AssetUpdate;import org.openremote.model.AttributeRef;-import org.openremote.model.Consumer;import org.openremote.model.datapoint.AssetDatapoint;import java.util.List;import java.util.logging.Logger;+import java.util.function.Consumer;/*** Store and retrieve datapoints.diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.javaindex d619c1784..7a6b5bd13 100644--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java@@ -20,6 +20,7 @@package org.openremote.manager.server.rules;import org.apache.camel.builder.RouteBuilder;+import org.openremote.agent3.protocol.AbstractProtocol;import org.openremote.container.Container;import org.openremote.container.ContainerService;import org.openremote.container.message.MessageBrokerSetupService;@@ -35,9 +36,9 @@ import org.openremote.manager.shared.rules.GlobalRulesDefinition;import org.openremote.manager.shared.rules.RulesDefinition;import org.openremote.manager.shared.rules.TenantRulesDefinition;import org.openremote.manager.shared.security.Tenant;-import org.openremote.model.Consumer;import java.util.*;+import java.util.function.Consumer;import java.util.logging.Logger;import java.util.stream.Collectors;@@ -132,6 +133,12 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons@Overridepublic void accept(AssetUpdate assetUpdate) {+ // If update was initiated by a protocol then rules shouldn't be allowed to prevent it+ // the update from progressing through the system+ if (AbstractProtocol.class.isAssignableFrom(assetUpdate.getSender())) {+ // TODO: Prevent rules RHS from blocking protocol initiated updates+ }+processAssetUpdate(assetUpdate);}diff --git a/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy b/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovyindex 221e4d4d8..424b13e6f 100644--- a/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy@@ -4,7 +4,6 @@ import elemental.json.Jsonimport elemental.json.JsonArrayimport elemental.json.JsonTypeimport org.openremote.agent3.protocol.simulator.SimulatorProtocol-import org.openremote.manager.server.asset.AssetProcessingServiceimport org.openremote.manager.server.asset.AssetStorageServiceimport org.openremote.manager.server.setup.SetupServiceimport org.openremote.manager.server.setup.builtin.ManagerDemoSetup@@ -28,7 +27,6 @@ class AgentDeploymentTest extends Specification implements ManagerContainerTraitdef managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)def simulatorProtocol = container.getService(SimulatorProtocol.class)def assetStorageService = container.getService(AssetStorageService.class)- def assetProcessingService = container.getService(AssetProcessingService.class)then: "the simulator elements should have the initial state"conditions.eventually {@@ -41,9 +39,9 @@ class AgentDeploymentTest extends Specification implements ManagerContainerTraitwhen: "a client wants to change a thing attributes' value, triggering an actuator"conditions = new PollingConditions(timeout: 3, initialDelay: 2)def light1DimmerChange = new AttributeEvent(- managerDemoSetup.thingId, "light1Dimmer", Json.create(66)+ managerDemoSetup.thingId, "light1Dimmer", Json.create(66), getClass())- assetProcessingService.processClientUpdate(light1DimmerChange)+ assetStorageService.updateAttributeValue(light1DimmerChange)then: "the simulator state and thing attribute value should be updated (simulator reflects actuator write as sensor read)"conditions.eventually {diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovyindex e948fe498..4ad0cfc9e 100644--- a/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovy@@ -19,7 +19,6 @@ import org.openremote.manager.shared.rules.TenantRulesDefinitionimport org.openremote.model.AttributeEventimport org.openremote.model.AttributeRefimport org.openremote.model.AttributeState-import org.openremote.model.Constantsimport org.openremote.test.ManagerContainerTraitimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions@@ -436,9 +435,9 @@ class RulesDeploymentTest extends Specification implements ManagerContainerTraitand: "an attribute event is pushed into the system"def apartment1LivingRoomDemoBooleanChange = new AttributeEvent(- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false))+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false)), getClass())- assetProcessingService.processClientUpdate(apartment1LivingRoomDemoBooleanChange)+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)then: "the rule engines in scope should fire the 'All' and 'All changed' rules"conditions.eventually {@@ -457,7 +456,7 @@ class RulesDeploymentTest extends Specification implements ManagerContainerTraitwhen: "an old (stale) attribute event is pushed into the system"conditions = new PollingConditions(timeout: 10, initialDelay: 5)- assetProcessingService.processClientUpdate(apartment1LivingRoomDemoBooleanChange)+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)then: "after a few seconds no rules should have fired on any engines"conditions.eventually {@@ -477,9 +476,9 @@ class RulesDeploymentTest extends Specification implements ManagerContainerTraitand: "an attribute event with the same value as current value is pushed into the system"apartment1LivingRoomDemoBooleanChange = new AttributeEvent(- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false))+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false)), getClass())- assetProcessingService.processClientUpdate(apartment1LivingRoomDemoBooleanChange)+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)then: "the rule engines in scope should fire the 'All' rule but not the 'All changed' rule"conditions.eventually {@@ -535,9 +534,9 @@ class RulesDeploymentTest extends Specification implements ManagerContainerTraitand: "an apartment 3 living room attribute event occurs"def apartment3LivingRoomDemoStringChange = new AttributeEvent(- new AttributeState(new AttributeRef(managerDemoSetup.apartment3LivingroomId, "demoString"), Json.create("demo2"))+ new AttributeState(new AttributeRef(managerDemoSetup.apartment3LivingroomId, "demoString"), Json.create("demo2")), getClass())- assetProcessingService.processClientUpdate(apartment3LivingRoomDemoStringChange)+ assetProcessingService.updateAttributeValue(apartment3LivingRoomDemoStringChange)then: "the apartment 3 rule engine should have fired the 'All', 'All changed' and 'Living Room All' rules but apartment 1 shouldn't have fired any"conditions.eventually {@@ -549,9 +548,9 @@ class RulesDeploymentTest extends Specification implements ManagerContainerTraitwhen: "an apartment 1 living room attribute event occurs"def apartment1LivingRoomDemoStringChange = new AttributeEvent(- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoDecimal"), Json.create(22.5))+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoDecimal"), Json.create(22.5)), getClass())- assetProcessingService.processClientUpdate(apartment1LivingRoomDemoStringChange)+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoStringChange)then: "the apartment 1 rule engine should have fired the 'All', 'All changed' and 'Living Room All' rules but apartment 3 shouldn't have fired any"conditions.eventually {diff --git a/model/src/main/java/org/openremote/model/Attribute.java b/model/src/main/java/org/openremote/model/Attribute.javaindex c22339690..71b2a9acc 100644--- a/model/src/main/java/org/openremote/model/Attribute.java+++ b/model/src/main/java/org/openremote/model/Attribute.java@@ -161,6 +161,7 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {return new AttributeEvent(getAttributeRef(assetId),getValue(),+ null,getValueTimestamp());}diff --git a/model/src/main/java/org/openremote/model/AttributeEvent.java b/model/src/main/java/org/openremote/model/AttributeEvent.javaindex 118d8154a..ddcd59392 100644--- a/model/src/main/java/org/openremote/model/AttributeEvent.java+++ b/model/src/main/java/org/openremote/model/AttributeEvent.java@@ -31,24 +31,25 @@ public class AttributeEvent extends Event {final protected AttributeState attributeState;- public AttributeEvent(AttributeState attributeState) {- this.attributeState = attributeState;+ public AttributeEvent(String entityId, String attributeName, JsonValue value, Class<?> sender) {+ this(new AttributeState(new AttributeRef(entityId, attributeName), value), sender);}- public AttributeEvent(String entityId, String attributeName, JsonValue value) {- this(new AttributeState(new AttributeRef(entityId, attributeName), value));+ public AttributeEvent(AttributeRef attributeRef, JsonValue value, Class<?> sender) {+ this(new AttributeState(attributeRef, value), sender);}- public AttributeEvent(AttributeRef attributeRef, JsonValue value) {- this(new AttributeState(attributeRef, value));+ public AttributeEvent(AttributeState attributeState, Class<?> sender) {+ super(sender);+ this.attributeState = attributeState;}- public AttributeEvent(AttributeRef attributeRef, JsonValue value, long timestamp) {- this(new AttributeState(attributeRef, value), timestamp);+ public AttributeEvent(AttributeRef attributeRef, JsonValue value, Class<?> sender, long timestamp) {+ this(new AttributeState(attributeRef, value), sender, timestamp);}- public AttributeEvent(AttributeState attributeState, long timestamp) {- super(timestamp);+ public AttributeEvent(AttributeState attributeState, Class<?> sender, long timestamp) {+ super(timestamp, sender);this.attributeState = attributeState;}diff --git a/model/src/main/java/org/openremote/model/Event.java b/model/src/main/java/org/openremote/model/Event.javaindex 9c36c589f..898460a27 100644--- a/model/src/main/java/org/openremote/model/Event.java+++ b/model/src/main/java/org/openremote/model/Event.java@@ -37,19 +37,25 @@ import java.util.Locale;*/// TODO Any subclass that is not @JsonIgnoreType will be made serializable (and must be compilable in GWT!)// TODO This needs more work when we know how to do client/server pub/sub message bus+// TODO Should sender be compulsory?/**- * A timestamped event.+ * A timestamped event with optional sender.*/public abstract class Event {-+ protected Class<?> sender;protected long timestamp;- protected Event(long timestamp) {+ protected Event(long timestamp, Class<?> sender) {this.timestamp = timestamp;+ this.sender = sender;}protected Event() {- timestamp = System.currentTimeMillis();+ this(System.currentTimeMillis(), null);+ }++ protected Event(Class<?> sender) {+ this(System.currentTimeMillis(), sender);}/**@@ -108,6 +114,11 @@ public abstract class Event {return timestamp;}+ @JsonIgnore+ public Class<?> getSender() {+ return sender;+ }+@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/asset/AssetMeta.java b/model/src/main/java/org/openremote/model/asset/AssetMeta.javaindex ddcbb4f27..264c2b760 100644--- a/model/src/main/java/org/openremote/model/asset/AssetMeta.java+++ b/model/src/main/java/org/openremote/model/asset/AssetMeta.java@@ -108,7 +108,9 @@ public enum AssetMeta {/*** Should attribute values be stored in time series database*/- STORE_DATA_POINTS(ASSET_META_NAMESPACE + ":storeDataPoints", new Access(true, false, true), JsonType.BOOLEAN);+ STORE_DATA_POINTS(ASSET_META_NAMESPACE + ":storeDataPoints", new Access(true, false, true), JsonType.BOOLEAN),++ RULES_FACT(ASSET_META_NAMESPACE + ":rulesFact", new Access(true, false, true), JsonType.BOOLEAN);final protected String name;final protected Access access;