Skip to content

armgit / core

Public Imported from GitHub
0

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

Fixed a bug with simulator not getting the next datapoint

Michael Rademaker807fbc73531c
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.java
index 8ae770250..c3b8d04ec 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
@@ -553,26 +553,27 @@ public class SimulatorProtocol extends AbstractProtocol {
protected ScheduledFuture scheduleReplay(AttributeRef attributeRef, ReplaySimulatorElement replaySimulatorElement) {
LOG.fine("Scheduling value update");
- try {
- LocalDateTime now = LocalDateTime.now();
- ReplaySimulatorElement.ReplaySimulatorDatapoint nextDatapoint = replaySimulatorElement.getNextDatapoint(now.get(ChronoField.SECOND_OF_DAY));
- LocalDateTime nextRun = LocalDateTime.of(now.toLocalDate(), LocalTime.ofSecondOfDay(nextDatapoint.timestamp));
-
- if (now.isAfter(nextRun)) { //now is after so nextRun is next day
- nextRun = nextRun.plusDays(1);
+ 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(() -> {
+ 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;
}
- Duration duration = Duration.between(now, nextRun);
-
- LOG.info("Next update for asset " + attributeRef.getEntityId() + "for attribute " + attributeRef.getAttributeName() + " in " + duration.getSeconds() + " second(s)");
- return executorService.schedule(() -> {
- 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));
- }, duration.getSeconds(), 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/model/src/main/java/org/openremote/model/simulator/element/ReplaySimulatorElement.java b/model/src/main/java/org/openremote/model/simulator/element/ReplaySimulatorElement.java
index 3d1e87307..864e816a4 100644
--- a/model/src/main/java/org/openremote/model/simulator/element/ReplaySimulatorElement.java
+++ b/model/src/main/java/org/openremote/model/simulator/element/ReplaySimulatorElement.java
@@ -42,11 +42,14 @@ public class ReplaySimulatorElement extends SimulatorElement {
public ReplaySimulatorDatapoint getNextDatapoint(long millis) throws JsonProcessingException {
ReplaySimulatorDatapoint[] datapoints = new ObjectMapper().registerModule(new ModelModule()).readValue(elementValue.toJson(), ReplaySimulatorDatapoint[].class);
- return Arrays.stream(datapoints).filter(replaySimulatorDatapoint -> replaySimulatorDatapoint.timestamp >= millis).findFirst().orElse(datapoints[0]);
+ return Arrays.stream(datapoints)
+ .filter(replaySimulatorDatapoint -> replaySimulatorDatapoint.timestamp > millis)
+ .findFirst()
+ .orElse(datapoints[0]);
}
public ReplaySimulatorDatapoint[] getDatapoints() throws JsonProcessingException {
- return new ObjectMapper().readValue(elementValue.toJson(), ReplaySimulatorDatapoint[].class);
+ return new ObjectMapper().registerModule(new ModelModule()).readValue(elementValue.toJson(), ReplaySimulatorDatapoint[].class);
}
public static class ReplaySimulatorDatapoint {