Skip to content

armgit / core

Public Imported from GitHub
0

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

Migration to new value model

Christian Bauer6bc5e4db8fd7
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
index 94cb7d93e..f8370dee1 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
@@ -29,6 +29,7 @@ import org.openremote.manager.client.widget.*;
import org.openremote.model.*;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.ReadAssetAttributesEvent;
import org.openremote.model.datapoint.DatapointInterval;
import org.openremote.model.datapoint.NumberDatapoint;
import org.openremote.model.event.bus.EventRegistration;
@@ -262,14 +263,14 @@ public abstract class AttributesBrowser
protected void readAllAttributeValues() {
environment.getEventService().dispatch(
- new ReadAttributesEvent(asset.getId())
+ new ReadAssetAttributesEvent(asset.getId())
);
}
protected void readAttributeValue(AssetAttribute attribute) {
attribute.getReference().ifPresent(attributeRef ->
environment.getEventService().dispatch(
- new ReadAttributesEvent(attributeRef.getEntityId(), attributeRef.getAttributeName())
+ new ReadAssetAttributesEvent(attributeRef.getEntityId(), attributeRef.getAttributeName())
)
);
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/GeoJSONSource.java b/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/GeoJSONSource.java
index 70feb6c67..e558ddfe1 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/GeoJSONSource.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/GeoJSONSource.java
@@ -19,12 +19,12 @@
*/
package org.openremote.manager.client.interop.mapbox;
-import elemental.json.JsonObject;
+import com.google.gwt.core.client.JavaScriptObject;
import jsinterop.annotations.JsType;
@JsType(isNative = true, namespace = "mapboxgl")
public interface GeoJSONSource {
- void setData(JsonObject geoJson);
+ void setData(JavaScriptObject geoJson);
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/MapboxMap.java b/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/MapboxMap.java
index 13a033013..1b5d2bc7d 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/MapboxMap.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/MapboxMap.java
@@ -21,8 +21,6 @@ package org.openremote.manager.client.interop.mapbox;
import com.google.gwt.core.client.JavaScriptObject;
import elemental.dom.Node;
-import elemental.json.JsonArray;
-import elemental.json.JsonObject;
import jsinterop.annotations.JsProperty;
import jsinterop.annotations.JsType;
@@ -38,14 +36,14 @@ public class MapboxMap {
public MapboxMap(JavaScriptObject options) {}
public native MapboxMap addClass(String className);
- public native MapboxMap addClass(String className, JsonObject styleOptions);
+ public native MapboxMap addClass(String className, JavaScriptObject styleOptions);
public native MapboxMap addControl(Control control);
public native MapboxMap addLayer(JavaScriptObject layer);
public native MapboxMap addLayer(JavaScriptObject layer, String beforeLayerId);
- public native MapboxMap addSource(String sourceId, JsonObject source);
+ public native MapboxMap addSource(String sourceId, JavaScriptObject source);
public native GeoJSONSource getSource(String sourceId);
// TODO: add batch method fucntionality to map
@@ -57,17 +55,17 @@ public class MapboxMap {
public native MapboxMap easeTo(CameraOptions cameraOptions);
public native MapboxMap easeTo(AnimationOptions animationOptions);
- public native MapboxMap featuresAt(Point point, JsonObject options, FeaturesCallbackFn callbackFn);
+ public native MapboxMap featuresAt(Point point, JavaScriptObject options, FeaturesCallbackFn callbackFn);
- public native MapboxMap featuresIn(JsonObject options, FeaturesCallbackFn callbackFn);
- public native MapboxMap featuresIn(Point[] bounds, JsonObject options, FeaturesCallbackFn callbackFn);
+ public native MapboxMap featuresIn(JavaScriptObject options, FeaturesCallbackFn callbackFn);
+ public native MapboxMap featuresIn(Point[] bounds, JavaScriptObject options, FeaturesCallbackFn callbackFn);
- public native MapboxMap fitBounds(LngLatBounds bounds, JsonObject options);
- public native MapboxMap fitBounds(LngLatBounds bounds, JsonObject options, EventData eventData);
+ public native MapboxMap fitBounds(LngLatBounds bounds, JavaScriptObject options);
+ public native MapboxMap fitBounds(LngLatBounds bounds, JavaScriptObject options, EventData eventData);
- public native MapboxMap flyTo(JsonObject options);
+ public native MapboxMap flyTo(JavaScriptObject options);
- public native MapboxMap flyTo(JsonObject options, EventData eventData);
+ public native MapboxMap flyTo(JavaScriptObject options, EventData eventData);
public native double getBearing();
@@ -83,7 +81,7 @@ public class MapboxMap {
public native Node getContainer();
- public native JsonArray getFilter(String layerId);
+ public native JavaScriptObject getFilter(String layerId);
public native JavaScriptObject getLayer(String layerId);
@@ -95,7 +93,7 @@ public class MapboxMap {
public native double getPitch();
- public native JsonObject getStyle();
+ public native JavaScriptObject getStyle();
public native int getZoom();
@@ -110,12 +108,12 @@ public class MapboxMap {
public native MapboxMap panTo(LngLat lngLat, AnimationOptions options);
public native MapboxMap panTo(LngLat lngLat, AnimationOptions options, EventData eventData);
- public native JsonObject project(LngLat lngLat);
+ public native JavaScriptObject project(LngLat lngLat);
public native void remove();
public native MapboxMap removeClass(String className);
- public native MapboxMap removeClass(String className, JsonObject options);
+ public native MapboxMap removeClass(String className, JavaScriptObject options);
public native MapboxMap removeLayer(String layerId);
@@ -142,9 +140,9 @@ public class MapboxMap {
public native MapboxMap setCenter(LngLat center, EventData eventData);
public native MapboxMap setClasses(String[] classNames);
- public native MapboxMap setClasses(String[] classNames, JsonObject options);
+ public native MapboxMap setClasses(String[] classNames, JavaScriptObject options);
- public native MapboxMap setFilter(String layerId, JsonObject filter);
+ public native MapboxMap setFilter(String layerId, JavaScriptObject filter);
public native MapboxMap setLayerZoomRange(String layerId, int minZoom, int maxZoom);
@@ -156,7 +154,7 @@ public class MapboxMap {
public native MapboxMap setPitch(int pitch);
public native MapboxMap setPitch(int pitch, EventData eventData);
- public native MapboxMap setStyle(JsonObject style);
+ public native MapboxMap setStyle(JavaScriptObject style);
public native MapboxMap setZoom(int zoom);
public native MapboxMap setZoom(int zoom, EventData eventData);
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/Popup.java b/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/Popup.java
index 461421220..29cb08b50 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/Popup.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/mapbox/Popup.java
@@ -19,14 +19,14 @@
*/
package org.openremote.manager.client.interop.mapbox;
-import elemental.json.JsonObject;
+import com.google.gwt.core.client.JavaScriptObject;
import jsinterop.annotations.JsType;
@JsType(isNative = true, name = "Popup", namespace = "mapboxgl")
public class Popup extends Control<Popup> {
public Popup() {}
- public Popup(JsonObject options) {}
+ public Popup(JavaScriptObject options) {}
public native LngLat getLngLat();
diff --git a/manager/client/src/main/java/org/openremote/manager/client/widget/MapWidget.java b/manager/client/src/main/java/org/openremote/manager/client/widget/MapWidget.java
index f6492e4ea..a86459545 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/widget/MapWidget.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/widget/MapWidget.java
@@ -21,16 +21,14 @@ package org.openremote.manager.client.widget;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Document;
-import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.FlowPanel;
import elemental.client.Browser;
import elemental.js.util.*;
-import elemental.json.Json;
-import elemental.json.JsonArray;
-import elemental.json.JsonObject;
import org.openremote.manager.client.interop.mapbox.*;
import org.openremote.model.geo.GeoJSON;
+import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.ObjectValue;
+import org.openremote.model.value.Values;
import java.util.logging.Logger;
@@ -153,7 +151,7 @@ public class MapWidget extends FlowPanel {
mapOptions.put("container", hostElementId);
mapOptions.put("attributionControl", false);
- mapboxMap = new MapboxMap((JavaScriptObject) mapOptions.toNative());
+ mapboxMap = new MapboxMap(mapOptions.asNativeObject());
mapboxMap.on(EventType.LOAD, eventData -> {
initFeatureLayers();
@@ -199,10 +197,10 @@ public class MapWidget extends FlowPanel {
public void showPopup(double lng, double lat, String text) {
if (!isMapReady())
throw new IllegalStateException("Map not ready");
- JsonObject popupOptions = Json.createObject();
- popupOptions.put("closeOnClick", Json.create(false));
- popupOptions.put("closeButton", Json.create(false));
- popup = new Popup(popupOptions);
+ ObjectValue popupOptions = Values.createObject();
+ popupOptions.put("closeOnClick", Values.create(false));
+ popupOptions.put("closeButton", Values.create(false));
+ popup = new Popup(popupOptions.asNativeObject());
popup.setLngLat(new LngLat(lng, lat)).setText(text);
popup.addTo(mapboxMap);
}
@@ -220,7 +218,7 @@ public class MapWidget extends FlowPanel {
if (mapboxMap.getSource(featureSourceId) == null)
throw new IllegalArgumentException("Map has no such feature source: " + featureSourceId);
LOG.fine("Showing feature on source '" + featureSourceId + "': " + geoFeature);
- mapboxMap.getSource(featureSourceId).setData(geoFeature.getJsonObject());
+ mapboxMap.getSource(featureSourceId).setData(geoFeature.getObjectValue().asNativeObject());
}
public void flyTo(double[] coordinates) {
@@ -228,42 +226,39 @@ public class MapWidget extends FlowPanel {
throw new IllegalStateException("Map not ready");
if (coordinates == null || coordinates.length != 2)
return;
- JsonObject json = Json.createObject();
- JsonArray center = Json.createArray();
+ ObjectValue options = Values.createObject();
+ ArrayValue center = Values.createArray();
center.set(0, coordinates[0]);
center.set(1, coordinates[1]);
- json.put("center", center);
- mapboxMap.flyTo(json);
+ options.put("center", center);
+ mapboxMap.flyTo(options.asNativeObject());
}
protected void initFeatureLayers() {
LOG.fine("Adding GeoJSON feature sources and layers on map load");
- JsonObject sourceOptionsSelection = prepareSourceOptions(FEATURE_SOURCE_DROPPED_PIN);
- mapboxMap.addSource(FEATURE_SOURCE_DROPPED_PIN, sourceOptionsSelection);
+ ObjectValue sourceOptionsSelection = prepareSourceOptions(FEATURE_SOURCE_DROPPED_PIN);
+ mapboxMap.addSource(FEATURE_SOURCE_DROPPED_PIN, sourceOptionsSelection.asNativeObject());
mapboxMap.addLayer(LAYER_DROPPED_PIN);
- JsonObject sourceOptionsAll = prepareSourceOptions(FEATURE_SOURCE_CIRCLE);
- sourceOptionsAll.put("maxzoom", Json.create(20));
- sourceOptionsAll.put("buffer", Json.create(128));
- sourceOptionsAll.put("tolerance", Json.create(0.375));
- sourceOptionsAll.put("cluster", Json.create(false));
- sourceOptionsAll.put("clusterRadius", Json.create(50));
- sourceOptionsAll.put("clusterMaxZoom", Json.create(15));
+ ObjectValue sourceOptionsAll = prepareSourceOptions(FEATURE_SOURCE_CIRCLE);
+ sourceOptionsAll.put("maxzoom", Values.create(20));
+ sourceOptionsAll.put("buffer", Values.create(128));
+ sourceOptionsAll.put("tolerance", Values.create(0.375));
+ sourceOptionsAll.put("cluster", Values.create(false));
+ sourceOptionsAll.put("clusterRadius", Values.create(50));
+ sourceOptionsAll.put("clusterMaxZoom", Values.create(15));
- // TODO something weird is going on with JS interop, this helps for now
- sourceOptionsAll = Json.parse(sourceOptionsAll.toJson());
-
- mapboxMap.addSource(FEATURE_SOURCE_CIRCLE, sourceOptionsAll);
+ mapboxMap.addSource(FEATURE_SOURCE_CIRCLE, sourceOptionsAll.asNativeObject());
mapboxMap.addLayer(LAYER_CIRCLE, FEATURE_LAYER_DROPPED_PIN);
}
- protected JsonObject prepareSourceOptions(String featureSourceId) {
- JsonObject sourceOptions = Json.createObject();
+ protected ObjectValue prepareSourceOptions(String featureSourceId) {
+ ObjectValue sourceOptions = Values.createObject();
// Initialize with empty collection
- JsonObject initialData = GeoJSON.EMPTY_FEATURE_COLLECTION.getJsonObject();
+ ObjectValue initialData = GeoJSON.EMPTY_FEATURE_COLLECTION.getObjectValue();
LOG.fine("Preparing initial data on feature source: " + featureSourceId);
- sourceOptions.put("type", Json.create("geojson"));
+ sourceOptions.put("type", Values.create("geojson"));
sourceOptions.put("data", initialData);
return sourceOptions;
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
index 573898c78..e9bf857f8 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
@@ -38,7 +38,7 @@ import org.openremote.manager.shared.security.ClientRole;
import org.openremote.manager.shared.security.Tenant;
import org.openremote.model.AttributeEvent;
import org.openremote.model.Pair;
-import org.openremote.model.ReadAttributesEvent;
+import org.openremote.model.asset.ReadAssetAttributesEvent;
import org.openremote.model.asset.*;
import org.postgresql.util.PGobject;
@@ -145,12 +145,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// React if a client wants to read attribute state
from(INCOMING_EVENT_TOPIC)
- .filter(body().isInstanceOf(ReadAttributesEvent.class))
+ .filter(body().isInstanceOf(ReadAssetAttributesEvent.class))
.process(exchange -> {
- ReadAttributesEvent event = exchange.getIn().getBody(ReadAttributesEvent.class);
+ ReadAssetAttributesEvent event = exchange.getIn().getBody(ReadAssetAttributesEvent.class);
LOG.fine("Handling from client: " + event);
- if (event.getEntityId() == null || event.getEntityId().isEmpty())
+ if (event.getAssetId() == null || event.getAssetId().isEmpty())
return;
String sessionKey = getSessionKey(exchange);
@@ -158,7 +158,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// Superuser can get all
if (auth.isSuperUser()) {
- ServerAsset asset = find(event.getEntityId(), true);
+ ServerAsset asset = find(event.getAssetId(), true);
if (asset != null)
replyWithAttributeEvents(sessionKey, asset, event.getAttributeNames());
return;
@@ -170,7 +170,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
}
ServerAsset asset = find(
- event.getEntityId(),
+ event.getAssetId(),
true,
managerIdentityService.isRestrictedUser(auth.getUserId()) // Restricted users get filtered state
);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java b/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java
index 8a7e73fab..33dcdee12 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/map/MapService.java
@@ -26,7 +26,6 @@ import org.openremote.manager.server.security.ManagerIdentityService;
import org.openremote.manager.server.web.ManagerWebService;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.StringValue;
import org.openremote.model.value.Values;
import javax.ws.rs.core.UriBuilder;
@@ -39,7 +38,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
-import java.util.Optional;
import java.util.logging.Logger;
import static org.openremote.container.util.MapAccess.getString;
@@ -108,7 +106,7 @@ public class MapService implements ContainerService {
throw new RuntimeException("Error parsing map settings: " + mapSettingsPath.toAbsolutePath(), ex);
}
- ObjectValue style = mapSettings.<ObjectValue>get("style").orElseThrow(() ->
+ ObjectValue style = mapSettings.getObject("style").orElseThrow(() ->
new RuntimeException("Missing 'style' field in map settings style: " + mapSettingsPath.toAbsolutePath())
);
@@ -117,9 +115,9 @@ public class MapService implements ContainerService {
if (style.hasKey("sprite")) {
// Rewrite path to sprite file to our external host
String spritePath =
- style.<StringValue>get("sprite").orElseThrow(() ->
+ style.getString("sprite").orElseThrow(() ->
new RuntimeException("Missing 'sprite' field in map settings style: " + mapSettingsPath.toAbsolutePath())
- ).getString();
+ );
String spriteUri =
baseUriBuilder.clone()
.replacePath(ManagerWebService.MANAGER_PATH)
@@ -165,7 +163,7 @@ public class MapService implements ContainerService {
ObjectValue metadataJson = Values.<ObjectValue>parse(resultMap.get("json")).orElseThrow(() ->
new RuntimeException("Error parsing JSON metadata from map database")
);
- ArrayValue vectorLayers = metadataJson.<ArrayValue>get("vector_layers").orElseThrow(() ->
+ ArrayValue vectorLayers = metadataJson.getArray("vector_layers").orElseThrow(() ->
new RuntimeException("Missing 'vector_layers' field in metadata from map database")
);
vectorTiles.put("vector_layers", vectorLayers);
diff --git a/model/src/main/java/org/openremote/model/AbstractTypeHolder.java b/model/src/main/java/org/openremote/model/AbstractTypeHolder.java
new file mode 100644
index 000000000..0abd9448f
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/AbstractTypeHolder.java
@@ -0,0 +1,73 @@
+/*
+ * 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;
+
+import org.openremote.model.value.ObjectValue;
+import org.openremote.model.value.Values;
+
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * Base class for all model classes which have to internally store data in an
+ * {@link ObjectValue} that has a <code>type</code> field that accepts a string.
+ */
+public abstract class AbstractTypeHolder {
+
+ protected static final String TYPE_FIELD_NAME = "type";
+
+ final protected ObjectValue objectValue;
+
+ public AbstractTypeHolder() {
+ this(Values.createObject());
+ }
+
+ public AbstractTypeHolder(String type) {
+ this(Values.createObject(), type);
+ }
+
+ public AbstractTypeHolder(ObjectValue objectValue) {
+ Objects.requireNonNull(objectValue);
+ this.objectValue = objectValue;
+ }
+
+ public AbstractTypeHolder(ObjectValue objectValue, String type) {
+ Objects.requireNonNull(objectValue);
+ this.objectValue = objectValue;
+ setType(type);
+ }
+
+ public ObjectValue getObjectValue() {
+ return objectValue;
+ }
+
+ public void setType(String type) {
+ objectValue.put(TYPE_FIELD_NAME, type);
+ }
+
+ public Optional<String> getType() {
+ return objectValue.getString(TYPE_FIELD_NAME);
+ }
+
+ @Override
+ public String toString() {
+ return objectValue.toJson();
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.java
index 6b88b10c2..da5426e44 100644
--- a/model/src/main/java/org/openremote/model/asset/Asset.java
+++ b/model/src/main/java/org/openremote/model/asset/Asset.java
@@ -33,7 +33,10 @@ import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.math.RoundingMode;
-import java.util.*;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -336,7 +339,6 @@ public class Asset implements IdentifiableEntity {
}
-
public void addAttribute(AssetAttribute attribute) throws IllegalArgumentException {
if (getAttributesStream().anyMatch(attr -> isAttributeNameEqualTo(attr, attribute.getName().orElse(null)))) {
throw new IllegalArgumentException("Attribute by this name already exists");
@@ -579,11 +581,11 @@ public class Asset implements IdentifiableEntity {
public GeoJSON getGeoFeature(int maxNameLength) {
if (!hasGeoFeature())
return GeoJSON.EMPTY_FEATURE_COLLECTION;
- return new GeoJSON().setType("FeatureCollection").setFeatures(
- new GeoJSONFeature().setType("Feature")
+ return new GeoJSON("FeatureCollection").setFeatures(
+ new GeoJSONFeature("Feature")
.setProperty("id", getId())
.setProperty("title", TextUtil.ellipsize(getName(), maxNameLength))
- .setGeometry(new GeoJSONGeometry().setPoint(getCoordinates()))
+ .setGeometry(new GeoJSONGeometry(getCoordinates()))
);
}
diff --git a/model/src/main/java/org/openremote/model/ReadAttributesEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAssetAttributesEvent.java
similarity index 69%
rename from model/src/main/java/org/openremote/model/ReadAttributesEvent.java
rename to model/src/main/java/org/openremote/model/asset/ReadAssetAttributesEvent.java
index e13d12123..13879795f 100644
--- a/model/src/main/java/org/openremote/model/ReadAttributesEvent.java
+++ b/model/src/main/java/org/openremote/model/asset/ReadAssetAttributesEvent.java
@@ -17,8 +17,9 @@
* 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;
+package org.openremote.model.asset;
+import org.openremote.model.AttributeEvent;
import org.openremote.model.event.shared.SharedEvent;
import java.util.Arrays;
@@ -27,32 +28,32 @@ import java.util.Arrays;
* A client sends this event to the server to refresh its attribute state, expecting
* the server to answer "soon" with {@link AttributeEvent}s. If the server
* decides that the client doesn't have the right permissions, or if anything
- * else is not in order (e.g. the entity doesn't exist), the server might not react
+ * else is not in order (e.g. the asset doesn't exist), the server might not react
* at all.
* <p>
- * If no attribute names and only an entity identifier are provided, all attributes
- * of the entity, accessible by the client, will be read/returned.
+ * If no attribute names and only an asset identifier are provided, all attributes
+ * of the asset, accessible by the client, will be read/returned.
*/
-public class ReadAttributesEvent extends SharedEvent {
+public class ReadAssetAttributesEvent extends SharedEvent {
- protected String entityId;
+ protected String assetId;
protected String[] attributeNames;
- protected ReadAttributesEvent() {
+ protected ReadAssetAttributesEvent() {
}
- public ReadAttributesEvent(String entityId, String... attributeNames) {
- this.entityId = entityId;
+ public ReadAssetAttributesEvent(String assetId, String... attributeNames) {
+ this.assetId = assetId;
this.attributeNames = attributeNames;
}
- public String getEntityId() {
- return entityId;
+ public String getAssetId() {
+ return assetId;
}
- public void setEntityId(String entityId) {
- this.entityId = entityId;
+ public void setAssetId(String assetId) {
+ this.assetId = assetId;
}
public String[] getAttributeNames() {
@@ -66,7 +67,7 @@ public class ReadAttributesEvent extends SharedEvent {
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
- "entityId='" + entityId + '\'' +
+ "assetId='" + assetId + '\'' +
", attributeNames=" + Arrays.toString(attributeNames) +
'}';
}
diff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
index 7a343429e..587c1ece0 100644
--- a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
+++ b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.model.AttributeEvent;
-import org.openremote.model.ReadAttributesEvent;
+import org.openremote.model.asset.ReadAssetAttributesEvent;
import org.openremote.model.asset.AssetTreeModifiedEvent;
import org.openremote.model.event.Event;
@@ -33,7 +33,7 @@ import org.openremote.model.event.Event;
@JsonSubTypes({
// Events used on client and server (serializable)
@JsonSubTypes.Type(value = AttributeEvent.class, name = "attribute"),
- @JsonSubTypes.Type(value = ReadAttributesEvent.class, name = "read-attributes"),
+ @JsonSubTypes.Type(value = ReadAssetAttributesEvent.class, name = "read-asset-attributes"),
@JsonSubTypes.Type(value = AssetTreeModifiedEvent.class, name = "asset-tree-modified")
})
@JsonTypeInfo(
diff --git a/model/src/main/java/org/openremote/model/geo/GeoJSON.java b/model/src/main/java/org/openremote/model/geo/GeoJSON.java
index 49c79f025..b99cb67af 100644
--- a/model/src/main/java/org/openremote/model/geo/GeoJSON.java
+++ b/model/src/main/java/org/openremote/model/geo/GeoJSON.java
@@ -19,45 +19,25 @@
*/
package org.openremote.model.geo;
-import elemental.json.Json;
-import elemental.json.JsonArray;
-import elemental.json.JsonObject;
-import elemental.json.JsonType;
+import org.openremote.model.AbstractTypeHolder;
+import org.openremote.model.value.ArrayValue;
+import org.openremote.model.value.Values;
-public class GeoJSON {
+public class GeoJSON extends AbstractTypeHolder {
- public static final GeoJSON EMPTY_FEATURE_COLLECTION = new GeoJSON().setType("FeatureCollection").setEmptyFeatures();
+ public static final GeoJSON EMPTY_FEATURE_COLLECTION = new GeoJSON("FeatureCollection").setEmptyFeatures();;
- protected JsonObject jsonObject;
-
- public GeoJSON() {
- this(Json.createObject());
- }
-
- public GeoJSON(JsonObject jsonObject) {
- this.jsonObject = jsonObject;
- }
-
- public JsonObject getJsonObject() {
- return jsonObject;
- }
-
- public GeoJSON setType(String type) {
- jsonObject.put("type", type);
- return this;
- }
-
- public String getType() {
- return jsonObject.hasKey("type") ? jsonObject.get("type").asString() : null;
+ public GeoJSON(String type) {
+ super(type);
}
public GeoJSON setFeatures(GeoJSONFeature... features) {
if (features != null) {
for (int i = 0; i < features.length; i++) {
GeoJSONFeature feature = features[i];
- JsonArray array = Json.createArray();
- array.set(i, feature.getJsonObject());
- jsonObject.put("features", array);
+ ArrayValue array = Values.createArray();
+ array.set(i, feature.getObjectValue());
+ objectValue.put("features", array);
}
} else {
return setEmptyFeatures();
@@ -66,25 +46,7 @@ public class GeoJSON {
}
public GeoJSON setEmptyFeatures() {
- jsonObject.put("features", Json.createArray());
+ objectValue.put("features", Values.createArray());
return this;
}
-
- public GeoJSONFeature[] getFeatures() {
- if (jsonObject.hasKey("features") && jsonObject.get("features").getType() == JsonType.ARRAY) {
- JsonArray array = jsonObject.getArray("features");
- GeoJSONFeature[] result = new GeoJSONFeature[array.length()];
- for (int i = 0; i < array.length(); i++) {
- result[i] = new GeoJSONFeature(array.getObject(i));
- }
- return result;
- } else {
- return new GeoJSONFeature[0];
- }
- }
-
- @Override
- public String toString() {
- return jsonObject.toJson();
- }
}
diff --git a/model/src/main/java/org/openremote/model/geo/GeoJSONFeature.java b/model/src/main/java/org/openremote/model/geo/GeoJSONFeature.java
index eb6ec9b87..81b305f00 100644
--- a/model/src/main/java/org/openremote/model/geo/GeoJSONFeature.java
+++ b/model/src/main/java/org/openremote/model/geo/GeoJSONFeature.java
@@ -19,66 +19,35 @@
*/
package org.openremote.model.geo;
-import elemental.json.Json;
-import elemental.json.JsonObject;
-import elemental.json.JsonValue;
+import org.openremote.model.AbstractTypeHolder;
+import org.openremote.model.value.ObjectValue;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.Values;
-public class GeoJSONFeature {
+public class GeoJSONFeature extends AbstractTypeHolder {
- protected JsonObject jsonObject;
-
- public GeoJSONFeature() {
- this(Json.createObject());
- }
-
- public GeoJSONFeature(JsonObject jsonObject) {
- this.jsonObject = jsonObject;
- }
-
- public JsonObject getJsonObject() {
- return jsonObject;
- }
-
- public GeoJSONFeature setType(String type) {
- jsonObject.put("type", type);
- return this;
- }
-
- public String getType() {
- return jsonObject.hasKey("type") ? jsonObject.get("type").asString() : null;
+ public GeoJSONFeature(String type) {
+ super(type);
}
public GeoJSONFeature setProperty(String name, String value) {
- return setProperty(name, Json.create(value));
- }
-
- public GeoJSONFeature setProperty(String name, double value) {
- return setProperty(name, Json.create(value));
+ return setProperty(name, Values.create(value));
}
- public GeoJSONFeature setProperty(String name, boolean value) {
- return setProperty(name, Json.create(value));
- }
-
- protected GeoJSONFeature setProperty(String name, JsonValue value) {
- JsonObject properties = jsonObject.hasKey("properties") ? jsonObject.getObject("properties") : Json.createObject();
+ protected GeoJSONFeature setProperty(String name, Value value) {
+ ObjectValue properties = objectValue.getObject("properties").orElse(Values.createObject());
properties.put(name, value);
- if (!jsonObject.hasKey("properties"))
- jsonObject.put("properties", properties);
+ if (!objectValue.hasKey("properties"))
+ objectValue.put("properties", properties);
return this;
}
public GeoJSONFeature setGeometry(GeoJSONGeometry geometry) {
if (geometry != null) {
- jsonObject.put("geometry", geometry.getJsonObject());
+ objectValue.put("geometry", geometry.getObjectValue());
} else {
- jsonObject.remove("geometry");
+ objectValue.remove("geometry");
}
return this;
}
-
- @Override
- public String toString() {
- return jsonObject.toJson();
- }
}
diff --git a/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java b/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java
index edf013982..7a98f0bc6 100644
--- a/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java
+++ b/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java
@@ -19,47 +19,25 @@
*/
package org.openremote.model.geo;
-import elemental.json.Json;
-import elemental.json.JsonArray;
-import elemental.json.JsonObject;
+import org.openremote.model.AbstractTypeHolder;
+import org.openremote.model.value.ArrayValue;
+import org.openremote.model.value.Values;
-public class GeoJSONGeometry {
+public class GeoJSONGeometry extends AbstractTypeHolder {
- protected JsonObject jsonObject;
-
- public GeoJSONGeometry() {
- this(Json.createObject());
- }
-
- public GeoJSONGeometry(JsonObject jsonObject) {
- this.jsonObject = jsonObject;
+ public GeoJSONGeometry(String type) {
+ super(type);
}
- public JsonObject getJsonObject() {
- return jsonObject;
- }
-
- public GeoJSONGeometry setType(String type) {
- jsonObject.put("type", type);
- return this;
- }
-
- public String getType() {
- return jsonObject.hasKey("type") ? jsonObject.get("type").asString() : null;
- }
-
- public GeoJSONGeometry setPoint(double[] coordinates) {
- setType("Point");
- JsonArray array = Json.createArray();
+ /**
+ * Of type "Point".
+ */
+ public GeoJSONGeometry(double[] coordinates) {
+ this("Point");
+ ArrayValue array = Values.createArray();
for (int i = 0; i < coordinates.length; i++) {
array.set(i, coordinates[i]);
}
- jsonObject.put("coordinates", array);
- return this;
- }
-
- @Override
- public String toString() {
- return jsonObject.toJson();
+ objectValue.put("coordinates", array);
}
}
diff --git a/model/src/main/java/org/openremote/model/value/ArrayValue.java b/model/src/main/java/org/openremote/model/value/ArrayValue.java
index 4acec22bb..5caf76fb7 100644
--- a/model/src/main/java/org/openremote/model/value/ArrayValue.java
+++ b/model/src/main/java/org/openremote/model/value/ArrayValue.java
@@ -16,10 +16,21 @@
package org.openremote.model.value;
import java.util.Optional;
+import java.util.stream.Stream;
public interface ArrayValue extends Value {
- <T extends Value> Optional<T> get(int index);
+ Optional<Value> get(int index);
+
+ Optional<String> getString(int index);
+
+ Optional<Boolean> getBoolean(int index);
+
+ Optional<Double> getNumber(int index);
+
+ Optional<ArrayValue> getArray(int index);
+
+ Optional<ObjectValue> getObject(int index);
int length();
diff --git a/model/src/main/java/org/openremote/model/value/ObjectValue.java b/model/src/main/java/org/openremote/model/value/ObjectValue.java
index f52b39367..04e6484ab 100644
--- a/model/src/main/java/org/openremote/model/value/ObjectValue.java
+++ b/model/src/main/java/org/openremote/model/value/ObjectValue.java
@@ -24,7 +24,17 @@ import java.util.Optional;
*/
public interface ObjectValue extends Value {
- <T extends Value> Optional<T> get(String key);
+ Optional<Value> get(String key);
+
+ Optional<String> getString(String key);
+
+ Optional<Boolean> getBoolean(String key);
+
+ Optional<Double> getNumber(String key);
+
+ Optional<ArrayValue> getArray(String key);
+
+ Optional<ObjectValue> getObject(String key);
String[] keys();
diff --git a/model/src/main/java/org/openremote/model/value/Value.java b/model/src/main/java/org/openremote/model/value/Value.java
index 8368ae2ba..56a7994f2 100644
--- a/model/src/main/java/org/openremote/model/value/Value.java
+++ b/model/src/main/java/org/openremote/model/value/Value.java
@@ -15,6 +15,8 @@
*/
package org.openremote.model.value;
+import com.google.gwt.core.client.JavaScriptObject;
+
/**
* Base interface for all values. This is a Java API for a JSON model and instances
* may be serialized into JSON text with {@link #toJson()} or parsed from JSON text
@@ -35,8 +37,8 @@ public interface Value {
/**
* If used in a GWT context, converts the object to a native
* {@link com.google.gwt.core.client.JavaScriptObject} suitable for passing to
- * native and {@link jsinterop.annotations.JsType} methods. Otherwise, returns
- * the current object in other contexts, such as server-side use.
+ * native and {@link jsinterop.annotations.JsType} methods. Otherwise, throws
+ * {@link ValueException}.
*/
- Object toNative() throws ValueException;
+ JavaScriptObject asNativeObject() throws ValueException;
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/ArrayValueImpl.java b/model/src/main/java/org/openremote/model/value/impl/ArrayValueImpl.java
index 25afcd8b2..8a1badd9d 100644
--- a/model/src/main/java/org/openremote/model/value/impl/ArrayValueImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/ArrayValueImpl.java
@@ -30,17 +30,51 @@ public class ArrayValueImpl extends ValueImpl implements ArrayValue {
this.factory = factory;
}
- @SuppressWarnings("unchecked")
@Override
- public <T extends Value> Optional<T> get(int index) {
- return index >= 0 && values.size() > index ? Optional.of((T) values.get(index)) : Optional.empty();
+ public Optional<Value> get(int index) {
+ return index >= 0 && values.size() > index ? Optional.of(values.get(index)) : Optional.empty();
}
@Override
- public List<Object> getObject() {
+ public Optional<String> getString(int index) {
+ return get(index)
+ .filter(value -> value.getType() == ValueType.STRING && value instanceof StringValue)
+ .map(value -> ((StringValue)value).getString());
+ }
+
+ @Override
+ public Optional<Boolean> getBoolean(int index) {
+ return get(index)
+ .filter(value -> value.getType() == ValueType.BOOLEAN && value instanceof BooleanValue)
+ .map(value -> ((BooleanValue)value).getBoolean());
+ }
+
+ @Override
+ public Optional<Double> getNumber(int index) {
+ return get(index)
+ .filter(value -> value.getType() == ValueType.NUMBER && value instanceof NumberValue)
+ .map(value -> ((NumberValue)value).getNumber());
+ }
+
+ @Override
+ public Optional<ArrayValue> getArray(int index) {
+ return get(index)
+ .filter(value -> value.getType() == ValueType.ARRAY && value instanceof ArrayValue)
+ .map(value -> ((ArrayValue)value));
+ }
+
+ @Override
+ public Optional<ObjectValue> getObject(int index) {
+ return get(index)
+ .filter(value -> value.getType() == ValueType.OBJECT && value instanceof ObjectValue)
+ .map(value -> ((ObjectValue)value));
+ }
+
+ @Override
+ public List<Object> asObject() {
List<Object> objs = new ArrayList<>();
for (Value val : values) {
- objs.add(((ValueImpl) val).getObject());
+ objs.add(((ValueImpl) val).asObject());
}
return objs;
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/BooleanValueImpl.java b/model/src/main/java/org/openremote/model/value/impl/BooleanValueImpl.java
index 4c7de5678..53f89dd96 100644
--- a/model/src/main/java/org/openremote/model/value/impl/BooleanValueImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/BooleanValueImpl.java
@@ -32,7 +32,7 @@ public class BooleanValueImpl extends ValueImpl implements BooleanValue {
}
@Override
- public Boolean getObject() {
+ public Boolean asObject() {
return getBoolean();
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/NumberValueImpl.java b/model/src/main/java/org/openremote/model/value/impl/NumberValueImpl.java
index 4c2a3244f..d57afb510 100644
--- a/model/src/main/java/org/openremote/model/value/impl/NumberValueImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/NumberValueImpl.java
@@ -32,7 +32,7 @@ public class NumberValueImpl extends ValueImpl implements NumberValue {
}
@Override
- public Double getObject() {
+ public Double asObject() {
return getNumber();
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/ObjectValueImpl.java b/model/src/main/java/org/openremote/model/value/impl/ObjectValueImpl.java
index aa654d007..d4943961b 100644
--- a/model/src/main/java/org/openremote/model/value/impl/ObjectValueImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/ObjectValueImpl.java
@@ -43,17 +43,51 @@ public class ObjectValueImpl extends ValueImpl implements ObjectValue {
this.factory = factory;
}
- @SuppressWarnings("unchecked")
@Override
- public <T extends Value> Optional<T> get(String key) {
- return map.containsKey(key) ? Optional.of((T) map.get(key)) : Optional.empty();
+ public Optional<Value> get(String key) {
+ return map.containsKey(key) ? Optional.of(map.get(key)) : Optional.empty();
}
@Override
- public Map<String, Object> getObject() {
+ public Optional<String> getString(String key) {
+ return get(key)
+ .filter(value -> value.getType() == ValueType.STRING && value instanceof StringValue)
+ .map(value -> ((StringValue)value).getString());
+ }
+
+ @Override
+ public Optional<Boolean> getBoolean(String key) {
+ return get(key)
+ .filter(value -> value.getType() == ValueType.BOOLEAN && value instanceof BooleanValue)
+ .map(value -> ((BooleanValue)value).getBoolean());
+ }
+
+ @Override
+ public Optional<Double> getNumber(String key) {
+ return get(key)
+ .filter(value -> value.getType() == ValueType.NUMBER && value instanceof NumberValue)
+ .map(value -> ((NumberValue)value).getNumber());
+ }
+
+ @Override
+ public Optional<ArrayValue> getArray(String key) {
+ return get(key)
+ .filter(value -> value.getType() == ValueType.ARRAY && value instanceof ArrayValue)
+ .map(value -> ((ArrayValue)value));
+ }
+
+ @Override
+ public Optional<ObjectValue> getObject(String key) {
+ return get(key)
+ .filter(value -> value.getType() == ValueType.OBJECT && value instanceof ObjectValue)
+ .map(value -> ((ObjectValue)value));
+ }
+
+ @Override
+ public Map<String, Object> asObject() {
Map<String, Object> obj = new HashMap<>();
for (Map.Entry<String, Value> e : map.entrySet()) {
- obj.put(e.getKey(), ((ValueImpl) e.getValue()).getObject());
+ obj.put(e.getKey(), ((ValueImpl) e.getValue()).asObject());
}
return obj;
}
@@ -157,7 +191,7 @@ public class ObjectValueImpl extends ValueImpl implements ObjectValue {
public int hashCode() {
int result = 31;
result = result * 3;
- result = result * getObject().hashCode();
+ result = result * asObject().hashCode();
return result;
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/StringValueImpl.java b/model/src/main/java/org/openremote/model/value/impl/StringValueImpl.java
index bc010fa36..0dc4f76fb 100644
--- a/model/src/main/java/org/openremote/model/value/impl/StringValueImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/StringValueImpl.java
@@ -28,7 +28,7 @@ public class StringValueImpl extends ValueImpl implements StringValue {
}
@Override
- public String getObject() {
+ public String asObject() {
return getString();
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/ValueImpl.java b/model/src/main/java/org/openremote/model/value/impl/ValueImpl.java
index c759c008d..841385c40 100644
--- a/model/src/main/java/org/openremote/model/value/impl/ValueImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/ValueImpl.java
@@ -16,6 +16,7 @@
package org.openremote.model.value.impl;
import com.google.gwt.core.client.GWT;
+import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsonUtils;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueException;
@@ -23,20 +24,20 @@ import org.openremote.model.value.ValueException;
public abstract class ValueImpl implements Value {
@Override
- public Object toNative() throws ValueException {
+ public JavaScriptObject asNativeObject() throws ValueException {
if (GWT.isClient())
return JsonUtils.safeEval(this.toJson());
else
- return this;
+ throw new ValueException("Not a GWT/JavaScript runtime environment");
}
@Override
public String toString() {
- Object o = getObject();
+ Object o = asObject();
return o != null ? o.toString() : "null";
}
- public abstract Object getObject();
+ public abstract Object asObject();
public abstract void traverse(ValueVisitor visitor, ValueContext ctx) throws ValueException;