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
Any JSON value can be a datapoint value
Christian Baueraf3af9f2dea5
diff --git a/container/src/main/java/org/openremote/container/Constants.java b/container/src/main/java/org/openremote/container/json/ElementalJsonValueType.javasimilarity index 55%rename from container/src/main/java/org/openremote/container/Constants.javarename to container/src/main/java/org/openremote/container/json/ElementalJsonValueType.javaindex 3d3bda13d..ea3c38f9f 100644--- a/container/src/main/java/org/openremote/container/Constants.java+++ b/container/src/main/java/org/openremote/container/json/ElementalJsonValueType.java@@ -17,12 +17,29 @@* 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.container;+package org.openremote.container.json;-public interface Constants {+import elemental.json.JsonValue;+import org.hibernate.HibernateException;++import java.io.Serializable;++public class ElementalJsonValueType extends PostgreSQLJsonType {++ @Override+ public Serializable disassemble(Object value) throws HibernateException {+ return value == null ? null : ((JsonValue) value).toJson();+ }++ @Override+ public Object assemble(Serializable cached, Object owner) throws HibernateException {+ return cached == null ? null : elemental.json.impl.JsonUtil.parse(cached.toString());+ }++ @Override+ public Class returnedClass() {+ return JsonValue.class;+ }- String PERSISTENCE_SEQUENCE_ID_GENERATOR = "SEQUENCE_ID_GENERATOR";- String PERSISTENCE_UNIQUE_ID_GENERATOR = "UNIQUE_ID_GENERATOR";- String PERSISTENCE_JSON_OBJECT_TYPE = "json_object";- String PERSISTENCE_JSON_ARRAY_TYPE = "json_array";}+diff --git a/container/src/main/java/org/openremote/container/persistence/package-info.java b/container/src/main/java/org/openremote/container/persistence/package-info.javaindex 4134db069..186a775ee 100644--- a/container/src/main/java/org/openremote/container/persistence/package-info.java+++ b/container/src/main/java/org/openremote/container/persistence/package-info.java@@ -19,6 +19,10 @@})@org.hibernate.annotations.TypeDefs({+ @org.hibernate.annotations.TypeDef(+ name = Constants.PERSISTENCE_JSON_VALUE_TYPE,+ typeClass = ElementalJsonValueType.class+ ),@org.hibernate.annotations.TypeDef(name = Constants.PERSISTENCE_JSON_OBJECT_TYPE,typeClass = ElementalJsonObjectType.class@@ -31,6 +35,7 @@package org.openremote.container.persistence;-import org.openremote.container.Constants;+import org.openremote.model.Constants;import org.openremote.container.json.ElementalJsonArrayType;import org.openremote.container.json.ElementalJsonObjectType;+import org.openremote.container.json.ElementalJsonValueType;diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.javaindex 5e48d36b7..f4c83fad9 100644--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java@@ -108,7 +108,7 @@ public class AgentService extends RouteBuilder implements ContainerService {// TODO If success then... notify asset listener clients? If not, then handle error?if (success) {LOG.fine("Asset database update successful: " + attributeValueChange);- datapointService.addDatapoint(attributeValueChange);+ datapointService.storeAssetDatapoint(attributeValueChange);} else {throw new RuntimeException("Asset database update failed for: " + attributeValueChange);}diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/Datapoint.java b/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/Datapoint.javadeleted file mode 100644index b93f971c7..000000000--- a/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/Datapoint.java+++ /dev/null@@ -1,114 +0,0 @@-/*- * Copyright 2016, 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.server.asset.datapoint;--import static org.openremote.model.Constants.PERSISTENCE_JSON_OBJECT_TYPE;--import java.io.Serializable;-import java.util.Date;--import javax.persistence.Column;-import javax.persistence.Entity;-import javax.persistence.Id;-import javax.persistence.Table;-import javax.persistence.Temporal;-import javax.persistence.TemporalType;--import elemental.json.JsonObject;--/**- * <p>- * An datapoint is used to store time series data of an assets attribute.- * <p>- */-@Table(name = "DATAPOINT")-@Entity(name = "Datapoint")-public class Datapoint implements Serializable {--- /**- *- */- private static final long serialVersionUID = 7655761395187723632L;-- @Id- @Column(name = "ASSET_ID", length = 27, nullable = false)- protected String assetIdd;-- @Id- @Column(name = "ATTRIBUTE_NAME", nullable = false)- protected String attributeName;-- @Id- @Temporal(TemporalType.TIMESTAMP)- @Column(name = "CREATED_ON", nullable = false)- protected Date timestamp = new Date();-- @Id- @Column(name = "VALUE", columnDefinition = "jsonb", nullable = false)- @org.hibernate.annotations.Type(type = PERSISTENCE_JSON_OBJECT_TYPE)- protected JsonObject value;-- public Datapoint() {- }-- public Datapoint(String assetIdd, String attributeName, Date timestamp, JsonObject value) {- super();- this.assetIdd = assetIdd;- this.attributeName = attributeName;- this.timestamp = timestamp;- this.value = value;- }-- public String getAssetIdd() {- return assetIdd;- }-- public void setAssetIdd(String assetIdd) {- this.assetIdd = assetIdd;- }-- public String getAttributeName() {- return attributeName;- }-- public void setAttributeName(String attributeName) {- this.attributeName = attributeName;- }-- public Date getTimestamp() {- return timestamp;- }-- public void setTimestamp(Date timestamp) {- this.timestamp = timestamp;- }-- public JsonObject getValue() {- return value;- }-- public void setValue(JsonObject value) {- this.value = value;- }----}diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/DatapointService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/DatapointService.javaindex 8db7c35e2..6119a16bb 100644--- a/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/DatapointService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/datapoint/DatapointService.java@@ -1,18 +1,15 @@package org.openremote.manager.server.asset.datapoint;-import java.util.logging.Logger;-import org.openremote.container.Container;import org.openremote.container.ContainerService;import org.openremote.container.persistence.PersistenceService;-import org.openremote.manager.server.asset.ServerAsset;-import org.openremote.model.AttributeRef;import org.openremote.model.AttributeValueChange;+import org.openremote.model.datapoint.AssetDatapoint;-import elemental.json.JsonValue;+import java.util.logging.Logger;/**- * Store and retrieve datapoints+ * Store and retrieve datapoints.*/public class DatapointService implements ContainerService {@@ -27,21 +24,15 @@ public class DatapointService implements ContainerService {@Overridepublic void start(Container container) throws Exception {- // TODO Auto-generated method stub-}@Overridepublic void stop(Container container) throws Exception {- // TODO Auto-generated method stub-}- public void addDatapoint(AttributeValueChange attributeValueChange) {- AttributeRef attributeRef = attributeValueChange.getAttributeRef();- String attributeName = attributeRef.getAttributeName();- String assetId = attributeRef.getEntityId();- JsonValue value = attributeValueChange.getValue();+ public void storeAssetDatapoint(AttributeValueChange attributeValueChange) {+ AssetDatapoint assetDatapoint = new AssetDatapoint(attributeValueChange);+ // TODO}diff --git a/manager/server/src/main/resources/META-INF/persistence.xml b/manager/server/src/main/resources/META-INF/persistence.xmlindex 0e9265fbe..60103a139 100644--- a/manager/server/src/main/resources/META-INF/persistence.xml+++ b/manager/server/src/main/resources/META-INF/persistence.xml@@ -10,7 +10,7 @@<class>org.openremote.container.persistence</class><class>org.openremote.manager.server.asset.ServerAsset</class>- <class>org.openremote.manager.server.asset.datapoint.Datapoint</class>+ <class>org.openremote.model.datapoint.AssetDatapoint</class><exclude-unlisted-classes>true</exclude-unlisted-classes><properties>diff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.javaindex a78bc6da5..4f935385d 100644--- a/model/src/main/java/org/openremote/model/Constants.java+++ b/model/src/main/java/org/openremote/model/Constants.java@@ -28,6 +28,7 @@ public interface Constants {int ACCESS_TOKEN_LIFESPAN_SECONDS = 300; // 5 minutesString PERSISTENCE_SEQUENCE_ID_GENERATOR = "SEQUENCE_ID_GENERATOR";String PERSISTENCE_UNIQUE_ID_GENERATOR = "UNIQUE_ID_GENERATOR";+ String PERSISTENCE_JSON_VALUE_TYPE = "json_value";String PERSISTENCE_JSON_OBJECT_TYPE = "json_object";String PERSISTENCE_JSON_ARRAY_TYPE = "json_array";String NAMESPACE = "urn:openremote";diff --git a/model/src/main/java/org/openremote/model/datapoint/AssetDatapoint.java b/model/src/main/java/org/openremote/model/datapoint/AssetDatapoint.javanew file mode 100644index 000000000..a28137b29--- /dev/null+++ b/model/src/main/java/org/openremote/model/datapoint/AssetDatapoint.java@@ -0,0 +1,29 @@+package org.openremote.model.datapoint;++import elemental.json.JsonValue;+import org.openremote.model.AttributeRef;+import org.openremote.model.AttributeValueChange;++import javax.persistence.Entity;+import javax.persistence.Table;++@Entity+@Table(name = "ASSET_DATAPOINT")+public class AssetDatapoint extends Datapoint {++ public AssetDatapoint() {+ }++ public AssetDatapoint(AttributeValueChange attributeValueChange) {+ super(attributeValueChange);+ }++ public AssetDatapoint(AttributeRef attributeRef, JsonValue value) {+ super(attributeRef, value);+ }++ public AssetDatapoint(String entityId, String attributeName, long timestamp, JsonValue value) {+ super(entityId, attributeName, timestamp, value);+ }++}diff --git a/model/src/main/java/org/openremote/model/datapoint/Datapoint.java b/model/src/main/java/org/openremote/model/datapoint/Datapoint.javanew file mode 100644index 000000000..6075a5f30--- /dev/null+++ b/model/src/main/java/org/openremote/model/datapoint/Datapoint.java@@ -0,0 +1,119 @@+/*+ * Copyright 2016, 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.datapoint;++import elemental.json.JsonObject;+import elemental.json.JsonValue;+import org.openremote.model.AttributeRef;+import org.openremote.model.AttributeValueChange;++import javax.persistence.Column;+import javax.persistence.Id;+import javax.persistence.IdClass;+import javax.persistence.MappedSuperclass;+import java.io.Serializable;++import static org.openremote.model.Constants.PERSISTENCE_JSON_VALUE_TYPE;++/**+ * <p>+ * A datapoint is used to hold time series data of an entity attribute.+ * <p>+ */+@MappedSuperclass+public abstract class Datapoint implements Serializable {++ @Id+ @Column(name = "ENTITY_ID", length = 27, nullable = false)+ protected String entityId;++ @Id+ @Column(name = "ATTRIBUTE_NAME", nullable = false)+ protected String attributeName;++ @Id+ @Column(name = "TIMESTAMP", nullable = false)+ protected long timestamp = System.currentTimeMillis();++ @Id+ @Column(name = "VALUE", columnDefinition = "jsonb", nullable = false)+ @org.hibernate.annotations.Type(type = PERSISTENCE_JSON_VALUE_TYPE)+ protected JsonValue value;++ public Datapoint() {+ }++ public Datapoint(AttributeValueChange attributeValueChange) {+ this(attributeValueChange.getAttributeRef(), attributeValueChange.getValue());+ }++ public Datapoint(AttributeRef attributeRef, JsonValue value) {+ this(attributeRef.getEntityId(), attributeRef.getAttributeName(), System.currentTimeMillis(), value);+ }++ public Datapoint(String entityId, String attributeName, long timestamp, JsonValue value) {+ this.entityId = entityId;+ this.attributeName = attributeName;+ this.timestamp = timestamp;+ this.value = value;+ }++ public String getEntityId() {+ return entityId;+ }++ public void setEntityId(String entityId) {+ this.entityId = entityId;+ }++ public String getAttributeName() {+ return attributeName;+ }++ public void setAttributeName(String attributeName) {+ this.attributeName = attributeName;+ }++ public long getTimestamp() {+ return timestamp;+ }++ public void setTimestamp(long timestamp) {+ this.timestamp = timestamp;+ }++ public JsonValue getValue() {+ return value;+ }++ public void setValue(JsonValue value) {+ this.value = value;+ }++ @Override+ public String toString() {+ return getClass().getSimpleName() + "{" ++ "entityId='" + entityId + '\'' ++ ", attributeName='" + attributeName + '\'' ++ ", timestamp=" + timestamp ++ ", value=" + (value != null ? value.toJson() : "null") ++ '}';+ }+}