Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

DemoApartmentAllLightsOff test uses AssetEvent now.

Michal Rutkacda594927a1b
diff --git a/agent/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy
index 03483e43a..2abf91bd2 100644
--- a/agent/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy
+++ b/agent/src/test/groovy/org/openremote/test/rules/SimpleRuleTest.groovy
@@ -6,6 +6,7 @@ import org.openremote.agent.AgentService
import org.openremote.agent.rules.RulesProvider
import org.openremote.test.ContainerTrait
import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
import java.util.stream.Stream
@@ -42,36 +43,38 @@ class SimpleRuleTest extends Specification implements ContainerTrait {
)
def serverPort = findEphemeralPort()
def container = startContainer(defaultConfig(serverPort), [agentService])
+ def conditions = new PollingConditions(timeout: 10)
- when: "we wait for initial state polling of sensor and rule execution"
- Thread.sleep(500);
-
- then: "the rules should immediately switch the sensor off again"
- agentService.getContext().queryValue(123) == "off"
+ expect: "the rules should immediately switch the sensor off again"
+ conditions.eventually {
+ assert agentService.getContext().queryValue(123) == "off"
+ }
and: "the deployment model should work"
- def deployment = agentService.getContext().getDeployment()
- deployment.getCommandDefinition(456).getCommandID() == 456
- deployment.getCommandDefinition(123123) == null
- deployment.getCommandDefinition("TestDevice", "TestCommand").getCommandID() == 456
- deployment.getCommandDefinition("TestDevice", "NoSuchThing") == null
- deployment.getCommandDefinition("NoSuchThing", "NoSuchThing") == null
- deployment.getCommandDefinition("", "") == null
- deployment.getCommandDefinition(null, "") == null
- deployment.getCommandDefinition("", null) == null
- deployment.getCommandDefinition(null, null) == null
- deployment.getCommandDefinition("TestCommand").getCommandID() == 456
- deployment.getCommandDefinition("NoSuchThing") == null
- deployment.getCommandDefinition("") == null
- deployment.getCommandDefinition(null) == null
- deployment.getDevices().length == 1
- deployment.getDevices()[0].getName() == "TestDevice"
- deployment.getDevices()[0].getDeviceID() == 111
- deployment.getDevice("TestDevice").getName() == "TestDevice"
- deployment.getDevice("TestDevice").getDeviceID() == 111
- deployment.getConfig().get("someString") == "BAR"
- deployment.getConfig().get("someBoolean") == "true"
- deployment.getConfig().get("someInteger") == "333"
+ conditions.eventually {
+ def deployment = agentService.getContext().getDeployment()
+ deployment.getCommandDefinition(456).getCommandID() == 456
+ deployment.getCommandDefinition(123123) == null
+ deployment.getCommandDefinition("TestDevice", "TestCommand").getCommandID() == 456
+ deployment.getCommandDefinition("TestDevice", "NoSuchThing") == null
+ deployment.getCommandDefinition("NoSuchThing", "NoSuchThing") == null
+ deployment.getCommandDefinition("", "") == null
+ deployment.getCommandDefinition(null, "") == null
+ deployment.getCommandDefinition("", null) == null
+ deployment.getCommandDefinition(null, null) == null
+ deployment.getCommandDefinition("TestCommand").getCommandID() == 456
+ deployment.getCommandDefinition("NoSuchThing") == null
+ deployment.getCommandDefinition("") == null
+ deployment.getCommandDefinition(null) == null
+ deployment.getDevices().length == 1
+ deployment.getDevices()[0].getName() == "TestDevice"
+ deployment.getDevices()[0].getDeviceID() == 111
+ deployment.getDevice("TestDevice").getName() == "TestDevice"
+ deployment.getDevice("TestDevice").getDeviceID() == 111
+ deployment.getConfig().get("someString") == "BAR"
+ deployment.getConfig().get("someBoolean") == "true"
+ deployment.getConfig().get("someInteger") == "333"
+ }
cleanup: "the server should be stopped"
stopContainer(container)
diff --git a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
index e94b14141..aead3d805 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
@@ -281,14 +281,16 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment1.setType(RESIDENCE);
AssetAttributes apartment1Attributes = new AssetAttributes();
apartment1Attributes.put(
- new AssetAttribute("allLightsOffSwitch", AttributeType.BOOLEAN, Json.create(false))
+ new AssetAttribute("allLightsOffSwitch", AttributeType.BOOLEAN, Json.create(true))
.setMeta(
new Meta().add(
new MetaItem(AssetMeta.LABEL, Json.create("All Lights Off Switch"))
).add(
new MetaItem(AssetMeta.DESCRIPTION, Json.create("When triggered, turns all lights in the apartment off"))
).add(
- new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
+ new MetaItem(AssetMeta.RULES_EVENT, Json.create(true))
+ ).add(
+ new MetaItem(AssetMeta.RULES_EVENT_EXPIRES, Json.create("10s"))
)
)
);
@@ -323,7 +325,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
new Meta().add(
new MetaItem(AssetMeta.LABEL, Json.create("Light Switch"))
).add(
- new MetaItem(AssetMeta.RULES_FACT, Json.create(false))
+ new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
)
),
new AssetAttribute("windowOpen", AttributeType.BOOLEAN, Json.create(false))
@@ -331,6 +333,14 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
new Meta().add(
new MetaItem(AssetMeta.LABEL, Json.create("Window Open"))
)
+ ),
+ new AssetAttribute("co2Level", AttributeType.DECIMAL, Json.create(450))
+ .setMeta(
+ new Meta().add(
+ new MetaItem(AssetMeta.LABEL, Json.create("CO2 Level"))
+ ).add(
+ new MetaItem(AssetMeta.RULES_FACT, Json.create(false))
+ )
)
);
apartment1Livingroom.setAttributes(apartment1LivingroomAttributes.getJsonObject());
diff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl
index 4eb9e6f4e..1ef695898 100644
--- a/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl
+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl
@@ -2,15 +2,16 @@ package org.openremote.test.rules;
import org.openremote.model.*;
import org.openremote.model.asset.*
+import org.openremote.model.rules.*
import elemental.json.Json;
global java.util.logging.Logger LOG;
global org.openremote.model.rules.Assets assets;
-rule "Switch room lights off when apartment ALL LIGHTS OFF switch is off"
+rule "Switch room lights off when apartment ALL LIGHTS OFF switch is pressed"
when
- // In an asset of type RESIDENCE the power switch is turned off
- $e: AssetUpdate(attributeName == "allLightsOffSwitch", type == AssetType.RESIDENCE, value.asBoolean() == false)
+ // In an asset of type RESIDENCE the power switch is pressed
+ $e: AssetEvent(attributeName == "allLightsOffSwitch", type == AssetType.RESIDENCE, !value.asBoolean() )
then
assets.query() // Execute a query to get "other" assets
.parent($e.getId()) // Where parent is the apartment in LHS
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/ApartmentActionsTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/ApartmentActionsTest.groovy
index 224f69f78..6a9865c3c 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/ApartmentActionsTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/ApartmentActionsTest.groovy
@@ -23,7 +23,11 @@ class ApartmentActionsTest extends Specification implements ManagerContainerTrai
def "All Lights Off"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 5, delay: 1)
+ def conditions = new PollingConditions(timeout: 10, delay: 1)
+
+ and: "a pseudo rules engine clock"
+ RulesDeployment.DefaultClockType = ClockTypeOption.get("pseudo")
+ PseudoClockScheduler sessionClock
and: "the container is started"
def serverPort = findEphemeralPort()
@@ -47,13 +51,19 @@ class ApartmentActionsTest extends Specification implements ManagerContainerTrai
conditions.eventually {
assert rulesService.facts.size() == 8
assert customerAEngine.facts.size() == 8
+ assert customerAEngine.knowledgeSession.factCount == 8
}
and: "the room lights in an apartment to be on"
def livingRoomAsset = assetStorageService.find(managerDemoSetup.apartment1LivingroomId, true)
assert new AssetAttributes(livingRoomAsset).get("lightSwitch").valueAsBoolean
- when: "the ALL LIGHTS OFF switch is set to off for an apartment"
+ when: "we have prepared a rules clock"
+ sessionClock = customerAEngine.sessionClock as PseudoClockScheduler
+ // Set the session clock to wall time, as event expiration offsets are based on wall time
+ sessionClock.advanceTime(System.currentTimeMillis(), MILLISECONDS)
+
+ and: "the ALL LIGHTS OFF switch is pressed for an apartment"
def apartment1AllLightsOffChange = new AttributeEvent(
managerDemoSetup.apartment1Id, "allLightsOffSwitch", Json.create(false)
)
@@ -61,11 +71,21 @@ class ApartmentActionsTest extends Specification implements ManagerContainerTrai
then: "the room lights in the apartment should be off"
conditions.eventually {
+ assert customerAEngine.knowledgeSession.factCount == 9
livingRoomAsset = assetStorageService.find(managerDemoSetup.apartment1LivingroomId, true)
assert !new AssetAttributes(livingRoomAsset).get("lightSwitch").valueAsBoolean
}
+ when: "time advanced"
+ sessionClock.advanceTime(15, SECONDS)
+
+ then: "event expires"
+ conditions.eventually {
+ assert customerAEngine.knowledgeSession.factCount == 8
+ }
+
cleanup: "the server should be stopped"
+ RulesDeployment.DefaultClockType = null
stopContainer(container)
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
index 61d7af102..9ec950212 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
@@ -118,10 +118,10 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
when: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to false"
resetRuleExecutionLoggers()
- def apartment1LivingRoomLightSwitchChange = new AttributeEvent(
- managerDemoSetup.apartment1LivingroomId, "lightSwitch", Json.create(true)
+ def apartment1LivingRoomCO2Change = new AttributeEvent(
+ managerDemoSetup.apartment1LivingroomId, "co2Level", Json.create(500)
)
- assetProcessingService.updateAttributeValue(apartment1LivingRoomLightSwitchChange)
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomCO2Change)
then: "no rule engines should have fired after a few seconds"
new PollingConditions(initialDelay: 3).eventually assertNoRulesFired