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
Fixed presence detection test
Christian Bauerc99805a1cb0c
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.javaindex 26ecdcf84..2861da15f 100644--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java@@ -276,7 +276,7 @@ public class SimulatorProtocol extends AbstractProtocol {/*** Call this to simulate a send to actuator.*/- protected void putState(String entityId, String attributeName, Value value) {+ public void putState(String entityId, String attributeName, Value value) {putState(new AttributeState(new AttributeRef(entityId, attributeName), value));}@@ -290,14 +290,14 @@ public class SimulatorProtocol extends AbstractProtocol {/*** Call this to simulate a send to actuator.*/- protected void putState(AttributeEvent event) {+ public void putState(AttributeEvent event) {putState(event.getAttributeState());}/*** Call this to simulate a send to actuator.*/- protected void putState(AttributeState attributeState) {+ public void putState(AttributeState attributeState) {AttributeRef attributeRef = attributeState.getAttributeRef();AttributeRef instanceRef = attributeInstanceMap.get(attributeRef);diff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentPresenceDetectionCounter.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentPresenceDetectionCounter.drlindex f00c6f4ea..6fd2a524d 100644--- a/manager/server/src/main/resources/demo/rules/DemoApartmentPresenceDetectionCounter.drl+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentPresenceDetectionCounter.drl@@ -49,7 +49,7 @@ when$count >= 5)// And the latest counter increment event is newer than the recorded last presence detected time- AssetState(id == $room.id, attributeName == "lastPresenceDetected", $latestIncrementTimestamp > valueAsNumber)+ AssetState(id == $room.id, attributeName == "lastPresenceDetected", valueAsNumber == null || $latestIncrementTimestamp > valueAsNumber)then// Update the last presence detected timestamp to the latest counter increment eventassets.dispatch(diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovyindex 8433ca30a..1b11ad167 100644--- a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy@@ -1,5 +1,6 @@package org.openremote.test.rules.apartment+import org.openremote.agent.protocol.simulator.SimulatorProtocolimport org.openremote.manager.server.asset.AssetProcessingServiceimport org.openremote.manager.server.asset.AssetStorageServiceimport org.openremote.manager.server.rules.RulesEngine@@ -13,18 +14,16 @@ import org.openremote.model.rules.AssetRulesetimport org.openremote.model.rules.Rulesetimport org.openremote.model.value.Valuesimport org.openremote.test.ManagerContainerTrait-import spock.lang.Ignoreimport spock.lang.Specificationimport spock.util.concurrent.PollingConditionsimport static java.util.concurrent.TimeUnit.*import static org.openremote.manager.server.setup.builtin.ManagerDemoSetup.DEMO_RULE_STATES_APARTMENT_1import static org.openremote.manager.server.setup.builtin.ManagerDemoSetup.DEMO_RULE_STATES_APARTMENT_2+import static org.openremote.model.attribute.AttributeEvent.Source.SENSORclass ApartmentPresenceDetectionTest extends Specification implements ManagerContainerTrait {- // TODO: Rewrite with co2 level confirmation rules- @Ignoredef "Detect presence with motion counter"() {given: "the container environment is started"@@ -33,9 +32,9 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCondef container = startContainerWithPseudoClock(defaultConfig(serverPort), defaultServices())def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)def rulesService = container.getService(RulesService.class)- def assetProcessingService = container.getService(AssetProcessingService.class)def assetStorageService = container.getService(AssetStorageService.class)def rulesetStorageService = container.getService(RulesetStorageService.class)+ def simulatorProtocol = container.getService(SimulatorProtocol.class)RulesEngine apartment1Engine = nulland: "some rules"@@ -61,22 +60,23 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConwhen: "we have an events consumer for collecting test data"List<AssetEvent> insertedAssetEvents = []- apartment1Engine.assetEventsConsumer = { assetEvent->+ apartment1Engine.assetEventsConsumer = { assetEvent ->insertedAssetEvents << assetEvent}and: "several motion counter increments occur in the room, but not enough for presence detection"insertedAssetEvents = []setPseudoClocksToRealTime(container, apartment1Engine)- // Send 5 triggers each 3 minutes apart+ // The motion counter increments 5 times, 3 minutes apartfor (i in 1..5) {- // TODO Send event through sensor/simulatordef motionCountIncrement = new AttributeEvent(managerDemoSetup.apartment1LivingroomId, "motionCount", Values.create(i), getClockTimeOf(apartment1Engine))- assetProcessingService.sendAttributeEvent(motionCountIncrement)- new PollingConditions(timeout: 5, initialDelay: 0.2, delay: 0.2).eventually {- assert insertedAssetEvents.any() { it.matches(motionCountIncrement) }+ // Use the simulator to inject the update+ simulatorProtocol.putState(motionCountIncrement)+ // Wait until rules engine has the asset event+ new PollingConditions(timeout: 3).eventually {+ assert insertedAssetEvents.any() { it.matches(motionCountIncrement, SENSOR, true) }}advancePseudoClocks(3, MINUTES, container, apartment1Engine)}@@ -99,15 +99,17 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConwhen: "several motion counter increments occur in the room, fast enough for presence detection"insertedAssetEvents = []double expectedLastPresenceTimestamp = 0d- // Send 5 increments each 1 minute apart+ // The motion counter increments 5 times, 1 minute apartfor (i in 1..5) {def motionCountIncrement = new AttributeEvent(managerDemoSetup.apartment1LivingroomId, "motionCount", Values.create(i), getClockTimeOf(apartment1Engine))- expectedLastPresenceTimestamp = motionCountIncrement.timestamp- assetProcessingService.sendAttributeEvent(motionCountIncrement)- new PollingConditions(timeout: 5, initialDelay: 0.2, delay: 0.2).eventually {- assert insertedAssetEvents.any() { it.matches(motionCountIncrement) }+ simulatorProtocol.putState(motionCountIncrement)+ new PollingConditions(timeout: 3).eventually {+ assert insertedAssetEvents.any() {+ expectedLastPresenceTimestamp = it.valueTimestamp+ it.matches(motionCountIncrement, SENSOR, true)+ }}advancePseudoClocks(1, MINUTES, container, apartment1Engine)}@@ -122,15 +124,17 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConwhen: "time moves on and we keep incrementing the motion counter in short intervals "insertedAssetEvents = []- // Send 20 increments each 90 seconds apart+ // The motion counter increments 20 times, 90 seconds apartfor (i in 1..20) {def motionCountIncrement = new AttributeEvent(managerDemoSetup.apartment1LivingroomId, "motionCount", Values.create(i), getClockTimeOf(apartment1Engine))- expectedLastPresenceTimestamp = motionCountIncrement.timestamp- assetProcessingService.sendAttributeEvent(motionCountIncrement)- new PollingConditions(timeout: 5, initialDelay: 0.2, delay: 0.2).eventually {- assert insertedAssetEvents.any() { it.matches(motionCountIncrement) }+ simulatorProtocol.putState(motionCountIncrement)+ new PollingConditions(timeout: 3).eventually {+ assert insertedAssetEvents.any() {+ expectedLastPresenceTimestamp = it.valueTimestamp+ it.matches(motionCountIncrement, SENSOR, true)+ }}advancePseudoClocks(90, SECONDS, container, apartment1Engine)}@@ -143,7 +147,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConassert asset.getAttribute("lastPresenceDetected").get().getValueAsNumber().get() == expectedLastPresenceTimestamp}- when: "time moves on without the motion sensor being triggered"+ when: "time moves on without the motion counter incrementing"advancePseudoClocks(20, MINUTES, container, apartment1Engine)then: "presence should be gone but the last timestamp still available"@@ -207,7 +211,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConwhen: "we have an events consumer for collecting test data"List<AssetEvent> insertedAssetEvents = []- apartment2Engine.assetEventsConsumer = { assetEvent->+ apartment2Engine.assetEventsConsumer = { assetEvent ->insertedAssetEvents << assetEvent}@@ -221,7 +225,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConmanagerDemoSetup.apartment2BathroomId, "motionSensor", Values.create(true), getClockTimeOf(apartment2Engine))assetProcessingService.sendAttributeEvent(motionSensorTrigger)- if(i==1)+ if (i == 1)firstPresenceDetectedTimeStamp = motionSensorTrigger.timestampnew PollingConditions(timeout: 25, initialDelay: 0.2, delay: 0.2).eventually {assert insertedAssetEvents.any() { it.matches(motionSensorTrigger) }@@ -261,25 +265,25 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConmanagerDemoSetup.apartment2BathroomId, "motionSensor", Values.create(true), getClockTimeOf(apartment2Engine))def co2LevelIncrement = new AttributeEvent(- managerDemoSetup.apartment2LivingroomId, "co2Level", Values.create(400+i), getClockTimeOf(apartment2Engine)+ managerDemoSetup.apartment2LivingroomId, "co2Level", Values.create(400 + i), getClockTimeOf(apartment2Engine))expectedLastPresenceTimestampBathroom = motionSensorTriggerBathroom.timestampassetProcessingService.sendAttributeEvent(motionSensorTriggerBathroom)- if(i==1) {+ if (i == 1) {// Trigger motion sensorassetProcessingService.sendAttributeEvent(motionSensorTriggerLivingroom)assetProcessingService.sendAttributeEvent(co2LevelIncrement)expectedLastPresenceTimestampLivingroom = motionSensorTriggerLivingroom.timestamp- }else{+ } else {// Increase CO2 because person is not moving but breathingassetProcessingService.sendAttributeEvent(co2LevelIncrement)}new PollingConditions(timeout: 25, initialDelay: 0.2, delay: 0.2).eventually {assert insertedAssetEvents.any() { it.matches(motionSensorTriggerBathroom) }- if(i>1) {+ if (i > 1) {// TODO why the following test fails?// assert insertedAssetEvents.any() { it.matches(co2LevelIncrement) }- }else+ } elseassert insertedAssetEvents.any() { it.matches(motionSensorTriggerLivingroom) }}advancePseudoClocks(1, MINUTES, container, apartment2Engine)diff --git a/model/src/main/java/org/openremote/model/asset/AbstractAssetUpdate.java b/model/src/main/java/org/openremote/model/asset/AbstractAssetUpdate.javaindex 8cb3aa474..37e7be3d7 100644--- a/model/src/main/java/org/openremote/model/asset/AbstractAssetUpdate.java+++ b/model/src/main/java/org/openremote/model/asset/AbstractAssetUpdate.java@@ -264,10 +264,21 @@ public abstract class AbstractAssetUpdate {return attribute.getReferenceOrThrow();}+ /**+ * Compares entity identifier, attribute name, value, and timestamp.+ */public boolean matches(AttributeEvent event) {+ return matches(event, null, false);+ }++ /**+ * Compares entity identifier, attribute name, value, source, and optional timestamp.+ */+ public boolean matches(AttributeEvent event, AttributeEvent.Source source, boolean ignoreTimestamp) {return event.getAttributeRef().equals(getAttributeRef())&& Optional.ofNullable(getValue()).equals(event.getAttributeState().getCurrentValue())- && getValueTimestamp() == event.getTimestamp();+ && (ignoreTimestamp || getValueTimestamp() == event.getTimestamp())+ && (source == null || getSource() == source);}/**