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
Various rules updates - updated DateTimePredicate support and added support for it in rules and improved code reuse for asset query predicates
Rich Turner8aa3d82804e1
diff --git a/client/src/main/java/org/openremote/app/client/notifications/NotificationsActivity.java b/client/src/main/java/org/openremote/app/client/notifications/NotificationsActivity.javaindex 46a99146c..f4c8aa6f2 100644--- a/client/src/main/java/org/openremote/app/client/notifications/NotificationsActivity.java+++ b/client/src/main/java/org/openremote/app/client/notifications/NotificationsActivity.java@@ -444,6 +444,6 @@ public class NotificationsActivity extends AssetBrowsingActivity<NotificationsPl}protected Notification buildNotification() {- return new Notification("Manager Message", sendOptions.getMessage(), new Notification.Targets(sendOptions.selectedTargetType, sendOptions.selectedTarget));+ return new Notification("Manager Message", sendOptions.getMessage(), new Notification.Targets(sendOptions.selectedTargetType, sendOptions.selectedTarget), null, null);}}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex fc273f415..3495a9cac 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -40,6 +40,7 @@ import org.openremote.container.timer.TimerService;import org.openremote.container.web.WebService;import org.openremote.manager.asset.console.ConsoleResourceImpl;import org.openremote.manager.event.ClientEventService;+import org.openremote.manager.rules.AssetQueryPredicate;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.model.Constants;import org.openremote.model.ValidationFailure;@@ -54,7 +55,9 @@ import org.openremote.model.query.BaseAssetQuery;import org.openremote.model.query.filter.*;import org.openremote.model.security.ClientRole;import org.openremote.model.security.User;+import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;+import org.openremote.model.util.TimeUtil;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Value;import org.openremote.model.value.Values;@@ -64,8 +67,13 @@ import javax.persistence.EntityManager;import javax.persistence.NoResultException;import javax.persistence.TypedQuery;import java.sql.*;+import java.text.ParseException;+import java.text.SimpleDateFormat;+import java.time.temporal.ChronoUnit;+import java.time.temporal.TemporalUnit;import java.util.*;import java.util.Date;+import java.util.concurrent.TimeUnit;import java.util.function.Consumer;import java.util.logging.Level;import java.util.logging.Logger;@@ -411,7 +419,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}public boolean isUserAsset(String assetId) {- return isUserAsset(null, assetId);+ return isUserAsset((String)null, assetId);}public boolean isUserAsset(String userId, String assetId) {@@ -436,6 +444,21 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic});}+ public boolean isUserAsset(List<String> userIds, String assetId) {+ return persistenceService.doReturningTransaction(entityManager -> {+ try {+ return entityManager.createQuery(+ "select count(ua) from UserAsset ua where ua.id.userId in :userIds and ua.id.assetId = :assetId",+ Long.class)+ .setParameter("userIds", userIds)+ .setParameter("assetId", assetId)+ .getSingleResult() > 0;+ } catch (NoResultException ex) {+ return false;+ }+ });+ }+public boolean isUserAssets(String userId, List<String> assetIds) {return persistenceService.doReturningTransaction(entityManager -> {try {@@ -642,6 +665,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}sb.append(buildOrderByString(query));+ sb.append(buildLimitString(query));return new PreparedAssetQuery(sb.toString(), binders);}@@ -830,6 +854,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn sb.toString();}+ protected String buildLimitString(BaseAssetQuery query) {+ if (query.limit > 0) {+ return " LIMIT " + query.limit;+ }+ return "";+ }+protected String buildWhereClause(BaseAssetQuery query, int level, List<ParameterBinder> binders) {// level = 1 is main query// level = 2 is union@@ -1082,36 +1113,34 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicfinal int keyFormatPos = binders.size() + 1;binders.add(st -> st.setString(keyFormatPos, dateTimePredicate.dateFormat));+ Pair<Long, Long> fromAndTo = AssetQueryPredicate.asFromAndTo(timerService.getCurrentTimeMillis(), dateTimePredicate);+final int pos = binders.size() + 1;- binders.add(st -> st.setString(pos, dateTimePredicate.value));- final int formatPos = binders.size() + 1;- binders.add(st -> st.setString(formatPos, dateTimePredicate.dateFormat));+ binders.add(st -> st.setDate(pos, new java.sql.Date(fromAndTo.key)));switch (dateTimePredicate.operator) {case EQUALS:- attributeBuilder.append(" = to_timestamp(?, ?)");+ attributeBuilder.append(" = ?");break;case NOT_EQUALS:- attributeBuilder.append(" <> to_timestamp(?, ?)");+ attributeBuilder.append(" <> ?");break;case GREATER_THAN:- attributeBuilder.append(" > to_timestamp(?, ?)");+ attributeBuilder.append(" > ?");break;case GREATER_EQUALS:- attributeBuilder.append(" >= to_timestamp(?, ?)");+ attributeBuilder.append(" >= ?");break;case LESS_THAN:- attributeBuilder.append(" < to_timestamp(?, ?)");+ attributeBuilder.append(" < ?");break;case LESS_EQUALS:- attributeBuilder.append(" <= to_timestamp(?, ?)");+ attributeBuilder.append(" <= ?");break;case BETWEEN:- attributeBuilder.append(" BETWEEN to_timestamp(?, ?) AND to_timestamp(?, ?)");final int pos2 = binders.size() + 1;- binders.add(st -> st.setString(pos2, dateTimePredicate.rangeValue));- final int formatPos2 = binders.size() + 1;- binders.add(st -> st.setString(formatPos2, dateTimePredicate.dateFormat));+ binders.add(st -> st.setDate(pos2, new java.sql.Date(fromAndTo.value)));+ attributeBuilder.append(" BETWEEN ? AND ?");break;}} else if (attributePredicate.value instanceof NumberPredicate) {diff --git a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java b/manager/src/main/java/org/openremote/manager/notification/NotificationService.javaindex 0a7d788d3..9d0dfc981 100644--- a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java+++ b/manager/src/main/java/org/openremote/manager/notification/NotificationService.java@@ -526,7 +526,7 @@ public class NotificationService extends RouteBuilder implements ContainerServicbreak;}} else if (!TextUtil.isNullOrEmpty(notification.getRepeatInterval())) {- timestamp = lastSend.plus(TimeUtil.parseTimeString(notification.getRepeatInterval()), ChronoUnit.MILLIS);+ timestamp = lastSend.plus(TimeUtil.parseTimeDuration(notification.getRepeatInterval()), ChronoUnit.MILLIS);}return timestamp;diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex b5719242b..a8b3ec54e 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -22,17 +22,26 @@ package org.openremote.manager.rules;import com.vividsolutions.jts.geom.Coordinate;import com.vividsolutions.jts.geom.Envelope;import org.geotools.referencing.GeodeticCalculator;-import org.openremote.model.query.BaseAssetQuery;-import org.openremote.model.query.BaseAssetQuery.*;+import org.openremote.container.timer.TimerService;import org.openremote.model.attribute.AttributeType;+import org.openremote.model.attribute.Meta;+import org.openremote.model.attribute.MetaItem;import org.openremote.model.geo.GeoJSONPoint;-import org.openremote.model.query.filter.PathPredicate;+import org.openremote.model.query.BaseAssetQuery;+import org.openremote.model.query.BaseAssetQuery.NumberType;import org.openremote.model.query.filter.*;import org.openremote.model.rules.AssetState;+import org.openremote.model.util.Pair;+import org.openremote.model.util.TimeUtil;+import org.openremote.model.value.Value;+import org.openremote.model.value.Values;+import java.text.ParseException;+import java.text.SimpleDateFormat;import java.util.Arrays;import java.util.Locale;import java.util.function.Predicate;+import java.util.function.Supplier;import static org.openremote.model.query.BaseAssetQuery.Operator.LESS_EQUALS;import static org.openremote.model.query.BaseAssetQuery.Operator.LESS_THAN;@@ -43,8 +52,10 @@ import static org.openremote.model.query.BaseAssetQuery.Operator.LESS_THAN;public class AssetQueryPredicate implements Predicate<AssetState> {final protected BaseAssetQuery query;+ final protected TimerService timerService;- public AssetQueryPredicate(BaseAssetQuery query) {+ public AssetQueryPredicate(TimerService timerService, BaseAssetQuery query) {+ this.timerService = timerService;this.query = query;}@@ -75,14 +86,17 @@ public class AssetQueryPredicate implements Predicate<AssetState> {if (query.attribute != null) {for (AttributePredicate p : query.attribute) {- if (!asPredicate(p).test(assetState))+ if (!asPredicate(timerService::getCurrentTimeMillis, p).test(assetState))return false;}}if (query.attributeMeta != null) {- // TODO Would require meta items in AbstractAssetUpdate- throw new UnsupportedOperationException("Restriction by attribute meta not implemented in rules matching");+ for (AttributeMetaPredicate p : query.attributeMeta) {+ if (!asPredicate(timerService::getCurrentTimeMillis, p).test(assetState.getMeta())) {+ return false;+ }+ }}if (query.select != null) {@@ -106,7 +120,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {return true;}- protected Predicate<String> asPredicate(StringPredicate predicate) {+ public static Predicate<String> asPredicate(StringPredicate predicate) {return string -> {if (string == null && predicate.value == null)return true;@@ -130,7 +144,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {};}- protected Predicate<Boolean> asPredicate(BooleanPredicate predicate) {+ public static Predicate<Boolean> asPredicate(BooleanPredicate predicate) {return b -> {// If given a null, we assume it's false!if (b == null)@@ -139,7 +153,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {};}- protected Predicate<String[]> asPredicate(StringArrayPredicate predicate) {+ public static Predicate<String[]> asPredicate(StringArrayPredicate predicate) {return strings -> {if (strings == null && predicate.predicates == null)return true;@@ -158,13 +172,40 @@ public class AssetQueryPredicate implements Predicate<AssetState> {};}- protected Predicate<Long> asPredicate(DateTimePredicate predicate) {+ public static Predicate<Long> asPredicate(Supplier<Long> currentMillisProducer, DateTimePredicate predicate) {return timestamp -> {- throw new UnsupportedOperationException("NOT IMPLEMENTED");+ Pair<Long, Long> fromAndTo = asFromAndTo(currentMillisProducer.get(), predicate);++ if (fromAndTo.key == null) {+ throw new IllegalArgumentException("Date time predicate 'value' is not valid: " + predicate);+ }++ switch (predicate.operator) {++ case EQUALS:+ return timestamp == fromAndTo.key.longValue();+ case NOT_EQUALS:+ return timestamp != fromAndTo.key.longValue();+ case GREATER_THAN:+ return timestamp > fromAndTo.key;+ case GREATER_EQUALS:+ return timestamp >= fromAndTo.key;+ case LESS_THAN:+ return timestamp < fromAndTo.key;+ case LESS_EQUALS:+ return timestamp <= fromAndTo.key;+ case BETWEEN:+ if (fromAndTo.value == null) {+ throw new IllegalArgumentException("Date time predicate 'rangeValue' is not valid: " + predicate);+ }+ return timestamp > fromAndTo.key && timestamp < fromAndTo.value;+ }++ return false;};}- protected Predicate<Double> asPredicate(NumberPredicate predicate) {+ public static Predicate<Double> asPredicate(NumberPredicate predicate) {return d -> {if (d == null) {@@ -199,24 +240,24 @@ public class AssetQueryPredicate implements Predicate<AssetState> {};}- protected Predicate<AssetState> asPredicate(ParentPredicate predicate) {+ public static Predicate<AssetState> asPredicate(ParentPredicate predicate) {return assetState ->(predicate.id == null || predicate.id.equals(assetState.getParentId()))&& (predicate.type == null || predicate.type.equals(assetState.getParentTypeString()))&& (!predicate.noParent || assetState.getParentId() == null);}- protected Predicate<String[]> asPredicate(PathPredicate predicate) {+ public static Predicate<String[]> asPredicate(PathPredicate predicate) {return givenPath -> Arrays.equals(predicate.path, givenPath);}- protected Predicate<AssetState> asPredicate(TenantPredicate predicate) {+ public static Predicate<AssetState> asPredicate(TenantPredicate predicate) {return assetState ->(predicate.realm == null || predicate.realm.equals(assetState.getTenantRealm()))&& (predicate.realmId == null || predicate.realmId.equals(assetState.getRealmId()));}- protected Predicate<Coordinate> asPredicate(LocationPredicate predicate) {+ public static Predicate<Coordinate> asPredicate(LocationPredicate predicate) {return coordinate -> {if (predicate instanceof RadialLocationPredicate) {//TODO geotools version to gradle properties@@ -234,45 +275,104 @@ public class AssetQueryPredicate implements Predicate<AssetState> {rectangularLocationPredicate.lngMax);return envelope.contains(coordinate);} else {- throw new UnsupportedOperationException("Location predicate '" + query.location.getClass().getSimpleName() + "' not supported in rules matching");+ throw new UnsupportedOperationException("Location predicate '" + predicate.getClass().getSimpleName() + "' not supported in rules matching");}};}- protected Predicate<AssetState> asPredicate(AttributePredicate predicate) {+ public static Predicate<AssetState> asPredicate(Supplier<Long> currentMillisProducer, AttributePredicate predicate) {return assetState -> {if (predicate.name != null && !asPredicate(predicate.name).test(assetState.getAttributeName()))return false;- if (predicate.value == null)+ return asPredicate(currentMillisProducer, predicate.value).test(assetState.getValue().orElse(null));+ };+ }++ public static Predicate<Value> asPredicate(Supplier<Long> currentMillisProducer, ValuePredicate predicate) {+ return value -> {+ if (predicate == null)return true;- if (predicate.value instanceof ValueEmptyPredicate) {- return !assetState.getValue().isPresent();- } else if (predicate.value instanceof ValueNotEmptyPredicate) {- return assetState.getValue().isPresent();+ if (predicate instanceof ValueEmptyPredicate) {++ return value == null;+ } else if (predicate instanceof ValueNotEmptyPredicate) {++ return value != null;- } else if (predicate.value instanceof StringPredicate) {+ } else if (predicate instanceof StringPredicate) {- StringPredicate p = (StringPredicate) predicate.value;- return asPredicate(p).test(assetState.getValueAsString().orElse(null));+ StringPredicate p = (StringPredicate) predicate;+ return asPredicate(p).test(Values.getString(value).orElse(null));- } else if (predicate.value instanceof BooleanPredicate) {+ } else if (predicate instanceof BooleanPredicate) {- BooleanPredicate p = (BooleanPredicate) predicate.value;- return asPredicate(p).test(assetState.getValueAsBoolean().orElse(null));+ BooleanPredicate p = (BooleanPredicate) predicate;+ return asPredicate(p).test(Values.getBoolean(value).orElse(null));- } else if (predicate.value instanceof NumberPredicate) {+ } else if (predicate instanceof NumberPredicate) {++ NumberPredicate p = (NumberPredicate) predicate;+ return asPredicate(p).test(Values.getNumber(value).orElse(null));++ } else if (predicate instanceof DateTimePredicate) {++ DateTimePredicate p = (DateTimePredicate) predicate;+ return asPredicate(currentMillisProducer, p).test(Values.getNumber(value).map(Double::longValue).orElse(null));- NumberPredicate p = (NumberPredicate) predicate.value;- return asPredicate(p).test(assetState.getValueAsNumber().orElse(null));} else {// TODO Implement morethrow new UnsupportedOperationException(- "Restriction by attribute value not implemented in rules matching for " + predicate.value.getClass()+ "Restriction by attribute value not implemented in rules matching for " + predicate.getClass());}};}+ public static Predicate<Meta> asPredicate(Supplier<Long> currentMillisProducer, AttributeMetaPredicate predicate) {++ Predicate<MetaItem> metaItemPredicate = metaItem -> {+ if (predicate.itemNamePredicate != null) {+ if (!metaItem.getName().map(name -> asPredicate(predicate.itemNamePredicate).test(name)).orElse(false)) {+ return false;+ }+ }+ if (predicate.itemValuePredicate != null) {+ if (!metaItem.getValue().map(value -> asPredicate(currentMillisProducer, predicate.itemValuePredicate).test(value)).orElse(false)) {+ return false;+ }+ }+ return true;+ };++ return meta -> meta.stream().anyMatch(metaItemPredicate);+ }++ public static Pair<Long, Long> asFromAndTo(long currentMillis, DateTimePredicate dateTimePredicate) {++ Long from;+ Long to = null;++ try {+ if (TimeUtil.isTimeDuration(dateTimePredicate.value)) {+ from = currentMillis + TimeUtil.parseTimeDuration(dateTimePredicate.value);+ } else {+ from = new SimpleDateFormat(dateTimePredicate.dateFormat).parse(dateTimePredicate.value).getTime();+ }++ if (dateTimePredicate.operator == BaseAssetQuery.Operator.BETWEEN) {+ if (TimeUtil.isTimeDuration(dateTimePredicate.rangeValue)) {+ to = currentMillis + TimeUtil.parseTimeDuration(dateTimePredicate.rangeValue);+ } else {+ to = new SimpleDateFormat(dateTimePredicate.dateFormat).parse(dateTimePredicate.rangeValue).getTime();+ }+ }+ } catch (ParseException e) {+ from = null;+ to = null;+ }++ return new Pair<>(from, to);+ }}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesClock.java b/manager/src/main/java/org/openremote/manager/rules/RulesClock.javaindex b10bcd88b..403c6f026 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesClock.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesClock.java@@ -61,7 +61,7 @@ public class RulesClock {}public <T extends TemporaryFact> Predicate<T> last(String timeWindow) {- double timeWindowStart = getTimestamp() - TimeUtil.parseTimeString(timeWindow);+ double timeWindowStart = getTimestamp() - TimeUtil.parseTimeDuration(timeWindow);return fact -> timeWindowStart <= fact.getTimestamp();}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.javaindex f1ed5ae0c..3e7c8bf5d 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java@@ -124,7 +124,7 @@ public class RulesEngine<T extends Ruleset> {this.notificationFacade = new NotificationsFacade<>(id, notificationService);this.assetLocationPredicatesConsumer = assetLocationPredicatesConsumer;- this.facts = new RulesFacts(assetsFacade, this, RULES_LOG);+ this.facts = new RulesFacts(timerService, assetsFacade, this, RULES_LOG);engine = new InferenceRulesEngine(// Skip any other rules after the first failed rule (exception thrown in condition or action)new RulesEngineParameters(false, true, false, RulesEngineParameters.DEFAULT_RULE_PRIORITY_THRESHOLD)@@ -212,7 +212,7 @@ public class RulesEngine<T extends Ruleset> {updateDeploymentInfo();}- deployment = new RulesetDeployment(ruleset);+ deployment = new RulesetDeployment(ruleset, timerService, assetStorageService);boolean compilationSuccessful = deployment.registerRules(ruleset, assetsFacade, usersFacade, notificationFacade);diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.javaindex d2c168c36..bf447b219 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java@@ -22,6 +22,7 @@ package org.openremote.manager.rules;import org.jeasy.rules.api.Facts;import org.jeasy.rules.api.Rule;import org.jeasy.rules.api.RuleListener;+import org.openremote.container.timer.TimerService;import org.openremote.model.attribute.AttributeType;import org.openremote.model.query.AssetQuery;import org.openremote.model.attribute.AttributeEvent;@@ -56,6 +57,7 @@ public class RulesFacts extends Facts implements RuleListener {public static final String EXECUTION_VARS = "INTERNAL_EXECUTION_VAR";public static final String ANONYMOUS_FACTS = "ANONYMOUS_FACTS";+ final protected TimerService timerService;final protected Assets assetsFacade;final protected Object loggingContext;final protected Logger LOG;@@ -69,7 +71,8 @@ public class RulesFacts extends Facts implements RuleListener {protected boolean trackLocationRules;protected Map<String, Set<LocationPredicate>> assetStateLocationPredicateMap = null;- public RulesFacts(Assets assetsFacade, Object loggingContext, Logger logger) {+ public RulesFacts(TimerService timerService, Assets assetsFacade, Object loggingContext, Logger logger) {+ this.timerService = timerService;this.assetsFacade = assetsFacade;this.loggingContext = loggingContext;this.LOG = logger;@@ -243,7 +246,7 @@ public class RulesFacts extends Facts implements RuleListener {}public RulesFacts insertAssetEvent(String expires, AssetState assetState) {- return insertAssetEvent(TimeUtil.parseTimeString(expires), assetState);+ return insertAssetEvent(TimeUtil.parseTimeDuration(expires), assetState);}public RulesFacts insertAssetEvent(long expiresMilliSeconds, AssetState assetState) {@@ -256,7 +259,7 @@ public class RulesFacts extends Facts implements RuleListener {}public RulesFacts putTemporary(String expires, Object value) {- return putTemporary(TimeUtil.parseTimeString(expires), value);+ return putTemporary(TimeUtil.parseTimeDuration(expires), value);}public RulesFacts putTemporary(Duration expires, Object value) {@@ -273,7 +276,7 @@ public class RulesFacts extends Facts implements RuleListener {}public RulesFacts putTemporary(String name, String expires, Object value) {- return putTemporary(name, TimeUtil.parseTimeString(expires), value);+ return putTemporary(name, TimeUtil.parseTimeDuration(expires), value);}public RulesFacts putTemporary(String name, double expires, Object value) {@@ -444,7 +447,7 @@ public class RulesFacts extends Facts implements RuleListener {}}- Predicate<AssetState> p = new AssetQueryPredicate(assetQuery);+ Predicate<AssetState> p = new AssetQueryPredicate(timerService, assetQuery);// Match against all asset states by defaultStream<AssetState> assetStates = getAssetStates().stream();@@ -473,7 +476,7 @@ public class RulesFacts extends Facts implements RuleListener {}public Stream<TemporaryFact<AssetState>> matchAssetEvent(AssetQuery assetQuery) {- Predicate<AssetState> p = new AssetQueryPredicate(assetQuery);+ Predicate<AssetState> p = new AssetQueryPredicate(timerService, assetQuery);return getAssetEvents().stream().parallel().filter(fact -> matchFact(fact, AssetState.class, p).isPresent());}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.javaindex 64a3abaea..cfaff9520 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java@@ -31,6 +31,8 @@ import org.jeasy.rules.api.Rules;import org.jeasy.rules.core.RuleBuilder;import org.kohsuke.groovy.sandbox.GroovyValueFilter;import org.kohsuke.groovy.sandbox.SandboxTransformer;+import org.openremote.container.timer.TimerService;+import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.rules.facade.NotificationsFacade;import org.openremote.model.rules.Assets;import org.openremote.model.rules.Ruleset;@@ -112,11 +114,15 @@ public class RulesetDeployment {final protected Ruleset ruleset;final protected Rules rules = new Rules();+ final protected AssetStorageService assetStorageService;+ final protected TimerService timerService;protected RulesetStatus status;protected Throwable error;- public RulesetDeployment(Ruleset ruleset) {+ public RulesetDeployment(Ruleset ruleset, TimerService timerService, AssetStorageService assetStorageService) {this.ruleset = ruleset;+ this.timerService = timerService;+ this.assetStorageService = assetStorageService;}public long getId() {diff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.javaindex f1f796428..6c859593d 100644--- a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java@@ -51,7 +51,6 @@ import java.util.*;import java.util.concurrent.ScheduledFuture;import java.util.concurrent.atomic.AtomicBoolean;import java.util.logging.Logger;-import java.util.stream.Collectors;import java.util.stream.IntStream;import static org.openremote.container.concurrent.GlobalLock.withLock;@@ -271,7 +270,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements GeofeIntStream.range(0, rows).forEach(i -> {final List<String> subIds = ids.subList(10 * i, Math.min(10 + (10 * i), ids.size()));- final Notification notification = new Notification("GeofenceRefresh", new PushNotificationMessage().setData(data), null);+ final Notification notification = new Notification("GeofenceRefresh", new PushNotificationMessage().setData(data), null, null, null);notification.setTargets(new Notification.Targets(Notification.TargetType.ASSET, subIds));executorService.schedule(() -> {diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex b1da2272f..4361c231d 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -128,6 +128,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imsb.append(buildSelectString(userQuery));sb.append(buildFromString(userQuery));sb.append(buildWhereString(userQuery, parameters));+ sb.append(buildLimit(userQuery));return persistenceService.doReturningTransaction(entityManager -> {TypedQuery<User> query = entityManager.createQuery(sb.toString(), User.class);@@ -137,7 +138,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im});}-protected String buildSelectString(UserQuery query) {return "SELECT DISTINCT(u)";}@@ -181,6 +181,13 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imreturn sb.toString();}+ protected String buildLimit(UserQuery userQuery) {+ if (userQuery.limit > 0) {+ return " LIMIT " + userQuery.limit;+ }+ return "";+ }+@Overridepublic User getUser(ClientRequestInfo clientRequestInfo, String realm, String userId) {return convertUser(diff --git a/manager/src/main/resources/demo/rules/DemoConsoleLocation.groovy b/manager/src/main/resources/demo/rules/DemoConsoleLocation.groovyindex ee063b94b..2d5b6212f 100644--- a/manager/src/main/resources/demo/rules/DemoConsoleLocation.groovy+++ b/manager/src/main/resources/demo/rules/DemoConsoleLocation.groovy@@ -44,9 +44,9 @@ rules.add()List<String> consoleIds = facts.bound("consoleIds")Notification notification = new Notification(- "Welcome Home",- new PushNotificationMessage("Welcome Home", "No new events to report", null, null, null),- new Notification.Targets(Notification.TargetType.ASSET, consoleIds))+ "Welcome Home",+ new PushNotificationMessage("Welcome Home", "No new events to report", null, null, null),+ new Notification.Targets(Notification.TargetType.ASSET, consoleIds), null, null)notifications.send(notification)diff --git a/manager/src/main/resources/demo/rules/DemoResidenceNotifyAlarmTrigger.groovy b/manager/src/main/resources/demo/rules/DemoResidenceNotifyAlarmTrigger.groovyindex d0d82a305..6de41b7eb 100644--- a/manager/src/main/resources/demo/rules/DemoResidenceNotifyAlarmTrigger.groovy+++ b/manager/src/main/resources/demo/rules/DemoResidenceNotifyAlarmTrigger.groovy@@ -157,15 +157,15 @@ rules.add()LOG.info("Alerting users of " + alarmTrigger + ": " + userIds)Notification notification = new Notification(- "ApartmentAlarm",- new PushNotificationMessage()- .setTitle("Apartment Alarm")- .setBody("Aanwezigheid in " + alarmTrigger.roomName + " (" + facts.clock.time + ").")- .setButtons([- new PushNotificationButton("Details", new PushNotificationAction("#security")),- new PushNotificationButton("Alarm uit", PushNotificationAction.writeAttributeValueAction(new AttributeRef(alarmTrigger.residenceId, "alarmEnabled"), Values.create(false)))+ "ApartmentAlarm",+ new PushNotificationMessage()+ .setTitle("Apartment Alarm")+ .setBody("Aanwezigheid in " + alarmTrigger.roomName + " (" + facts.clock.time + ").")+ .setButtons([+ new PushNotificationButton("Details", new PushNotificationAction("#security")),+ new PushNotificationButton("Alarm uit", PushNotificationAction.writeAttributeValueAction(new AttributeRef(alarmTrigger.residenceId, "alarmEnabled"), Values.create(false)))]),- new Notification.Targets(Notification.TargetType.USER, userIds)+ new Notification.Targets(Notification.TargetType.USER, userIds), null, null)// Send the notification to all matched usersdiff --git a/model/src/main/java/org/openremote/model/notification/Notification.java b/model/src/main/java/org/openremote/model/notification/Notification.javaindex bef54a80d..c5eace290 100644--- a/model/src/main/java/org/openremote/model/notification/Notification.java+++ b/model/src/main/java/org/openremote/model/notification/Notification.java@@ -88,10 +88,12 @@ public class Notification {protected Targets targets;@JsonCreator- public Notification(@JsonProperty("name") String name, @JsonProperty("message") AbstractNotificationMessage message, @JsonProperty("targets") Targets targets) {+ public Notification(@JsonProperty("name") String name, @JsonProperty("message") AbstractNotificationMessage message, @JsonProperty("targets") Targets targets, @JsonProperty("repeatFrequency") RepeatFrequency repeatFrequency, @JsonProperty("repeatInterval") String repeatInterval) {this.name = name;this.message = message;this.targets = targets;+ this.repeatFrequency = repeatFrequency;+ this.repeatInterval = repeatInterval;}public String getName() {diff --git a/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java b/model/src/main/java/org/openremote/model/query/BaseAssetQuery.javaindex 3b5db8f72..8d37c17bc 100644--- a/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java+++ b/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java@@ -232,6 +232,7 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {'}';}}+// Projectionpublic Select select;// Restriction predicates@@ -248,9 +249,12 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {public LocationPredicate location;// Orderingpublic OrderBy orderBy;+ public int limit;+protected BaseAssetQuery() {}+ // TODO: Remove thispublic static final ArrayValue queriesAsValue(BaseAssetQuery... queries) {ArrayValue arrayValue = Values.createArray();@@ -274,6 +278,7 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {return arrayValue;}+ // TODO: Remove thispublic static final AssetQuery objectValueAsQuery(ObjectValue objectValue) {AssetQuery assetQuery = new AssetQuery();diff --git a/model/src/main/java/org/openremote/model/query/UserQuery.java b/model/src/main/java/org/openremote/model/query/UserQuery.javaindex ce70ba029..c237766e1 100644--- a/model/src/main/java/org/openremote/model/query/UserQuery.java+++ b/model/src/main/java/org/openremote/model/query/UserQuery.java@@ -11,6 +11,7 @@ public class UserQuery<CHILD extends UserQuery<CHILD>> {public TenantPredicate tenantPredicate;public UserAssetPredicate assetPredicate;public PathPredicate pathPredicate;+ public int limit;public UserQuery() {}@@ -32,4 +33,10 @@ public class UserQuery<CHILD extends UserQuery<CHILD>> {this.pathPredicate = pathPredicate;return (CHILD) this;}++ @SuppressWarnings("unchecked")+ public CHILD limit(int limit) {+ this.limit = limit;+ return (CHILD) this;+ }}diff --git a/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java b/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.javaindex fd31d4865..d23a42c2c 100644--- a/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java@@ -19,7 +19,6 @@*/package org.openremote.model.query.filter;-import org.openremote.model.query.BaseAssetQuery;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Values;diff --git a/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java b/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.javaindex 76dcba4ce..b77dcc862 100644--- a/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java@@ -25,10 +25,10 @@ import org.openremote.model.value.Values;public class DateTimePredicate implements ValuePredicate<Long> {- public String value;- public String rangeValue; // Used as upper bound when Operator.BETWEEN+ public String value; // Sliding window value e.g. 1h or fixed date time+ public String rangeValue; // Sliding window value e.g. 1h or fixed date time (used as upper bound when Operator.BETWEEN)public BaseAssetQuery.Operator operator = BaseAssetQuery.Operator.EQUALS;- public String dateFormat = "YYYY-MM-DD HH24:MI:SS";//postgres dateformat+ public String dateFormat = "yyyy-MM-dd HH:mm:ss"; // SimpleDateFormatpublic DateTimePredicate() {}@@ -96,10 +96,10 @@ public class DateTimePredicate implements ValuePredicate<Long> {@Overridepublic String toString() {return getClass().getSimpleName() + "{" +- "value='" + value + '\'' +- ", rangeValue='" + rangeValue + '\'' +- ", operator =" + operator +- ", dateFormat='" + dateFormat + '\'' +- '}';+ "value='" + value + '\'' ++ ", rangeValue='" + rangeValue + '\'' ++ ", operator =" + operator ++ ", dateFormat='" + dateFormat + '\'' ++ '}';}}diff --git a/model/src/main/java/org/openremote/model/rules/AssetState.java b/model/src/main/java/org/openremote/model/rules/AssetState.javaindex df77d7340..b3bf57d9e 100644--- a/model/src/main/java/org/openremote/model/rules/AssetState.java+++ b/model/src/main/java/org/openremote/model/rules/AssetState.java@@ -27,6 +27,7 @@ import org.openremote.model.asset.AssetAttribute;import org.openremote.model.asset.AssetType;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeValueType;+import org.openremote.model.attribute.Meta;import org.openremote.model.value.ArrayValue;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Value;@@ -89,6 +90,8 @@ public class AssetState implements Comparable<AssetState> {final protected String tenantRealm;+ final protected Meta meta;+public AssetState(AssetState that) {this.attributeName = that.attributeName;this.attributeValueType = that.attributeValueType;@@ -110,6 +113,7 @@ public class AssetState implements Comparable<AssetState> {this.parentType = that.parentType;this.realmId = that.realmId;this.tenantRealm = that.tenantRealm;+ this.meta = that.meta;}public AssetState(Asset asset, AssetAttribute attribute, AttributeEvent.Source source) {@@ -133,6 +137,7 @@ public class AssetState implements Comparable<AssetState> {this.parentType = asset.getParentWellKnownType();this.realmId = asset.getRealmId();this.tenantRealm = asset.getTenantRealm();+ this.meta = attribute.getMeta();}public String getAttributeName() {@@ -215,6 +220,10 @@ public class AssetState implements Comparable<AssetState> {return tenantRealm;}+ public Meta getMeta() {+ return meta;+ }+public boolean isValueChanged() {return !getValue().equals(Optional.ofNullable(oldValue));}diff --git a/model/src/main/java/org/openremote/model/rules/Ruleset.java b/model/src/main/java/org/openremote/model/rules/Ruleset.javaindex cf3d3fd18..eca85261e 100644--- a/model/src/main/java/org/openremote/model/rules/Ruleset.java+++ b/model/src/main/java/org/openremote/model/rules/Ruleset.java@@ -19,14 +19,12 @@*/package org.openremote.model.rules;-import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.annotation.JsonSubTypes;import com.fasterxml.jackson.annotation.JsonTypeInfo;import javax.persistence.*;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size;-import java.util.Date;import java.util.Optional;@@ -100,8 +98,7 @@ public abstract class Ruleset {" facts.updateAssetState(\n" +" facts.bound(\"assetId\") as String, \"someAttribute\", \"bar\"\n" +" )\n" +- " })"- );+ " })");final String fileExtension;final String emptyRulesExample;diff --git a/model/src/main/java/org/openremote/model/util/TimeUtil.java b/model/src/main/java/org/openremote/model/util/TimeUtil.javaindex d1fbd93ed..79984d1d8 100644--- a/model/src/main/java/org/openremote/model/util/TimeUtil.java+++ b/model/src/main/java/org/openremote/model/util/TimeUtil.java@@ -51,11 +51,11 @@ public class TimeUtil {private static final long WK_MS = 7 * DAY_MS;/**- * Parses the given time String and returns the corresponding time in milliseconds+ * Parses the given time duration String and returns the corresponding number of milliseconds.** @throws NullPointerException if time is null*/- public static long parseTimeString(String time) {+ public static long parseTimeDuration(String time) {time = time.trim();if (time.length() > 0) {String trimmed = time.trim();@@ -82,10 +82,10 @@ public class TimeUtil {r = -r;}result = r;- } else if ("*".equals(trimmed) || "+*".equals(trimmed)) {+ } else if (isTimeDurationPositiveInfinity(trimmed)) {// positive infinityresult = Long.MAX_VALUE;- } else if ("-*".equals(trimmed)) {+ } else if (isTimeDurationNegativeInfinity(trimmed)) {// negative infinityresult = Long.MIN_VALUE;} else {@@ -96,4 +96,22 @@ public class TimeUtil {}throw new RuntimeException("Empty parameters not allowed in: [" + time + "]");}++ public static boolean isTimeDuration(String time) {+ time = time != null ? time.trim() : null;+ return time != null && time.length() > 0+ && (SIMPLE.matcher(time).matches()+ || isTimeDurationPositiveInfinity(time)+ || isTimeDurationNegativeInfinity(time));+ }++ public static boolean isTimeDurationPositiveInfinity(String time) {+ time = time != null ? time.trim() : null;+ return "*".equals(time) || "+*".equals(time);+ }++ public static boolean isTimeDurationNegativeInfinity(String time) {+ time = time != null ? time.trim() : null;+ return "-*".equals(time);+ }}diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex 81a2dabfc..624aedc7f 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -27,7 +27,6 @@ import spock.util.concurrent.PollingConditionsimport javax.ws.rs.WebApplicationExceptionimport java.time.Instant-import java.time.ZoneIdimport java.time.temporal.ChronoUnitimport java.util.concurrent.TimeUnit@@ -110,14 +109,14 @@ class NotificationTest extends Specification implements ManagerContainerTrait {).tokendef notification = new Notification("TestAction",- new PushNotificationMessage("Test Action",- "Click to cancel",- writeAttributeValueAction(- new AttributeRef(- managerDemoSetup.apartment1LivingroomId,- "alarmEnabled"- ),- Values.create(false)),null, null), null)+ new PushNotificationMessage("Test Action",+ "Click to cancel",+ writeAttributeValueAction(+ new AttributeRef(+ managerDemoSetup.apartment1LivingroomId,+ "alarmEnabled"+ ),+ Values.create(false)), null, null), null, null, null)and: "the notification resource"def testuser1NotificationResource = getClientTarget(serverUri(serverPort), MASTER_REALM, testuser1AccessToken).proxy(NotificationResource.class)@@ -569,7 +568,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {def notification = new Notification("Test",new EmailNotificationMessage().setSubject("Test").setText("Hello world!"),- new Notification.Targets(Notification.TargetType.TENANT, managerDemoSetup.customerARealmId))+ new Notification.Targets(Notification.TargetType.TENANT, managerDemoSetup.customerARealmId), null, null)notificationService.sendNotification(notification, Notification.Source.TENANT_RULESET, managerDemoSetup.customerARealmId)then: "the email should have been sent to all tenant users"diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesFactsTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesFactsTest.groovyindex ee71f381c..c36ce79b4 100644--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesFactsTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesFactsTest.groovy@@ -3,6 +3,7 @@ package org.openremote.test.rulesimport groovy.transform.EqualsAndHashCodeimport groovy.transform.ToStringimport org.openremote.container.Container+import org.openremote.container.timer.TimerServiceimport org.openremote.manager.rules.RulesClockimport org.openremote.manager.rules.RulesEngineimport org.openremote.manager.rules.RulesFacts@@ -49,7 +50,9 @@ class BasicRulesFactsTest extends Specification {def setup() {given: "some rule facts"assetsFacade = Mock(Assets)- rulesFacts = new RulesFacts(assetsFacade, this, RulesEngine.RULES_LOG)+ def timerService = new TimerService()+ timerService.clock = TimerService.Clock.PSEUDO+ rulesFacts = new RulesFacts(timerService, assetsFacade, this, RulesEngine.RULES_LOG)and: "a rules clock"def rulesClock = new RulesClock(0)