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
Don't store attribute update in database if a processor consumed it completely
Christian Bauer86ff5a1df34f
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex 0689305d5..3458a817d 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -276,8 +276,9 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer/*** This deals with single {@link AssetAttribute} updates and pushes them through the chain where each- * processor is given the opportunity to consume the update or allow its progress to the next- * processor, see {@link AssetUpdateProcessor#processAssetUpdate}.+ * processor is given the opportunity to completely consume the update or allow its progress to the next+ * processor, see {@link AssetUpdateProcessor#processAssetUpdate}. If no processor completely consumed the+ * update, the attribute will be stored in the database.*/protected void processAssetUpdate(EntityManager em,Asset asset,@@ -289,7 +290,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer// is not updated so tests can't then detect the problem.lastProcessedEventTimestamp = System.currentTimeMillis();- boolean complete;+ boolean complete = false;for (AssetUpdateProcessor processor : processors) {LOG.finest("==> Processor " + processor + " accepts: " + attribute);try {@@ -303,20 +304,22 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer);}if (complete) {- LOG.finest("<== Processor " + processor + " finally handled: " + attribute);+ LOG.finest("<== Processor " + processor + " completely consumed: " + attribute);break;} else {LOG.finest("<== Processor " + processor + " done with: " + attribute);}}- // Store state in database after all processors are done- storeAttribute(em, asset, attribute);+ if (!complete) {+ LOG.finest("No processor consumed the update completely, storing: " + attribute);+ storeAttribute(em, asset, attribute);+ }LOG.fine("<<< Processing complete: " + attribute);}- public Processor processAttributeEvent() {+ protected Processor processAttributeEvent() {return exchange -> {AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);if (event.getEntityId() == null || event.getEntityId().isEmpty())@@ -448,7 +451,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer};}- public static Processor handleAssetProcessingException(Logger logger) {+ protected static Processor handleAssetProcessingException(Logger logger) {return exchange -> {AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovyindex 42f452abc..04282c2b6 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy@@ -163,7 +163,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitand: "a mock thing asset is created with a valid protocol attribute, an invalid protocol attribute and a plain attribute"def mockThing = new ServerAsset("Mock Thing Asset", AssetType.THING, mockAgent)mockThing.setAttributes(- new AssetAttribute("light1Toggle", AttributeType.BOOLEAN, Values.create(false))+ new AssetAttribute("light1Toggle", AttributeType.BOOLEAN, Values.create(true)).setMeta(new MetaItem(AssetMeta.DESCRIPTION,@@ -174,7 +174,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitnew AttributeRef(mockAgent.getId(), "mock123").toArrayValue())),- new AssetAttribute("light2Toggle", AttributeType.BOOLEAN, Values.create(false))+ new AssetAttribute("light2Toggle", AttributeType.BOOLEAN, Values.create(true)).setMeta(new MetaItem(AssetMeta.DESCRIPTION,@@ -205,7 +205,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait)assetProcessingService.sendAttributeEvent(light1toggleOn)- then: "the attribute event should reach the protocol and stop at the agent service with a status of COMPLETED"+ then: "the attribute event should reach the protocol and stop at the agent service, not be in the database"conditions.eventually {assert updatesPassedStartOfProcessingChain.size() == 1assert updatesPassedStartOfProcessingChain[0].nameOrThrow == "light1Toggle"@@ -215,6 +215,9 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitassert updatesPassedRulesService.size() == 0assert updatesPassedDatapointService.size() == 0assert updatesPassedAttributeLinkingService.size() == 0+ // Light toggle should still be on in database+ def asset = assetStorageService.find(mockThing.getId(), true)+ assert asset.getAttribute("light1Toggle").get().getValueAsBoolean().get()}when: "the protocol updates the attributes value with the value it just received"@@ -225,7 +228,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitupdatesPassedAttributeLinkingService.clear()mockProtocol.responseReceived()- then: "a new attribute event should occur and reach the end of the processing chain"+ then: "a new attribute event should occur and reach the end of the processing chain, be stored in database"conditions.eventually {assert updatesPassedStartOfProcessingChain.size() == 1assert updatesPassedStartOfProcessingChain[0].nameOrThrow == "light1Toggle"@@ -234,6 +237,9 @@ class AssetProcessingTest extends Specification implements ManagerContainerTraitassert updatesPassedRulesService.size() == 1assert updatesPassedDatapointService.size() == 1assert updatesPassedAttributeLinkingService.size() == 1+ // Light toggle should be off in database+ def asset = assetStorageService.find(mockThing.getId(), true)+ assert !asset.getAttribute("light1Toggle").get().getValueAsBoolean().get()}when: "an attribute event occurs for the invalid protocol attribute"