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
Added timestampOlderThan condition to JSON Rules AttributePredicate (#1583)
Dominique Kleeven5457c1e27f10
diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex 23a5cc4e6..c47d0093c 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -27,6 +27,7 @@ import org.openremote.model.attribute.MetaMap;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.LogicGroup;import org.openremote.model.query.filter.*;+import org.openremote.model.util.TimeUtil;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.MetaHolder;import org.openremote.model.value.NameValueHolder;@@ -197,9 +198,23 @@ public class AssetQueryPredicate implements Predicate<AttributeInfo> {if (condition.getItems().size() > 0) {condition.getItems().stream()- .forEach(p -> {- attributePredicates.add((Predicate<AttributeInfo>)(Predicate)asPredicate(currentMillisProducer, p));-+ .forEach(p -> {+ Predicate<AttributeInfo> wrappedPredicate = attributeInfo -> {++ // Check timestamp not updated for condition+ if (p.timestampOlderThan != null) {+ long currentTime = currentMillisProducer.get();+ long durationMillis = TimeUtil.parseTimeDuration(p.timestampOlderThan);+ if (attributeInfo.getTimestamp() > currentTime - durationMillis) {+ return false;+ }+ }++ Predicate<NameValueHolder<?>> basePredicates = asPredicate(currentMillisProducer, p);+ return basePredicates.test(attributeInfo);+ };+ attributePredicates.add(wrappedPredicate);+AtomicReference<Predicate<AttributeInfo>> metaPredicate = new AtomicReference<>(nameValueHolder -> true);AtomicReference<Predicate<AttributeInfo>> oldValuePredicate = new AtomicReference<>(value -> true);diff --git a/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java b/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.javaindex 74f7f98fa..8e54ffdbd 100644--- a/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java@@ -37,6 +37,9 @@ public class AttributePredicate extends NameValuePredicate {public NameValuePredicate[] meta;public ValuePredicate previousValue;+ // ISO 8601 duration expression (e.g. PT5M)+ public String timestampOlderThan;+public AttributePredicate() {}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 496336673..05d5fa3a2 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@@ -693,6 +693,177 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}}+ def "Trigger actions when an attribute is not updated for a specified duration"() {++ given: "the container environment is started"+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)+ def container = startContainer(defaultConfig(), defaultServices())+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def rulesService = container.getService(RulesService.class)+ 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 realmBuildingEngine++ and: "the pseudo clock is stopped"+ stopPseudoClock()++ when: "a light asset is added to the building realm"+ def lightId = UniqueIdentifierGenerator.generateId("LightTest")+ def lightAsset = new LightAsset("LightTest")+ .setId(lightId)+ .setRealm(keycloakTestSetup.realmBuilding.name)+ .setLocation(new GeoJSONPoint(0, 0))++ def ruleState = new MetaItem<>(MetaItemType.RULE_STATE)+ lightAsset.getAttributes().get("brightness").get().addMeta(ruleState)+ lightAsset.getAttributes().get("onOff").get().addMeta(ruleState)+ assetStorageService.merge(lightAsset)++ then: "the light asset should be present"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light != null+ }++ // RuleSet: WHEN:+ // Any light asset has its onOff attribute not updated for 3 minutes+ // AND+ // Any light asset has its brightness attribute greater than 50+ // THEN:+ // Set the notes of the light asset to "Matched"+ when: "a ruleset with a notUpdatedFor attribute condition has been added"+ def rulesStr = getClass().getResource("/org/openremote/test/rules/JsonNotUpdatedForRule.json").text+ def rule = parse(rulesStr, JsonRulesetDefinition.class).orElseThrow()+ Ruleset ruleset = new RealmRuleset(+ keycloakTestSetup.realmBuilding.name,+ "Not Updated For Rule",+ Ruleset.Lang.JSON,+ rulesStr)+ ruleset = rulesetStorageService.merge(ruleset)+ def lastFireTimestamp = 0++ then: "the rule engines to become available and be running with asset states inserted"+ conditions.eventually {+ realmBuildingEngine = rulesService.realmEngines.get(keycloakTestSetup.realmBuilding.name)+ assert realmBuildingEngine != null+ assert realmBuildingEngine.isRunning()+ assert realmBuildingEngine.facts.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING + 2 // 2 additional rule states+ assert realmBuildingEngine.lastFireTimestamp == timerService.getNow().toEpochMilli()+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp+ }++ when: "the light asset is updated with onOff set to true and brightness set to 100"+ lightAsset = assetStorageService.find(lightId)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.BRIGHTNESS, 100))+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.ON_OFF, true))++ then: "the light asset should have its onOff attribute set to true and brightness set to 100"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("onOff").get().getValue().orElse(false) == true+ assert light.getAttribute("brightness").get().getValue().orElse(0) == 100+ }++ when: "time advances for 6 seconds"+ advancePseudoClock(6, TimeUnit.SECONDS, container)++ then: "the rule engine should have fired"+ conditions.eventually {+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp+ }++ and: "the light asset note attribute should still be empty"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("notes").get().getValue().orElse("") == ""+ }++ when: "time advances for 1 minute"+ advancePseudoClock(1, TimeUnit.MINUTES, container)++ then: "the rule engine should have fired"+ conditions.eventually {+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp+ }++ and: "the light asset note attribute should still be empty"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("notes").get().getValue().orElse("") == ""+ }++ when: "time advances for 2 minutes"+ advancePseudoClock(2, TimeUnit.MINUTES, container)++ then: "the rule engine should have fired"+ conditions.eventually {+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp+ }++ and: "the light asset note attribute should be set to Matched" // because onOff was still unchanged for 3 minutes and brightness was above 50+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("notes").get().getValue().orElse("") == "Matched"+ }++ when: "brightness is updated to 0 and notes is updated to empty"+ lightAsset = assetStorageService.find(lightId)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.BRIGHTNESS, 0))+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.NOTES, ""))++ then: "the light asset should have its brightness set to 0 and notes set to empty"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("brightness").get().getValue().orElse(0) == 0+ assert light.getAttribute("notes").get().getValue().orElse("") == ""+ }++ when: "time advances for 6 seconds"+ advancePseudoClock(6, TimeUnit.SECONDS, container)++ then: "the rule engine should have fired"+ conditions.eventually {+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp+ }++ when: "brightness is updated to 100"+ lightAsset = assetStorageService.find(lightId)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.BRIGHTNESS, 100))++ then: "the light asset should have its brightness set to 100"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("brightness").get().getValue().orElse(0) == 100+ }++ and: "notes should still be empty until the rule engine fires"+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("notes").get().getValue().orElse("") == ""+ }++ when: "time advances for 6 seconds"+ advancePseudoClock(6, TimeUnit.SECONDS, container)++ then: "the rule engine should have fired"+ conditions.eventually {+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp+ }++ and: "the light asset note attribute should be set to Matched" // because onOff was still unchanged for 3 minutes and brightness was set to be above 50 again+ conditions.eventually {+ def light = assetStorageService.find(lightId)+ assert light.getAttribute("notes").get().getValue().orElse("") == "Matched"+ }+ }+def "Trigger actions when attribute conditions match for specified durations"() {given: "the container environment is started"diff --git a/test/src/test/resources/org/openremote/test/rules/JsonNotUpdatedForRule.json b/test/src/test/resources/org/openremote/test/rules/JsonNotUpdatedForRule.jsonnew file mode 100644index 000000000..9d7099e07--- /dev/null+++ b/test/src/test/resources/org/openremote/test/rules/JsonNotUpdatedForRule.json@@ -0,0 +1,55 @@+{+ "rules": [+ {+ "recurrence": { "mins": 0 },+ "when": {+ "operator": "OR",+ "groups": [+ {+ "operator": "AND",+ "items": [+ {+ "assets": {+ "types": ["LightAsset"],+ "attributes": {+ "items": [+ {+ "name": {+ "predicateType": "string",+ "match": "EXACT",+ "value": "onOff"+ },+ "timestampOlderThan": "PT3M"+ },+ {+ "name": {+ "predicateType": "string",+ "match": "EXACT",+ "value": "brightness"+ },+ "value": {+ "predicateType": "number",+ "operator": "GREATER_THAN",+ "value": 50+ }+ }+ ]+ }+ }+ }+ ]+ }+ ]+ },+ "then": [+ {+ "action": "write-attribute",+ "target": { "matchedAssets": { "types": ["LightAsset"] } },+ "value": "Matched",+ "attributeName": "notes"+ }+ ],+ "name": "My Light: OnOff not updated for"+ }+ ]+}diff --git a/ui/app/shared/locales/de/or.json b/ui/app/shared/locales/de/or.jsonindex e301a40de..d11562b11 100644--- a/ui/app/shared/locales/de/or.json+++ b/ui/app/shared/locales/de/or.json@@ -58,6 +58,7 @@"notEndsWith": "Endet nicht mit","containsKey": "Enthält Schlüssel","notContainsKey": "Enthält nicht den Schlüssel",+ "notUpdatedFor": "Nicht aktualisiert für","true": "Wahr","false": "Falsch","radiusMin": "Radius (min. 100m)",diff --git a/ui/app/shared/locales/en/or.json b/ui/app/shared/locales/en/or.jsonindex 36452c57d..b18418d0f 100644--- a/ui/app/shared/locales/en/or.json+++ b/ui/app/shared/locales/en/or.json@@ -61,6 +61,7 @@"notEndsWith": "Does not end with","containsKey": "Contains key","notContainsKey": "Does not contain key",+ "notUpdatedFor": "Not updated for","true": "Is true","false": "Is false","radiusMin": "Radius (min. 100m)",diff --git a/ui/app/shared/locales/nl/or.json b/ui/app/shared/locales/nl/or.jsonindex 58d5628e3..3a99c710b 100644--- a/ui/app/shared/locales/nl/or.json+++ b/ui/app/shared/locales/nl/or.json@@ -61,6 +61,7 @@"notEndsWith": "Eindigt niet met","containsKey": "Bevat key","notContainsKey": "Bevat geen keys",+ "notUpdatedFor": "Niet bijgewerkt voor","true": "Waar","false": "Niet waar","radiusMin": "Straal (min. 100m)",diff --git a/ui/component/or-rules/src/index.ts b/ui/component/or-rules/src/index.tsindex 0162fb416..78f30ebc6 100644--- a/ui/component/or-rules/src/index.ts+++ b/ui/component/or-rules/src/index.ts@@ -94,7 +94,8 @@ export enum AssetQueryOperator {WITHIN_RADIUS = "withinRadius",OUTSIDE_RADIUS = "outsideRadius",WITHIN_RECTANGLE = "withinRectangle",- OUTSIDE_RECTANGLE = "outsideRectangle"+ OUTSIDE_RECTANGLE = "outsideRectangle",+ NOT_UPDATED_FOR = "notUpdatedFor"}export interface AllowedActionTargetTypes {diff --git a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.tsindex 043474cc2..e966ed016 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts@@ -142,7 +142,8 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {AssetQueryOperator.WITHIN_RECTANGLE,AssetQueryOperator.OUTSIDE_RECTANGLE,AssetQueryOperator.VALUE_EMPTY,- AssetQueryOperator.VALUE_NOT_EMPTY+ AssetQueryOperator.VALUE_NOT_EMPTY,+ AssetQueryOperator.NOT_UPDATED_FOR];this._queryOperatorsMap["string"] = [AssetQueryOperator.EQUALS,@@ -154,7 +155,8 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {AssetQueryOperator.ENDS_WITH,AssetQueryOperator.NOT_ENDS_WITH,AssetQueryOperator.VALUE_EMPTY,- AssetQueryOperator.VALUE_NOT_EMPTY+ AssetQueryOperator.VALUE_NOT_EMPTY,+ AssetQueryOperator.NOT_UPDATED_FOR];this._queryOperatorsMap["number"] = [AssetQueryOperator.EQUALS,@@ -166,13 +168,15 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {AssetQueryOperator.BETWEEN,AssetQueryOperator.NOT_BETWEEN,AssetQueryOperator.VALUE_EMPTY,- AssetQueryOperator.VALUE_NOT_EMPTY+ AssetQueryOperator.VALUE_NOT_EMPTY,+ AssetQueryOperator.NOT_UPDATED_FOR];this._queryOperatorsMap["boolean"] = [AssetQueryOperator.IS_TRUE,AssetQueryOperator.IS_FALSE,AssetQueryOperator.VALUE_EMPTY,- AssetQueryOperator.VALUE_NOT_EMPTY+ AssetQueryOperator.VALUE_NOT_EMPTY,+ AssetQueryOperator.NOT_UPDATED_FOR];this._queryOperatorsMap["array"] = [AssetQueryOperator.CONTAINS,@@ -184,13 +188,15 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {AssetQueryOperator.LENGTH_LESS_THAN,AssetQueryOperator.LENGTH_GREATER_THAN,AssetQueryOperator.VALUE_EMPTY,- AssetQueryOperator.VALUE_NOT_EMPTY+ AssetQueryOperator.VALUE_NOT_EMPTY,+ AssetQueryOperator.NOT_UPDATED_FOR];this._queryOperatorsMap["object"] = [AssetQueryOperator.CONTAINS_KEY,AssetQueryOperator.NOT_CONTAINS_KEY,AssetQueryOperator.VALUE_EMPTY,- AssetQueryOperator.VALUE_NOT_EMPTY+ AssetQueryOperator.VALUE_NOT_EMPTY,+ AssetQueryOperator.NOT_UPDATED_FOR];}@@ -238,6 +244,25 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {}protected attributePredicateValueEditorTemplate(assetDescriptor: AssetDescriptor, asset: Asset | undefined, attributePredicate: AttributePredicate) {+ const operator = this.getOperator(attributePredicate);++ if (operator === AssetQueryOperator.NOT_UPDATED_FOR) {+ const duration = attributePredicate.timestampOlderThan ?+ moment.duration(attributePredicate.timestampOlderThan) : undefined;+ return html`+ <or-mwc-input type="${InputType.NUMBER}"+ min="0"+ .value="${duration?.asMinutes()}"+ label="${i18next.t("rulesEditorDuration")}"+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => {+ const minutes = ev.detail.value;+ const newDuration = moment.duration(minutes, "minutes");+ attributePredicate.timestampOlderThan = minutes > 0 ?+ newDuration.toISOString() : undefined;+ this.dispatchEvent(new OrRulesJsonRuleChangedEvent());+ }}">+ </or-mwc-input>`;+ }const valuePredicate = attributePredicate.value;@@ -250,7 +275,6 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {const attribute = asset && asset.attributes && attributeName ? asset.attributes[attributeName] : undefined;const descriptors = AssetModelUtil.getAttributeAndValueDescriptors(assetType, attributeName, attribute);-// @ts-ignoreconst value = valuePredicate ? valuePredicate.value : undefined;@@ -288,6 +312,14 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {}protected attributeDurationTemplate(durationMap: Map<number, string | undefined>, index: number, onAdd: (index: number) => void, onChange: (index: number, duration: string | undefined) => void) {+ const attributePredicate = this.query.attributes?.items?.[index];+ const operator = attributePredicate ? this.getOperator(attributePredicate) : undefined;++ // Don't show duration button if NOT_UPDATED_FOR is selected+ if (operator === AssetQueryOperator.NOT_UPDATED_FOR) {+ return html``;+ }+if(durationMap.has(index)) {const isoDuration = durationMap.get(index);const duration = isoDuration ? moment.duration(isoDuration) : undefined;@@ -529,8 +561,16 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {}protected getOperator(attributePredicate: AttributePredicate): string | undefined {+ if (!attributePredicate) {+ return;+ }++ // Check for timestampOlderThan, it's independent of value predicate+ if (attributePredicate.timestampOlderThan !== undefined) {+ return AssetQueryOperator.NOT_UPDATED_FOR;+ }- if (!attributePredicate || !attributePredicate.value) {+ if (!attributePredicate.value) {return;}@@ -591,6 +631,20 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {}}+ protected updateDurationMap(index: number): void {+ if (this.duration) {+ // re-assign durations by filtering out the index and adjusting remaining indices+ const newDurationEntries = Array.from(this.duration.entries())+ .filter(([k, _]) => k !== index)+ .map(([k, v]) => {+ const newIndex = k > index ? k - 1 : k;+ return [newIndex, v] as [number, string | undefined]; // adjust indices after the removed index+ });++ this.duration = new Map<number, string | undefined>(newDurationEntries);+ }+ }+protected setOperator(assetDescriptor: AssetDescriptor, attribute: Attribute<any> | undefined, attributeName: string, attributePredicate: AttributePredicate, operator: string | undefined) {if (!this.query@@ -617,6 +671,8 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {return;}+ attributePredicate.timestampOlderThan = undefined;+const valueDescriptor = descriptors[1];let predicate: ValuePredicateUnion | undefined;@@ -794,6 +850,13 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {match: AssetQueryMatch.CONTAINS};}+ // operator without value predicate - since timestamp is being used rather than attribute value+ case AssetQueryOperator.NOT_UPDATED_FOR:+ attributePredicate.timestampOlderThan = "";++ const index = this.query.attributes.items.indexOf(attributePredicate);+ this.updateDurationMap(index);+ break;}attributePredicate.value = predicate;@@ -823,18 +886,7 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {const index = group.items!.indexOf(attributePredicate);if (index >= 0) {group.items!.splice(index, 1);-- if (this.duration) {- // re-assign durations- const newDurationEntries = Array.from(this.duration.entries())- .filter(([k, _]) => k !== index) // filter out the index- .map(([k, v]) => {- const newIndex = k > index ? k - 1 : k;- return [newIndex, v] as [number, string | undefined]; // adjust indices after the removed index- })-- this.duration = new Map<number, string | undefined>(newDurationEntries);- }+ this.updateDurationMap(index);}this.dispatchEvent(new OrRulesJsonRuleChangedEvent());this.requestUpdate();diff --git a/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.ts b/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.tsindex 28b71de57..3606f60aa 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-json-viewer.ts@@ -423,7 +423,7 @@ export class OrRuleJsonViewer extends translate(i18next)(LitElement) implementsif (!attribute.name || !attribute.name.match || !attribute.name.value) {return false;}- if (!attribute.value || !this._validateValuePredicate(attribute.value)) {+ if (!attribute.timestampOlderThan && (!attribute.value || !this._validateValuePredicate(attribute.value))) {return false;}}