Skip to content

armgit / core

Public Imported from GitHub
0

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

Updated CalendarEvent to support full recurrence logic (now uses cal4j Recur model directly)

Rich Turner4b807134f2db
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index c127cba30..b5a0ca1d1 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -1836,7 +1836,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
.append(joinCounter)
.append(".VALUE #>> '{value,end}')::float / 1000) > ? OR jsonb_typeof(AX")
.append(joinCounter)
- .append(".VALUE #> '{value,recurrence}') = 'object'))");
+ .append(".VALUE #> '{value,recurrence}') = 'string'))");
binders.add((st) -> st.setTimestamp(pos, when));
binders.add((st) -> st.setTimestamp(pos+1, when));
} else {
diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
index 8ea1004e6..4f4ea27ea 100644
--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
@@ -21,14 +21,14 @@ package org.openremote.manager.rules;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
-import net.fortuna.ical4j.model.*;
+import net.fortuna.ical4j.model.DateList;
+import net.fortuna.ical4j.model.Recur;
import org.geotools.referencing.GeodeticCalculator;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.model.attribute.Meta;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.calendar.CalendarEvent;
-import org.openremote.model.calendar.RecurrenceRule;
import org.openremote.model.geo.GeoJSONPoint;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.AssetQuery.NumberType;
@@ -44,7 +44,6 @@ import org.openremote.model.value.Values;
import java.time.temporal.ChronoUnit;
import java.util.*;
-import java.util.Date;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -491,29 +490,15 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
return new Pair<>(calendarEvent.getStart().getTime(), calendarEvent.getEnd().getTime());
}
- if (calendarEvent.getRecurrence() == null) {
+ Recur recurrence = calendarEvent.getRecurrence();
+
+ if (recurrence == null) {
if (calendarEvent.getEnd().before(when)) {
return null;
}
return new Pair<>(calendarEvent.getStart().getTime(), calendarEvent.getEnd().getTime());
}
- RecurrenceRule recurrenceRule = calendarEvent.getRecurrence();
- Recur recurrence;
-
- if (recurrenceRule.getCount() != null) {
- recurrence = new Recur(recurrenceRule.getFrequency().name(), recurrenceRule.getCount());
- } else if (recurrenceRule.getUntil() != null) {
- recurrence = new Recur(recurrenceRule.getFrequency().name(),
- new net.fortuna.ical4j.model.Date(recurrenceRule.getUntil()));
- } else {
- recurrence = new Recur(recurrenceRule.getFrequency().name(), null);
- }
-
- if (recurrenceRule.getInterval() != null) {
- recurrence.setInterval(recurrenceRule.getInterval());
- }
-
long whenMillis = when.toInstant().minus(calendarEvent.getEnd().getTime() - calendarEvent.getStart().getTime(), ChronoUnit.MILLIS).toEpochMilli();
DateList matches = recurrence.getDates(new net.fortuna.ical4j.model.DateTime(calendarEvent.getStart()), new net.fortuna.ical4j.model.DateTime(whenMillis), new net.fortuna.ical4j.model.DateTime(Long.MAX_VALUE), net.fortuna.ical4j.model.parameter.Value.DATE_TIME, 2);
diff --git a/model/build.gradle b/model/build.gradle
index 1f1685ee9..603a562a7 100644
--- a/model/build.gradle
+++ b/model/build.gradle
@@ -25,9 +25,10 @@ dependencies {
compile "org.keycloak:keycloak-core:$keycloakVersion:sources"
compile "org.jeasy:easy-rules-core:$easyRulesVersion"
+ compile "org.mnode.ical4j:ical4j:$ical4jVersion"
}
jar {
from sourceSets.main.allJava
archivesBaseName = "openremote-${project.name}"
-}
\ No newline at end of file
+}
diff --git a/model/src/main/java/org/openremote/model/calendar/CalendarEvent.java b/model/src/main/java/org/openremote/model/calendar/CalendarEvent.java
index d6fb0dede..d95d7dbc6 100644
--- a/model/src/main/java/org/openremote/model/calendar/CalendarEvent.java
+++ b/model/src/main/java/org/openremote/model/calendar/CalendarEvent.java
@@ -20,7 +20,10 @@
package org.openremote.model.calendar;
import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gwt.core.shared.GwtIncompatible;
+import net.fortuna.ical4j.model.Recur;
import org.openremote.model.asset.Asset;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
@@ -69,21 +72,30 @@ import java.util.Optional;
{
"start": 1441177200000,
"ends": 1441206000000,
- "recurrence": {
- "frequency": "DAILY",
- "interval": 1,
- "until": 1517443200000
- }
+ "recurrence": "RRULE:FREQ=DAILY;INTERVAL=2;COUNT=4"
}
* }</pre></blockquote>
*/
public class CalendarEvent {
protected Date start;
protected Date end;
- protected RecurrenceRule recurrence;
+ @JsonIgnore
+ protected Recur recurrence;
@JsonCreator
- public CalendarEvent(@JsonProperty("start") Date start, @JsonProperty("end") Date end, @JsonProperty("recurrence") RecurrenceRule recurrence) {
+ public CalendarEvent(@JsonProperty("start") Date start, @JsonProperty("end") Date end, @JsonProperty("recurrence") String recurrence) {
+ Recur recur = null;
+
+ try {
+ recur = new Recur(recurrence);
+ } catch (Exception e) {}
+
+ this.start = start;
+ this.end = end;
+ this.recurrence = recur;
+ }
+
+ public CalendarEvent(Date start, Date end, Recur recurrence) {
this.start = start;
this.end = end;
this.recurrence = recurrence;
@@ -97,10 +109,18 @@ public class CalendarEvent {
return end;
}
- public RecurrenceRule getRecurrence() {
+ @JsonIgnore
+ public Recur getRecurrence() {
return recurrence;
}
+ // TODO: Remove once GWT removed
+ @JsonProperty("recurrence")
+ protected String getRecurrenceInternal() {
+ return recurrence != null ? recurrence.toString() : null;
+ }
+
+ @GwtIncompatible
public static Optional<CalendarEvent> fromValue(Value value) {
if (value == null || value.getType() != ValueType.OBJECT) {
return Optional.empty();
@@ -109,12 +129,19 @@ public class CalendarEvent {
ObjectValue objectValue = (ObjectValue) value;
Optional<Long> start = objectValue.get("start").flatMap(Values::getLongCoerced);
Optional<Long> end = objectValue.get("end").flatMap(Values::getLongCoerced);
- Optional<RecurrenceRule> recurrence = RecurrenceRule.fromValue(objectValue.getObject("recurrence").orElse(null));
+ Optional<Recur> recurrence = objectValue.get("recurrence").flatMap(Values::getString).map(str -> {
+ try {
+ return new Recur(str);
+ } catch (Exception e) {
+ return null;
+ }
+ });
return start.map(aLong -> new CalendarEvent(new Date(aLong),
end.map(Date::new).orElse(null), recurrence.orElse(null)));
}
+ @GwtIncompatible
public Value toValue() {
ObjectValue objectValue = Values.createObject();
objectValue.put("start", start.getTime());
@@ -122,7 +149,7 @@ public class CalendarEvent {
objectValue.put("end", end.getTime());
}
if (recurrence != null) {
- objectValue.put("recurrence", recurrence.toValue());
+ objectValue.put("recurrence", recurrence.toString());
}
return objectValue;
diff --git a/model/src/main/java/org/openremote/model/calendar/RecurrenceRule.java b/model/src/main/java/org/openremote/model/calendar/RecurrenceRule.java
deleted file mode 100644
index efc6fb115..000000000
--- a/model/src/main/java/org/openremote/model/calendar/RecurrenceRule.java
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * Copyright 2017, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.model.calendar;
-
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import org.openremote.model.util.EnumUtil;
-import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.Value;
-import org.openremote.model.value.ValueType;
-import org.openremote.model.value.Values;
-
-import java.util.Date;
-import java.util.Optional;
-
-public class RecurrenceRule {
-
- public enum Frequency {
- YEARLY,
- MONTHLY,
- WEEKLY,
- DAILY
- }
-
- protected Frequency frequency;
- protected Integer interval;
- protected Integer count;
- protected Date until;
-
- @JsonCreator
- public RecurrenceRule(@JsonProperty("frequency") Frequency frequency, @JsonProperty("interval") Integer interval, @JsonProperty("count") Integer count, @JsonProperty("until") Date until) {
- this.frequency = frequency;
- this.interval = interval;
- this.count = count;
- this.until = until;
- }
-
- public Frequency getFrequency() {
- return frequency;
- }
-
- public Integer getInterval() {
- return interval;
- }
-
- public Integer getCount() {
- return count;
- }
-
- public Date getUntil() {
- return until;
- }
-
- public static Optional<RecurrenceRule> fromValue(Value value) {
- if (value == null || value.getType() != ValueType.OBJECT) {
- return Optional.empty();
- }
-
- ObjectValue objectValue = (ObjectValue) value;
- Optional<Frequency> frequency = objectValue.getString("frequency")
- .flatMap(s -> EnumUtil.enumFromString(Frequency.class, s));
- Integer interval = objectValue.get("interval").flatMap(Values::getIntegerCoerced).orElse(null);
- Integer count = objectValue.get("count").flatMap(Values::getIntegerCoerced).orElse(null);
- Optional<Long> until = objectValue.get("until").flatMap(Values::getLongCoerced);
-
- if (!frequency.isPresent()) {
- return Optional.empty();
- }
-
- RecurrenceRule rRule = new RecurrenceRule(frequency.get(), interval, count, until.map(Date::new).orElse(null));
- return Optional.of(rRule);
- }
-
- public Value toValue() {
- ObjectValue objectValue = Values.createObject();
- objectValue.put("frequency", Values.create(frequency.name()));
- if (interval != null) {
- objectValue.put("interval", Values.create(interval));
- }
- if (count != null) {
- objectValue.put("count", Values.create(count));
- }
- if (until != null) {
- objectValue.put("until", Values.create(until.getTime()));
- }
-
- return objectValue;
- }
-}
diff --git a/model/src/main/java/org/openremote/model/rules/AssetRuleset.java b/model/src/main/java/org/openremote/model/rules/AssetRuleset.java
index a5dfc19fd..6ec8bd31b 100644
--- a/model/src/main/java/org/openremote/model/rules/AssetRuleset.java
+++ b/model/src/main/java/org/openremote/model/rules/AssetRuleset.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.rules;
+import com.google.gwt.core.shared.GwtIncompatible;
import org.hibernate.annotations.Formula;
import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.value.ObjectValue;
@@ -166,6 +167,7 @@ public class AssetRuleset extends Ruleset {
return this;
}
+ @GwtIncompatible
@Override
public AssetRuleset setValidity(CalendarEvent calendarEvent) {
super.setValidity(calendarEvent);
diff --git a/model/src/main/java/org/openremote/model/rules/GlobalRuleset.java b/model/src/main/java/org/openremote/model/rules/GlobalRuleset.java
index e98940fe6..258c0c524 100644
--- a/model/src/main/java/org/openremote/model/rules/GlobalRuleset.java
+++ b/model/src/main/java/org/openremote/model/rules/GlobalRuleset.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.rules;
+import com.google.gwt.core.shared.GwtIncompatible;
import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
@@ -127,6 +128,7 @@ public class GlobalRuleset extends Ruleset {
return this;
}
+ @GwtIncompatible
@Override
public GlobalRuleset setValidity(CalendarEvent calendarEvent) {
super.setValidity(calendarEvent);
diff --git a/model/src/main/java/org/openremote/model/rules/Ruleset.java b/model/src/main/java/org/openremote/model/rules/Ruleset.java
index a8ae17a7d..c9cf4b723 100644
--- a/model/src/main/java/org/openremote/model/rules/Ruleset.java
+++ b/model/src/main/java/org/openremote/model/rules/Ruleset.java
@@ -19,8 +19,10 @@
*/
package org.openremote.model.rules;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.google.gwt.core.shared.GwtIncompatible;
import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
@@ -359,10 +361,14 @@ public abstract class Ruleset {
return this;
}
+ @JsonIgnore
+ @GwtIncompatible
public CalendarEvent getValidity() {
return Values.getObject(meta).flatMap(objValue -> objValue.getObject(META_KEY_VALIDITY)).flatMap(CalendarEvent::fromValue).orElse(null);
}
+ @JsonIgnore
+ @GwtIncompatible
public Ruleset setValidity(CalendarEvent calendarEvent) {
if (meta == null) {
meta = Values.createObject();
diff --git a/model/src/main/java/org/openremote/model/rules/TenantRuleset.java b/model/src/main/java/org/openremote/model/rules/TenantRuleset.java
index cfa3dc80a..04a52ba5d 100644
--- a/model/src/main/java/org/openremote/model/rules/TenantRuleset.java
+++ b/model/src/main/java/org/openremote/model/rules/TenantRuleset.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.rules;
+import com.google.gwt.core.shared.GwtIncompatible;
import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
@@ -153,6 +154,7 @@ public class TenantRuleset extends Ruleset {
return this;
}
+ @GwtIncompatible
@Override
public TenantRuleset setValidity(CalendarEvent calendarEvent) {
super.setValidity(calendarEvent);
diff --git a/model/src/main/java/org/openremote/model/rules/json/JsonRule.java b/model/src/main/java/org/openremote/model/rules/json/JsonRule.java
index 584d65254..ca19e8c51 100644
--- a/model/src/main/java/org/openremote/model/rules/json/JsonRule.java
+++ b/model/src/main/java/org/openremote/model/rules/json/JsonRule.java
@@ -48,11 +48,6 @@ import org.openremote.model.query.LogicGroup;
* {@link JsonRule#when} but excluding the {@link AssetQuery#attributes} predicates and excluding any assets that match
* the entire {@link JsonRule#when}. If the number of these assets is greater than 0 then these {@link RuleAction}s will be
* executed.
- * <h2>Recurrence</h2>
- * <p>
- * {@link #recurrence} - Optional logic used to control when rule firing can recur.
- * <b>NOTE: Rule trigger history is not persisted so on system restart this information is lost and a rule will be
- * able to fire again.</b>
*/
public class JsonRule {
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
index 6cd3c1dbd..63199bd51 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
@@ -1,6 +1,6 @@
package org.openremote.test.assets
-
+import net.fortuna.ical4j.model.Recur
import org.openremote.container.persistence.PersistenceService
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
@@ -14,7 +14,6 @@ import org.openremote.model.attribute.AttributeType
import org.openremote.model.attribute.AttributeValueType
import org.openremote.model.attribute.MetaItemType
import org.openremote.model.calendar.CalendarEvent
-import org.openremote.model.calendar.RecurrenceRule
import org.openremote.model.geo.GeoJSONPoint
import org.openremote.model.query.AssetQuery
import org.openremote.model.query.AssetQuery.OrderBy
@@ -842,7 +841,8 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
def start = calendar.getTime()
calendar.add(Calendar.HOUR, 2)
def end = calendar.getTime()
- def recur = new RecurrenceRule(RecurrenceRule.Frequency.DAILY, 2, 5, null)
+ def recur = new Recur(Recur.DAILY, 5)
+ recur.setInterval(2)
lobby.addAttributes(
new AssetAttribute("test", AttributeValueType.CALENDAR_EVENT, new CalendarEvent(start, end, recur).toValue())
@@ -859,7 +859,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
new AssetQuery()
.select(new Select().excludePath(true).excludeAttributeMeta(true)) // Need attributes to do calendar filtering
.tenant(new TenantPredicate(keycloakTestSetup.masterTenant.realm))
- .attributes(new AttributePredicate(new StringPredicate("test"), new CalendarEventPredicate(new Date(1517155200000)))) // 30/01/2018 @ 6:00pm (UTC)
+ .attributes(new AttributePredicate(new StringPredicate("test"), new CalendarEventPredicate(new Date(1517155200000)))) // 28/01/2018 @ 4:00pm (UTC)
.orderBy(new OrderBy(NAME))
)
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 079c196d1..d3f4316a4 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
@@ -2,6 +2,7 @@ package org.openremote.test.rules.residence
import com.google.firebase.messaging.Message
+import net.fortuna.ical4j.model.Recur
import org.openremote.container.Container
import org.openremote.container.timer.TimerService
import org.openremote.manager.asset.AssetProcessingService
@@ -19,7 +20,6 @@ import org.openremote.manager.setup.builtin.KeycloakTestSetup
import org.openremote.manager.setup.builtin.ManagerTestSetup
import org.openremote.model.attribute.AttributeEvent
import org.openremote.model.calendar.CalendarEvent
-import org.openremote.model.calendar.RecurrenceRule
import org.openremote.model.console.ConsoleProvider
import org.openremote.model.console.ConsoleRegistration
import org.openremote.model.console.ConsoleResource
@@ -357,10 +357,12 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
version = ruleset.version
def validityStart = Instant.ofEpochMilli(getClockTimeOf(container)).plus(2000, ChronoUnit.MILLIS)
def validityEnd = Instant.ofEpochMilli(getClockTimeOf(container)).plus(4000, ChronoUnit.MILLIS)
+ def recur = new Recur(Recur.DAILY, 3)
+ recur.setInterval(2)
def calendarEvent = new CalendarEvent(
Date.from(validityStart),
Date.from(validityEnd),
- new RecurrenceRule(RecurrenceRule.Frequency.DAILY, 2, 3, null))
+ recur)
ruleset = rulesetStorageService.merge(ruleset.addMeta(Ruleset.META_KEY_VALIDITY,calendarEvent.toValue()))
then: "the ruleset should be redeployed and paused until 1st occurrence"
@@ -467,7 +469,9 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
RulesEngine.UNPAUSE_SCHEDULER = originalUnpause
ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS = originalDebounceMillis
TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- notificationService.notificationHandlerMap.put(emailNotificationHandler.getTypeName(), emailNotificationHandler)
- notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), pushNotificationHandler)
+ if (notificationService != null) {
+ notificationService.notificationHandlerMap.put(emailNotificationHandler.getTypeName(), emailNotificationHandler)
+ notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), pushNotificationHandler)
+ }
}
}