Skip to content

armgit / core

Public Imported from GitHub
0

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

Fixed rules sun trigger to only trigger within 1 min of condition (#1103)

Rich Turner9a96f6b892fb
diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
index 1365c1bb2..5d8c1beb5 100644
--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
@@ -160,10 +160,10 @@ public class JsonRulesBuilder extends RulesBuilder {
long nextMillis = occurrence.toInstant().toEpochMilli() + offsetMillis;
- // If occurrence is before requested time then advance the sun calculator to either reset occurrence or requested time (whichever is later)
+ // If occurrence is before requested time then advance the sun calculator to either reset occurrence or 5 mins before requested time (whichever is later)
if (nextMillis < time) {
ZonedDateTime resetOccurrence = useRiseTime ? sunTimes.get().getSet() : sunTimes.get().getRise();
- sunTimes.set(sunCalculator.on(new Date(Math.max(resetOccurrence.toEpochSecond()*1000, time))).execute());
+ sunTimes.set(sunCalculator.on(new Date(Math.max(resetOccurrence.toInstant().toEpochMilli(), time - 300000))).execute());
}
return nextMillis;
@@ -172,7 +172,8 @@ public class JsonRulesBuilder extends RulesBuilder {
timePredicate = (time) -> {
long nextExecute = nextExecuteMillisCalculator.apply(time);
- if (time >= nextExecute) {
+ // Next execute must be within a minute of requested time
+ if (time >= nextExecute && time - nextExecute < 60000) {
log(Level.INFO, "Rule condition sun position has triggered at: " + timerService.getCurrentTimeMillis());
return true;
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
index 2537eb39d..3a6a899d4 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
@@ -43,7 +43,6 @@ import org.openremote.model.asset.Asset;
import org.openremote.model.query.filter.GeofencePredicate;
import org.openremote.model.query.filter.LocationAttributePredicate;
import org.openremote.model.rules.*;
-import org.openremote.model.util.TextUtil;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -226,7 +225,7 @@ public class RulesEngine<T extends Ruleset> {
public boolean isError() {
for (RulesetDeployment deployment : deployments.values()) {
- if (deployment.isError() || deployment.getError() instanceof RulesLoopException) {
+ if (deployment.isError()) {
return true;
}
}
@@ -256,13 +255,6 @@ public class RulesEngine<T extends Ruleset> {
return null;
}
- /**
- * @return <code>true</code> if all rulesets are not in an error state.
- */
- public boolean canStart() {
- return deployments.values().stream().noneMatch(rd -> rd.getStatus() == COMPILATION_ERROR && !rd.isContinueOnError());
- }
-
public void addRuleset(T ruleset) {
// Check for previous version of this ruleset
@@ -275,39 +267,10 @@ public class RulesEngine<T extends Ruleset> {
removeRuleset(deployment.ruleset);
}
-
deployment = new RulesetDeployment(ruleset, timerService, assetStorageService, executorService, assetsFacade, usersFacade, notificationFacade, webhooksFacade, historicFacade, predictedFacade);
- boolean compiled;
-
- try {
- deployment.init();
-
- if (TextUtil.isNullOrEmpty(ruleset.getRules())) {
- LOG.finest("Ruleset is empty so no rules to deploy: " + ruleset.getName());
- deployment.setStatus(EMPTY);
- publishRulesetStatus(deployment);
- } else if (!ruleset.isEnabled()) {
- LOG.finest("Ruleset is disabled: " + ruleset.getName());
- deployment.setStatus(DISABLED);
- publishRulesetStatus(deployment);
- } else {
- compiled = deployment.compile();
-
- if (!compiled) {
- LOG.log(Level.SEVERE, "Ruleset compilation error: " + ruleset.getName(), deployment.getError());
- deployment.setStatus(COMPILATION_ERROR);
- publishRulesetStatus(deployment);
- } else if (running) {
- startRuleset(deployment);
- }
- }
- } catch (IllegalStateException e) {
- LOG.log(Level.WARNING, e.getMessage());
- deployment.setStatus(VALIDITY_PERIOD_ERROR);
- publishRulesetStatus(deployment);
- }
-
+ deployment.init();
deployments.put(ruleset.getId(), deployment);
+ publishRulesetStatus(deployment);
updateDeploymentInfo();
if (wasRunning) {
@@ -322,19 +285,19 @@ public class RulesEngine<T extends Ruleset> {
RulesetDeployment deployment = deployments.get(ruleset.getId());
if (deployment == null) {
- LOG.finest("Ruleset cannot be retracted as it was never deployed: " + ruleset);
- return deployments.size() == 0;
+ LOG.fine("Ruleset cannot be retracted as it was never deployed: " + ruleset);
+ } else {
+ boolean wasRunning = this.running;
+ stop();
+ stopRuleset(deployment);
+ deployments.values().remove(deployment);
+ updateDeploymentInfo();
+ if (wasRunning && !deployments.isEmpty()) {
+ start();
+ }
}
- stopRuleset(deployment);
-
- deployment.setStatus(REMOVED);
- publishRulesetStatus(deployment);
- deployments.remove(ruleset.getId());
- updateDeploymentInfo();
- start();
-
- return deployments.size() == 0;
+ return deployments.isEmpty();
}
public void start() {
@@ -347,8 +310,10 @@ public class RulesEngine<T extends Ruleset> {
return;
}
- if (!canStart()) {
- LOG.info("Cannot start rules engine one or more rulesets in an error state");
+ boolean canAnyStart = deployments.values().stream().noneMatch(RulesetDeployment::canStart);
+
+ if (canAnyStart) {
+ LOG.info("Cannot start rules engine as no rulesets are able to be started");
return;
}
@@ -389,10 +354,6 @@ public class RulesEngine<T extends Ruleset> {
}
public void stop() {
- stop(false);
- }
-
- public void stop(boolean systemShutdownInProgress) {
if (!running) {
return;
}
@@ -407,12 +368,7 @@ public class RulesEngine<T extends Ruleset> {
statsTimer = null;
}
- deployments.values().forEach(this::stopRuleset);
-
- if (systemShutdownInProgress) {
- deployments.clear();
- return;
- }
+ new HashSet<>(deployments.values()).forEach(this::stopRuleset);
if (assetLocationPredicatesConsumer != null) {
assetLocationPredicatesConsumer.accept(this, null);
@@ -426,23 +382,13 @@ public class RulesEngine<T extends Ruleset> {
return;
}
- RulesetStatus status = deployment.getStatus();
-
- if (status == COMPILATION_ERROR || status == DISABLED || status == EXPIRED) {
- LOG.finest("Ruleset '" + deployment.getName() + "': status=" + status);
+ if (deployment.start(facts)) {
publishRulesetStatus(deployment);
- return;
}
-
- deployment.setStatus(DEPLOYED);
- deployment.start(facts);
- publishRulesetStatus(deployment);
}
protected synchronized void stopRuleset(RulesetDeployment deployment) {
- if (deployment.getStatus() == DEPLOYED) {
- deployment.stop(facts);
- deployment.setStatus(READY);
+ if (deployment.stop(facts)) {
publishRulesetStatus(deployment);
}
}
@@ -549,7 +495,6 @@ public class RulesEngine<T extends Ruleset> {
deployment.setError(ex);
publishRulesetStatus(deployment);
- // TODO We only get here on LHS runtime errors, RHS runtime errors are in RuleFacts.onFailure()
if (ex instanceof RulesLoopException || !deployment.ruleset.isContinueOnError()) {
stop();
break;
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
index 9648d1dd2..27d5038f4 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
@@ -348,13 +348,13 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
}
}
- assetEngines.forEach((assetId, rulesEngine) -> rulesEngine.stop(true));
+ assetEngines.forEach((assetId, rulesEngine) -> rulesEngine.stop());
assetEngines.clear();
- realmEngines.forEach((realm, rulesEngine) -> rulesEngine.stop(true));
+ realmEngines.forEach((realm, rulesEngine) -> rulesEngine.stop());
realmEngines.clear();
if (globalEngine != null) {
- globalEngine.stop(true);
+ globalEngine.stop();
globalEngine = null;
}
@@ -663,7 +663,6 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
}
if (globalEngine.removeRuleset(ruleset)) {
- globalEngine.stop();
globalEngine = null;
}
}
@@ -705,7 +704,6 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
}
if (rulesEngine.removeRuleset(ruleset)) {
- rulesEngine.stop();
realmEngines.remove(ruleset.getRealm());
}
}
@@ -781,7 +779,6 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
}
if (rulesEngine.removeRuleset(ruleset)) {
- rulesEngine.stop();
assetEngines.remove(ruleset.getAssetId());
}
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
index cf1fe9666..c1bb77360 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
@@ -37,6 +37,7 @@ import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.rules.*;
import org.openremote.model.rules.flow.NodeCollection;
import org.openremote.model.util.Pair;
+import org.openremote.model.util.TextUtil;
import org.openremote.model.util.ValueUtil;
import javax.script.*;
@@ -48,6 +49,8 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.openremote.model.rules.RulesetStatus.*;
+
public class RulesetDeployment {
/**
@@ -129,6 +132,7 @@ public class RulesetDeployment {
final protected HistoricDatapoints historicDatapointsFacade;
final protected PredictedDatapoints predictedDatapointsFacade;
final protected List<ScheduledFuture<?>> scheduledRuleActions = Collections.synchronizedList(new ArrayList<>());
+ protected boolean running;
protected RulesetStatus status = RulesetStatus.READY;
protected Throwable error;
protected JsonRulesBuilder jsonRulesBuilder;
@@ -157,10 +161,27 @@ public class RulesetDeployment {
validity = ruleset.getValidity();
if (validity == null) {
- String msg = "Ruleset '" + ruleset.getName() + "' has invalid validity value: " + ruleset.getMeta().get(Ruleset.VALIDITY);
- throw new IllegalStateException(msg);
+ RulesEngine.LOG.log(Level.WARNING, "Ruleset '" + ruleset.getName() + "' has invalid validity value: " + ruleset.getMeta().get(Ruleset.VALIDITY));
+ status = VALIDITY_PERIOD_ERROR;
+ return;
}
}
+
+ if (TextUtil.isNullOrEmpty(ruleset.getRules())) {
+ RulesEngine.LOG.finest("Ruleset is empty so no rules to deploy: " + ruleset.getName());
+ status = EMPTY;
+ return;
+ }
+
+ if (!ruleset.isEnabled()) {
+ RulesEngine.LOG.finest("Ruleset is disabled: " + ruleset.getName());
+ status = DISABLED;
+ }
+
+ if (!compile()) {
+ RulesEngine.LOG.log(Level.SEVERE, "Ruleset compilation error: " + ruleset.getName(), getError());
+ status = COMPILATION_ERROR;
+ }
}
public long getId() {
@@ -233,20 +254,37 @@ public class RulesetDeployment {
return false;
}
+ public boolean canStart() {
+ return status != COMPILATION_ERROR && status != DISABLED && status != RulesetStatus.EXPIRED;
+ }
+
/**
* Called when a ruleset is started (allows for initialisation tasks)
*/
- public void start(RulesFacts facts) {
+ public boolean start(RulesFacts facts) {
+ if (!canStart()) {
+ return false;
+ }
+
if (jsonRulesBuilder != null) {
jsonRulesBuilder.start(facts);
}
+
+ running = true;
+ return true;
}
/**
* Called when this deployment is stopped, could be the ruleset is being updated, removed or an error has occurred
* during execution
*/
- public void stop(RulesFacts facts) {
+ public boolean stop(RulesFacts facts) {
+ if (!running) {
+ return false;
+ }
+
+ running = false;
+
synchronized (scheduledRuleActions) {
scheduledRuleActions.removeIf(scheduledFuture -> {
scheduledFuture.cancel(true);
@@ -257,6 +295,8 @@ public class RulesetDeployment {
if (jsonRulesBuilder != null) {
jsonRulesBuilder.stop(facts);
}
+
+ return true;
}
public void onAssetStatesChanged(RulesFacts facts, RulesEngine.AssetStateChangeEvent event) {
@@ -490,7 +530,7 @@ public class RulesetDeployment {
public RulesetStatus getStatus() {
- if (isError()) {
+ if (isError() || status == DISABLED) {
return status;
}
@@ -505,6 +545,10 @@ public class RulesetDeployment {
}
}
+ if (running) {
+ return DEPLOYED;
+ }
+
return status;
}
@@ -525,7 +569,7 @@ public class RulesetDeployment {
}
public boolean isError() {
- return status == RulesetStatus.LOOP_ERROR || ((status == RulesetStatus.EXECUTION_ERROR || status == RulesetStatus.COMPILATION_ERROR) && !isContinueOnError());
+ return status == RulesetStatus.LOOP_ERROR || status == VALIDITY_PERIOD_ERROR || ((status == RulesetStatus.EXECUTION_ERROR || status == RulesetStatus.COMPILATION_ERROR) && !isContinueOnError());
}
public boolean isContinueOnError() {
diff --git a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
index 0d73c6197..df23907bd 100644
--- a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
@@ -590,7 +590,7 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {
.filter({ it instanceof PushNotificationMessage })
.filter({
((PushNotificationMessage) it).data.get("action").asText() == "GEOFENCE_REFRESH"
- }).count() == 4
+ }).count() == 2
}
then: "the geofences of testUser3Console1 should still contain two geofences but the location of the second should have been updated"
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
index 0e01c7a5e..19151e026 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
@@ -191,10 +191,8 @@ class BasicRulesDeploymentTest extends Specification implements ManagerContainer
then: "the realms rule engine should stop and all asset rule engines in this realm should also stop"
conditions.eventually {
assert !realmBuildingEngine.isRunning()
- assert realmBuildingEngine.deployments.size() == 2
assert rulesService.realmEngines.get(keycloakTestSetup.realmBuilding.name) == null
assert !apartment3Engine.isRunning()
- assert apartment3Engine.deployments.size() == 1
assert rulesService.assetEngines.get(managerTestSetup.apartment3Id) == null
}
@@ -244,7 +242,8 @@ class BasicRulesDeploymentTest extends Specification implements ManagerContainer
assert realmBuildingEngine != null
assert realmBuildingEngine.isRunning()
assert realmBuildingEngine.deployments.size() == 3
- assert realmBuildingEngine.deployments[ruleset.id].status == EXECUTION_ERROR
+ assert realmBuildingEngine.deployments[ruleset.id].status == DEPLOYED
+ assert realmBuildingEngine.deployments[ruleset.id].error != null
assert !realmBuildingEngine.isError()
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index d86742f89..0bd84c125 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -42,6 +42,7 @@ import org.openremote.model.notification.NotificationSendResult
import org.openremote.model.notification.PushNotificationMessage
import org.openremote.model.rules.RealmRuleset
import org.openremote.model.rules.Ruleset
+import org.openremote.model.rules.RulesetStatus
import org.openremote.model.rules.json.JsonRulesetDefinition
import org.openremote.model.util.ValueUtil
import org.openremote.model.value.MetaItemType
@@ -692,6 +693,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
}
when: "time advances slightly past the next sunset time and the rule engine fires"
+ sunTimes = sunsetCalculator.on(sunTimes.getSet().plusHours(1)).execute()
advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getSet().plusSeconds(10)).getSeconds()+1, TimeUnit.SECONDS, container)
then: "the rule engine should have fired again and the rule should have triggered"
@@ -702,6 +704,37 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert thingAsset.getAttribute("sunset").get().getValue().orElse(0) == 100
}
+ when: "a schedule is added to the rule to enable it 2 hours after sunset each day"
+ ruleset.setValidity(new CalendarEvent(sunTimes.getSet().plusHours(2).toDate(), sunTimes.getSet().plusHours(12).toDate(), new Recur(Recur.Frequency.DAILY, 5)))
+ ruleset = rulesetStorageService.merge(ruleset)
+
+ then: "the rule should become paused in the engine"
+ conditions.eventually {
+ assert realmBuildingEngine.deployments.get(ruleset.id).status == PAUSED
+ }
+
+ when: "the updated attribute is cleared"
+ thingAsset.getAttribute("sunset").get().setValue(null)
+ thingAsset = assetStorageService.merge(thingAsset)
+
+ and: "time advances into the next active time of the rule"
+ sunTimes = sunsetCalculator.on(sunTimes.getSet().plusHours(1)).execute()
+ advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getSet().plusHours(4)).getSeconds(), TimeUnit.SECONDS, container)
+
+ then: "the rule should become un-paused in the engine"
+ conditions.eventually {
+ assert realmBuildingEngine.deployments.get(ruleset.id).status == DEPLOYED
+ }
+
+ then: "the rule engine should have fired again and the rule should not have triggered (as past sunset)"
+ conditions.eventually {
+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
+ assert realmBuildingEngine.lastFireTimestamp == timerService.getNow().toEpochMilli()
+ thingAsset = assetStorageService.find(thingId) as ThingAsset
+ assert thingAsset.getAttribute("sunset").get().getValue().orElse(0) == 0
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ }
+
when: "a ruleset with sunrise offset trigger condition is added whose action updates the thing asset"
rulesStr = getClass().getResource("/org/openremote/test/rules/JsonRuleSunriseOffset.json").text
rule = parse(rulesStr, JsonRulesetDefinition.class).orElseThrow()
diff --git a/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy
index 2c42538f9..5136fd0cf 100644
--- a/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy
+++ b/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy
@@ -155,13 +155,7 @@ trait ContainerTrait {
def assetEngines = new HashSet<RulesEngine>(rulesService.assetEngines.values())
LOG.info("Purging ${assetEngines.size()} asset engine(s)")
assetEngines.forEach {
- it.stop(true)
- while (!it.deployments.isEmpty()) {
- if (counter++ > 20) {
- throw new IllegalStateException("Failed to stop asset engine: " + it.id)
- }
- Thread.sleep(100)
- }
+ it.stop()
rulesService.assetEngines.values().remove(it)
}
def assetRulesets = getRulesets(AssetRuleset.class)
@@ -172,13 +166,7 @@ trait ContainerTrait {
def realmEngines = new HashSet<RulesEngine>(rulesService.realmEngines.values())
LOG.info("Purging ${realmEngines.size()} realm engine(s)")
realmEngines.forEach {
- it.stop(true)
- while (!it.deployments.isEmpty()) {
- if (counter++ > 20) {
- throw new IllegalStateException("Failed to stop realm engine: " + it.id)
- }
- Thread.sleep(100)
- }
+ it.stop()
rulesService.realmEngines.values().remove(it)
}
def realmRulesets = getRulesets(RealmRuleset.class)
@@ -189,13 +177,7 @@ trait ContainerTrait {
def globalEngine = rulesService.globalEngine
if (globalEngine != null) {
LOG.info("Purging global engine")
- globalEngine.stop(true)
- while (!globalEngine.deployments.isEmpty()) {
- if (counter++ > 20) {
- throw new IllegalStateException("Failed to stop global engine: " + globalEngine.id)
- }
- Thread.sleep(100)
- }
+ globalEngine.stop()
rulesService.globalEngine = null
}
def globalRulesets = getRulesets(GlobalRuleset.class)