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
Got presence pred rules through compiler. WIP
Michal Rutka8fdd2dab27d1
diff --git a/container/src/main/java/org/openremote/container/util/Util.java b/container/src/main/java/org/openremote/container/util/Util.javaindex db2c090ea..324d1ba5a 100644--- a/container/src/main/java/org/openremote/container/util/Util.java+++ b/container/src/main/java/org/openremote/container/util/Util.java@@ -70,6 +70,17 @@ public class Util {return (sdf.format(new Date(timestamp)));}+ /**+ * @param timestamp Epoch time+ * @return The timestamp formatted as 'EEE' or <code>null</code> if the timestamp is <= 0.+ */+ public static String formatDayOfWeek(long timestamp) {+ if (timestamp <= 0)+ return null;+ SimpleDateFormat sdf = new SimpleDateFormat("EEE");+ return (sdf.format(new Date(timestamp)));+ }+/*** @param o A timestamp string as 'HH:mm' or '-'.* @param minutes The minutes to increment/decrement from timestamp.diff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drlindex ec0f8a559..af8249e7d 100644--- a/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl@@ -2,10 +2,12 @@ package org.openremote.test.rules;import org.openremote.model.*;import org.openremote.model.asset.*-import org.openremote.model.value.Values;+import org.openremote.model.value.*;+import org.openremote.model.attribute.*;global java.util.logging.Logger LOG;global org.openremote.model.rules.Assets assets;+global org.openremote.container.util.Util util;/*New:@@ -66,28 +68,29 @@ Old:// Here emulate PIR from motionCount by Enervalisrule "person sense"when- AssetEvent(type == AssetType.ROOM, attributeName == "motionCount", oldValue.asNumber < value.asNumber)- AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", value.asNumber!=1)+ AssetEvent(type == AssetType.ROOM, attributeName == "motionCount", isValueGreaterThanOldValue() )+ $room: AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", getValueAsNumber() !=1)thenLOG.info("@"+(new java.util.Date(drools.getWorkingMemory().getSessionClock().getCurrentTime())).toString());- assets.dispatch( new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Json .create(1)) );+ assets.dispatch( new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Values.create(1) ) );end+// This rule models monostable PIR sensor behaviour, which goes to off state after 2s timeoutrule "no person sense"timer(int: 2s)when- AssetEvent(type == AssetType.ROOM, attributeName == "motionCount", oldValue.asNumber < value.asNumber)- AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber!=0)+ AssetEvent(type == AssetType.ROOM, attributeName == "motionCount", isValueGreaterThanOldValue())+ AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", getValueAsNumber()!=0)thenLOG.info("@"+(new java.util.Date(drools.getWorkingMemory().getSessionClock().getCurrentTime())).toString());- assets.dispatch( new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Json .create(0)) );+ assets.dispatch( new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Values.create(0)) );end// Fetch last time when presence was detected. This time is used for period window adjustment.rule "last move sense timestamp fetch"when- AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", value.asNumber==1, valueChanged)+ AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", getValueAsNumber()==1, isValueChanged() )thenlong currentTime = drools.getWorkingMemory().getSessionClock().getCurrentTime();LOG.info("Move sense on "+$id+" @"+(new java.util.Date(currentTime)).toString() );@@ -95,26 +98,26 @@ then// we want that this time stamp is persistent in memory because periods are quite long and it// is likely that a rule engine can restart in this time. Experience learns that this can// disturb the prediction time.- new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Json .create(currentTime))+ new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Values.create(currentTime)));end// Set the presence state which was previously used in the UI "Somebody is in the office"rule "Presence"when- AssetEvent(type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber==1)- AssetState(type == AssetType.RESIDENCE, attributeName=="currentScene", value=="Departure") // within expected attendance schedule- AssetState($id: id, type == AssetType.RESIDENCE, attributeName=="presenceDetectedUI", value.asString!="Yes")+ AssetEvent(type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", getValueAsNumber()==1)+ AssetState(type == AssetType.RESIDENCE, attributeName=="currentScene", getValueAsString()=="Departure") // within expected attendance schedule+ AssetState($id: id, type == AssetType.RESIDENCE, attributeName=="presenceDetectedUI", getValueAsString()!="Yes")then- assets.dispatch( new AttributeEvent($id, "presenceDetectedUI", Json .create("Yes")) );+ assets.dispatch( new AttributeEvent($id, "presenceDetectedUI", Values.create("Yes")) );endrule "Absence after 15 min"timer(int: 15m)when- AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber==0)+ AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", getValueAsNumber()==0)then- assets.dispatch( new AttributeEvent($id, "presenceDetectedUI", Json .create("No")) );+ assets.dispatch( new AttributeEvent($id, "presenceDetectedUI", Values.create("No")) );end// ##########################################################################################################@@ -130,34 +133,34 @@ endrule "reset homeActualSceneStartTime"when- AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, value.asString=="night")- AssetState($id, attributeName=="homeActualSceneStartTime", type == AssetType.RESIDENCE, value.asString != "-")+ AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, getValueAsString()=="night")+ AssetState($id: id, attributeName=="homeActualSceneStartTime", type == AssetType.RESIDENCE, getValueAsString() != "-")then- assets.dispatch( new AttributeEvent($id, "homeActualSceneStartTime", Json .create("-")) );+ assets.dispatch( new AttributeEvent($id, "homeActualSceneStartTime", Values.create("-")) );endrule "reset awayActualSceneStartTime"when- AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, value.asString=="home")- AssetState($id, attributeName=="awayActualSceneStartTime", type == AssetType.RESIDENCE, value.asString != "-")+ AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, getValueAsString()=="home")+ AssetState($id: id, attributeName=="awayActualSceneStartTime", type == AssetType.RESIDENCE, getValueAsString() != "-")then- assets.dispatch( new AttributeEvent($id, "awayActualSceneStartTime", Json .create("-")) );+ assets.dispatch( new AttributeEvent($id, "awayActualSceneStartTime", Values.create("-")) );endrule "reset eveningActualSceneStartTime"when- AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, value.asString=="away")- AssetState($id, attributeName=="eveningActualSceneStartTime", type == AssetType.RESIDENCE, value.asString != "-")+ AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, getValueAsString()=="away")+ AssetState($id: id, attributeName=="eveningActualSceneStartTime", type == AssetType.RESIDENCE, getValueAsString() != "-")then- assets.dispatch( new AttributeEvent($id, "eveningActualSceneStartTime", Json .create("-")) );+ assets.dispatch( new AttributeEvent($id, "eveningActualSceneStartTime", Values.create("-")) );endrule "reset nightActualSceneStartTime"when- AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, value.asString=="evening")- AssetState($id, attributeName=="nightActualSceneStartTime", type == AssetType.RESIDENCE, value.asString != "-")+ AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, getValueAsString()=="evening")+ AssetState($id: id, attributeName=="nightActualSceneStartTime", type == AssetType.RESIDENCE, getValueAsString() != "-")then- assets.dispatch( new AttributeEvent($id, "nightActualSceneStartTime", Json .create("-")) );+ assets.dispatch( new AttributeEvent($id, "nightActualSceneStartTime", Values.create("-")) );end// Removed rules:@@ -167,29 +170,29 @@ endrule "fetch the actual scene start time"when- $timestamp : util.formatTimestamp(drools.clock.currentTime)- AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, $scene: value.asString)- AssetState($id:id, $attributeName: attributeName==$scene."ActualSceneStartTime", value.asString == "-")+// $timestamp : util.formatTimestamp(drools.clock.currentTime)+ AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, $scene: getValueAsString())+//TODO AssetState($id:id, $attributeName: attributeName==$scene."ActualSceneStartTime", getValueAsString() == "-")// TODO the actual arrive state was detected by presence sensor. Now we have 2 scenes when presence is// detected and 2 when absence is detected- AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", value.asNumber==1)+ AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", getValueAsNumber()==1)then- assets.dispatch( new AttributeEvent($id, $attributeName, $timestamp));+ assets.dispatch( new AttributeEvent($id, "nightActualSceneStartTime", Values.create(drools.getWorkingMemory().getSessionClock().getCurrentTime()) ));endrule "fetch earlier actual home scene start time up to 30 min v02"when// Earliest home scene start time is 4AM- $timestamp : util.formatTimestamp(drools.clock.currentTime) >= util.parseTimestamp("04:00")- AssetState($id:id, $attributeName: attributeName=="homeActualSceneStartTime", value.asString == "-")- AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, value.asString == "night")- AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", value.asNumber==1)- $dayOfWeek : util.formatDate("DAY_OF_WEEK", drools.clock.currentTime)+// TODO $timestamp : util.formatTimestamp(drools.clock.currentTime) >= util.parseTimestamp("04:00")+ AssetState($id: id, $attributeName: attributeName=="homeActualSceneStartTime", getValueAsString() == "-")+ AssetState(attributeName=="currentScene", type == AssetType.RESIDENCE, getValueAsString() == "night")+ AssetEvent(type == AssetType.ROOM, attributeName=="presenceDetectedInteger", getValueAsNumber()==1)// Take the current value from the scene table- AssetState($ref: attributeRef, type == AssetType.RESIDENCE, attributeName matches "homeSceneStart" + $dayOfWeek,- $estimated_time: value.asNumberutil.parseTimestamp(util.shiftTime($estimated_time,-30)) <= util.parseTimestamp($timestamp))+ AssetState($ref: attributeRef, type == AssetType.RESIDENCE, attributeName matches ("homeSceneStart" + util.formatDayOfWeek(drools.getWorkingMemory().getSessionClock().getCurrentTime())),+ $estimated_time: getValueAsNumber() -30 <= util.parseTimestamp(drools.getWorkingMemory().getSessionClock().getCurrentTime()) )//,+// drools.getWorkingMemory().getSessionClock().getCurrentTime() >= util.parseTimestamp("04:00") )then- assets.dispatch( new AttributeEvent($id, "homeActualSceneStartTime", $timestamp));+ assets.dispatch( new AttributeEvent($id, "homeActualSceneStartTime", Values.create(drools.getWorkingMemory().getSessionClock().getCurrentTime()) ));end// Removed rule for switching scene after departure@@ -214,24 +217,23 @@ rule "adjust estimated times at next scene change"when// Question: isn't AssetType checking relevant here? attributeName should be unique here or not?// We are in the self learning mode- AssetState(attributeName == "selfLearning", type == AssetType.RESIDENCE, value.asBoolean)+ AssetState(attributeName == "selfLearning", type == AssetType.RESIDENCE, isValueTrue() )// We don't do this when on Vacations- AssetState(attributeName == "vacationDays", type == AssetType.RESIDENCE, value.asNumber() > 0 )+ AssetState(attributeName == "vacationDays", type == AssetType.RESIDENCE, isValueGreaterThan(0) )// Let's get in which scene are we now- AssetState(attributeName == "currentScene", type == AssetType.RESIDENCE, $scene: value.asString )+ AssetState(attributeName == "currentScene", type == AssetType.RESIDENCE, $scene: getValueAsString() )// For which day of week are we calculating this?- $dayOfWeek : util.formatDate("DAY_OF_WEEK", drools.clock.currentTime)// Take the current value from the scene table- AssetState($ref: attributeRef, type == AssetType.RESIDENCE, attributeName matches $scene + "SceneStart" + $dayOfWeek, $estimated_time: value.asNumber)+ AssetState($ref: id, type == AssetType.RESIDENCE, attributeName matches ($scene + "SceneStart" + util.formatDayOfWeek(drools.getWorkingMemory().getSessionClock().getCurrentTime())), $estimated_time: getValueAsNumber())// Now take the lastPresenceDetected (TODO here we will need probably a different logic dependent on scene)- AssetState(attributeName == "lastPresenceDetected", type == AssetType.ROOM, $actual_time: value.asNumber > 0)+ AssetState(attributeName == "lastPresenceDetected", type == AssetType.ROOM, $actual_time: getValueAsNumber(), isValueGreaterThan(0) )then// Take arbitrary coefficient which works goodDouble alpha = 2.0/(3+1); // 3 stays for 3 days exponential average// Calculate exponential moving average from the known formulalong new_estimated_time = (long) (alpha*util.parseTimestamp($actual_time) + (1-alpha)*util.parseTimestamp($estimated_time));// Update value in the table- assets.dispatch( new AttributeEvent($ref, Json.create(new_estimated_time)) );+ assets.dispatch( new AttributeEvent($ref, "newEstimatedTime", Values.create(new_estimated_time)) );end// ##########################################################################################################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 3016a652a..f86969ae6 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@@ -26,7 +26,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCondef "Presence detection with motion sensor"() {given: "the container environment is started"- def conditions = new PollingConditions(timeout: 10, delay: 1)+ def conditions = new PollingConditions(timeout: 100, delay: 1)def serverPort = findEphemeralPort()def container = startContainerWithPseudoClock(defaultConfig(serverPort), defaultServices())def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)@@ -227,4 +227,38 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerConcleanup: "the server should be stopped"stopContainer(container)}++ def "Presence prediction rules compilation"() {++ given: "the container environment is started"+ def conditions = new PollingConditions(timeout: 100, delay: 1)+ def serverPort = findEphemeralPort()+ def container = startContainerWithPseudoClock(defaultConfig(serverPort), defaultServices())+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)+ def rulesService = container.getService(RulesService.class)+ def assetStorageService = container.getService(AssetStorageService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)+ def simulatorProtocol = container.getService(SimulatorProtocol.class)+ RulesEngine apartment1Engine = null++ and: "some rules"+ Ruleset ruleset = new AssetRuleset(+ "Demo Apartment - Presence Detection with motion sensor",+ managerDemoSetup.apartment1Id,+ getClass().getResource("/demo/rules/DemoApartmentPresencePrediction.drl").text+ )+ rulesetStorageService.merge(ruleset)++ expect: "the rule engines to become available and be running"+ conditions.eventually {+ apartment1Engine = rulesService.assetEngines.get(managerDemoSetup.apartment1Id)+ assert apartment1Engine != null+ assert apartment1Engine.isRunning()+// assert apartment1Engine.knowledgeSession.factCount == DEMO_RULE_STATES_APARTMENT_1+ }++ cleanup: "the server should be stopped"+ stopContainer(container)+ }+}