Skip to content

armgit / core

Public Imported from GitHub
0

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

Added ApplyPredictedDataPointsService (#2530)

Eric Bariaux802a34880dfc
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/ApplyPredictedDataPointsService.java b/manager/src/main/java/org/openremote/manager/datapoint/ApplyPredictedDataPointsService.java
new file mode 100644
index 000000000..a4afa1d90
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/datapoint/ApplyPredictedDataPointsService.java
@@ -0,0 +1,283 @@
+/*
+ * Copyright 2026, 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.manager.datapoint;
+
+import org.openremote.container.timer.TimerService;
+import org.openremote.manager.asset.AssetProcessingService;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.model.Container;
+import org.openremote.model.ContainerService;
+import org.openremote.model.asset.Asset;
+import org.openremote.model.attribute.Attribute;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.MetaMap;
+import org.openremote.model.datapoint.AssetPredictedDatapointEvent;
+import org.openremote.model.datapoint.ValueDatapoint;
+import org.openremote.model.query.AssetQuery;
+import org.openremote.model.query.LogicGroup;
+import org.openremote.model.query.filter.AttributePredicate;
+import org.openremote.model.query.filter.BooleanPredicate;
+import org.openremote.model.query.filter.NameValuePredicate;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+import static org.openremote.model.value.MetaItemType.APPLY_PREDICTED_DATA_POINTS;
+import static org.openremote.model.value.MetaItemType.HAS_PREDICTED_DATA_POINTS;
+
+/**
+ * Applies predicted datapoints as attribute values when timestamps match for attributes tagged with
+ * {@link org.openremote.model.value.MetaItemType#HAS_PREDICTED_DATA_POINTS} and
+ * {@link org.openremote.model.value.MetaItemType#APPLY_PREDICTED_DATA_POINTS}.
+ */
+public class ApplyPredictedDataPointsService implements ContainerService {
+
+ protected TimerService timerService;
+ protected AssetStorageService assetStorageService;
+ protected AssetPredictedDatapointService assetPredictedDatapointService;
+ protected AssetProcessingService assetProcessingService;
+ protected ClientEventService clientEventService;
+ protected ScheduledExecutorService scheduledExecutorService;
+
+ protected final Object scheduleLock = new Object();
+ protected final PriorityQueue<ScheduledEntry> scheduleQueue = new PriorityQueue<>(Comparator.comparingLong(e -> e.timestamp));
+ protected final Map<AttributeRef, ScheduledEntry> scheduledEntries = new HashMap<>();
+ protected ScheduledFuture<?> scheduledFuture;
+
+ @Override
+ public int getPriority() {
+ return ContainerService.DEFAULT_PRIORITY;
+ }
+
+ @Override
+ public void init(Container container) throws Exception {
+ timerService = container.getService(TimerService.class);
+ assetStorageService = container.getService(AssetStorageService.class);
+ assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class);
+ assetProcessingService = container.getService(AssetProcessingService.class);
+ clientEventService = container.getService(ClientEventService.class);
+ scheduledExecutorService = container.getScheduledExecutor();
+ }
+
+ @Override
+ public void start(Container container) throws Exception {
+ clientEventService.addSubscription(AssetPredictedDatapointEvent.class, null, this::onPredictedDatapointsChanged);
+ clientEventService.addSubscription(AttributeEvent.class, null, this::onAttributeEvent);
+
+ loadInitialAttributes();
+ scheduleNext();
+ }
+
+ @Override
+ public void stop(Container container) throws Exception {
+ if (scheduledFuture != null) {
+ scheduledFuture.cancel(true);
+ scheduledFuture = null;
+ }
+ synchronized (scheduleLock) {
+ scheduleQueue.clear();
+ scheduledEntries.clear();
+ }
+ }
+
+ protected void onPredictedDatapointsChanged(AssetPredictedDatapointEvent event) {
+ rescheduleAttribute(event.getRef());
+ }
+
+ protected void onAttributeEvent(AttributeEvent event) {
+ if (event.isDeleted()) {
+ removeScheduledEntry(event.getRef());
+ return;
+ }
+
+ MetaMap meta = event.getMeta();
+ if (meta != null && !hasRequiredMeta(meta)) {
+ removeScheduledEntry(event.getRef());
+ return;
+ }
+
+ rescheduleAttribute(event.getRef());
+ }
+
+ protected void processDueEntries() {
+ List<AttributeRef> dueRefs = new ArrayList<>();
+ long now = timerService.getCurrentTimeMillis();
+
+ synchronized (scheduleLock) {
+ while (!scheduleQueue.isEmpty()) {
+ ScheduledEntry entry = scheduleQueue.peek();
+ if (entry.timestamp > now) {
+ break;
+ }
+ scheduleQueue.poll();
+ scheduledEntries.remove(entry.ref);
+ dueRefs.add(entry.ref);
+ }
+ }
+
+ dueRefs.forEach(this::rescheduleAttribute);
+
+ scheduleNext();
+ }
+
+ /**
+ * Based on the next predicted data point to be applied, schedule the next execution of our processing.
+ */
+ protected void scheduleNext() {
+ long delayMillis = -1;
+ synchronized (scheduleLock) {
+ if (!scheduleQueue.isEmpty()) {
+ long now = timerService.getCurrentTimeMillis();
+ long nextTime = scheduleQueue.peek().timestamp;
+ delayMillis = Math.max(0, nextTime - now);
+ }
+ }
+
+ if (scheduledFuture != null) {
+ scheduledFuture.cancel(false);
+ scheduledFuture = null;
+ }
+
+ if (delayMillis >= 0) {
+ scheduledFuture = scheduledExecutorService.schedule(this::processDueEntries, delayMillis, TimeUnit.MILLISECONDS);
+ }
+ }
+
+ protected void loadInitialAttributes() {
+ LogicGroup<AttributePredicate> group = new LogicGroup<>(List.of(
+ new AttributePredicate().meta(new NameValuePredicate(HAS_PREDICTED_DATA_POINTS, new BooleanPredicate(true))),
+ new AttributePredicate().meta(new NameValuePredicate(APPLY_PREDICTED_DATA_POINTS, new BooleanPredicate(true)))
+ ));
+ group.operator = LogicGroup.Operator.AND;
+
+ List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().attributes(group));
+ assets.forEach(asset ->
+ asset.getAttributes()
+ .stream()
+ .filter(attr -> hasRequiredMeta(attr.getMeta())) // Required, need to ensure at attribute level that both MetaItem are present
+ .forEach(attr -> rescheduleValidatedAttribute(new AttributeRef(asset.getId(), attr.getName()), attr))
+ );
+ }
+
+ protected void rescheduleAttribute(AttributeRef ref) {
+ Attribute<?> attribute = getAttribute(ref);
+ if (attribute == null) {
+ removeScheduledEntry(ref);
+ return;
+ }
+
+ MetaMap meta = attribute.getMeta();
+ if (meta == null || !hasRequiredMeta(meta)) {
+ removeScheduledEntry(ref);
+ return;
+ }
+
+ rescheduleValidatedAttribute(ref, attribute);
+ }
+
+ protected void rescheduleValidatedAttribute(AttributeRef ref, Attribute<?> attribute) {
+ List<ValueDatapoint> datapoints = assetPredictedDatapointService.getDatapoints(ref);
+ if (datapoints == null || datapoints.isEmpty()) {
+ removeScheduledEntry(ref);
+ return;
+ }
+
+ long now = timerService.getCurrentTimeMillis();
+ ValueDatapoint<?> lastPast = null;
+ ValueDatapoint<?> nextFuture = null;
+
+ // We gather the most recent datapoint that is in the past as of now and the earliest one in the future
+ for (ValueDatapoint<?> datapoint : datapoints) {
+ long ts = datapoint.getTimestamp();
+ if (ts <= now) {
+ if (lastPast == null || ts > lastPast.getTimestamp()) {
+ lastPast = datapoint;
+ }
+ } else {
+ if (nextFuture == null || ts < nextFuture.getTimestamp()) {
+ nextFuture = datapoint;
+ }
+ }
+ }
+
+ long currentTimestamp = attribute.getTimestamp().orElse(0L);
+ // We apply the most recent past prediction unless the attribute was updated more recently
+ if (lastPast != null && currentTimestamp < lastPast.getTimestamp()) {
+ assetProcessingService.sendAttributeEvent(
+ new AttributeEvent(ref, lastPast.getValue(), lastPast.getTimestamp()),
+ getClass().getSimpleName()
+ );
+ }
+
+ // And schedule the next prediction to apply
+ if (nextFuture != null) {
+ upsertScheduledEntry(ref, nextFuture.getTimestamp(), nextFuture.getValue());
+ } else {
+ removeScheduledEntry(ref);
+ }
+ }
+
+ protected Attribute<?> getAttribute(AttributeRef ref) {
+ Asset<?> asset = assetStorageService.find(ref.getId(), true);
+ if (asset == null) {
+ return null;
+ }
+ return asset.getAttribute(ref.getName()).orElse(null);
+ }
+
+ protected boolean hasRequiredMeta(MetaMap meta) {
+ return meta != null && meta.has(HAS_PREDICTED_DATA_POINTS) && meta.has(APPLY_PREDICTED_DATA_POINTS);
+ }
+
+ protected void upsertScheduledEntry(AttributeRef ref, long timestamp, Object value) {
+ synchronized (scheduleLock) {
+ ScheduledEntry existing = scheduledEntries.remove(ref);
+ if (existing != null) {
+ scheduleQueue.remove(existing);
+ }
+ ScheduledEntry entry = new ScheduledEntry(ref, timestamp, value);
+ scheduledEntries.put(ref, entry);
+ scheduleQueue.add(entry);
+ }
+ scheduleNext();
+ }
+
+ protected void removeScheduledEntry(AttributeRef ref) {
+ synchronized (scheduleLock) {
+ ScheduledEntry existing = scheduledEntries.remove(ref);
+ if (existing != null) {
+ scheduleQueue.remove(existing);
+ }
+ }
+ scheduleNext();
+ }
+
+ protected record ScheduledEntry(AttributeRef ref, long timestamp, Object value) {
+ }
+}
diff --git a/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService b/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService
index 288f25105..47f7117a5 100644
--- a/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService
+++ b/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService
@@ -22,6 +22,7 @@ org.openremote.manager.notification.PushNotificationHandler
org.openremote.manager.notification.EmailNotificationHandler
org.openremote.manager.notification.LocalizedNotificationHandler
org.openremote.manager.datapoint.AssetPredictedDatapointService
+org.openremote.manager.datapoint.ApplyPredictedDataPointsService
org.openremote.manager.gateway.GatewayService
org.openremote.manager.gateway.GatewayClientService
org.openremote.manager.mqtt.MQTTBrokerService
diff --git a/model/src/main/java/org/openremote/model/value/MetaItemType.java b/model/src/main/java/org/openremote/model/value/MetaItemType.java
index 6bbfbf607..a7ab01295 100644
--- a/model/src/main/java/org/openremote/model/value/MetaItemType.java
+++ b/model/src/main/java/org/openremote/model/value/MetaItemType.java
@@ -102,6 +102,11 @@ public final class MetaItemType {
// TODO: Re-evaluate this can this info be retrieved automatically using prediction service
public static final MetaItemDescriptor<Boolean> HAS_PREDICTED_DATA_POINTS = new MetaItemDescriptor<>("hasPredictedDataPoints", ValueType.BOOLEAN);
+ /**
+ * Apply predicted data points as current attribute values when timestamps match.
+ */
+ public static final MetaItemDescriptor<Boolean> APPLY_PREDICTED_DATA_POINTS = new MetaItemDescriptor<>("applyPredictedDataPoints", ValueType.BOOLEAN);
+
/**
* The forecast service calculates predicted data points based on the forecast configuration.
*/
diff --git a/test/src/test/groovy/org/openremote/test/assets/ApplyPredictedDataPointsServiceTest.groovy b/test/src/test/groovy/org/openremote/test/assets/ApplyPredictedDataPointsServiceTest.groovy
new file mode 100644
index 000000000..d63a29c4b
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/assets/ApplyPredictedDataPointsServiceTest.groovy
@@ -0,0 +1,642 @@
+package org.openremote.test.assets
+
+import org.openremote.manager.asset.AssetProcessingService
+import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.datapoint.ApplyPredictedDataPointsService
+import org.openremote.manager.datapoint.AssetPredictedDatapointService
+import org.openremote.manager.event.ClientEventService
+import org.openremote.manager.setup.SetupService
+import org.openremote.model.attribute.AttributeEvent
+import org.openremote.model.attribute.AttributeRef
+import org.openremote.model.attribute.MetaItem
+import org.openremote.model.datapoint.ValueDatapoint
+import org.openremote.model.value.ValueType
+import org.openremote.setup.integration.ManagerTestSetup
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.ScheduledExecutorService
+import java.util.concurrent.ScheduledFuture
+import java.util.concurrent.TimeUnit
+import java.util.function.Consumer
+
+import static org.openremote.model.value.MetaItemType.APPLY_PREDICTED_DATA_POINTS
+import static org.openremote.model.value.MetaItemType.HAS_PREDICTED_DATA_POINTS
+
+class ApplyPredictedDataPointsServiceTest extends Specification implements ManagerContainerTrait {
+
+ ScheduledExecutorService executor = Mock(ScheduledExecutorService)
+ ScheduledFuture<?> future = Mock(ScheduledFuture)
+ Long delay
+
+ def "Applies predicted datapoints when timestamps match"() {
+ given: "a running container and target attribute with required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "the attribute is tagged for predicted datapoint application"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue1")
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+
+ when: "predicted datapoints are added in the future"
+ def predictedDatapoints = [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d)
+ ]
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ predictedDatapoints
+ )
+
+ and: "time advances to the first datapoint"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ future.get()
+ then: "the first predicted value should be applied"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 10d
+ assert attribute.getTimestamp().orElse(0L) == now + TimeUnit.MINUTES.toMillis(1)
+ }
+
+ when: "time advances to the next datapoint"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ future.get()
+
+ then: "the second predicted value should be applied"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 20d
+ assert attribute.getTimestamp().orElse(0L) == now + TimeUnit.MINUTES.toMillis(2)
+ }
+ }
+
+ def "Applies the last past datapoint when current value is older"() {
+ given: "a running container and target attribute with required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "the attribute has a value timestamp at the origin of test"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue2")
+ assetProcessingService.sendAttributeEvent(
+ new AttributeEvent(attributeRef, 1d, now)
+ )
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 1d
+ assert attribute.getTimestamp().orElse(0L) == now
+ }
+
+ and: "the attribute is tagged for predicted datapoint application"
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ assert asset.getAttribute(attributeRef.getName()).get().getMeta().has(HAS_PREDICTED_DATA_POINTS)
+ assert asset.getAttribute(attributeRef.getName()).get().getMeta().has(APPLY_PREDICTED_DATA_POINTS)
+ }
+
+ and: "the clock is moved forward"
+ advancePseudoClock(3, TimeUnit.MINUTES)
+ now = getClockTimeOf(container)
+
+ when: "predicted datapoints are added in the past"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now - TimeUnit.MINUTES.toMillis(1), 5d),
+ new ValueDatapoint<>(now - TimeUnit.SECONDS.toMillis(10), 7d)
+ ]
+ )
+
+ then: "the last predicted value should be applied immediately"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 7d
+ assert attribute.getTimestamp().orElse(0L) == now - TimeUnit.SECONDS.toMillis(10)
+ }
+ }
+
+ def "Skips past datapoints when current value is newer"() {
+ given: "a running container and target attribute with required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "the attribute has a newer value timestamp"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue3")
+ assetProcessingService.sendAttributeEvent(
+ new AttributeEvent(attributeRef, 99d, now)
+ )
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 99d
+ assert attribute.getTimestamp().orElse(0L) == now
+ }
+
+ and: "the attribute is tagged for predicted datapoint application"
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ assert asset.getAttribute(attributeRef.getName()).get().getMeta().has(HAS_PREDICTED_DATA_POINTS)
+ assert asset.getAttribute(attributeRef.getName()).get().getMeta().has(APPLY_PREDICTED_DATA_POINTS)
+ }
+
+ when: "predicted datapoints are added in the past"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now - TimeUnit.MINUTES.toMillis(1), 5d),
+ new ValueDatapoint<>(now - TimeUnit.SECONDS.toMillis(10), 7d)
+ ]
+ )
+
+ then: "the current attribute value should remain unchanged"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 99d
+ assert attribute.getTimestamp().orElse(0L) == now + 1 // +1 because of merge asset logic
+ }
+ }
+
+ def "Does not apply values when required meta is missing"() {
+ given: "a running container and target attribute without required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists without meta"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue4")
+
+ when: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d)
+ ]
+ )
+
+ and: "time advances beyond the datapoints"
+ advancePseudoClock(3, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "the attribute value should remain unchanged"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 0d
+ }
+ }
+
+ def "Does not apply values when only HAS_PREDICTED_DATA_POINTS is set"() {
+ given: "a running container and target attribute with only HAS_PREDICTED_DATA_POINTS"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists with only HAS_PREDICTED_DATA_POINTS"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue5")
+ addMeta(assetStorageService, attributeRef, new MetaItem<>(HAS_PREDICTED_DATA_POINTS, true))
+
+ when: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d)
+ ]
+ )
+
+ and: "time advances beyond the datapoints"
+ advancePseudoClock(3, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "the attribute value should remain unchanged"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 0d
+ }
+ }
+
+ def "Does not apply values when only APPLY_PREDICTED_DATA_POINTS is set"() {
+ given: "a running container and target attribute with only APPLY_PREDICTED_DATA_POINTS"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists with only APPLY_PREDICTED_DATA_POINTS"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue6")
+ addMeta(assetStorageService, attributeRef, new MetaItem<>(APPLY_PREDICTED_DATA_POINTS, true))
+
+ when: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d)
+ ]
+ )
+
+ and: "time advances beyond the datapoints"
+ advancePseudoClock(3, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "the attribute value should remain unchanged"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 0d
+ }
+ }
+
+ def "Applies values after adding required meta to existing attribute"() {
+ given: "a running container and target attribute without required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists without meta"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue7")
+
+ and: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d)
+ ]
+ )
+
+ when: "time advances to the first datapoint"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ and: "required meta is added"
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+
+ and: "time advances to the second datapoint"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+ then: "the second predicted value should be applied"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 20d
+ assert attribute.getTimestamp().orElse(0L) == now + TimeUnit.MINUTES.toMillis(2)
+ }
+ }
+
+ def "Does not apply values after removing required meta from attribute"() {
+ given: "a running container and target attribute with required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists with required meta"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue8")
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+
+ and: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d)
+ ]
+ )
+
+ when: "time advances to the first datapoint"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "the first predicted value should be applied"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 10d
+ assert attribute.getTimestamp().orElse(0L) == now + TimeUnit.MINUTES.toMillis(1)
+ }
+
+ when: "required meta is removed"
+ removeMeta(assetStorageService, attributeRef, HAS_PREDICTED_DATA_POINTS)
+ removeMeta(assetStorageService, attributeRef, APPLY_PREDICTED_DATA_POINTS)
+
+ and: "time advances beyond the datapoints"
+ advancePseudoClock(2, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "the attribute value should remain unchanged"
+ conditions.eventually {
+ def asset = assetStorageService.find(attributeRef.getId(), true)
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ assert attribute.getValue(Double.class).orElse(null) == 10d
+ }
+ }
+
+ def "Does not emit attribute events after attribute deletion"() {
+ given: "a running container and target attribute with required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def clientEventService = container.getService(ClientEventService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists with required meta"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue9")
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+
+ and: "we capture attribute events"
+ List<AttributeEvent> events = new CopyOnWriteArrayList<>()
+ Consumer<AttributeEvent> consumer = { event -> events.add(event) }
+ clientEventService.addSubscription(AttributeEvent.class, null, consumer)
+
+ and: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(3), 30d)
+ ]
+ )
+
+ when: "time advances to the middle of the predicted datapoints"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "an attribute event should have been emitted for the applied value"
+ conditions.eventually {
+ assert events.any { it.ref == attributeRef && it.value.orElse(null) == 10d }
+ }
+
+ when: "the attribute is deleted"
+ def asset = assetStorageService.find(attributeRef.getId())
+ asset.getAttributes().remove(attributeRef.getName())
+ assetStorageService.merge(asset)
+
+ then: "the deletion event is observed"
+ conditions.eventually {
+ assert events.any { it.ref == attributeRef && it.deleted }
+ }
+
+ when: "we clear events and advance time past all datapoints"
+ events.clear()
+ advancePseudoClock(3, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "no further attribute events should be emitted"
+ conditions.eventually {
+ assert events.isEmpty()
+ }
+ }
+
+ def "Does not emit attribute events after asset deletion"() {
+ given: "a running container and target attribute with required meta"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
+ def clientEventService = container.getService(ClientEventService.class)
+ def applyService = container.getService(ApplyPredictedDataPointsService.class)
+
+ and: "the clock is stopped for deterministic scheduling"
+ stopPseudoClock()
+ def now = getClockTimeOf(container)
+
+ and: "the scheduled executor is mocked to allow manual triggering"
+ setupScheduler(applyService)
+
+ and: "a plain attribute exists with required meta"
+ def attributeRef = createTestAttribute(assetStorageService, managerTestSetup.thingId, "predictedValue10")
+ enablePredictedApplyMeta(assetStorageService, attributeRef.getId(), attributeRef.getName())
+
+ and: "we capture attribute events"
+ List<AttributeEvent> events = new CopyOnWriteArrayList<>()
+ Consumer<AttributeEvent> consumer = { event -> events.add(event) }
+ clientEventService.addSubscription(AttributeEvent.class, null, consumer)
+
+ and: "predicted datapoints are added in the future"
+ assetPredictedDatapointService.updateValues(
+ attributeRef.getId(),
+ attributeRef.getName(),
+ [
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(1), 10d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(2), 20d),
+ new ValueDatapoint<>(now + TimeUnit.MINUTES.toMillis(3), 30d)
+ ]
+ )
+
+ when: "time advances to the middle of the predicted datapoints"
+ advancePseudoClock(1, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "an attribute event should have been emitted for the applied value"
+ conditions.eventually {
+ assert events.any { it.ref == attributeRef && it.value.orElse(null) == 10d }
+ }
+
+ when: "the asset is deleted"
+ assetStorageService.delete(List.of(attributeRef.getId()))
+
+ then: "the deletion event is observed"
+ conditions.eventually {
+ assert events.any { it.ref == attributeRef && it.deleted }
+ }
+
+ when: "we clear events and advance time past all datapoints"
+ events.clear()
+ advancePseudoClock(3, TimeUnit.MINUTES, container)
+ if (future != null) {
+ future.get()
+ }
+
+ then: "no further attribute events should be emitted"
+ conditions.eventually {
+ assert events.isEmpty()
+ }
+ }
+
+ private static void enablePredictedApplyMeta(AssetStorageService assetStorageService, String assetId, String attributeName) {
+ def asset = assetStorageService.find(assetId)
+ def attribute = asset.getAttribute(attributeName).get()
+ attribute.addOrReplaceMeta(
+ new MetaItem<>(HAS_PREDICTED_DATA_POINTS, true),
+ new MetaItem<>(APPLY_PREDICTED_DATA_POINTS, true)
+ )
+ assetStorageService.merge(asset)
+ }
+
+ private static AttributeRef createTestAttribute(AssetStorageService assetStorageService, String assetId, String attributeName) {
+ def asset = assetStorageService.find(assetId)
+ asset.getAttributes().addOrReplace(
+ new org.openremote.model.attribute.Attribute<>(attributeName, ValueType.NUMBER, 0d)
+ )
+ assetStorageService.merge(asset)
+ return new AttributeRef(assetId, attributeName)
+ }
+
+ private static void addMeta(AssetStorageService assetStorageService, AttributeRef attributeRef, MetaItem<?> metaItem) {
+ def asset = assetStorageService.find(attributeRef.getId())
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ attribute.addOrReplaceMeta(metaItem)
+ assetStorageService.merge(asset)
+ }
+
+ private static void removeMeta(AssetStorageService assetStorageService, AttributeRef attributeRef, def metaType) {
+ def asset = assetStorageService.find(attributeRef.getId())
+ def attribute = asset.getAttribute(attributeRef.getName()).get()
+ attribute.getMeta().remove(metaType)
+ assetStorageService.merge(asset)
+ }
+
+ private void setupScheduler(ApplyPredictedDataPointsService applyService) {
+ executor = Mock(ScheduledExecutorService)
+ future = Mock(ScheduledFuture)
+ delay = null
+
+ executor.schedule(_ as Runnable, _ as Long, _ as TimeUnit) >> { args ->
+ delay = args[1] as Long
+ future = Mock(ScheduledFuture)
+ future.get() >> {
+ (args[0] as Runnable).run()
+ return true
+ }
+ return future
+ }
+
+ applyService.scheduledFuture?.cancel(true)
+ applyService.scheduledFuture = null
+ applyService.scheduledExecutorService = executor
+ }
+}
diff --git a/ui/test/fixtures/data.ts b/ui/test/fixtures/data.ts
index 3df9853b3..2291a4e94 100644
--- a/ui/test/fixtures/data.ts
+++ b/ui/test/fixtures/data.ts
@@ -58,6 +58,10 @@ export const metaItemDescriptors = {
name: "hasPredictedDataPoints",
type: "boolean",
},
+ applyPredictedDataPoints: {
+ name: "applyPredictedDataPoints",
+ type: "boolean",
+ },
dataPointsMaxAgeDays: {
name: "dataPointsMaxAgeDays",
type: "positiveInteger",