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
Added CalendarEvent attribute for time sensitive assets
Rich Turnerae3f183e1195
diff --git a/model/src/main/java/org/openremote/model/asset/CalendarEventConfiguration.java b/model/src/main/java/org/openremote/model/asset/CalendarEventConfiguration.javanew file mode 100644index 000000000..67d86802e--- /dev/null+++ b/model/src/main/java/org/openremote/model/asset/CalendarEventConfiguration.java@@ -0,0 +1,83 @@+/*+ * 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.asset;++import org.openremote.model.AbstractValueHolder;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeType;+import org.openremote.model.calendar.CalendarEvent;+import org.openremote.model.value.ObjectValue;+import org.openremote.model.value.Value;++import java.util.Optional;++import static org.openremote.model.Constants.NAMESPACE;++/**+ * An {@link Attribute} that can be added to an {@link Asset} to indicate that the asset is time sensitive. This is to+ * be used for informational purposes to; for example, allow console apps to only show Assets that are 'active' at a+ * particular time.+ * <p>+ * The attribute name must exactly match {@value CALENDAR_EVENT_ATTRIBUTE_NAME} and its value should be an+ * {@link ObjectValue} that represents a {@link CalendarEvent}.+ */+public final class CalendarEventConfiguration {++ public static final String CALENDAR_EVENT_ATTRIBUTE_NAME = "urnOpenremoteCalendarEvent";++ private CalendarEventConfiguration() {+ }++ public static boolean isCalendarEventConfiguration(AssetAttribute attribute) {+ return attribute != null+ && attribute.getName().map(CALENDAR_EVENT_ATTRIBUTE_NAME::equals).orElse(false);+ }++ public static Optional<CalendarEvent> getCalendarEvent(Asset asset) {+ if (asset == null) {+ return Optional.empty();+ }++ return asset.getAttribute(CALENDAR_EVENT_ATTRIBUTE_NAME)+ .flatMap(AbstractValueHolder::getValue)+ .flatMap(CalendarEventConfiguration::getCalendarEvent);+ }++ public static Optional<CalendarEvent> getCalendarEvent(AssetAttribute attribute) {+ if (!isCalendarEventConfiguration(attribute)) {+ return Optional.empty();+ }++ return attribute.getValue().flatMap(CalendarEventConfiguration::getCalendarEvent);+ }+++ public static Optional<CalendarEvent> getCalendarEvent(Value value) {+ if (value == null) {+ return Optional.empty();+ }++ return CalendarEvent.fromValue(value);+ }++ public static AssetAttribute toAttribute(CalendarEvent calendarEvent) {+ return new AssetAttribute(CALENDAR_EVENT_ATTRIBUTE_NAME, AttributeType.OBJECT, calendarEvent.toValue());+ }+}diff --git a/model/src/main/java/org/openremote/model/calendar/CalendarEvent.java b/model/src/main/java/org/openremote/model/calendar/CalendarEvent.javanew file mode 100644index 000000000..9bf07c7b4--- /dev/null+++ b/model/src/main/java/org/openremote/model/calendar/CalendarEvent.java@@ -0,0 +1,137 @@+/*+ * 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 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;++/**+ * Represents an event that occurs at a point in time with a duration and optional recurrence. This is inspired by the+ * following standards:+ * <ul>+ * <li><a href="https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/">iCalendar RFC-5545</a></li>+ * <li><a href="https://tools.ietf.org/id/draft-jenkins-jscalendar-01.html">JSCalendar</a></li>+ * </ul>+ * <p>+ * Example 1:+ * <blockquote><pre>{@code+{+ "start": 1517011200,+ "end": 1517014800+}+ * }</pre></blockquote>+ * Example 2 (Repeat every other week, 3 occurrences):+ * <blockquote><pre>{@code+{+ "start": 1517011200,+ "end": 1517014800,+ "recurrence": {+ "frequency": "WEEKLY",+ "interval": 2,+ "count": 3+ }+}+ * }</pre></blockquote>+ * Example 3 (Repeat every day until 1st Feb 2018):+ * <blockquote><pre>{@code+{+ "start": 1517011200,+ "end": 1517014800,+ "recurrence": {+ "frequency": "WEEKLY",+ "interval": 2,+ "until": 1517443200+ }+}+ * }</pre></blockquote>+ * <b>+ * <h1>NOTES</h1>+ * <ul>+ * <li>date and time values must be in Unix timestamp seconds</li>+ * </ul>+ * </b>+ */+public class CalendarEvent {+ protected Date start;+ protected Date end;+ protected RecurrenceRule recurrence;++ protected CalendarEvent() {+ }++ public CalendarEvent(Date start, Date end, RecurrenceRule recurrence) {+ this.start = start;+ this.end = end;+ this.recurrence = recurrence;+ }++ public CalendarEvent(Date start, Date end) {+ this.start = start;+ this.end = end;+ }++ public Date getStart() {+ return start;+ }++ public Date getEnd() {+ return end;+ }++ public RecurrenceRule getRecurrence() {+ return recurrence;+ }++ public static Optional<CalendarEvent> fromValue(Value value) {+ if (value == null || value.getType() != ValueType.OBJECT) {+ return Optional.empty();+ }++ 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));++ if (!start.isPresent() || !end.isPresent()) {+ return Optional.empty();+ }++ return Optional.of(+ new CalendarEvent(new Date(1000L*start.get()),+ new Date(1000L*end.get()), recurrence.orElse(null))+ );+ }++ public Value toValue() {+ ObjectValue objectValue = Values.createObject();+ objectValue.put("start", start.getTime() / 1000);+ objectValue.put("end", end.getTime() / 1000);+ if (recurrence != null) {+ objectValue.put("recurrence", recurrence.toValue());+ }++ 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.javanew file mode 100644index 000000000..4c97c4c5d--- /dev/null+++ b/model/src/main/java/org/openremote/model/calendar/RecurrenceRule.java@@ -0,0 +1,118 @@+/*+ * 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 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;++ protected RecurrenceRule() {+ }++ public RecurrenceRule(Frequency frequency) {+ this.frequency = frequency;+ }++ public RecurrenceRule(Frequency frequency, int interval, int count) {+ this.frequency = frequency;+ this.interval = interval;+ this.count = count;+ }++ public RecurrenceRule(Frequency frequency, int interval, Date until) {+ this.frequency = frequency;+ this.interval = interval;+ 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());+ rRule.interval = interval;+ rRule.count = count;+ until.ifPresent(l -> rRule.until = new Date(1000L * l));+ 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()/1000));+ }++ return objectValue;+ }+}