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
JSON rules bug fixes
Rich Turnerc4d9acadbcdb
diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.javaindex 788ae2c89..9afbf7e39 100644--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java@@ -236,7 +236,7 @@ public class JsonRulesBuilder extends RulesBuilder {unfilteredAssetStates.stream().collect(Collectors.groupingBy(AssetState::getId)).forEach((id, states) -> {- Map<Boolean, List<AssetState>> assetResults = unfilteredAssetStates.stream().collect(Collectors.groupingBy(assetStatePredicate::test));+ Map<Boolean, List<AssetState>> assetResults = states.stream().collect(Collectors.groupingBy(assetStatePredicate::test));matched.addAll(assetResults.getOrDefault(true, Collections.emptyList()));unmatched.addAll(assetResults.getOrDefault(false, Collections.emptyList()));});@@ -261,16 +261,12 @@ public class JsonRulesBuilder extends RulesBuilder {}}- // Remove previous matches where a recurrence timer doesn't exist for the asset- // And also depending on reset option whether the asset state no longer matches or the value has changed- previouslyMatchedAssetStates.removeIf(previousAssetState -> {-- boolean hasActiveRecurTimer = nextRecurAssetIdMap.containsKey(previousAssetState.getId())- && nextRecurAssetIdMap.get(previousAssetState.getId()) > timerService.getCurrentTimeMillis();+ // Remove matches that have an active recurrence timer+ matchedAssetStates.removeIf(matchedAssetState -> nextRecurAssetIdMap.containsKey(matchedAssetState.getId())+ && nextRecurAssetIdMap.get(matchedAssetState.getId()) > timerService.getCurrentTimeMillis());- if (hasActiveRecurTimer) {- return false;- }+ // Remove previous matches where the asset state no longer matches or the value has changed (depending on reset option)+ previouslyMatchedAssetStates.removeIf(previousAssetState -> {int matchIndex = matchedAssetStates.indexOf(previousAssetState);boolean valueChangedOrNoLongerMatches = matchIndex < 0;@@ -515,7 +511,10 @@ public class JsonRulesBuilder extends RulesBuilder {return false;}- // Update each trigger state+ // Clear out expired recurrence timers+ ruleState.nextRecurAssetIdMap.entrySet().removeIf(entry -> entry.getValue() <= timerService.getCurrentTimeMillis());++ // Update each condition statelog(Level.FINEST, "Updating rule condition states for rule: " + rule.name);ruleState.conditionStateMap.values().forEach(ruleConditionState -> ruleConditionState.update(ruleState.nextRecurAssetIdMap));@@ -551,13 +550,10 @@ public class JsonRulesBuilder extends RulesBuilder {throw e;} finally {- // Clear out expired recurrence timers- ruleState.nextRecurAssetIdMap.entrySet().removeIf(entry -> entry.getValue() <= timerService.getCurrentTimeMillis());-// Store recurrence times as requiredboolean recurPerAsset = rule.recurrence == null || rule.recurrence.scope != RuleRecurrence.Scope.GLOBAL;long currentTime = timerService.getCurrentTimeMillis();- long nextRecur = rule.recurrence == null || rule.recurrence.mins == null ? Long.MAX_VALUE : currentTime + rule.recurrence.mins;+ long nextRecur = rule.recurrence == null || rule.recurrence.mins == null ? Long.MAX_VALUE : currentTime + (rule.recurrence.mins * 60000);if (nextRecur > currentTime) {if (recurPerAsset) {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 8e8ff53ef..83e20f82c 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@@ -288,16 +288,50 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {assert tenantBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.DEPLOYED}+ and: "another notification should have been sent to the console when rule is redeployed"+ conditions.eventually {+ assert notificationMessages.findAll {it.title == "Test title"}.size() == 3+ assert targets[2].id == consoleRegistration.id+ }++ when: "the console device moves back inside the home geofence (as defined in the rule)"+ lastFireTimestamp = tenantBuildingEngine.lastFireTimestamp+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue()), AttributeEvent.Source.CLIENT)++ then: "the engine fires at least one more time"+ conditions.eventually {+ assert tenantBuildingEngine.lastFireTimestamp > lastFireTimestamp+ }++ when: "the console device moves outside the home geofence again (as defined in the rule)"+ attributeEvent = new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d, 0d).toValue())+ assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)++ then: "after a few seconds the rule should not have fired again"+ new PollingConditions(initialDelay: 1).eventually {+ assert notificationMessages.findAll {it.title == "Test title"}.size() == 3+ }++ when: "the console device moves back inside the home geofence (as defined in the rule)"+ lastFireTimestamp = tenantBuildingEngine.lastFireTimestamp+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue()), AttributeEvent.Source.CLIENT)++ then: "the engine fires at least one more time"+ conditions.eventually {+ assert tenantBuildingEngine.lastFireTimestamp > lastFireTimestamp+ }+when: "when time advances 5 hours"advancePseudoClock(5, HOURS, container)- and: "the same AttributeEvent is sent"+ and: "the console device moves outside the home geofence again (as defined in the rule)"+ attributeEvent = new AttributeEvent(consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d, 0d).toValue())assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)then: "another notification should have been sent to the console"conditions.eventually {- assert notificationMessages.findAll {it.title == "Test title"}.size() == 3- assert targets[2].id == consoleRegistration.id+ assert notificationMessages.findAll {it.title == "Test title"}.size() == 4+ assert targets[3].id == consoleRegistration.id}when: "the Rules PAUSE_SCHEDULER is overridden to facilitate testing"@@ -349,7 +383,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}and: "no notification should have been sent as outside the validity period"- assert notificationMessages.findAll {it.title == "Test title"}.size() == 3+ assert notificationMessages.findAll {it.title == "Test title"}.size() == 4when: "the pause elapses"engine.unPauseRuleset(deployment)@@ -372,8 +406,8 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {and: "another notification should have been sent as inside the validity period"conditions.eventually {- assert notificationMessages.findAll {it.title == "Test title"}.size() == 4- assert targets[3].id == consoleRegistration.id+ assert notificationMessages.findAll {it.title == "Test title"}.size() == 5+ assert targets[4].id == consoleRegistration.id}when: "the un-pause elapses"@@ -396,7 +430,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}and: "no notification should have been sent as outside the validity period"- assert notificationMessages.findAll {it.title == "Test title"}.size() == 4+ assert notificationMessages.findAll {it.title == "Test title"}.size() == 5when: "the pause elapses"engine.unPauseRuleset(deployment)