100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added AssetPredictedDatapoint and the rest of the classes associated with it
Michael Rademaker958010ea987b
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.javaindex 0524060b0..0e671b873 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java@@ -21,6 +21,7 @@ package org.openremote.manager.agent;import com.jayway.jsonpath.Configuration;import com.jayway.jsonpath.JsonPath;+import com.jayway.jsonpath.Option;import com.jayway.jsonpath.ParseContext;import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;@@ -139,10 +140,11 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse);jsonPathParser = JsonPath.using(- new Configuration.ConfigurationBuilder()+ Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).build()+ .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL));}@@ -914,4 +916,4 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn Values.parse(pathJson).orElse(null);}-}\ No newline at end of file+}diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.javaindex 5044d7f73..a765877ed 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java@@ -29,7 +29,6 @@ import org.openremote.model.datapoint.AssetDatapointResource;import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;import org.openremote.model.http.RequestParams;-import org.openremote.model.value.Value;import javax.ws.rs.BeanParam;import javax.ws.rs.WebApplicationException;diff --git a/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointHealthStatusProvider.java b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointHealthStatusProvider.javanew file mode 100644index 000000000..3c9aca100--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointHealthStatusProvider.java@@ -0,0 +1,70 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.predicted;++import org.openremote.container.Container;+import org.openremote.container.ContainerHealthStatusProvider;+import org.openremote.container.ContainerService;+import org.openremote.model.value.ObjectValue;+import org.openremote.model.value.Value;+import org.openremote.model.value.Values;++public class AssetPredictedDatapointHealthStatusProvider implements ContainerHealthStatusProvider {+ public static final String NAME = "predicted";+ public static final String VERSION = "1.0";+ protected AssetPredictedDatapointService assetPredictedDatapointService;++ @Override+ public int getPriority() {+ return ContainerService.DEFAULT_PRIORITY;+ }++ @Override+ public void init(Container container) throws Exception {+ assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class);+ }++ @Override+ public void start(Container container) throws Exception {++ }++ @Override+ public void stop(Container container) throws Exception {++ }++ @Override+ public String getHealthStatusName() {+ return NAME;+ }++ @Override+ public String getHealthStatusVersion() {+ return VERSION;+ }++ @Override+ public Value getHealthStatus() {+ ObjectValue value = Values.createObject();+ value.put("totalDatapoints", assetPredictedDatapointService.getDatapointsCount());+ return value;+ }+}diff --git a/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointResourceImpl.javanew file mode 100644index 000000000..16b60601b--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointResourceImpl.java@@ -0,0 +1,89 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.predicted;++import org.openremote.container.timer.TimerService;+import org.openremote.manager.asset.AssetStorageService;+import org.openremote.manager.security.ManagerIdentityService;+import org.openremote.manager.web.ManagerWebResource;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetAttribute;+import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.http.RequestParams;+import org.openremote.model.predicted.AssetPredictedDatapointResource;++import javax.ws.rs.BeanParam;+import javax.ws.rs.WebApplicationException;+import javax.ws.rs.core.Response;+import java.util.logging.Logger;++public class AssetPredictedDatapointResourceImpl extends ManagerWebResource implements AssetPredictedDatapointResource {++ private static final Logger LOG = Logger.getLogger(AssetPredictedDatapointResourceImpl.class.getName());++ protected final AssetStorageService assetStorageService;+ protected final AssetPredictedDatapointService assetPredictedDatapointService;++ public AssetPredictedDatapointResourceImpl(TimerService timerService,+ ManagerIdentityService identityService,+ AssetStorageService assetStorageService,+ AssetPredictedDatapointService assetPredictedDatapointService) {+ super(timerService, identityService);+ this.assetStorageService = assetStorageService;+ this.assetPredictedDatapointService = assetPredictedDatapointService;+ }++ @Override+ public ValueDatapoint[] getPredictedDatapoints(@BeanParam RequestParams requestParams,+ String assetId,+ String attributeName,+ long fromTimestamp,+ long toTimestamp) {+ 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);+ }++ if (!isTenantActiveAndAccessible(asset)) {+ LOG.fine("Forbidden access for user '" + getUsername() + "': " + asset);+ throw new WebApplicationException(Response.Status.FORBIDDEN);+ }++ AssetAttribute attribute = asset.getAttribute(attributeName).orElseThrow(() ->+ new WebApplicationException(Response.Status.NOT_FOUND)+ );++ return assetPredictedDatapointService.getValueDatapoints(+ attribute,+ toTimestamp,+ fromTimestamp+ );+ } catch (IllegalStateException ex) {+ throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);+ }+ }+}diff --git a/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.java b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.javanew file mode 100644index 000000000..e7780825d--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/predicted/AssetPredictedDatapointService.java@@ -0,0 +1,159 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.predicted;++import org.hibernate.Session;+import org.hibernate.jdbc.AbstractReturningWork;+import org.openremote.container.Container;+import org.openremote.container.ContainerService;+import org.openremote.container.persistence.PersistenceService;+import org.openremote.container.timer.TimerService;+import org.openremote.manager.asset.AssetStorageService;+import org.openremote.manager.concurrent.ManagerExecutorService;+import org.openremote.manager.security.ManagerIdentityService;+import org.openremote.manager.web.ManagerWebService;+import org.openremote.model.asset.AssetAttribute;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.value.Value;+import org.openremote.model.value.Values;++import javax.persistence.TypedQuery;+import java.sql.Connection;+import java.sql.PreparedStatement;+import java.sql.ResultSet;+import java.sql.SQLException;+import java.util.ArrayList;+import java.util.List;+import java.util.logging.Logger;++public class AssetPredictedDatapointService implements ContainerService {++ private static final Logger LOG = Logger.getLogger(AssetPredictedDatapointService.class.getName());+ protected PersistenceService persistenceService;+ protected AssetStorageService assetStorageService;+ protected TimerService timerService;+ protected ManagerExecutorService managerExecutorService;++ @Override+ public int getPriority() {+ return ContainerService.DEFAULT_PRIORITY;+ }++ @Override+ public void init(Container container) throws Exception {+ persistenceService = container.getService(PersistenceService.class);+ assetStorageService = container.getService(AssetStorageService.class);+ timerService = container.getService(TimerService.class);+ managerExecutorService = container.getService(ManagerExecutorService.class);++ container.getService(ManagerWebService.class).getApiSingletons().add(+ new AssetPredictedDatapointResourceImpl(+ container.getService(TimerService.class),+ container.getService(ManagerIdentityService.class),+ container.getService(AssetStorageService.class),+ this+ )+ );+ }++ @Override+ public void start(Container container) throws Exception {++ }++ @Override+ public void stop(Container container) throws Exception {++ }++ public long getDatapointsCount() {+ return getDatapointsCount(null);+ }++ public long getDatapointsCount(AttributeRef attributeRef) {+ return persistenceService.doReturningTransaction(entityManager -> {++ String queryStr = attributeRef == null ?+ "select count(dp) from AssetPredictedDatapoint dp" :+ "select count(dp) from AssetPredictedDatapoint dp where dp.entityId = :assetId and dp.attributeName = :attributeName";++ TypedQuery<Long> query = entityManager.createQuery(+ queryStr,+ Long.class);++ if (attributeRef != null) {+ query+ .setParameter("assetId", attributeRef.getEntityId())+ .setParameter("attributeName", attributeRef.getAttributeName());+ }++ return query.getSingleResult();+ });+ }++ public ValueDatapoint[] getValueDatapoints(AssetAttribute attribute,+ long fromTimestamp,+ long toTimestamp) {++ AttributeRef attributeRef = attribute.getReferenceOrThrow();++ LOG.fine("Getting predicted datapoints for: " + attributeRef);++ return persistenceService.doReturningTransaction(entityManager ->+ entityManager.unwrap(Session.class).doReturningWork(new AbstractReturningWork<ValueDatapoint[]>() {+ @Override+ public ValueDatapoint[] execute(Connection connection) throws SQLException {++ StringBuilder query = new StringBuilder();++ query.append("select distinct TIMESTAMP AS X, value AS Y from ASSET_PREDICTED_DATAPOINT " ++ "where " ++ "TIMESTAMP >= to_timestamp(?) - ? " ++ "and " ++ "TIMESTAMP < to_timestamp(?) " ++ "and " ++ "ENTITY_ID = ? and ATTRIBUTE_NAME = ? "+ );++ try (PreparedStatement st = connection.prepareStatement(query.toString())) {++ long fromTimestampSeconds = fromTimestamp / 1000;+ long toTimestampSeconds = toTimestamp / 1000;++ st.setLong(1, fromTimestampSeconds);+ st.setLong(2, toTimestampSeconds);+ st.setString(3, attributeRef.getEntityId());+ st.setString(4, attributeRef.getAttributeName());++ try (ResultSet rs = st.executeQuery()) {+ List<ValueDatapoint<?>> result = new ArrayList<>();+ while (rs.next()) {+ Value value = rs.getObject(2) != null ? Values.parseOrNull(rs.getString(2)) : null;+ result.add(new ValueDatapoint<>(rs.getTimestamp(1).getTime(), value));+ }+ return result.toArray(new ValueDatapoint[result.size()]);+ }+ }+ }+ })+ );+ }+}diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.javaindex e4761a93f..3d1accb79 100644--- a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java@@ -28,6 +28,7 @@ import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.concurrent.ManagerExecutorService;import org.openremote.manager.datapoint.AssetDatapointService;import org.openremote.manager.persistence.ManagerPersistenceService;+import org.openremote.manager.predicted.AssetPredictedDatapointService;import org.openremote.manager.rules.RulesetStorageService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.model.asset.Asset;@@ -60,6 +61,7 @@ public abstract class AbstractManagerSetup implements Setup {final protected AssetStorageService assetStorageService;final protected AssetProcessingService assetProcessingService;final protected AssetDatapointService assetDatapointService;+ final protected AssetPredictedDatapointService assetPredictedDatapointService;final protected RulesetStorageService rulesetStorageService;final protected SetupService setupService;@@ -70,6 +72,7 @@ public abstract class AbstractManagerSetup implements Setup {this.assetStorageService = container.getService(AssetStorageService.class);this.assetProcessingService = container.getService(AssetProcessingService.class);this.assetDatapointService = container.getService(AssetDatapointService.class);+ this.assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class);this.rulesetStorageService = container.getService(RulesetStorageService.class);this.setupService = container.getService(SetupService.class);}diff --git a/manager/src/main/resources/META-INF/persistence.xml b/manager/src/main/resources/META-INF/persistence.xmlindex 4473bc8f4..9bfb8f398 100644--- a/manager/src/main/resources/META-INF/persistence.xml+++ b/manager/src/main/resources/META-INF/persistence.xml@@ -15,6 +15,7 @@<class>org.openremote.model.notification.SentNotification</class><class>org.openremote.manager.security.UserConfiguration</class><class>org.openremote.model.datapoint.AssetDatapoint</class>+ <class>org.openremote.model.predicted.AssetPredictedDatapoint</class><class>org.openremote.model.asset.UserAsset</class><class>org.openremote.model.security.Tenant</class><class>org.openremote.model.security.User</class>@@ -34,4 +35,4 @@</properties></persistence-unit>-</persistence>\ No newline at end of file+</persistence>diff --git a/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerServiceindex 5ab40cd46..cb2f184ca 100644--- a/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService+++ b/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService@@ -23,4 +23,5 @@ org.openremote.manager.apps.ConsoleAppServiceorg.openremote.manager.web.ManagerWebServiceorg.openremote.container.message.MessageBrokerSetupServiceorg.openremote.manager.notification.PushNotificationHandler-org.openremote.manager.notification.EmailNotificationHandler\ No newline at end of file+org.openremote.manager.notification.EmailNotificationHandler+org.openremote.manager.predicted.AssetPredictedDatapointServicediff --git a/manager/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProvider b/manager/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProviderindex 4184de35b..39292d156 100644--- a/manager/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProvider+++ b/manager/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProvider@@ -3,4 +3,5 @@ org.openremote.manager.system.SslHealthStatusProviderorg.openremote.manager.system.SystemHealthStatusProviderorg.openremote.manager.rules.RulesHealthStatusProviderorg.openremote.manager.agent.AgentHealthStatusProvider-org.openremote.manager.datapoint.AssetDatapointHealthStatusProvider\ No newline at end of file+org.openremote.manager.datapoint.AssetDatapointHealthStatusProvider+org.openremote.manager.predicted.AssetPredictedDatapointHealthStatusProviderdiff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sqlindex 6ea0c37bb..adcf8b877 100644--- a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql@@ -121,6 +121,14 @@ create table SYSLOG_EVENT (primary key (ID));+create table ASSET_PREDICTED_DATAPOINT (+ TIMESTAMP timestamp with time zone not null,+ ENTITY_ID varchar(36) not null,+ ATTRIBUTE_NAME varchar(255) not null,+ VALUE jsonb not null,+ primary key (TIMESTAMP, ENTITY_ID, ATTRIBUTE_NAME)+);+/*############################# FUNCTIONS #############################*/diff --git a/model/src/main/java/org/openremote/model/predicted/AssetPredictedDatapoint.java b/model/src/main/java/org/openremote/model/predicted/AssetPredictedDatapoint.javanew file mode 100644index 000000000..063c9627b--- /dev/null+++ b/model/src/main/java/org/openremote/model/predicted/AssetPredictedDatapoint.java@@ -0,0 +1,54 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.predicted;++import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.attribute.AttributeState;+import org.openremote.model.datapoint.Datapoint;+import org.openremote.model.value.Value;++import javax.persistence.Entity;+import javax.persistence.Table;++@Entity+@Table(name = "ASSET_PREDICTED_DATAPOINT")+public class AssetPredictedDatapoint extends Datapoint {++ public AssetPredictedDatapoint() {+ }++ public AssetPredictedDatapoint(AttributeState attributeState, long timestamp) {+ super(attributeState, timestamp);+ }++ public AssetPredictedDatapoint(AttributeEvent stateEvent) {+ super(stateEvent);+ }++ public AssetPredictedDatapoint(AttributeRef attributeRef, Value value, long timestamp) {+ super(attributeRef, value, timestamp);+ }++ public AssetPredictedDatapoint(String entityId, String attributeName, Value value, long timestamp) {+ super(entityId, attributeName, value, timestamp);+ }+}diff --git a/model/src/main/java/org/openremote/model/predicted/AssetPredictedDatapointResource.java b/model/src/main/java/org/openremote/model/predicted/AssetPredictedDatapointResource.javanew file mode 100644index 000000000..00f69a0de--- /dev/null+++ b/model/src/main/java/org/openremote/model/predicted/AssetPredictedDatapointResource.java@@ -0,0 +1,55 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.predicted;++import jsinterop.annotations.JsType;+import org.openremote.model.Constants;+import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.http.RequestParams;+import org.openremote.model.http.SuccessStatusCode;++import javax.annotation.security.RolesAllowed;+import javax.ws.rs.*;++import static javax.ws.rs.core.MediaType.APPLICATION_JSON;++@Path("asset/predicted")+@JsType(isNative = true)+public interface AssetPredictedDatapointResource {+ /**+ * Retrieve the historical datapoints of an asset attribute. Regular users can only access assets in their+ * authenticated realm, the superuser can access assets in other (all) realms. A 403 status is returned if a+ * regular user tries to access an asset in a realm different than its authenticated realm, or if the user is+ * restricted and the asset is not linked to the user. A 400 status is returned if the asset attribute does+ * not have datapoint storage enabled.+ */+ @GET+ @Path("{assetId}/attribute/{attributeName}")+ @Produces(APPLICATION_JSON)+ @SuccessStatusCode(200)+ @RolesAllowed({Constants.READ_ASSETS_ROLE})+ @SuppressWarnings("unusable-by-js")+ ValueDatapoint[] getPredictedDatapoints(@BeanParam RequestParams requestParams,+ @PathParam("assetId") String assetId,+ @PathParam("attributeName") String attributeName,+ @QueryParam("fromTimestamp") long fromTimestamp,+ @QueryParam("toTimeStamp") long toTimeStamp);++}