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
Implemented vacation mode rules and test
Christian Bauerc575431f62dc
diff --git a/manager/server/src/main/java/org/openremote/manager/server/setup/SetupService.java b/manager/server/src/main/java/org/openremote/manager/server/setup/SetupService.javaindex 35b0f9690..1306477eb 100644--- a/manager/server/src/main/java/org/openremote/manager/server/setup/SetupService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/setup/SetupService.java@@ -21,6 +21,7 @@ package org.openremote.manager.server.setup;import org.openremote.container.Container;import org.openremote.container.ContainerService;+import org.openremote.container.timer.TimerService;import org.openremote.manager.server.setup.builtin.BuiltinSetupTasks;import java.util.ArrayList;@@ -88,6 +89,12 @@ public class SetupService implements ContainerService {} catch (Exception ex) {throw new RuntimeException("Error setting up application", ex);}++ // When setup is complete, initialize the clock again - effectively set the PSEUDO clock to+ // wall clock time. If the PSEUDO clock is enabled in tests, we must advance time after setup+ // imports asset data. The protocols which are started after setup can trigger asset attribute+ // events, and event source time must be later than asset state time.+ container.getService(TimerService.class).getClock().init();}@Overridediff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentVacationMode.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentVacationMode.drlindex d863f1441..acd4b531e 100644--- a/manager/server/src/main/resources/demo/rules/DemoApartmentVacationMode.drl+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentVacationMode.drl@@ -3,20 +3,42 @@ package org.openremote.test.rules;import org.openremote.model.*;import org.openremote.model.asset.*import org.openremote.model.value.*-import org.openremote.model.attribute.*;+import org.openremote.model.attribute.*+import java.time.DayOfWeek+import org.drools.core.util.Droolsglobal java.util.logging.Logger LOG;global org.openremote.model.rules.Assets assets;-rule "When vacation timestamp was set, execute AWAY scene"+rule "When vacation mode is enabled, execute AWAY scene and disable scene timers"when- $residence : AssetState(type == AssetType.RESIDENCE, attributeName == "vacationUntil", valueAsNumber > 0, oldValue == null)+ $residence : AssetState(type == AssetType.RESIDENCE, attributeName == "vacationUntil", value != null, oldValue == null)thenassets.dispatch(- new AttributeEvent(- $residence.getId(),- "awayScene",- AttributeExecuteStatus.REQUEST_START.asValue()- )+ new AttributeEvent($residence.getId(), "awayScene", AttributeExecuteStatus.REQUEST_START.asValue()),+ new AttributeEvent($residence.getId(), "disableSceneTimer", AttributeExecuteStatus.REQUEST_START.asValue()));-end\ No newline at end of file+end++rule "When vacation mode is disabled, enable scene timers"+when+ $residence : AssetState(type == AssetType.RESIDENCE, attributeName == "vacationUntil", value == null, oldValue != null)+then+ assets.dispatch(+ new AttributeEvent($residence.getId(), "enableSceneTimer", AttributeExecuteStatus.REQUEST_START.asValue())+ );+end++rule "When vacation mode is on, check periodically if vacation mode should be disabled"+ // Run every hour+ timer (cron: 0 0 0/1 1/1 * ? *)+when+ $residence : AssetState(type == AssetType.RESIDENCE, attributeName == "vacationUntil", $vacationUntil: valueAsNumber, valueAsNumber > 0)+then+ if ($vacationUntil <= drools.getWorkingMemory().getSessionClock().getCurrentTime()) {+ assets.dispatch(+ new AttributeEvent($residence.getId(), "vacationUntil", null)+ );+ }+end+diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentVacationModeTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentVacationModeTest.groovyindex 8d9c57e0e..487b117cc 100644--- a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentVacationModeTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentVacationModeTest.groovy@@ -16,12 +16,16 @@ import org.openremote.test.ManagerContainerTraitimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions+import java.time.DayOfWeek++import static java.util.concurrent.TimeUnit.DAYS+import static java.util.concurrent.TimeUnit.HOURSimport static org.openremote.manager.server.setup.builtin.BuiltinSetupTasks.SETUP_IMPORT_DEMO_SCENESimport static org.openremote.manager.server.setup.builtin.ManagerDemoSetup.DEMO_RULE_STATES_APARTMENT_1_WITH_SCENESclass ApartmentVacationModeTest extends Specification implements ManagerContainerTrait {- def "Decrement vacation days at midnight"() {+ def "Start and end vacation mode"() {given: "the container environment is started"def conditions = new PollingConditions(timeout: 15, delay: 1)@@ -49,56 +53,76 @@ class ApartmentVacationModeTest extends Specification implements ManagerContaineassert apartment1Engine.isRunning()assert apartment1Engine.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1_WITH_SCENESassert apartment1Engine.knowledgeSession.factCount == DEMO_RULE_STATES_APARTMENT_1_WITH_SCENES+ setPseudoClocksToRealTime(container, apartment1Engine)+ }++ and: "scene timers should be enabled"+ conditions.eventually {+ def asset = assetStorageService.find(managerDemoSetup.apartment1Id, true)+ assert asset.getAttribute("sceneTimerEnabled").orElse(null).getValueAsBoolean().orElse(null)+ DayOfWeek.values().each {+ assert asset.getAttribute("homeSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert asset.getAttribute("awaySceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert asset.getAttribute("eveningSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert asset.getAttribute("nightSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ }}when: "the vacation days are set to 5"setPseudoClocksToRealTime(container, apartment1Engine)- long fiveDaysInFuture = getClockTimeOf(container) + (5 * 24 * 60 * 60 * 1000)+ double fiveDaysInFuture = getClockTimeOf(container) + (5 * 24 * 60 * 60 * 1000)assetProcessingService.sendAttributeEvent(new AttributeEvent(managerDemoSetup.apartment1Id, "vacationUntil", Values.create(fiveDaysInFuture)))- then: "the AWAY scene should be executed"+ then: "the AWAY scene should be executed and scene timers disabled"conditions.eventually {def asset = assetStorageService.find(managerDemoSetup.apartment1Id, true)- def executionStatus = AttributeExecuteStatus.fromString(asset.getAttribute("awayScene").get().getValueAsString().orElse(null))- assert executionStatus.orElse(null) == AttributeExecuteStatus.COMPLETED+ def executionStatus = AttributeExecuteStatus.fromString(+ asset.getAttribute("awayScene").orElse(null).getValueAsString().orElse(null)+ ).orElse(null)+ assert executionStatus == AttributeExecuteStatus.COMPLETED+ assert !asset.getAttribute("sceneTimerEnabled").orElse(null).getValueAsBoolean().orElse(null)+ DayOfWeek.values().each {+ assert !asset.getAttribute("homeSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert !asset.getAttribute("awaySceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert !asset.getAttribute("eveningSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert !asset.getAttribute("nightSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ }}- /* TODO fix this- when: "time advanced to the next day, which should trigger the cron rule"+ when: "time advanced to the next day"advancePseudoClocks(24, HOURS, container, apartment1Engine)- then: "the vacation days should be decremented"+ then: "vacation mode is still on"conditions.eventually {def asset = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert asset.getAttribute("vacationDays").get().getValueAsInteger().isPresent()- assert asset.getAttribute("vacationDays").get().getValueAsInteger().get() == 4+ assert asset.getAttribute("vacationUntil").orElse(null).getValueAsNumber().orElse(null) == fiveDaysInFuture+ assert !asset.getAttribute("sceneTimerEnabled").orElse(null).getValueAsBoolean().orElse(null)+ DayOfWeek.values().each {+ assert !asset.getAttribute("homeSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert !asset.getAttribute("awaySceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert !asset.getAttribute("eveningSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert !asset.getAttribute("nightSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ }}- when: "time advanced again (to test that the rule only fires once per day)"- advancePseudoClocks(10, SECONDS, container, apartment1Engine)+ when: "time advanced a few days"+ advancePseudoClocks(5, DAYS, container, apartment1Engine)- then: "the vacation days should NOT be decremented"- new PollingConditions(initialDelay: 2).eventually {+ then: "vacation mode is off and scene timers are enabled"+ conditions.eventually {def asset = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert asset.getAttribute("vacationDays").get().getValueAsInteger().isPresent()- assert asset.getAttribute("vacationDays").get().getValueAsInteger().get() == 4- }-- expect: "the remaining vacation days to be decremented with each passing day"- int remainingDays = 4- while (remainingDays > 0) {-- remainingDays---- advancePseudoClocks(1, DAYS, container, apartment1Engine)- conditions.eventually {- def asset = assetStorageService.find(managerDemoSetup.apartment1Id, true)- assert asset.getAttribute("vacationDays").get().getValueAsInteger().get() == remainingDays+ assert !asset.getAttribute("vacationUntil").orElse(null).getValue().isPresent()+ assert asset.getAttribute("sceneTimerEnabled").orElse(null).getValueAsBoolean().orElse(null)+ DayOfWeek.values().each {+ assert asset.getAttribute("homeSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert asset.getAttribute("awaySceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert asset.getAttribute("eveningSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)+ assert asset.getAttribute("nightSceneEnabled" + it.name()).orElse(null).getValueAsBoolean().orElse(null)}}- */+cleanup: "the server should be stopped"stopContainer(container)}