Skip to content

armgit / core

Public Imported from GitHub
0

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

Added endpoint to upsert predicted datapoints (Closes #1129)

Rich Turner7352400a625b
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolDatapointService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolDatapointService.java
index 978279f47..edb5b54eb 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolDatapointService.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolDatapointService.java
@@ -20,7 +20,7 @@
package org.openremote.agent.protocol;
import org.openremote.model.ContainerService;
-import org.openremote.model.util.Pair;
+import org.openremote.model.datapoint.ValueDatapoint;
import java.time.LocalDateTime;
import java.util.List;
@@ -29,5 +29,7 @@ public interface ProtocolDatapointService extends ContainerService {
void upsertValue(String assetId, String attributeName, Object value, LocalDateTime timestamp) throws IllegalStateException;
- void upsertValues(String assetId, String attributeName, List<Pair<?, LocalDateTime>> valuesAndTimestamps) throws IllegalStateException;
+ void upsertValue(String assetId, String attributeName, Object value, long timestamp) throws IllegalStateException;
+
+ void upsertValues(String assetId, String attributeName, List<ValueDatapoint<?>> valuesAndTimestamps) throws IllegalStateException;
}
diff --git a/gradle.properties b/gradle.properties
index c05879140..99e4863c3 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -8,7 +8,7 @@ javaxELVersion = 4.0.2
slf4jVersion = 2.0.6
guavaVersion = 31.1-jre
undertowVersion = 2.3.4.Final
-resteasyVersion = 6.2.2.Final
+resteasyVersion = 6.2.6.Final
jbossLoggingAnnotationsVersion = 2.0.2.Final
keycloakVersion = 18.0.2
classgraphVersion=4.8.149
diff --git a/manager/src/main/java/org/openremote/manager/asset/ForecastService.java b/manager/src/main/java/org/openremote/manager/asset/ForecastService.java
index bcb2e704c..4d619d324 100644
--- a/manager/src/main/java/org/openremote/manager/asset/ForecastService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/ForecastService.java
@@ -35,13 +35,12 @@ import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeMap;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.datapoint.AssetDatapoint;
-import org.openremote.model.datapoint.AssetPredictedDatapoint;
import org.openremote.model.datapoint.Datapoint;
+import org.openremote.model.datapoint.ValueDatapoint;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.filter.AttributePredicate;
import org.openremote.model.query.filter.NameValuePredicate;
import org.openremote.model.query.filter.StringPredicate;
-import org.openremote.model.util.Pair;
import org.openremote.model.value.ForecastConfiguration;
import org.openremote.model.value.ForecastConfigurationWeightedExponentialAverage;
import org.openremote.model.value.MetaItemType;
@@ -49,9 +48,6 @@ import org.openremote.model.value.MetaItemType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Duration;
-import java.time.Instant;
-import java.time.LocalDateTime;
-import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
@@ -362,6 +358,7 @@ public class ForecastService extends RouteBuilder implements ContainerService {
}
}
+ @SuppressWarnings("OptionalGetWithoutIsPresent")
private void calculateForecasts() {
final long now = timerService.getCurrentTimeMillis();
List<ForecastAttribute> attributesToCalculate = new ArrayList<>();
@@ -412,12 +409,12 @@ public class ForecastService extends RouteBuilder implements ContainerService {
}).toList();
if (forecastTimestamps.size() >= forecastValues.size()) {
- List<Pair<?, LocalDateTime>> datapoints = IntStream
+ List<ValueDatapoint<?>> datapoints = IntStream
.range(0, forecastValues.size())
.filter(i -> forecastValues.get(i).isPresent())
- .mapToObj(i -> new Pair<>(
- forecastValues.get(i).get(),
- LocalDateTime.ofInstant(Instant.ofEpochMilli(forecastTimestamps.get(i)), ZoneId.systemDefault()))
+ .mapToObj(i -> new ValueDatapoint<>(
+ forecastTimestamps.get(i),
+ forecastValues.get(i).get())
)
.collect(Collectors.toList());
@@ -732,10 +729,10 @@ public class ForecastService extends RouteBuilder implements ContainerService {
}
private List<Long> loadForecastTimestampsFromDb(AttributeRef attributeRef, long now) {
- List<AssetPredictedDatapoint> datapoints = assetPredictedDatapointService.getDatapoints(attributeRef);
+ List<ValueDatapoint> datapoints = assetPredictedDatapointService.getDatapoints(attributeRef);
List<Long> timestamps = datapoints
.stream()
- .map(datapoint -> datapoint.getTimestamp())
+ .map(ValueDatapoint::getTimestamp)
.filter(timestamp -> timestamp >= now)
.sorted()
.collect(Collectors.toList());;
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java
index 834aaf69a..980800ac3 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java
@@ -20,6 +20,8 @@
package org.openremote.manager.datapoint;
import com.fasterxml.jackson.databind.JsonNode;
+import jakarta.persistence.TypedQuery;
+import jakarta.validation.constraints.NotNull;
import org.hibernate.Session;
import org.hibernate.jdbc.AbstractReturningWork;
import org.openremote.container.persistence.PersistenceService;
@@ -34,17 +36,16 @@ import org.openremote.model.datapoint.Datapoint;
import org.openremote.model.datapoint.DatapointPeriod;
import org.openremote.model.datapoint.ValueDatapoint;
import org.openremote.model.datapoint.query.AssetDatapointQuery;
-import org.openremote.model.util.Pair;
import org.openremote.model.util.ValueUtil;
import org.postgresql.util.PGobject;
-import jakarta.persistence.TypedQuery;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
+import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
@@ -90,6 +91,9 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
}
public void upsertValue(String assetId, String attributeName, Object value, LocalDateTime timestamp) throws IllegalStateException {
+ upsertValue(assetId, attributeName, value, timestamp.toInstant(ZoneOffset.UTC).toEpochMilli());
+ }
+ public void upsertValue(String assetId, String attributeName, Object value, long timestamp) throws IllegalStateException {
persistenceService.doTransaction(em ->
em.unwrap(Session.class).doWork(connection -> {
@@ -108,7 +112,7 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
}));
}
- public void upsertValues(String assetId, String attributeName, List<Pair<?, LocalDateTime>> valuesAndTimestamps) throws IllegalStateException {
+ public void upsertValues(String assetId, String attributeName, List<ValueDatapoint<?>> valuesAndTimestamps) throws IllegalStateException {
persistenceService.doTransaction(em ->
em.unwrap(Session.class).doWork(connection -> {
@@ -118,8 +122,8 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
try {
st = getUpsertPreparedStatement(connection);
- for (Pair<?, LocalDateTime> valueAndTimestamp : valuesAndTimestamps) {
- setUpsertValues(st, assetId, attributeName, valueAndTimestamp.key, valueAndTimestamp.value);
+ for (ValueDatapoint<?> valueAndTimestamp : valuesAndTimestamps) {
+ setUpsertValues(st, assetId, attributeName, valueAndTimestamp.getValue(), valueAndTimestamp.getTimestamp());
st.addBatch();
}
st.executeBatch();
@@ -131,14 +135,14 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
}));
}
- public List<T> getDatapoints(AttributeRef attributeRef) {
+ public List<ValueDatapoint> getDatapoints(AttributeRef attributeRef) {
return persistenceService.doReturningTransaction(entityManager ->
entityManager.createQuery(
- "select dp from " + getDatapointClass().getSimpleName() + " dp " +
+ "select new org.openremote.model.datapoint.ValueDatapoint(dp.timestamp, dp.value) from " + getDatapointClass().getSimpleName() + " dp " +
"where dp.assetId = :assetId " +
"and dp.attributeName = :attributeName " +
"order by dp.timestamp desc",
- getDatapointClass())
+ ValueDatapoint.class)
.setParameter("assetId", attributeRef.getId())
.setParameter("attributeName", attributeRef.getName())
.getResultList());
@@ -169,7 +173,7 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
});
}
- public ValueDatapoint<?>[] queryDatapoints(String assetId, String attributeName, AssetDatapointQuery datapointQuery) {
+ public List<ValueDatapoint<?>> queryDatapoints(String assetId, String attributeName, AssetDatapointQuery datapointQuery) {
Asset<?> asset = assetStorageService.find(assetId, true);
if(asset == null) {
throw new IllegalStateException("Asset not found: " + assetId);
@@ -177,10 +181,10 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
Attribute<?> assetAttribute = asset.getAttribute(attributeName)
.orElseThrow(() -> new IllegalStateException("Attribute not found: " + attributeName));
- return this.queryDatapoints(asset.getId(), assetAttribute, datapointQuery);
+ return queryDatapoints(asset.getId(), assetAttribute, datapointQuery);
}
- public ValueDatapoint<?>[] queryDatapoints(String assetId, Attribute<?> attribute, AssetDatapointQuery datapointQuery) {
+ public List<ValueDatapoint<?>> queryDatapoints(String assetId, Attribute<?> attribute, @NotNull AssetDatapointQuery datapointQuery) {
AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
Map<Integer, Object> parameters = datapointQuery.getSQLParameters(attributeRef);
@@ -191,7 +195,7 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
entityManager.unwrap(Session.class).doReturningWork(new AbstractReturningWork<>() {
@Override
- public ValueDatapoint<?>[] execute(Connection connection) throws SQLException {
+ public List<ValueDatapoint<?>> execute(Connection connection) throws SQLException {
Class<?> attributeType = attribute.getTypeClass();
boolean isNumber = Number.class.isAssignableFrom(attributeType);
@@ -233,7 +237,7 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
}
result.add(new ValueDatapoint<>(rs.getTimestamp(1).getTime(), value));
}
- return result.toArray(new ValueDatapoint<?>[0]);
+ return result;
}
}
}
@@ -277,14 +281,14 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C
"SET value = excluded.value");
}
- protected void setUpsertValues(PreparedStatement st, String assetId, String attributeName, Object value, LocalDateTime timestamp) throws Exception {
+ protected void setUpsertValues(PreparedStatement st, String assetId, String attributeName, Object value, long timestamp) throws Exception {
PGobject pgJsonValue = new PGobject();
pgJsonValue.setType("jsonb");
pgJsonValue.setValue(ValueUtil.asJSON(value).orElse("null"));
st.setString(1, assetId);
st.setString(2, attributeName);
st.setObject(3, pgJsonValue);
- st.setObject(4, timestamp);
+ st.setObject(4, Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC).toLocalDateTime());
}
protected abstract Class<T> getDatapointClass();
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java
index 1d7674112..a28f36671 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java
@@ -20,6 +20,14 @@
package org.openremote.manager.datapoint;
import com.fasterxml.jackson.core.JsonProcessingException;
+import jakarta.ws.rs.BadRequestException;
+import jakarta.ws.rs.BeanParam;
+import jakarta.ws.rs.NotSupportedException;
+import jakarta.ws.rs.WebApplicationException;
+import jakarta.ws.rs.container.AsyncResponse;
+import jakarta.ws.rs.container.ConnectionCallback;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
@@ -29,25 +37,19 @@ import org.openremote.model.Constants;
import org.openremote.model.asset.Asset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeRef;
-import org.openremote.model.datapoint.query.AssetDatapointQuery;
import org.openremote.model.datapoint.AssetDatapointResource;
import org.openremote.model.datapoint.DatapointPeriod;
import org.openremote.model.datapoint.ValueDatapoint;
+import org.openremote.model.datapoint.query.AssetDatapointQuery;
import org.openremote.model.http.RequestParams;
import org.openremote.model.security.ClientRole;
import org.openremote.model.syslog.SyslogCategory;
-
-import jakarta.ws.rs.BadRequestException;
-import jakarta.ws.rs.BeanParam;
-import jakarta.ws.rs.NotSupportedException;
-import jakarta.ws.rs.WebApplicationException;
-import jakarta.ws.rs.container.AsyncResponse;
-import jakarta.ws.rs.container.ConnectionCallback;
-import jakarta.ws.rs.core.HttpHeaders;
-import jakarta.ws.rs.core.Response;
import org.openremote.model.value.MetaItemType;
-import java.io.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -61,7 +63,7 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements As
private static final Logger LOG = Logger.getLogger(AssetDatapointResourceImpl.class.getName());
private static final Logger DATA_EXPORT_LOG = SyslogCategory.getLogger(DATA, AssetDatapointResourceImpl.class);
-
+ private static final BlockingQueue<AsyncResponse> suspended = new ArrayBlockingQueue<>(5);
protected final AssetStorageService assetStorageService;
protected final AssetDatapointService assetDatapointService;
@@ -128,8 +130,11 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements As
throw new WebApplicationException(Response.Status.FORBIDDEN);
});
}
- return assetDatapointService.queryDatapoints(assetId, attribute, query);
+ if (query != null) {
+ return assetDatapointService.queryDatapoints(assetId, attribute, query).toArray(ValueDatapoint[]::new);
+ }
+ return assetDatapointService.getDatapoints(new AttributeRef(assetId, attributeName)).toArray(ValueDatapoint[]::new);
} catch (IllegalStateException ex) {
throw new BadRequestException(ex);
} catch (UnsupportedOperationException ex) {
@@ -168,7 +173,9 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements As
}
@Override
- public void getDatapointExport(AsyncResponse asyncResponse, String attributeRefsString, long fromTimestamp, long toTimestamp) {
+ public void getDatapointExport(String attributeRefsString, long fromTimestamp, long toTimestamp) throws InterruptedException {
+ final AsyncResponse asyncResponse = suspended.take();
+
try {
AttributeRef[] attributeRefs = JSON.readValue(attributeRefsString, AttributeRef[].class);
@@ -196,9 +203,8 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements As
DATA_EXPORT_LOG.info("User '" + getUsername() + "' started data export for " + attributeRefsString + " from " + fromTimestamp + " to " + toTimestamp);
ScheduledFuture<File> exportFuture = assetDatapointService.exportDatapoints(attributeRefs, fromTimestamp, toTimestamp);
- asyncResponse.register((ConnectionCallback) disconnected -> {
- exportFuture.cancel(true);
- });
+
+ asyncResponse.register((ConnectionCallback) disconnected -> exportFuture.cancel(true));
File exportFile = null;
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java
index c549fecff..286b58a88 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java
@@ -37,6 +37,7 @@ import jakarta.ws.rs.core.Response;
import org.openremote.model.security.ClientRole;
import org.openremote.model.value.MetaItemType;
+import java.util.Arrays;
import java.util.logging.Logger;
public class AssetPredictedDatapointResourceImpl extends ManagerWebResource implements AssetPredictedDatapointResource {
@@ -109,7 +110,65 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource impl
throw new WebApplicationException(Response.Status.FORBIDDEN);
});
}
- return assetPredictedDatapointService.queryDatapoints(assetId, attribute, query);
+ return assetPredictedDatapointService.queryDatapoints(assetId, attribute, query).toArray(ValueDatapoint[]::new);
+
+ } catch (IllegalStateException ex) {
+ throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);
+ }
+ }
+
+ @Override
+ public void writePredictedDatapoints(RequestParams requestParams, String assetId, String attributeName, ValueDatapoint<?>[] predictedDatapoints) {
+ try {
+
+ if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ Asset<?> asset = assetStorageService.find(assetId, true);
+
+ if (asset == null) {
+ throw new WebApplicationException(Response.Status.NOT_FOUND);
+ }
+
+ // Realm should be accessible
+ if(!isRealmActiveAndAccessible(asset.getRealm())) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ // If not logged in, asset should be PUBLIC READ
+ if(!isAuthenticated() && !asset.isAccessPublicRead()) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ // If logged in, user should have READ ASSETS role
+ if(isAuthenticated() && !hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
+ LOG.info("Forbidden access for user '" + getUsername() + "': " + asset.getRealm());
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ Attribute<?> attribute = asset.getAttribute(attributeName).orElseThrow(() ->
+ new WebApplicationException(Response.Status.NOT_FOUND)
+ );
+
+ // If restricted, the attribute should also be restricted
+ if(isRestrictedUser()) {
+ attribute.getMeta().getValue(MetaItemType.ACCESS_RESTRICTED_READ).ifPresentOrElse((v) -> {
+ if(!v) { throw new WebApplicationException(Response.Status.FORBIDDEN); }
+ }, () -> {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ });
+ }
+
+ // If not logged in, attribute should be PUBLIC READ
+ if(!isAuthenticated()) {
+ attribute.getMeta().getValue(MetaItemType.ACCESS_PUBLIC_READ).ifPresentOrElse((v) -> {
+ if(!v) { throw new WebApplicationException(Response.Status.FORBIDDEN); }
+ }, () -> {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ });
+ }
+ assetPredictedDatapointService.updateValues(assetId, attributeName, Arrays.asList(predictedDatapoints));
} catch (IllegalStateException ex) {
throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java
index 414059ad2..a5e6385c7 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java
@@ -27,7 +27,7 @@ import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.Container;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.datapoint.AssetPredictedDatapoint;
-import org.openremote.model.util.Pair;
+import org.openremote.model.datapoint.ValueDatapoint;
import java.time.Duration;
import java.time.Instant;
@@ -80,7 +80,7 @@ public class AssetPredictedDatapointService extends AbstractDatapointService<Ass
upsertValue(assetId, attributeName, value, timestamp);
}
- public void updateValues(String assetId, String attributeName, List<Pair<?, LocalDateTime>> valuesAndTimestamps) {
+ public void updateValues(String assetId, String attributeName, List<ValueDatapoint<?>> valuesAndTimestamps) {
persistenceService.doTransaction(em -> upsertValues(assetId, attributeName, valuesAndTimestamps));
}
diff --git a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java
index cd6485b9f..3b9ad30f6 100644
--- a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java
+++ b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java
@@ -44,7 +44,6 @@ 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.StringPredicate;
-import org.openremote.model.util.Pair;
import org.openremote.model.util.ValueUtil;
import org.openremote.model.value.MetaItemType;
@@ -729,9 +728,8 @@ public class EnergyOptimisationService extends RouteBuilder implements Container
}
// Push the setpoints into the prediction service for the storage asset's setpoint attribute and set current setpoint
- List<Pair<?, LocalDateTime>> valuesAndTimestamps = IntStream.range(1, setpoints.length).mapToObj(i ->
- new Pair<>(setpoints[i],
- LocalDateTime.ofInstant(optimisationTime.plus(periodSeconds * i, ChronoUnit.SECONDS), ZoneId.systemDefault()))
+ List<ValueDatapoint<?>> valuesAndTimestamps = IntStream.range(1, setpoints.length).mapToObj(i ->
+ new ValueDatapoint<>(optimisationTime.plus(periodSeconds * i, ChronoUnit.SECONDS).toEpochMilli(), setpoints[i])
).collect(Collectors.toList());
assetPredictedDatapointService.updateValues(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT.getName(), valuesAndTimestamps);
@@ -799,7 +797,7 @@ public class EnergyOptimisationService extends RouteBuilder implements Container
if (attribute.hasMeta(MetaItemType.HAS_PREDICTED_DATA_POINTS)) {
LocalDateTime timestamp = LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault());
- ValueDatapoint<?>[] predictedData = assetPredictedDatapointService.queryDatapoints(
+ List<ValueDatapoint<?>> predictedData = assetPredictedDatapointService.queryDatapoints(
ref.getId(),
ref.getName(),
new AssetDatapointIntervalQuery(
@@ -810,13 +808,13 @@ public class EnergyOptimisationService extends RouteBuilder implements Container
true
)
);
- if (predictedData.length != values.length) {
- LOG.warning("Returned predicted data point count does not match interval count: Ref=" + ref + ", expected=" + values.length + ", actual=" + predictedData.length);
+ if (predictedData.size() != values.length) {
+ LOG.warning("Returned predicted data point count does not match interval count: Ref=" + ref + ", expected=" + values.length + ", actual=" + predictedData.size());
} else {
- IntStream.range(0, predictedData.length).forEach(i -> {
- if (predictedData[i].getValue() != null) {
- values[i] = (double) (Object) predictedData[i].getValue();
+ IntStream.range(0, predictedData.size()).forEach(i -> {
+ if (predictedData.get(i).getValue() != null) {
+ values[i] = (double) (Object) predictedData.get(i).getValue();
} else {
// Average previous and next values to fill in gaps (goes up to 5 back and forward) - this fixes
// issues with resolution differences between stored predicted data and optimisation interval
@@ -824,12 +822,12 @@ public class EnergyOptimisationService extends RouteBuilder implements Container
Double next = null;
int j = i-1;
while (previous == null && j >= 0) {
- previous = (Double) predictedData[j].getValue();
+ previous = (Double) predictedData.get(j).getValue();
j--;
}
j = i+1;
- while (next == null && j < predictedData.length) {
- next = (Double) predictedData[j].getValue();
+ while (next == null && j < predictedData.size()) {
+ next = (Double) predictedData.get(j).getValue();
j++;
}
if (next == null) {
diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java
index c107ce7fc..167f848c9 100644
--- a/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java
+++ b/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java
@@ -42,6 +42,6 @@ public class HistoricFacade<T extends Ruleset> extends HistoricDatapoints {
@Override
public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, AssetDatapointQuery query) {
- return assetDatapointService.queryDatapoints(attributeRef.getId(), attributeRef.getName(), query);
+ return assetDatapointService.queryDatapoints(attributeRef.getId(), attributeRef.getName(), query).toArray(ValueDatapoint[]::new);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java
index ba75018f1..d1d87a709 100644
--- a/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java
+++ b/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java
@@ -42,7 +42,7 @@ public class PredictedFacade<T extends Ruleset> extends PredictedDatapoints {
@Override
public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, AssetDatapointQuery query) {
- return assetPredictedDatapointService.queryDatapoints(attributeRef.getId(), attributeRef.getName(), query);
+ return assetPredictedDatapointService.queryDatapoints(attributeRef.getId(), attributeRef.getName(), query).toArray(ValueDatapoint[]::new);
}
@Override
diff --git a/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java b/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java
index fbe8fd9b2..7cf6b539b 100644
--- a/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java
+++ b/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java
@@ -25,8 +25,6 @@ import org.openremote.model.datapoint.query.AssetDatapointQuery;
import org.openremote.model.http.RequestParams;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.*;
-import jakarta.ws.rs.container.AsyncResponse;
-import jakarta.ws.rs.container.Suspended;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
@@ -42,7 +40,7 @@ public interface AssetDatapointResource {
* not have datapoint storage enabled.
*/
@POST
- @Path("{assetId}/attribute/{attributeName}")
+ @Path("{assetId}/{attributeName}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
ValueDatapoint<?>[] getDatapoints(@BeanParam RequestParams requestParams,
@@ -62,9 +60,8 @@ public interface AssetDatapointResource {
@Path("export")
@Produces("application/zip")
@RolesAllowed({Constants.READ_ASSETS_ROLE})
- void getDatapointExport(@Suspended AsyncResponse asyncResponse,
- @QueryParam("attributeRefs") String attributeRefsString,
+ void getDatapointExport(@QueryParam("attributeRefs") String attributeRefsString,
@QueryParam("fromTimestamp") long fromTimestamp,
- @QueryParam("toTimestamp") long toTimestamp);
+ @QueryParam("toTimestamp") long toTimestamp) throws InterruptedException;
}
diff --git a/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java b/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java
index 390cc0b95..1cf719a0c 100644
--- a/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java
+++ b/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java
@@ -20,11 +20,9 @@
package org.openremote.model.datapoint;
import io.swagger.v3.oas.annotations.tags.Tag;
-import org.openremote.model.Constants;
+import jakarta.ws.rs.*;
import org.openremote.model.datapoint.query.AssetDatapointQuery;
import org.openremote.model.http.RequestParams;
-import jakarta.annotation.security.RolesAllowed;
-import jakarta.ws.rs.*;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
@@ -39,7 +37,7 @@ public interface AssetPredictedDatapointResource {
* not have datapoint storage enabled.
*/
@POST
- @Path("{assetId}/attribute/{attributeName}")
+ @Path("{assetId}/{attributeName}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
ValueDatapoint<?>[] getPredictedDatapoints(@BeanParam RequestParams requestParams,
@@ -47,4 +45,11 @@ public interface AssetPredictedDatapointResource {
@PathParam("attributeName") String attributeName,
AssetDatapointQuery query);
+ @PUT
+ @Path("{assetId}/{attributeName}")
+ @Consumes(APPLICATION_JSON)
+ void writePredictedDatapoints(@BeanParam RequestParams requestParams,
+ @PathParam("assetId") String assetId,
+ @PathParam("attributeName") String attributeName,
+ ValueDatapoint<?>[] predictedDatapoints);
}
diff --git a/model/src/main/java/org/openremote/model/datapoint/ValueDatapoint.java b/model/src/main/java/org/openremote/model/datapoint/ValueDatapoint.java
index 7cb0cb925..a6c62423b 100644
--- a/model/src/main/java/org/openremote/model/datapoint/ValueDatapoint.java
+++ b/model/src/main/java/org/openremote/model/datapoint/ValueDatapoint.java
@@ -22,6 +22,8 @@ package org.openremote.model.datapoint;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.Date;
+
public class ValueDatapoint<T> {
protected long timestamp;
@@ -30,6 +32,11 @@ public class ValueDatapoint<T> {
protected ValueDatapoint() {
}
+ public ValueDatapoint(@JsonProperty("x") Date timestamp,
+ @JsonProperty("y") T value) {
+ this(timestamp.getTime(), value);
+ }
+
@JsonCreator
public ValueDatapoint(@JsonProperty("x") long timestamp,
@JsonProperty("y") T value) {
@@ -37,6 +44,7 @@ public class ValueDatapoint<T> {
this.value = value;
}
+
@JsonProperty("x")
public long getTimestamp() {
return timestamp;
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy
index 0460c71f0..a6e9c9514 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy
@@ -10,6 +10,7 @@ import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeRef
import org.openremote.model.attribute.MetaItem
import org.openremote.model.datapoint.AssetDatapoint
+import org.openremote.model.datapoint.ValueDatapoint
import org.openremote.model.datapoint.query.AssetDatapointIntervalQuery
import org.openremote.model.datapoint.query.AssetDatapointLTTBQuery
import org.openremote.model.geo.GeoJSONPoint
@@ -68,17 +69,17 @@ class AssetDatapointQueryTest extends Specification implements ManagerContainerT
when: "datapoints are added to the asset"
assetDatapointService.upsertValues(asset.getId(), attributeName,
- [
- new Pair<>(50d, dateTime.minusMinutes(25)),
- new Pair<>(40d, dateTime.minusMinutes(20)),
- new Pair<>(30d, dateTime.minusMinutes(15)),
- new Pair<>(20d, dateTime.minusMinutes(10)),
- new Pair<>(10d, dateTime.minusMinutes(5)),
- ]
+ [
+ new ValueDatapoint<>(dateTime.minusMinutes(25).toDate(), 50d),
+ new ValueDatapoint<>(dateTime.minusMinutes(20).toDate(), 40d),
+ new ValueDatapoint<>(dateTime.minusMinutes(15).toDate(), 30d),
+ new ValueDatapoint<>(dateTime.minusMinutes(10).toDate(), 20d),
+ new ValueDatapoint<>(dateTime.minusMinutes(5).toDate(), 10d),
+ ]
)
then: "datapoints should exist"
- def allDatapoints = new ArrayList<AssetDatapoint>()
+ def allDatapoints = new ArrayList<ValueDatapoint>()
conditions.eventually {
allDatapoints = assetDatapointService.getDatapoints(new AttributeRef(asset.getId(), attributeName))
assert allDatapoints.size() == 5
@@ -122,15 +123,15 @@ class AssetDatapointQueryTest extends Specification implements ManagerContainerT
and: "datapoints are added that have a spike in value, it should be included in any downsample"
assetDatapointService.upsertValues(asset.getId(), attributeName,
- [
- // placing them in a random order to verify order that is returned with the query
- new Pair<>(25d, dateTime.minusMinutes(10)),
- new Pair<>(30d, dateTime.minusMinutes(5)),
- new Pair<>(10d, dateTime.minusMinutes(30)),
- new Pair<>(90d, dateTime.minusMinutes(20)),
- new Pair<>(15d, dateTime.minusMinutes(25)),
- new Pair<>(20d, dateTime.minusMinutes(15)),
- ]
+ [
+ // placing them in a random order to verify order that is returned with the query
+ new ValueDatapoint<>(dateTime.minusMinutes(10).toDate(), 25d),
+ new ValueDatapoint<>(dateTime.minusMinutes(5).toDate(), 30d),
+ new ValueDatapoint<>(dateTime.minusMinutes(30).toDate(), 10d),
+ new ValueDatapoint<>(dateTime.minusMinutes(20).toDate(), 90d),
+ new ValueDatapoint<>(dateTime.minusMinutes(25).toDate(), 15d),
+ new ValueDatapoint<>(dateTime.minusMinutes(15).toDate(), 20d),
+ ]
)
then: "the spike should be present as the 2nd value"
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
index ac1506a3c..412b45611 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
@@ -2,14 +2,15 @@ package org.openremote.test.assets
import org.openremote.agent.protocol.simulator.SimulatorProtocol
import org.openremote.manager.agent.AgentService
-import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.datapoint.AssetDatapointService
import org.openremote.manager.datapoint.AssetPredictedDatapointService
import org.openremote.manager.setup.SetupService
import org.openremote.model.attribute.AttributeRef
+import org.openremote.model.datapoint.AssetDatapointResource
+import org.openremote.model.datapoint.AssetPredictedDatapointResource
+import org.openremote.model.datapoint.ValueDatapoint
import org.openremote.model.datapoint.query.AssetDatapointIntervalQuery
-import org.openremote.model.util.Pair
import org.openremote.model.util.ValueUtil
import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
@@ -24,6 +25,8 @@ import java.util.concurrent.TimeUnit
import static java.util.concurrent.TimeUnit.*
import static org.openremote.manager.datapoint.AssetDatapointService.OR_DATA_POINTS_MAX_AGE_DAYS_DEFAULT
+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
+import static org.openremote.model.Constants.MASTER_REALM
import static org.openremote.setup.integration.ManagerTestSetup.thingLightToggleAttributeName
import static spock.util.matcher.HamcrestMatchers.closeTo
@@ -40,7 +43,6 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def agentService = container.getService(AgentService.class)
def assetStorageService = container.getService(AssetStorageService.class)
- def assetProcessingService = container.getService(AssetProcessingService.class)
def assetDatapointService = container.getService(AssetDatapointService.class)
def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
@@ -48,6 +50,17 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
stopPseudoClock()
advancePseudoClock(Instant.ofEpochMilli(getClockTimeOf(container)).truncatedTo(ChronoUnit.HOURS).plus(1, ChronoUnit.HOURS).toEpochMilli() - getClockTimeOf(container), TimeUnit.MILLISECONDS, container)
+ and: "a resource client is created"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ "testuser1",
+ "testuser1"
+ ).token
+ def datapointResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetDatapointResource.class)
+ def predictedDatapointResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetPredictedDatapointResource.class)
+
then: "the simulator protocol instance should have been initialised and attributes linked"
conditions.eventually {
assert agentService.protocolInstanceMap.get(managerTestSetup.agentId) != null
@@ -115,7 +128,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
expect: "the datapoints to be stored"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List
assert datapoints.size() == 5
// Note that the "No value" sensor update should not have created a datapoint, the first
@@ -138,9 +151,10 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
and: "the aggregated datapoints should match"
conditions.eventually {
def thing = assetStorageService.find(managerTestSetup.thingId, true)
- def aggregatedDatapoints = assetDatapointService.queryDatapoints(
+ def aggregatedDatapoints = datapointResource.getDatapoints(
+ null,
thing.getId(),
- thing.getAttribute("light1PowerConsumption").orElseThrow({ new RuntimeException("Missing attribute") }),
+ "light1PowerConsumption",
new AssetDatapointIntervalQuery(
LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),
LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()),
@@ -162,9 +176,10 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
and: "when the step size is set on the datapoint retrieval then the datapoints should match"
conditions.eventually {
def thing = assetStorageService.find(managerTestSetup.thingId, true)
- def aggregatedDatapoints = assetDatapointService.queryDatapoints(
+ def aggregatedDatapoints = datapointResource.getDatapoints(
+ null,
thing.getId(),
- thing.getAttribute("light1PowerConsumption").orElseThrow({ new RuntimeException("Missing attribute") }),
+ "light1PowerConsumption",
new AssetDatapointIntervalQuery(
LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),
LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()),
@@ -218,7 +233,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
expect: "the datapoints to be stored"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List
assert datapoints.size() == 3
assert !ValueUtil.getBoolean(datapoints.get(0).value).orElse(null)
@@ -234,9 +249,10 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
and: "the aggregated datapoints should match"
conditions.eventually {
def thing = assetStorageService.find(managerTestSetup.thingId, true)
- def aggregatedDatapoints = assetDatapointService.queryDatapoints(
+ def aggregatedDatapoints = datapointResource.getDatapoints(
+ null,
thing.getId(),
- thing.getAttribute(thingLightToggleAttributeName).orElseThrow({ new RuntimeException("Missing attribute") }),
+ thingLightToggleAttributeName,
new AssetDatapointIntervalQuery(
LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),
LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()),
@@ -267,8 +283,8 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "the datapoints should be stored"
conditions.eventually {
- def powerDatapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))
- def toggleDatapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+ def powerDatapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List
+ def toggleDatapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List
assert powerDatapoints.size() == 6
assert ValueUtil.getValue(powerDatapoints.get(0).value, Double.class).orElse(null) == 17.5d
@@ -287,7 +303,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "data points older than purge days should be purged for the power sensor"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List
assert datapoints.size() == 1
assert ValueUtil.getValue(datapoints.get(0).value, Double.class).orElse(null) == 17.5d
assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp
@@ -295,7 +311,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
and: "no data points should have been purged for the toggle sensor"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List
assert datapoints.size() == 4
assert ValueUtil.getBoolean(datapoints.get(0).value).orElse(false)
assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp
@@ -309,13 +325,13 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "all data points should be purged for power sensor"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List
assert datapoints.isEmpty()
}
and: "no data points should have been purged for the toggle sensor"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List
assert datapoints.size() == 4
assert ValueUtil.getBoolean(datapoints.get(0).value).orElse(false)
assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp
@@ -329,7 +345,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "data points older than 7 days should have been purged for the toggle sensor"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List
assert datapoints.size() == 1
assert ValueUtil.getBoolean(datapoints.get(0).value).orElse(false)
assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp
@@ -343,28 +359,28 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "all data points should have been purged for the toggle sensor"
conditions.eventually {
- def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+ def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List
assert datapoints.isEmpty()
}
when: "predicted data points are added"
- assetPredictedDatapointService.upsertValues(managerTestSetup.thingId, "light1PowerConsumption",
- [
- new Pair<>(10d, LocalDateTime.now().plusSeconds(60)),
- new Pair<>(20d, LocalDateTime.now().plusSeconds(120)),
- new Pair<>(30d, LocalDateTime.now().plusSeconds(180)),
- new Pair<>(40d, LocalDateTime.now().plusSeconds(240)),
- new Pair<>(50d, LocalDateTime.now().plusSeconds(300))
- ]
+ predictedDatapointResource.writePredictedDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption",
+ [
+ new ValueDatapoint<>(getClockTimeOf(container)+60000, 10d),
+ new ValueDatapoint<>(getClockTimeOf(container)+120000, 20d),
+ new ValueDatapoint<>(getClockTimeOf(container)+180000, 30d),
+ new ValueDatapoint<>(getClockTimeOf(container)+240000, 40d),
+ new ValueDatapoint<>(getClockTimeOf(container)+300000, 50d)
+ ] as ValueDatapoint<?>[]
)
- assetPredictedDatapointService.upsertValues(managerTestSetup.thingId, thingLightToggleAttributeName,
- [
- new Pair<>(true, LocalDateTime.now().plusSeconds(60)),
- new Pair<>(true, LocalDateTime.now().plusSeconds(120)),
- new Pair<>(true, LocalDateTime.now().plusSeconds(180)),
- new Pair<>(false, LocalDateTime.now().plusSeconds(240)),
- new Pair<>(false, LocalDateTime.now().plusSeconds(300))
- ]
+ predictedDatapointResource.writePredictedDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName,
+ [
+ new ValueDatapoint<>(getClockTimeOf(container)+60000, true),
+ new ValueDatapoint<>(getClockTimeOf(container)+120000, true),
+ new ValueDatapoint<>(getClockTimeOf(container)+180000, true),
+ new ValueDatapoint<>(getClockTimeOf(container)+240000, false),
+ new ValueDatapoint<>(getClockTimeOf(container)+300000, false)
+ ] as ValueDatapoint<?>[]
)
then: "the predicted data should be available"
diff --git a/test/src/test/groovy/org/openremote/test/assets/ForecastServiceTest.groovy b/test/src/test/groovy/org/openremote/test/assets/ForecastServiceTest.groovy
index 1e67be4cd..abe580be6 100644
--- a/test/src/test/groovy/org/openremote/test/assets/ForecastServiceTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/ForecastServiceTest.groovy
@@ -12,7 +12,7 @@ import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeRef
import org.openremote.model.attribute.MetaItem
import org.openremote.model.datapoint.AssetPredictedDatapoint
-import org.openremote.model.util.Pair
+import org.openremote.model.datapoint.ValueDatapoint
import org.openremote.model.value.ForecastConfigurationWeightedExponentialAverage
import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
@@ -23,8 +23,6 @@ import spock.util.concurrent.PollingConditions
import java.time.Duration
import java.time.Instant
-import java.time.LocalDateTime
-import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.stream.IntStream
@@ -303,9 +301,9 @@ class ForecastServiceTest extends Specification implements ManagerContainerTrait
config.getForecastPeriod().toMillis() * hourIndex - // THE HOUR OFFSET
offsetMillis
- return new Pair<>(
- periodHourValue,
- LocalDateTime.ofInstant(Instant.ofEpochMilli(timerService.getCurrentTimeMillis() - timeOffset), ZoneId.systemDefault()))
+ return new ValueDatapoint<>(
+ timerService.getCurrentTimeMillis() - timeOffset,
+ periodHourValue)
}
}
}