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
Made PredictedAssetService available to Protocols Some improvements in SimulatorProtocol
Michael Rademaker8b56de30f5df
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.javaindex 898161b8a..43a96131e 100644--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java@@ -118,6 +118,7 @@ public abstract class AbstractProtocol implements Protocol {protected TimerService timerService;protected ProtocolExecutorService executorService;protected ProtocolAssetService assetService;+ protected ProtocolPredictedAssetService predictedAssetService;@Overridepublic int getPriority() {@@ -130,6 +131,7 @@ public abstract class AbstractProtocol implements Protocol {timerService = container.getService(TimerService.class);executorService = container.getService(ProtocolExecutorService.class);assetService = container.getService(ProtocolAssetService.class);+ predictedAssetService = container.getService(ProtocolPredictedAssetService.class);}@Overridediff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolPredictedAssetService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolPredictedAssetService.javanew file mode 100644index 000000000..23812a357--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolPredictedAssetService.java@@ -0,0 +1,31 @@+/*+ * 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.agent.protocol;++import org.openremote.container.ContainerService;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.value.Value;++public interface ProtocolPredictedAssetService extends ContainerService {++ void updateValue(AttributeRef attributeRef, Value value, long timestamp);++ void updateValue(String assetId, String attributeName, Value value, long timestamp);+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.javaindex fa67c8acc..a760c6ab6 100644--- a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java@@ -164,7 +164,7 @@ public class HttpClientProtocol extends AbstractProtocol {public String method;public MultivaluedMap<String, String> headers;public MultivaluedMap<String, String> queryParameters;- protected String path;+ public String path;protected List<Integer> failureCodes;protected String contentType;protected WebTarget client;diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.javaindex 09cb97813..eaaf2e1f8 100644--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java@@ -37,9 +37,7 @@ import org.openremote.model.value.ArrayValue;import org.openremote.model.value.Value;import org.openremote.model.value.ValueType;-import java.time.Duration;import java.time.LocalDateTime;-import java.time.LocalTime;import java.time.temporal.ChronoField;import java.util.*;import java.util.concurrent.ScheduledFuture;@@ -553,27 +551,27 @@ public class SimulatorProtocol extends AbstractProtocol {protected ScheduledFuture scheduleReplay(AttributeRef attributeRef, ReplaySimulatorElement replaySimulatorElement) {LOG.fine("Scheduling value update");- return withLockReturning(getProtocolName() + "::scheduleReplay", () -> {- try {- long now = LocalDateTime.now().get(ChronoField.SECOND_OF_DAY);- ReplaySimulatorElement.ReplaySimulatorDatapoint nextDatapoint = replaySimulatorElement.getNextDatapoint(now);- long nextRun = nextDatapoint.timestamp;- if (nextRun <= now) { //now is after so nextRun is next day- nextRun += 86400;//day in seconds- }- long nextRunRelative = nextRun - now;-- LOG.info("Next update for asset " + attributeRef.getEntityId() + " for attribute " + attributeRef.getAttributeName() + " in " + nextRunRelative + " second(s)");- return executorService.schedule(() -> {+ try {+ long now = LocalDateTime.now().get(ChronoField.SECOND_OF_DAY);+ ReplaySimulatorElement.ReplaySimulatorDatapoint nextDatapoint = replaySimulatorElement.getNextDatapoint(now);+ long nextRun = nextDatapoint.timestamp;+ if (nextRun <= now) { //now is after so nextRun is next day+ nextRun += 86400;//day in seconds+ }+ long nextRunRelative = nextRun - now;++ LOG.info("Next update for asset " + attributeRef.getEntityId() + " for attribute " + attributeRef.getAttributeName() + " in " + nextRunRelative + " second(s)");+ return executorService.schedule(() -> {+ withLock(getProtocolName() + "::firingNextUpdate", () -> {LOG.info("Updating asset " + attributeRef.getEntityId() + "for attribute " + attributeRef.getAttributeName() + " with value " + nextDatapoint.value.toString());updateLinkedAttribute(new AttributeState(attributeRef, nextDatapoint.value));replayMap.put(attributeRef, scheduleReplay(attributeRef, replaySimulatorElement));- }, nextRunRelative, TimeUnit.SECONDS);- } catch (JsonProcessingException e) {- LOG.log(Level.SEVERE, "Exception thrown when scheduling value update: %s", e);- return null;- }- });+ });+ }, nextRunRelative, TimeUnit.SECONDS);+ } catch (JsonProcessingException e) {+ LOG.log(Level.SEVERE, "Exception thrown when scheduling value update: %s", e);+ return null;+ }}public static Optional<String> getElementType(AssetAttribute attribute) {diff --git a/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.java b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.javaindex 75f8de996..19f21ac1d 100644--- a/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.java@@ -21,6 +21,7 @@ package org.openremote.manager.predicted;import org.hibernate.Session;import org.hibernate.jdbc.AbstractReturningWork;+import org.openremote.agent.protocol.ProtocolPredictedAssetService;import org.openremote.container.Container;import org.openremote.container.ContainerService;import org.openremote.container.persistence.PersistenceService;@@ -47,7 +48,7 @@ import java.util.ArrayList;import java.util.List;import java.util.logging.Logger;-public class AssetPredictedDatapointService implements ContainerService {+public class AssetPredictedDatapointService implements ContainerService, ProtocolPredictedAssetService {private static final Logger LOG = Logger.getLogger(AssetPredictedDatapointService.class.getName());