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 demo residence smart notify switch rules and test
Christian Bauerc3625d34073f
diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.javaindex b191baead..6be630bb5 100644--- a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java@@ -124,14 +124,6 @@ public abstract class AbstractManagerSetup implements Setup {new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),new MetaItem(READ_ONLY, Values.create(true)),new MetaItem(RULE_STATE, Values.create(true))- ),- new AssetAttribute("smartStart", AttributeType.BOOLEAN, Values.create(false))- .setMeta(- new MetaItem(LABEL, Values.create("Smart Start enabled")),- new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),- new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),- new MetaItem(RULE_STATE, Values.create(true)),- new MetaItem(SHOW_ON_DASHBOARD, Values.create(true))));return apartment;@@ -245,53 +237,58 @@ public abstract class AbstractManagerSetup implements Setup {}protected void addDemoApartmentSmartSwitch(ServerAsset room,- String switchAttributePrefix,- String switchLabelPrefix,+ String switchName,boolean shouldBeLinked,- // Integer represents attribute number:- // 0 = On/Off- // 1 = Start time- // 2 = Stop time- // 3 = Smart enabled+ // Integer represents attribute:+ // 0 = Mode+ // 1 = Time+ // 2 = StartTime+ // 3 = StopTime+ // 4 = EnabledFunction<Integer, MetaItem[]> agentLinker) {room.addAttributes(- // On/Off- // TODO What is this? The only usage is in the frontend app, and only the LABEL meta item is displayed!- new AssetAttribute(switchAttributePrefix + "OnOff", NUMBER)+ // Mode+ new AssetAttribute("smartSwitchMode" + switchName, STRING).setMeta(- new MetaItem(LABEL, Values.create(switchLabelPrefix + " on/off status")),+ new MetaItem(LABEL, Values.create("Smart Switch mode " + switchName)),+ new MetaItem(DESCRIPTION, Values.create("NOW_ON (default when empty) or ON_AT or READY_AT")),new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),+ new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),new MetaItem(RULE_STATE, Values.create(true)),- new MetaItem(RULE_EVENT, Values.create(true))+ new MetaItem(RULE_EVENT, Values.create(true)),+ new MetaItem(RULE_EVENT_EXPIRES, Values.create("48h"))).addMeta(shouldBeLinked ? agentLinker.apply(0) : null),- // Start time- new AssetAttribute(switchAttributePrefix + "StartTime", TIMESTAMP_SECONDS)+ // Time+ new AssetAttribute("smartSwitchBeginEnd" + switchName, TIMESTAMP_MILLIS).setMeta(- new MetaItem(LABEL, Values.create(switchLabelPrefix + " earliest start time")),- new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),+ new MetaItem(LABEL, Values.create("Smart Switch begin/end cycle " + switchName)),+ new MetaItem(DESCRIPTION, Values.create("User-provided begin/end time of appliance cycle")),new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),- new MetaItem(RULE_STATE, Values.create(true)),- new MetaItem(RULE_EVENT, Values.create(true))+ new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),+ new MetaItem(RULE_STATE, Values.create(true))).addMeta(shouldBeLinked ? agentLinker.apply(1) : null),- // Stop time- new AssetAttribute(switchAttributePrefix + "StopTime", TIMESTAMP_SECONDS)+ // StartTime+ new AssetAttribute("smartSwitchStartTime" + switchName, TIMESTAMP_SECONDS).setMeta(- new MetaItem(LABEL, Values.create(switchLabelPrefix + " latest stop time")),- new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),- new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),- new MetaItem(RULE_STATE, Values.create(true)),- new MetaItem(RULE_EVENT, Values.create(true))+ new MetaItem(LABEL, Values.create("Smart Switch actuator earliest start time " + switchName)),+ new MetaItem(DESCRIPTION, Values.create("Earliest computed start time sent to actuator")),+ new MetaItem(RULE_STATE, Values.create(true))).addMeta(shouldBeLinked ? agentLinker.apply(2) : null),- // Smart enabled- new AssetAttribute(switchAttributePrefix + "SmartStart", NUMBER)+ // StopTime+ new AssetAttribute("smartSwitchStopTime" + switchName, TIMESTAMP_SECONDS).setMeta(- new MetaItem(LABEL, Values.create(switchLabelPrefix + " Smart Start enabled")),- new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),- new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),- new MetaItem(RULE_STATE, Values.create(true)),- new MetaItem(RULE_EVENT, Values.create(true))- ).addMeta(shouldBeLinked ? agentLinker.apply(3) : null)+ new MetaItem(LABEL, Values.create("Smart Switch actuator latest stop time " + switchName)),+ new MetaItem(DESCRIPTION, Values.create("Latest computed stop time sent to actuator")),+ new MetaItem(RULE_STATE, Values.create(true))+ ).addMeta(shouldBeLinked ? agentLinker.apply(3) : null),+ // Enabled+ new AssetAttribute("smartSwitchEnabled" + switchName, NUMBER)+ .setMeta(+ new MetaItem(LABEL, Values.create("Smart Switch actuator enabled " + switchName)),+ new MetaItem(DESCRIPTION, Values.create("1 if actuator only provides power at ideal time between start/stop")),+ new MetaItem(RULE_STATE, Values.create(true))+ ).addMeta(shouldBeLinked ? agentLinker.apply(4) : null));}diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.javaindex 917f49df5..03dbe6703 100644--- a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java@@ -50,7 +50,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {final protected boolean importDemoScenes;// Update these numbers whenever you change a RULE_STATE flag in test data- public static final int DEMO_RULE_STATES_APARTMENT_1 = 37;+ public static final int DEMO_RULE_STATES_APARTMENT_1 = 39;public static final int DEMO_RULE_STATES_APARTMENT_2 = 11;public static final int DEMO_RULE_STATES_APARTMENT_3 = 0;public static final int DEMO_RULE_STATES_SMART_HOME = DEMO_RULE_STATES_APARTMENT_1 + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;@@ -386,81 +386,29 @@ public class ManagerDemoSetup extends AbstractManagerSetup {new MetaItem(AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))});- addDemoApartmentSmartSwitch(apartment1Kitchen, "smartSwitchA", "Smart switch A", true, attributeIndex -> {- switch (attributeIndex) {- case 0:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 1:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 2:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 3:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- }- return null;- });- addDemoApartmentSmartSwitch(apartment1Kitchen, "smartSwitchB", "Smart switch B", true, attributeIndex -> {- switch (attributeIndex) {- case 0:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 1:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 2:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 3:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- }- return null;- });- addDemoApartmentSmartSwitch(apartment1Kitchen, "smartSwitchC", "Smart switch C", true, attributeIndex -> {- switch (attributeIndex) {- case 0:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 1:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 2:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- case 3:- return new MetaItem[]{- new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),- new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))- };- }- return null;- });++ for (String switchName : new String[]{"A", "B", "C"}) {+ addDemoApartmentSmartSwitch(apartment1Kitchen, switchName, true, attributeIndex -> {+ switch (attributeIndex) {+ case 2:+ return new MetaItem[]{+ new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),+ new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))+ };+ case 3:+ return new MetaItem[]{+ new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),+ new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))+ };+ case 4:+ return new MetaItem[]{+ new MetaItem(AssetMeta.AGENT_LINK, new AttributeRef(apartment1ServiceAgentId, "apartmentSimulator").toArrayValue()),+ new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))+ };+ }+ return null;+ });+ }apartment1Kitchen = assetStorageService.merge(apartment1Kitchen);apartment1KitchenId = apartment1Kitchen.getId();diff --git a/manager/src/main/resources/demo/rules/DemoResidenceSmartSwitch.groovy b/manager/src/main/resources/demo/rules/DemoResidenceSmartSwitch.groovynew file mode 100644index 000000000..9e086fec1--- /dev/null+++ b/manager/src/main/resources/demo/rules/DemoResidenceSmartSwitch.groovy@@ -0,0 +1,273 @@+package demo.rules++import org.openremote.manager.rules.RulesBuilder+import org.openremote.manager.rules.RulesFacts+import org.openremote.model.asset.AssetQuery+import org.openremote.model.rules.AssetState+import org.openremote.model.value.Values++import java.time.Instant+import java.time.LocalDateTime+import java.time.ZoneId+import java.util.logging.Logger+import java.util.stream.Stream++import static org.openremote.model.asset.AssetType.ROOM+import static org.openremote.model.asset.BaseAssetQuery.Match.BEGIN++Logger LOG = binding.LOG+RulesBuilder rules = binding.rules++final CYCLE_TIME_SECONDS = 2.5 * 60 * 60++enum SmartSwitchAttribute {+ Mode, BeginEnd, StartTime, StopTime, Enabled+}++enum SmartSwitchMode {+ NOW_ON, ON_AT, READY_AT++ static boolean matches(AssetState smartSwitch, SmartSwitchMode mode) {+ smartSwitch.valueAsString.map({ it == mode.name() }).orElse(false)+ }+}++Closure<Stream<AssetState>> smartSwitchAttributesMatch = { RulesFacts facts, SmartSwitchAttribute attribute ->+ facts.matchAssetState(+ new AssetQuery().type(ROOM).attributeName(BEGIN, "smartSwitch" + attribute.name())+ )+}++Closure<Character> getSmartSwitchName = { AssetState attribute ->+ attribute.attributeName.charAt(attribute.attributeName.length() - 1)+}++Closure<Optional<AssetState>> smartSwitchAttributeMatch = { RulesFacts facts, AssetState smartSwitch, SmartSwitchAttribute attribute ->+ facts.matchFirstAssetState(+ new AssetQuery().type(ROOM).attributeName("smartSwitch" + attribute.name() + getSmartSwitchName(smartSwitch))+ )+}++rules.add()+ .name("Clear start/stop time and disable actuator when begin/end of cycle is empty")+ .when(+ { facts ->+ smartSwitchAttributesMatch(+ facts, SmartSwitchAttribute.BeginEnd+ ).filter({ beginEnd ->+ !beginEnd.value.isPresent()+ }).filter({ emptyBeginEnd ->+ // Any of start time, stop time, or enabled is greater than zero+ def startTime = smartSwitchAttributeMatch(facts, emptyBeginEnd, SmartSwitchAttribute.StartTime)+ def stopTime = smartSwitchAttributeMatch(facts, emptyBeginEnd, SmartSwitchAttribute.StopTime)+ def enabled = smartSwitchAttributeMatch(facts, emptyBeginEnd, SmartSwitchAttribute.Enabled)+ (startTime.isPresent() && startTime.get().isValueGreaterThan(0)) ||+ (stopTime.isPresent() && stopTime.get().isValueGreaterThan(0)) ||+ (enabled.isPresent() && enabled.get().isValueGreaterThan(0))+ }).findFirst().map({ emptyBeginEnd ->+ facts.bind("beginEnd", emptyBeginEnd)+ true+ }).orElse(false)+ })+ .then(+ { facts ->+ AssetState beginEnd = facts.bound("beginEnd")+ LOG.info("Empty smart switch begin/end, clearing start/stop time and disabling actuator: " + beginEnd)+ String smartSwitchName = getSmartSwitchName(beginEnd)+ facts.updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.StartTime + smartSwitchName)+ .updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.StopTime + smartSwitchName)+ .updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.Enabled + smartSwitchName)+ })++rules.add()+ .name("Clear begin/end of cycle when mode is NOW_ON")+ .when(+ { facts ->+ smartSwitchAttributesMatch(+ facts, SmartSwitchAttribute.Mode+ ).filter({ mode ->+ SmartSwitchMode.matches(mode, SmartSwitchMode.NOW_ON)+ }).filter { modeNowOn ->+ // Begin/end time of cycle is greater than zero+ smartSwitchAttributeMatch(facts, modeNowOn, SmartSwitchAttribute.BeginEnd)+ .flatMap({ it.valueAsNumber })+ .map({ it > 0 })+ .orElse(false)+ }.findFirst().map({ modeNowOn ->+ facts.bind("mode", modeNowOn)+ true+ }).orElse(false)+ })+ .then(+ { facts ->+ AssetState mode = facts.bound("mode")+ LOG.info("Smart switch mode is NOW_ON, clearing begin/end of cycle: " + mode)+ String smartSwitchName = getSmartSwitchName(mode)+ facts.updateAssetState(mode.id, "smartSwitch" + SmartSwitchAttribute.BeginEnd + smartSwitchName)+ })++rules.add()+ .name("Enable actuator when mode is ON_AT and begin/end of cycle is now or in future and different than actuator start time")+ .when(+ { facts ->+ smartSwitchAttributesMatch(+ facts, SmartSwitchAttribute.Mode+ ).filter({ mode ->+ SmartSwitchMode.matches(mode, SmartSwitchMode.ON_AT)+ }).map({ modeOnAt ->+ smartSwitchAttributeMatch(facts, modeOnAt, SmartSwitchAttribute.BeginEnd)+ }).filter({ beginEnd ->+ // User-provided begin/end time of cycle+ beginEnd.flatMap({ it.valueAsNumber }).filter({+ // is now or in future+ it >= facts.clock.timestamp+ }).map({ expectedStartTime ->+ // and actuator start time attribute+ def startTime = beginEnd.flatMap({+ smartSwitchAttributeMatch(facts, it, SmartSwitchAttribute.StartTime)+ })+ // is present+ startTime.isPresent() &&+ // the value is not the same as the expected start time+ startTime.flatMap({ it.valueAsNumber }).map({+ (it * 1000) != expectedStartTime+ }).orElse(true) // or actuator start time is empty+ }).orElse(false)+ }).findFirst().map({ beginEnd ->+ facts.bind("beginEnd", beginEnd.get())+ true+ }).orElse(false)+ })+ .then(+ { facts ->+ AssetState beginEnd = facts.bound("beginEnd")+ // Start time is user-provided begin/end (in seconds)+ def startSeconds = beginEnd.valueAsNumber.map({ it / 1000 }).orElse(0)+ // Stop time is start time plus (CYCLE_TIME_SECONDS + 1m)+ def stopSeconds = startSeconds + (CYCLE_TIME_SECONDS + 60)+ LOG.info("Smart switch mode is ON_AT, enabling actuator with start/stop times " ++ LocalDateTime.ofInstant(Instant.ofEpochMilli((long) startSeconds * 1000), ZoneId.systemDefault()) ++ "/" ++ LocalDateTime.ofInstant(Instant.ofEpochMilli((long) stopSeconds * 1000), ZoneId.systemDefault()) ++ ": " + beginEnd)+ String smartSwitchName = getSmartSwitchName(beginEnd)+ facts.updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.StartTime + smartSwitchName, startSeconds)+ .updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.StopTime + smartSwitchName, stopSeconds)+ .updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.Enabled + smartSwitchName, Values.create(1))+ })++rules.add()+ .name("Set future end time of cycle when mode is READY_AT and cycle can't finish until end time")+ .when(+ { facts ->+ smartSwitchAttributesMatch(+ facts, SmartSwitchAttribute.Mode+ ).filter({ mode ->+ SmartSwitchMode.matches(mode, SmartSwitchMode.READY_AT)+ }).map({ modeReadyAt ->+ smartSwitchAttributeMatch(facts, modeReadyAt, SmartSwitchAttribute.BeginEnd)+ }).filter({ beginEnd ->+ // Computed stop time (desired end time of cycle minus duration) is in the past+ beginEnd.flatMap({ it.valueAsNumber })+ .map({ it - ((CYCLE_TIME_SECONDS + 60) * 1000) })+ .map({ it < facts.clock.timestamp })+ .orElse(false)+ }).findFirst().map({ beginEnd ->+ facts.bind("beginEnd", beginEnd.get())+ true+ }).orElse(false)+ })+ .then(+ { facts ->+ AssetState beginEnd = facts.bound("beginEnd")+ def newBeginEnd = facts.clock.timestamp + ((CYCLE_TIME_SECONDS + 60) * 1000)+ LOG.info("Smart switch mode is READY_AT, increasing insufficient begin/end time to " ++ LocalDateTime.ofInstant(Instant.ofEpochMilli((long) newBeginEnd), ZoneId.systemDefault()) ++ ": " + beginEnd)+ String smartSwitchName = getSmartSwitchName(beginEnd)+ facts.updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.BeginEnd + smartSwitchName, newBeginEnd)+ })++rules.add()+ .name("Enable actuator when mode is READY_AT and cycle will complete in future and different than actuator stop time")+ .when(+ { facts ->+ smartSwitchAttributesMatch(+ facts, SmartSwitchAttribute.Mode+ ).filter({ mode ->+ SmartSwitchMode.matches(mode, SmartSwitchMode.READY_AT)+ }).map({ modeReadyAt ->+ smartSwitchAttributeMatch(facts, modeReadyAt, SmartSwitchAttribute.BeginEnd)+ }).filter({ beginEnd ->+ // User-provided begin/end time of cycle+ beginEnd.flatMap({+ it.valueAsNumber+ }).filter({+ // is now or in future+ it >= facts.clock.timestamp+ }).map({ expectedStopTime ->+ // and actuator stop time attribute+ def stopTime = beginEnd.flatMap({+ smartSwitchAttributeMatch(facts, it, SmartSwitchAttribute.StopTime)+ })+ // is present+ stopTime.isPresent() &&+ // the value is not the same as the expected stop time+ stopTime.flatMap({ it.valueAsNumber }).map({+ (it * 1000) != expectedStopTime+ }).orElse(true) // or actuator stop time is empty+ }).orElse(false)+ }).findFirst().map({ beginEnd ->+ facts.bind("beginEnd", beginEnd.get())+ true+ }).orElse(false)+ })+ .then(+ { facts ->+ AssetState beginEnd = facts.bound("beginEnd")+ // Start time is current time (in seconds)+ def startSeconds = (double) facts.clock.timestamp / 1000+ // Stop time is user-provided smart switch time (in seconds)+ def stopSeconds = beginEnd.valueAsNumber.map({ it / 1000 }).orElse(0)+ LOG.info("Smart switch mode is READY_AT, enabling actuator with start/stop times " ++ LocalDateTime.ofInstant(Instant.ofEpochMilli((long) startSeconds * 1000), ZoneId.systemDefault()) + "/" ++ LocalDateTime.ofInstant(Instant.ofEpochMilli((long) stopSeconds * 1000), ZoneId.systemDefault()) ++ ": " ++ beginEnd)+ String smartSwitchName = getSmartSwitchName(beginEnd)+ facts.updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.StartTime + smartSwitchName, startSeconds)+ .updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.StopTime + smartSwitchName, stopSeconds)+ .updateAssetState(beginEnd.id, "smartSwitch" + SmartSwitchAttribute.Enabled + smartSwitchName, Values.create(1))+ })++rules.add()+ .name("Set mode NOW_ON when mode is ON_AT or READY_AT and actuator stop time is in the past")+ .when(+ { facts ->+ smartSwitchAttributesMatch(+ facts, SmartSwitchAttribute.Mode+ ).filter({ mode ->+ SmartSwitchMode.matches(mode, SmartSwitchMode.ON_AT) || SmartSwitchMode.matches(mode, SmartSwitchMode.READY_AT)+ }).map({ modeOnAtOrReadyAt ->+ smartSwitchAttributeMatch(facts, modeOnAtOrReadyAt, SmartSwitchAttribute.StopTime)+ }).filter({ stopTime ->+ // Actuator stop time attribute is present and value (in seconds) is in the past+ stopTime.flatMap({ it.valueAsNumber }).map({+ (it * 1000) < facts.clock.timestamp+ }).orElse(false)+ }).findFirst().map({ stopTime ->+ facts.bind("stopTime", stopTime.get())+ true+ }).orElse(false)+ })+ .then(+ { facts ->+ AssetState stopTime = facts.bound("stopTime")+ LOG.info("Smart switch actuator stop time is in the past, setting mode NOW_ON: " + stopTime)+ String smartSwitchName = getSmartSwitchName(stopTime)+ facts.updateAssetState(+ stopTime.id,+ "smartSwitch" + SmartSwitchAttribute.Mode + smartSwitchName,+ Values.create(SmartSwitchMode.NOW_ON.name())+ )+ })diff --git a/model/src/main/java/org/openremote/model/asset/BaseAssetQuery.java b/model/src/main/java/org/openremote/model/asset/BaseAssetQuery.javaindex bf8f92322..2d5cd3cce 100644--- a/model/src/main/java/org/openremote/model/asset/BaseAssetQuery.java+++ b/model/src/main/java/org/openremote/model/asset/BaseAssetQuery.java@@ -827,6 +827,10 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {return attributes(new AttributePredicate(attributeName));}+ public CHILD attributeName(Match match, String attributeName) {+ return attributes(new AttributePredicate(new StringPredicate(match, attributeName)));+ }+/*** Match non-empty attribute value.*/diff --git a/model/src/main/java/org/openremote/model/rules/AssetQueryPredicate.java b/model/src/main/java/org/openremote/model/rules/AssetQueryPredicate.javaindex 2ac43a119..ad573292e 100644--- a/model/src/main/java/org/openremote/model/rules/AssetQueryPredicate.java+++ b/model/src/main/java/org/openremote/model/rules/AssetQueryPredicate.java@@ -88,18 +88,18 @@ public class AssetQueryPredicate implements Predicate<AssetState> {if (predicate.value == null)return false;- String have = predicate.caseSensitive ? predicate.value : predicate.value.toUpperCase(Locale.ROOT);- String given = predicate.caseSensitive ? string : string.toUpperCase(Locale.ROOT);+ String shouldMatch = predicate.caseSensitive ? predicate.value : predicate.value.toUpperCase(Locale.ROOT);+ String have = predicate.caseSensitive ? string : string.toUpperCase(Locale.ROOT);switch (predicate.match) {case BEGIN:- return have.startsWith(given);+ return have.startsWith(shouldMatch);case END:- return have.endsWith(given);+ return have.endsWith(shouldMatch);case CONTAINS:- return have.contains(given);+ return have.contains(shouldMatch);}- return have.equals(given);+ return have.equals(shouldMatch);};}diff --git a/model/src/main/java/org/openremote/model/rules/AssetState.java b/model/src/main/java/org/openremote/model/rules/AssetState.javaindex 1d633d8ea..52891e838 100644--- a/model/src/main/java/org/openremote/model/rules/AssetState.java+++ b/model/src/main/java/org/openremote/model/rules/AssetState.java@@ -342,10 +342,10 @@ public class AssetState implements Comparable<AssetState> {", type='" + getType() + '\'' +", attributeName='" + getAttributeName() + '\'' +", attributeType=" + getAttributeType() +- ", value='" + (getValue().isPresent() ? getValue().get().toJson() : "null") + '\'' + // TODO Performance?+ ", value=" + (getValue().isPresent() ? getValue().get().toJson() : "null") + // TODO Performance?", timestamp=" + getTimestamp() +", time=" + getTime() +- ", oldValue='" + (getOldValue().isPresent() ? getOldValue().get().toJson() : "null") + '\'' ++ ", oldValue=" + (getOldValue().isPresent() ? getOldValue().get().toJson() : "null") +", oldValueTimestamp=" + getOldValueTimestamp() +", source=" + getSource() +'}';diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovyindex caa35337b..8820fd082 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy@@ -167,7 +167,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {assets[1].tenantDisplayName == nullassets[1].coordinates == nullassets[1].path == null- assets[1].getAttributesList().size() == 9+ assets[1].getAttributesList().size() == 8assets[1].getAttribute("ventilationAuto").isPresent()assets[1].getAttribute("ventilationLevel").isPresent()assets[1].getAttribute("alarmEnabled").isPresent()@@ -176,7 +176,6 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {assets[1].getAttribute("lastExecutedScene").isPresent()assets[1].getAttribute("presenceDetected").isPresent()assets[1].getAttribute("lastDetectedScene").isPresent()- assets[1].getAttribute("smartStart").isPresent()assets[1].getAttribute("alarmEnabled").get().meta.size() == 1assets[1].getAttribute("alarmEnabled").get().getMetaItem(AssetMeta.LABEL).isPresent()assets[1].getAttribute("alarmEnabled").get().getLabelOrName().get() == "Alarm enabled"diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovynew file mode 100644index 000000000..73d5f7799--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy@@ -0,0 +1,374 @@+package org.openremote.test.rules.residence++import org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.rules.RulesEngine+import org.openremote.manager.rules.RulesService+import org.openremote.manager.rules.RulesetStorageService+import org.openremote.manager.setup.SetupService+import org.openremote.manager.setup.builtin.ManagerDemoSetup+import org.openremote.model.attribute.AttributeEvent+import org.openremote.model.rules.AssetRuleset+import org.openremote.model.rules.Ruleset+import org.openremote.model.value.Values+import org.openremote.test.ManagerContainerTrait+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++import static java.util.concurrent.TimeUnit.HOURS+import static org.openremote.manager.setup.builtin.ManagerDemoSetup.DEMO_RULE_STATES_APARTMENT_1++class ResidenceSmartSwitchTest extends Specification implements ManagerContainerTrait {++ def "Set mode ON_AT and begin/end cycle time in future"() {++ given: "the container environment is started"+ def conditions = new PollingConditions(timeout: 10, 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 assetProcessingService = container.getService(AssetProcessingService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)+ RulesEngine apartment1Engine = null++ and: "some rules"+ Ruleset ruleset = new AssetRuleset(+ "Demo Apartment - Smart Start",+ managerDemoSetup.apartment1Id,+ getClass().getResource("/demo/rules/DemoResidenceSmartSwitch.groovy").text,+ Ruleset.Lang.GROOVY+ )+ 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.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1+ }++ and: "smart switches should be off"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert !kitchen.getAttribute("smartSwitchModeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "mode is set to ON_AT"+ assetProcessingService.sendAttributeEvent(+ new AttributeEvent(managerDemoSetup.apartment1KitchenId, "smartSwitchModeA", Values.create("ON_AT"))+ )+ noEventProcessedIn(assetProcessingService, 500)++ then: "mode should be ON_AT but actuator not enabled"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "ON_AT"+ assert !kitchen.getAttribute("smartSwitchBeginEndA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "begin/end cycle time is set to future"+ long oneMinuteInFutureMillis = getClockTimeOf(container) + 60000+ assetProcessingService.sendAttributeEvent(+ new AttributeEvent(managerDemoSetup.apartment1KitchenId, "smartSwitchBeginEndA", Values.create(oneMinuteInFutureMillis))+ )+ noEventProcessedIn(assetProcessingService, 500)++ then: "mode should be ON_AT and actuator enabled with correct start/stop time"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "ON_AT"+ assert kitchen.getAttribute("smartSwitchBeginEndA").get().getValueAsNumber().get() == (double)oneMinuteInFutureMillis+ assert kitchen.getAttribute("smartSwitchStartTimeA").get().getValueAsNumber().get() == oneMinuteInFutureMillis/1000+ assert kitchen.getAttribute("smartSwitchStopTimeA").get().getValueAsNumber().get() == (oneMinuteInFutureMillis/1000) + (2.5 * 60 * 60) + 60+ assert kitchen.getAttribute("smartSwitchEnabledA").get().getValueAsNumber().get() == 1+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "time advances beyond end of cycle"+ advancePseudoClock(5, HOURS, container)++ then: "mode should be NOW_ON and actuator not enabled"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "NOW_ON"+ assert !kitchen.getAttribute("smartSwitchBeginEndA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ cleanup: "the server should be stopped"+ stopContainer(container)+ }++ def "Set mode ON_AT and begin/end cycle time in past"() {++ given: "the container environment is started"+ def conditions = new PollingConditions(timeout: 10, 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 assetProcessingService = container.getService(AssetProcessingService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)+ RulesEngine apartment1Engine = null++ and: "some rules"+ Ruleset ruleset = new AssetRuleset(+ "Demo Apartment - Smart Start",+ managerDemoSetup.apartment1Id,+ getClass().getResource("/demo/rules/DemoResidenceSmartSwitch.groovy").text,+ Ruleset.Lang.GROOVY+ )+ 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.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1+ }++ and: "smart switches should be off"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert !kitchen.getAttribute("smartSwitchModeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "mode is set to ON_AT"+ assetProcessingService.sendAttributeEvent(+ new AttributeEvent(managerDemoSetup.apartment1KitchenId, "smartSwitchModeA", Values.create("ON_AT"))+ )+ noEventProcessedIn(assetProcessingService, 500)++ then: "mode should be ON_AT but actuator not enabled"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "ON_AT"+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "begin/end cycle time is set to past"+ long fiveMinutesInPastMillis = getClockTimeOf(container) - (5 * 60 * 1000)+ assetProcessingService.sendAttributeEvent(+ new AttributeEvent(managerDemoSetup.apartment1KitchenId, "smartSwitchBeginEndA", Values.create(fiveMinutesInPastMillis))+ )+ noEventProcessedIn(assetProcessingService, 500)++ then: "mode should be ON_AT and actuator not enabled"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "ON_AT"+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ cleanup: "the server should be stopped"+ stopContainer(container)+ }++ def "Set mode READY_AT and begin/end cycle time in past"() {++ given: "the container environment is started"+ def conditions = new PollingConditions(timeout: 10, 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 assetProcessingService = container.getService(AssetProcessingService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)+ RulesEngine apartment1Engine = null++ and: "some rules"+ Ruleset ruleset = new AssetRuleset(+ "Demo Apartment - Smart Start",+ managerDemoSetup.apartment1Id,+ getClass().getResource("/demo/rules/DemoResidenceSmartSwitch.groovy").text,+ Ruleset.Lang.GROOVY+ )+ 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.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1+ }++ and: "smart switches should be off"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert !kitchen.getAttribute("smartSwitchModeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "mode is set to READY_AT"+ assetProcessingService.sendAttributeEvent(+ new AttributeEvent(managerDemoSetup.apartment1KitchenId, "smartSwitchModeA", Values.create("READY_AT"))+ )+ noEventProcessedIn(assetProcessingService, 500)++ then: "mode should be READY_AT but actuator not enabled"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "READY_AT"+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "begin/end cycle time is set to past"+ long fiveMinutesInPastMillis = getClockTimeOf(container) - (5 * 60 * 1000)+ assetProcessingService.sendAttributeEvent(+ new AttributeEvent(managerDemoSetup.apartment1KitchenId, "smartSwitchBeginEndA", Values.create(fiveMinutesInPastMillis))+ )+ noEventProcessedIn(assetProcessingService, 500)++ then: "mode should be READY_AT and actuator not enabled with correct start/stop time"+ conditions.eventually {+ def expectedTimeMillis = getClockTimeOf(container) + ((2.5 * 60 * 60) + 60) * 1000+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "READY_AT"+ assert kitchen.getAttribute("smartSwitchBeginEndA").get().getValueAsNumber().get() == (double)expectedTimeMillis+ assert kitchen.getAttribute("smartSwitchStartTimeA").get().getValueAsNumber().get() == (double)getClockTimeOf(container)/1000+ assert kitchen.getAttribute("smartSwitchStopTimeA").get().getValueAsNumber().get() == (double)(expectedTimeMillis/1000)+ assert kitchen.getAttribute("smartSwitchEnabledA").get().getValueAsNumber().get() == 1+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ when: "time advances beyond end of cycle"+ advancePseudoClock(5, HOURS, container)++ then: "mode should be NOW_ON and actuator not enabled"+ conditions.eventually {+ def kitchen = assetStorageService.find(managerDemoSetup.apartment1KitchenId, true)+ assert kitchen.getAttribute("smartSwitchModeA").get().getValueAsString().get() == "NOW_ON"+ assert !kitchen.getAttribute("smartSwitchBeginEndA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledA").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledB").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchModeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchBeginEndC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStartTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchStopTimeC").get().getValue().isPresent()+ assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()+ }++ cleanup: "the server should be stopped"+ stopContainer(container)+ }++}