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
Changed processing chain, an actuator write should be send to the protocol and not reach any other processors
Christian Bauer6f6a05614266
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 02da305d1..80d703534 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@@ -519,10 +519,10 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons}/**- * If this is not a southbound update from a client, or if the changed attribute is not linked to- * an agent's protocol configuration, we ignore it.+ * If this is an update from a sensor, or if the changed attribute is not linked to an agent's protocol+ * configuration, it's ignored.* <p>- * Otherwise we push the update to the attributes' linked protocol to handle and prevent any further+ * Otherwise push the update to the attributes' linked 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 with the database).*/@@ -560,7 +560,7 @@ public class AgentService extends RouteBuilder implements ContainerService, ConsLOG.warning("Attribute doesn't have a valid attribute event: " + assetState);}- assetState.setProcessingStatus(AssetState.ProcessingStatus.HANDLED);+ assetState.setProcessingStatus(AssetState.ProcessingStatus.COMPLETED);return protocolConfiguration;}).orElseGet(() -> {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 5fa1c4633..a7b299450 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@@ -72,25 +72,11 @@ import static org.openremote.model.attribute.AttributeEvent.Source.INTERNAL;* <p>* The regular processing chain is:* <ul>- * <li>{@link RulesService}</li>* <li>{@link AgentService}</li>+ * <li>{@link RulesService}</li>* <li>{@link AssetStorageService}</li>* <li>{@link AssetDatapointService}</li>* </ul>- * <h2>Rules Service processing logic</h2>- * <p>- * Checks if attribute is {@link AssetAttribute#isRuleState} or {@link AssetAttribute#isRuleEvent}, and if- * so the message is passed through the rule engines that are in scope for the asset.- * <p>- * For {@link AssetState} messages, the rules service keeps the facts and thus the state of each rules- * knowledge session in sync with the asset state changes that occur. If an asset attribute value changes,- * the {@link AssetState} in each rules session will be updated to reflect the change.- * <p>- * For {@link AssetEvent} messages (obtained by converting the {@link AssetState}), they are inserted in the rules- * sessions in scope and expired automatically either by a) the rules session if no time-pattern can possibly match- * the event source timestamp anymore or b) by the rules service if the event lifetime set in- * {@link RulesService#RULE_EVENT_EXPIRES} is reached or c) by the rules service if the event lifetime set in the- * attribute {@link AssetMeta#RULE_EVENT_EXPIRES} is reached.* <h2>Agent service processing logic</h2>* <p>* The agent service's role is to communicate asset attribute writes to actuators, through protocols.@@ -100,7 +86,9 @@ import static org.openremote.model.attribute.AttributeEvent.Source.INTERNAL;* If the updated attribute has an invalid agent link, the message status is set to {@link AssetState.ProcessingStatus#ERROR}.* <p>* If the updated attribute has a valid agent link, an {@link AttributeEvent} is sent on the {@link Protocol#ACTUATOR_TOPIC},- * for execution on an actual device or service 'things'.+ * for execution on an actual device or service 'things'. The update is then marked as+ * {@link AssetState.ProcessingStatus#COMPLETED} and no further processing is necessary. The update will not reach the+ * rules engine or the database.* <p>* This means that a protocol implementation is responsible for producing a new {@link AttributeEvent} to* indicate to the system that the attribute value has/has not changed. The protocol should know best when to@@ -108,6 +96,20 @@ import static org.openremote.model.attribute.AttributeEvent.Source.INTERNAL;* with a new sensor read, or they might send a later "sensor changed" message or both or neither (fire and* forget). The protocol must decide what the best course of action is based on the 'things' it communicates with* and the transport layer it uses etc.+ * <h2>Rules Service processing logic</h2>+ * <p>+ * Checks if attribute is {@link AssetAttribute#isRuleState} or {@link AssetAttribute#isRuleEvent}, and if+ * so the message is passed through the rule engines that are in scope for the asset.+ * <p>+ * For {@link AssetState} messages, the rules service keeps the facts and thus the state of each rules+ * knowledge session in sync with the asset state changes that occur. If an asset attribute value changes,+ * the {@link AssetState} in each rules session will be updated to reflect the change.+ * <p>+ * For {@link AssetEvent} messages (obtained by converting the {@link AssetState}), they are inserted in the rules+ * sessions in scope and expired automatically either by a) the rules session if no time-pattern can possibly match+ * the event source timestamp anymore or b) by the rules service if the event lifetime set in+ * {@link RulesService#RULE_EVENT_EXPIRES} is reached or c) by the rules service if the event lifetime set in the+ * attribute {@link AssetMeta#RULE_EVENT_EXPIRES} is reached.* <h2>Asset Storage Service processing logic</h2>* <p>* Always tries to persist the attribute value in the database and allows the message to continue if the commit was@@ -185,8 +187,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerreturn false;});- processors.add(rulesService);processors.add(agentService);+ processors.add(rulesService);processors.add(assetStorageService);processors.add(assetDatapointService);@@ -363,7 +365,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer}switch (assetState.getProcessingStatus()) {- case HANDLED:+ case COMPLETED:LOG.fine("<== Processor " + processor + " finally handled: " + assetState);break processorLoop;case ERROR:@@ -374,7 +376,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer}}if (assetState.getProcessingStatus() != AssetState.ProcessingStatus.ERROR) {- assetState.setProcessingStatus(AssetState.ProcessingStatus.COMPLETED);+ if (assetState.getProcessingStatus() != AssetState.ProcessingStatus.COMPLETED)+ assetState.setProcessingStatus(AssetState.ProcessingStatus.COMPLETED);eventService.publishEvent(new AttributeEvent(assetState.getId(), assetState.getAttributeName(), assetState.getValue()));diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovyindex 4d74d4427..04d6c5191 100644--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy@@ -117,8 +117,8 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitthen: "register mock asset processors"assetProcessingService.processors.add(mockDatapointServiceConsumer)assetProcessingService.processors.add(3, mockAssetStorageConsumer)- assetProcessingService.processors.add(2, mockAgentServiceConsumer)- assetProcessingService.processors.add(1, mockRulesServiceConsumer)+ assetProcessingService.processors.add(2, mockRulesServiceConsumer)+ assetProcessingService.processors.add(1, mockAgentServiceConsumer)assetProcessingService.processors.add(0, mockStartConsumer)when: "a mock agent that uses the mock protocol is created"@@ -179,8 +179,8 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitthen: "the attribute event should reach the protocol and stop at the agent service with a status of COMPLETED"conditions.eventually {assert updatesPassedStartOfProcessingChain.size() == 1- assert updatesPassedRulesService.size() == 1assert updatesPassedAgentService.size() == 0+ assert updatesPassedRulesService.size() == 0assert updatesPassedAssetStorageService.size() == 0assert updatesPassedDatapointService.size() == 0assert updatesPassedStartOfProcessingChain[0].attribute.getAssetId().orElse("") == mockThing.getId()@@ -192,25 +192,33 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait}when: "the protocol updates the attributes value with the value it just received"+ updatesPassedStartOfProcessingChain.clear()+ updatesPassedRulesService.clear()+ updatesPassedAgentService.clear()+ updatesPassedAssetStorageService.clear()+ updatesPassedDatapointService.clear()mockProtocol.responseReceived()then: "a new attribute event should occur and reach the end of the processing chain"conditions.eventually {- assert updatesPassedStartOfProcessingChain.size() == 2- assert updatesPassedRulesService.size() == 2+ assert updatesPassedStartOfProcessingChain.size() == 1assert updatesPassedAgentService.size() == 1+ assert updatesPassedRulesService.size() == 1assert updatesPassedAssetStorageService.size() == 1assert updatesPassedDatapointService.size() == 1- assert updatesPassedStartOfProcessingChain[1].attribute.getAssetId().orElse("") == mockThing.getId()- assert updatesPassedStartOfProcessingChain[1].attribute.getName().orElse("") == "light1Toggle"- assert !Values.getBoolean(updatesPassedStartOfProcessingChain[1].getOldValue()).orElse(true)- assert !updatesPassedStartOfProcessingChain[1].getValueAsBoolean()- assert updatesPassedStartOfProcessingChain[1].error == null- assert updatesPassedStartOfProcessingChain[1].processingStatus == AssetState.ProcessingStatus.COMPLETED+ assert updatesPassedStartOfProcessingChain[0].attribute.getAssetId().orElse("") == mockThing.getId()+ assert updatesPassedStartOfProcessingChain[0].attribute.getName().orElse("") == "light1Toggle"+ assert !Values.getBoolean(updatesPassedStartOfProcessingChain[0].getOldValue()).orElse(true)+ assert !updatesPassedStartOfProcessingChain[0].getValueAsBoolean()+ assert updatesPassedStartOfProcessingChain[0].error == null+ assert updatesPassedStartOfProcessingChain[0].processingStatus == AssetState.ProcessingStatus.COMPLETED}when: "an attribute event occurs for the invalid protocol attribute"updatesPassedStartOfProcessingChain.clear()+ updatesPassedRulesService.clear()+ updatesPassedAgentService.clear()+ updatesPassedAssetStorageService.clear()updatesPassedDatapointService.clear()sendToActuatorEvents.clear()def light2toggleOn = new AttributeEvent(@@ -221,6 +229,9 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitthen: "the attribute event should pass the start of the processing chain, but not reach the mock protocol or the end of the processing chain and error should be populated"conditions.eventually {assert updatesPassedStartOfProcessingChain.size() == 1+ assert updatesPassedAgentService.size() == 0+ assert updatesPassedRulesService.size() == 0+ assert updatesPassedAssetStorageService.size() == 0assert updatesPassedDatapointService.size() == 0assert sendToActuatorEvents.size() == 0assert updatesPassedStartOfProcessingChain[0].id == mockThing.id@@ -232,6 +243,9 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitwhen: "an attribute event occurs for the plain attribute"updatesPassedStartOfProcessingChain.clear()+ updatesPassedRulesService.clear()+ updatesPassedAgentService.clear()+ updatesPassedAssetStorageService.clear()updatesPassedDatapointService.clear()sendToActuatorEvents.clear()def plainAttributeTest = new AttributeEvent(@@ -242,8 +256,11 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitthen: "the attribute event should pass the start of the processing chain and reach the end of the processing chain"conditions.eventually {assert updatesPassedStartOfProcessingChain.size() == 1- assert sendToActuatorEvents.size() == 0+ assert updatesPassedAgentService.size() == 1+ assert updatesPassedRulesService.size() == 1+ assert updatesPassedAssetStorageService.size() == 1assert updatesPassedDatapointService.size() == 1+ assert sendToActuatorEvents.size() == 0assert updatesPassedDatapointService[0].name == "Mock Thing Asset"assert updatesPassedDatapointService[0].attributeName == "plainAttribute"assert Values.getString(updatesPassedDatapointService[0].oldValue).orElse(null) == "demo"diff --git a/model/src/main/java/org/openremote/model/asset/AssetState.java b/model/src/main/java/org/openremote/model/asset/AssetState.javaindex 5da4e4a66..103abd6cc 100644--- a/model/src/main/java/org/openremote/model/asset/AssetState.java+++ b/model/src/main/java/org/openremote/model/asset/AssetState.java@@ -41,19 +41,13 @@ public class AssetState extends AbstractAssetUpdate {*/CONTINUE,- /**- * Processor has finally handled the update, cancel further processing.- */- HANDLED,-/*** Processor encountered an error trying to process the update, cancel further processing and escalate.*/ERROR,/**- * Indicates that this update has been through the entire processing chain; the object can no longer be- * mutated at this stage.+ * Indicates that this update has been handled; the object can no longer be mutated at this stage.*/COMPLETED}