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
Made reset.timer work. Update the tests for all reset possibilities
Michael Rademakerf270f1cae7bc
diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.javaindex 9373d194c..330eea702 100644--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java@@ -306,6 +306,21 @@ public class JsonRulesBuilder extends RulesBuilder {}return expired;});++ if (ruleCondition.reset.timestampChanges) {+ previouslyMatchedAssetStates.removeIf(previousAssetState -> {+ int index = matchedAssetStates.indexOf(previousAssetState);+ if (index < 0) {+ return false;+ }++ boolean timestampChanged = previousAssetState.getTimestamp() != matchedAssetStates.get(index).getTimestamp();+ if (timestampChanged) {+ log(Level.FINER, "Rule trigger previously matched asset state timestamp has changed so resetting: " + previousAssetState);+ }+ return timestampChanged;+ });+ }}if (ruleCondition.reset != null && ruleCondition.reset.valueChanges) {@@ -324,7 +339,7 @@ public class JsonRulesBuilder extends RulesBuilder {}// Filter out previous matches to avoid re-triggering- matchedAssetStates.removeIf(assetState -> previouslyMatchedAssetStates.contains(assetState));+ matchedAssetStates.removeIf(previouslyMatchedAssetStates::contains);// Select unique asset states based on asset idStream<AssetState> matchedAssetStateStream = matchedAssetStates.stream().filter(distinctByKey(AssetState::getId));// Order asset states before applying limit@@ -541,12 +556,12 @@ public class JsonRulesBuilder extends RulesBuilder {RuleConditionReset reset = ruleConditionState.ruleCondition.reset;if (ruleConditionResetHasTimer(reset)) {- if (reset.timestampChanges) {+ if (ruleConditionState.resetDurationMillis > 0) {ruleConditionState.lastTriggerResult.matchedAssetStates.forEach(assetState ->- ruleConditionState.previouslyMatchedExpiryTimes.put(assetState, assetState.getTimestamp()));- } else if (ruleConditionState.resetDurationMillis > 0) {+ ruleConditionState.previouslyMatchedExpiryTimes.put(assetState, timerService.getCurrentTimeMillis() + ruleConditionState.resetDurationMillis));+ } else if (reset.timestampChanges) {ruleConditionState.lastTriggerResult.matchedAssetStates.forEach(assetState ->- ruleConditionState.previouslyMatchedExpiryTimes.put(assetState, timerService.getCurrentTimeMillis() + ruleConditionState.resetDurationMillis));+ ruleConditionState.previouslyMatchedExpiryTimes.put(assetState, assetState.getTimestamp()));}}if (ruleConditionState.trackUnmatched) {diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovyindex 2bbf69bfe..2b5daf01f 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy@@ -3,6 +3,7 @@ package org.openremote.test.rules.residenceimport com.google.common.collect.Listsimport com.google.firebase.messaging.Messageimport org.openremote.container.timer.TimerService+import org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.notification.PushNotificationHandlerimport org.openremote.manager.rules.RulesEngine@@ -12,7 +13,7 @@ import org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapterimport org.openremote.manager.setup.SetupServiceimport org.openremote.manager.setup.builtin.KeycloakDemoSetupimport org.openremote.manager.setup.builtin.ManagerDemoSetup-import org.openremote.model.asset.AssetResource+import org.openremote.model.attribute.AttributeEventimport org.openremote.model.console.ConsoleProviderimport org.openremote.model.console.ConsoleRegistrationimport org.openremote.model.console.ConsoleResource@@ -27,6 +28,7 @@ import org.openremote.test.ManagerContainerTraitimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions+import static java.util.concurrent.TimeUnit.HOURSimport static java.util.concurrent.TimeUnit.MINUTESimport static org.openremote.manager.setup.builtin.ManagerDemoSetup.DEMO_RULE_STATES_CUSTOMER_Aimport static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID@@ -70,6 +72,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {def rulesetStorageService = container.getService(RulesetStorageService.class)def timerService = container.getService(TimerService.class)def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)RulesEngine tenantAEngineand: "some rules"@@ -109,7 +112,6 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {and: "a console is registered by that user"def authenticatedConsoleResource = getClientApiTarget(serverUri(serverPort), keycloakDemoSetup.tenantA.realm, accessToken).proxy(ConsoleResource.class)- def authenticatedAssetResource = getClientApiTarget(serverUri(serverPort), keycloakDemoSetup.tenantA.realm, accessToken).proxy(AssetResource.class)def consoleRegistration = new ConsoleRegistration(null,"Test Console","1.0",@@ -144,7 +146,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {assert consoleRegistration.id != nullwhen: "the console location is set to the apartment"- authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue().toJson())+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue()), AttributeEvent.Source.CLIENT)then: "the consoles location should have been updated"conditions.eventually {@@ -169,7 +171,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}when: "the console device moves outside the home geofence (as defined in the rule)"- authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d,0d).toValue().toJson())+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d, 0d).toValue()), AttributeEvent.Source.CLIENT)then: "the apartment lights should be switched off"conditions.eventually {@@ -194,7 +196,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves back inside the home geofence (as defined in the rule)"def lastFireTimestamp = tenantAEngine.lastFireTimestamp- authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue().toJson())+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue()), AttributeEvent.Source.CLIENT)and: "the engine fires at least one more time"conditions.eventually {@@ -202,7 +204,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}and: "the console device moves outside the home geofence again (as defined in the rule)"- authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d,0d).toValue().toJson())+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d, 0d).toValue()), AttributeEvent.Source.CLIENT)then: "another notification should have been sent to the console"conditions.eventually {@@ -212,7 +214,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console sends a location update with the same location but a newer timestamp"advancePseudoClock(35, MINUTES, container)- authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d,0d).toValue().toJson())+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d, 0d).toValue()), AttributeEvent.Source.CLIENT)then: "another notification should have been sent to the console (because the reset condition includes reset on timestampChanges)"conditions.eventually {@@ -221,7 +223,8 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}when: "the console sends a location update with a new location but still outside the geofence"- authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(10d,10d).toValue().toJson())+ def attributeEvent = new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(10d, 10d).toValue(), timerService.getCurrentTimeMillis())+ assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)then: "another notification should have been sent to the console (because the reset condition includes reset on valueChanges)"conditions.eventually {@@ -229,6 +232,18 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {assert targetIds[3] == consoleRegistration.id}+ when: "when time advances 5 hours"+ advancePseudoClock(5, HOURS, container)++ and: "the same AttributeEvent is send"+ assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)++ then: "another notification should have been sent to the console (because the reset condition includes reset on timer)"+ conditions.eventually {+ assert notificationMessages.size() == 5+ assert targetIds[4] == consoleRegistration.id+ }+cleanup: "stop the container"stopContainer(container)}diff --git a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.jsonindex f258b118f..96cc507f4 100644--- a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json+++ b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json@@ -122,7 +122,8 @@"reset": {"noLongerMatches": true,"valueChanges": true,- "timestampChanges": true+ "timestampChanges": true,+ "timer": "4h"}}]