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
Improved views with coordinates based on ObjectValue and added REST endpoints for location updates
Michael Rademaker5cbc5693f86a
diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AbstractAssetActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AbstractAssetActivity.javaindex fe371d91b..9a6bbe97f 100644--- a/client/src/main/java/org/openremote/app/client/assets/asset/AbstractAssetActivity.java+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AbstractAssetActivity.java@@ -204,9 +204,9 @@ public abstract class AbstractAssetActivity<Vview.getAssetNavigation().setEnabled(assetId != null);view.setName(asset.getName());view.setCreatedOn(asset.getCreatedOn());- view.setLocation(asset.getCoordinates());+ view.setLocation(asset.getCoordinates().orElse(null));view.showDroppedPin(asset.getGeoFeature(20));- view.flyTo(asset.getCoordinates());+ view.flyTo(asset.getCoordinates().orElse(null));view.setAccessPublicRead(asset.isAccessPublicRead());}diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetBaseView.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetBaseView.javaindex 627be6c76..d3c81a041 100644--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetBaseView.java+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetBaseView.java@@ -69,11 +69,11 @@ public interface AssetBaseView<P extends AssetBaseView.Presenter> extends IsWidgboolean isMapInitialised();- void setLocation(double[] coordinates);+ void setLocation(ObjectValue coordinates);void showDroppedPin(GeoJSON geoFeature);- void flyTo(double[] coordinates);+ void flyTo(ObjectValue coordinates);void setAccessPublicRead(boolean enabled);diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.javaindex f78b44262..41a0c4aff 100644--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java@@ -49,8 +49,10 @@ import org.openremote.model.interop.Consumer;import org.openremote.model.map.MapResource;import org.openremote.model.util.EnumUtil;import org.openremote.model.util.Pair;+import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Value;import org.openremote.model.value.ValueType;+import org.openremote.model.value.Values;import javax.inject.Inject;import java.util.*;@@ -82,7 +84,7 @@ public class AssetEditActivityprotected final Consumer<ConstraintViolation[]> validationErrorHandler;protected List<ProtocolDescriptor> protocolDescriptors = new ArrayList<>();protected List<MetaItemDescriptor> metaItemDescriptors = new ArrayList<>(Arrays.asList(AssetMeta.values())); // TODO Get meta item descriptors from server- double[] selectedCoordinates;+ ObjectValue selectedCoordinates;protected List<AssetAttribute> initialAssetAttributes;@Inject@@ -190,7 +192,7 @@ public class AssetEditActivity@Overridepublic void onMapClicked(double lng, double lat) {- selectedCoordinates = new double[]{lng, lat};+ selectedCoordinates = Values.createObject().put("longitude", lng).put("latitude", lat);view.hideMapPopup();view.showMapPopup(lng, lat, environment.getMessages().selectedLocation());view.setLocation(selectedCoordinates);@@ -462,8 +464,8 @@ public class AssetEditActivityif (selectedCoordinates != null) {view.flyTo(selectedCoordinates);}- else if (asset.getCoordinates() != null) {- view.flyTo(asset.getCoordinates());+ else if (asset.getCoordinates().isPresent()) {+ view.flyTo(asset.getCoordinates().get());}}diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditImpl.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditImpl.javaindex 5bdaf7541..ce2bdcdf8 100644--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditImpl.java+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditImpl.java@@ -327,7 +327,7 @@ public class AssetEditImpl extends FormViewImpl implements AssetEdit {}@Override- public void setLocation(double[] coordinates) {+ public void setLocation(ObjectValue coordinates) {if (locationOutput.setCoordinates(managerMessages.selectLocation(), coordinates)) {centerMapButton.setEnabled(true);} else {@@ -374,7 +374,7 @@ public class AssetEditImpl extends FormViewImpl implements AssetEdit {}@Override- public void flyTo(double[] coordinates) {+ public void flyTo(ObjectValue coordinates) {if (mapWidget.isMapReady()) {mapWidget.flyTo(coordinates);}diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.javaindex f979f7b6a..88bf47537 100644--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java@@ -164,7 +164,7 @@ public class AssetViewActivity@Overridepublic void centerMap() {- view.flyTo(asset.getCoordinates());+ view.flyTo(asset.getCoordinates().orElse(null));}@Overridediff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewImpl.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewImpl.javaindex f3cc8defe..fcf411ea3 100644--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewImpl.java+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewImpl.java@@ -294,7 +294,7 @@ public class AssetViewImpl extends Composite implements AssetView {/* ############################################################################ */@Override- public void setLocation(double[] coordinates) {+ public void setLocation(ObjectValue coordinates) {if (locationOutput.setCoordinates(managerMessages.selectLocation(), coordinates)) {locationGroup.setVisible(true);mapWidget.setVisible(true);@@ -327,7 +327,7 @@ public class AssetViewImpl extends Composite implements AssetView {}@Override- public void flyTo(double[] coordinates) {+ public void flyTo(ObjectValue coordinates) {if (mapWidget.isMapReady()) {mapWidget.flyTo(coordinates);}diff --git a/client/src/main/java/org/openremote/app/client/map/MapActivity.java b/client/src/main/java/org/openremote/app/client/map/MapActivity.javaindex 348aef649..e6a8828eb 100644--- a/client/src/main/java/org/openremote/app/client/map/MapActivity.java+++ b/client/src/main/java/org/openremote/app/client/map/MapActivity.java@@ -189,7 +189,7 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapVif (asset.hasGeoFeature()) {GeoJSON geoFeature = asset.getGeoFeature(30);view.showDroppedPin(geoFeature);- view.flyTo(asset.getCoordinates());+ view.flyTo(asset.getCoordinates().get());}}diff --git a/client/src/main/java/org/openremote/app/client/map/MapView.java b/client/src/main/java/org/openremote/app/client/map/MapView.javaindex 2c2bf94fc..d8132079a 100644--- a/client/src/main/java/org/openremote/app/client/map/MapView.java+++ b/client/src/main/java/org/openremote/app/client/map/MapView.java@@ -41,7 +41,7 @@ public interface MapView extends IsWidget {void showDroppedPin(GeoJSON geoFeature);- void flyTo(double[] coordinates);+ void flyTo(ObjectValue coordinates);void showInfoItems(List<MapInfoItem> infoItems);}diff --git a/client/src/main/java/org/openremote/app/client/map/MapViewImpl.java b/client/src/main/java/org/openremote/app/client/map/MapViewImpl.javaindex 57ec28f35..7a6fdc939 100644--- a/client/src/main/java/org/openremote/app/client/map/MapViewImpl.java+++ b/client/src/main/java/org/openremote/app/client/map/MapViewImpl.java@@ -153,7 +153,7 @@ public class MapViewImpl extends Composite implements MapView {}@Override- public void flyTo(double[] coordinates) {+ public void flyTo(ObjectValue coordinates) {if (mapWidget.isMapReady()) {mapWidget.flyTo(coordinates);}diff --git a/client/src/main/java/org/openremote/app/client/widget/FormOutputLocation.java b/client/src/main/java/org/openremote/app/client/widget/FormOutputLocation.javaindex 249501cef..8d69770e6 100644--- a/client/src/main/java/org/openremote/app/client/widget/FormOutputLocation.java+++ b/client/src/main/java/org/openremote/app/client/widget/FormOutputLocation.java@@ -22,6 +22,7 @@ package org.openremote.app.client.widget;import com.google.gwt.user.client.ui.Composite;import com.google.gwt.user.client.ui.FlowPanel;import com.google.gwt.user.client.ui.InlineLabel;+import org.openremote.model.value.ObjectValue;import java.math.BigDecimal;import java.math.RoundingMode;@@ -33,7 +34,7 @@ public class FormOutputLocation extends Composite {protected FormButton toggleButton = new FormButton();protected boolean reversed = false;- protected double[] coordinates;+ protected ObjectValue coordinates;public FormOutputLocation() {initWidget(panel);@@ -51,8 +52,8 @@ public class FormOutputLocation extends Composite {});}- public boolean setCoordinates(String noLocationText, double[] coordinates) {- if (coordinates != null && coordinates.length == 2) {+ public boolean setCoordinates(String noLocationText, ObjectValue coordinates) {+ if (coordinates != null && coordinates.hasKey("latitude") && coordinates.hasKey("longitude")) {this.coordinates = coordinates;toggleButton.setVisible(true);update();@@ -67,15 +68,16 @@ public class FormOutputLocation extends Composite {// TODO: This assumes 0 is Lng and 1 is Lat, which is true for PostGIS backend// Rounding to 5 decimals gives us precision of about 1 meter, should be enoughif (reversed) {- coordinatesLabel.setText(round(coordinates[0], 5) + " " + round(coordinates[1], 5));+ coordinatesLabel.setText(round(coordinates.getNumber("latitude").orElse(0d), 5) + " " + round(coordinates.getNumber("longitude").orElse(0d), 5));toggleButton.getUpFace().setText("Lng | Lat");toggleButton.getDownFace().setText("Lng | Lat");} else {- coordinatesLabel.setText(round(coordinates[1], 5) + " " + round(coordinates[0], 5));+ coordinatesLabel.setText(round(coordinates.getNumber("longitude").orElse(0d), 5) + " " + round(coordinates.getNumber("latitude").orElse(0d), 5));toggleButton.getUpFace().setText("Lat | Lng");toggleButton.getDownFace().setText("Lat | Lng");}}+protected String round(double d, int places) {return new BigDecimal(d).setScale(places, RoundingMode.HALF_UP).toString();}diff --git a/client/src/main/java/org/openremote/app/client/widget/MapWidget.java b/client/src/main/java/org/openremote/app/client/widget/MapWidget.javaindex bc8806def..324d80de4 100644--- a/client/src/main/java/org/openremote/app/client/widget/MapWidget.java+++ b/client/src/main/java/org/openremote/app/client/widget/MapWidget.java@@ -229,15 +229,15 @@ public class MapWidget extends FlowPanel {mapboxMap.getSource(featureSourceId).setData(geoFeature.getObjectValue().asAny());}- public void flyTo(double[] coordinates) {+ public void flyTo(ObjectValue coordinates) {if (!isMapReady())throw new IllegalStateException("Map not ready");- if (coordinates == null || coordinates.length != 2)+ if (coordinates == null || !coordinates.hasKey("latitude") || !coordinates.hasKey("longitude"))return;ObjectValue options = Values.createObject();ArrayValue center = Values.createArray();- center.set(0, coordinates[0]);- center.set(1, coordinates[1]);+ center.set(0, coordinates.getNumber("longitude").get());+ center.set(1, coordinates.getNumber("latitude").get());options.put("center", center);mapboxMap.flyTo(options.asAny());}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex 12f90335d..9790bccd9 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -27,12 +27,11 @@ import org.openremote.model.Constants;import org.openremote.model.asset.*;import org.openremote.model.asset.BaseAssetQuery.Select;import org.openremote.model.attribute.*;+import org.openremote.model.geo.GeoJSONGeometry;import org.openremote.model.http.RequestParams;import org.openremote.model.security.Tenant;import org.openremote.model.util.TextUtil;-import org.openremote.model.value.Value;-import org.openremote.model.value.ValueException;-import org.openremote.model.value.Values;+import org.openremote.model.value.*;import javax.ws.rs.WebApplicationException;import javax.ws.rs.core.HttpHeaders;@@ -600,4 +599,51 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourreturn result;}++ @Override+ public void updateLocation(RequestParams requestParams, String assetId, GeoJSONGeometry location) {+ try {+ Asset asset = get(requestParams, assetId);++ AssetAttribute locationAttribute = asset.getAttribute(AttributeDescriptorImpl.LOCATION.getName())+ .orElse(AssetAttribute.createWithDescriptor(AttributeDescriptorImpl.LOCATION));++ ObjectValue currentCoordinates = locationAttribute.getValueAsObject().orElse(Values.createObject());+ ArrayValue arrayValue = location.getObjectValue().getArray("coordinates").orElseThrow(() -> new IllegalStateException("location doesn't contain 'coordinates' key"));+ locationAttribute.setValue(currentCoordinates.put("latitude", arrayValue.getNumber(0).orElse(0d)).put("longitude", arrayValue.getNumber(1).orElse(0d)));++ asset.replaceAttribute(locationAttribute);++ asset = assetStorageService.merge(asset);++ } catch (IllegalStateException ex) {+ throw new WebApplicationException(ex, BAD_REQUEST);+ }+ }++ @Override+ public void updatePublicAssetLocation(RequestParams requestParams, String assetId, GeoJSONGeometry location) {+ try {+ Asset[] result = queryPublicAssets(requestParams, new AssetQuery().id(assetId));++ if (result.length == 0) {+ throw new WebApplicationException(NOT_FOUND);+ }++ Asset asset = result[0];++ AssetAttribute locationAttribute = asset.getAttribute(AttributeDescriptorImpl.LOCATION.getName())+ .orElse(AssetAttribute.createWithDescriptor(AttributeDescriptorImpl.LOCATION));++ ObjectValue currentCoordinates = locationAttribute.getValueAsObject().orElse(Values.createObject());+ ArrayValue arrayValue = location.getObjectValue().getArray("coordinates").orElseThrow(() -> new IllegalStateException("location doesn't contain 'coordinates' key"));+ locationAttribute.setValue(currentCoordinates.put("latitude", arrayValue.getNumber(0).orElse(0d)).put("longitude", arrayValue.getNumber(1).orElse(0d)));++ asset.replaceAttribute(locationAttribute);++ asset = assetStorageService.merge(asset);+ } catch (IllegalStateException ex) {+ throw new WebApplicationException(ex, BAD_REQUEST);+ }+ }}diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.javaindex 0a27064d9..9e3c0bf49 100644--- a/model/src/main/java/org/openremote/model/asset/Asset.java+++ b/model/src/main/java/org/openremote/model/asset/Asset.java@@ -616,50 +616,37 @@ public class Asset implements IdentifiableEntity {// FUNCTIONAL METHODS BELOW// ---------------------------------------------------- public double[] getCoordinates() {+ public Optional<ObjectValue> getCoordinates() {Optional<AssetAttribute> locationAttribute = getAttributesStream().filter(attribute -> attribute.getNameOrThrow().equals(LOCATION.getName())).findFirst();if (locationAttribute.isPresent()) {- double[] coordinates = new double[2];- locationAttribute.get().getValueAsObject().ifPresent(objectValue -> {- coordinates[1] = objectValue.getNumber("latitude").orElse(0d);- coordinates[0] = objectValue.getNumber("longitude").orElse(0d);- });- return coordinates;+ return locationAttribute.get().getValueAsObject();} else {- return null;+ return Optional.empty();}}- public void setCoordinates(double[] coordinates) {+ public void setCoordinates(ObjectValue coordinates) {AssetAttribute locationAttribute = getAttributesStream().filter(attribute -> attribute.getNameOrThrow().equals(LOCATION.getName())).findFirst().orElse(new AssetAttribute(LOCATION.getName(), LOCATION.getType()));- ObjectValue objectValue = locationAttribute.getValueAsObject().orElse(Values.createObject());- objectValue- .put("latitude", coordinates[1])- .put("longitude", coordinates[0]);- locationAttribute.setValue(objectValue);-+ locationAttribute.setValue(coordinates);replaceAttribute(locationAttribute);}/**- * - * This assumes {@link #getCoordinates} array index 0 is Lng and index 1 is Lat,- * - * which is true for PostGIS backend. Because Lat/Lng is the 'right way', we flip- * - * it here for display. Rounding to 5 decimal places gives us precision of about 1 meter.- * -+ * Use {@link #hasGeoFeature()} before using this method to check if the is a Label to display*/public String getCoordinatesLabel() {return- new BigDecimal(getCoordinates()[1]).setScale(5, RoundingMode.HALF_UP) + " " +- new BigDecimal(getCoordinates()[0]).setScale(5, RoundingMode.HALF_UP) + " Lat|Lng";+ new BigDecimal(getCoordinates().get().getNumber("latitude").orElse(0d)).setScale(5, RoundingMode.HALF_UP) + " " ++ new BigDecimal(getCoordinates().get().getNumber("longitude").orElse(0d)).setScale(5, RoundingMode.HALF_UP) + " Lat|Lng";}public boolean hasGeoFeature() {- return getCoordinates() != null && getCoordinates().length == 2;+ return getCoordinates().isPresent() && getCoordinates().get().hasKey("latitude") && getCoordinates().get().hasKey("longitude");}public GeoJSON getGeoFeature(int maxNameLength) {@@ -669,7 +656,7 @@ public class Asset implements IdentifiableEntity {new GeoJSONFeature("Feature").setProperty("id", getId()).setProperty("title", TextUtil.ellipsize(getName(), maxNameLength))- .setGeometry(new GeoJSONGeometry(getCoordinates()))+ .setGeometry(new GeoJSONGeometry(getCoordinates().get())));}diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AssetAttribute.javaindex 6a42cd038..38264fa5e 100644--- a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java+++ b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java@@ -489,6 +489,10 @@ public class AssetAttribute extends Attribute {return Optional.of(objectValue);}+ public static AssetAttribute createWithDescriptor(AttributeDescriptor descriptor) {+ return createWithDescriptor(descriptor, null);+ }+public static AssetAttribute createWithDescriptor(AttributeDescriptor descriptor, Value value) {return new AssetAttribute(descriptor.getName(), descriptor.getType(), value).setMeta(descriptor.getDefaultMetaItems());diff --git a/model/src/main/java/org/openremote/model/asset/AssetResource.java b/model/src/main/java/org/openremote/model/asset/AssetResource.javaindex 570088f4b..ddd9640ee 100644--- a/model/src/main/java/org/openremote/model/asset/AssetResource.java+++ b/model/src/main/java/org/openremote/model/asset/AssetResource.java@@ -20,6 +20,9 @@package org.openremote.model.asset;import jsinterop.annotations.JsType;+import org.openremote.model.geo.GeoJSON;+import org.openremote.model.geo.GeoJSONFeature;+import org.openremote.model.geo.GeoJSONGeometry;import org.openremote.model.http.RequestParams;import org.openremote.model.http.SuccessStatusCode;@@ -47,12 +50,13 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON;* <p>* The only operations, always limited to linked assets, a restricted user is able to perform are:* <ul>- * <li>{@link #getCurrentUserAssets}</li>- * <li>{@link #queryAssets}</li>- * <li>{@link #queryPublicAssets}</li>- * <li>{@link #get}</li>- * <li>{@link #update}</li>- * <li>{@link #writeAttributeValue}</li>+ * <li>{@link #getCurrentUserAssets}</li>+ * <li>{@link #queryAssets}</li>+ * <li>{@link #queryPublicAssets}</li>+ * <li>{@link #get}</li>+ * <li>{@link #update}</li>+ * <li>{@link #writeAttributeValue}</li>+ * <li>{@link #updateLocation}</li>* </ul>*/@Path("asset")@@ -60,6 +64,7 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON;public interface AssetResource {// TODO This returns the same as #queryAssets, can it be removed?+/*** Retrieve the linked assets of the currently authenticated user. If the request is made by the superuser, an empty* result is returned. If the request is made by a regular user, but the user has no linked assets and is therefore@@ -259,4 +264,27 @@ public interface AssetResource {@SuccessStatusCode(200)@SuppressWarnings("unusable-by-js")Asset[] getPublicAssets(@BeanParam RequestParams requestParams, @QueryParam("q") String q);++ /**+ * Updates the attribute "location" of the asset+ */+ @POST+ @Path("{assetId}/updateLocation")+ @Consumes(APPLICATION_JSON)+ @SuccessStatusCode(204)+ @RolesAllowed({"write:assets"})+ @SuppressWarnings("unusable-by-js")+ void updateLocation(@BeanParam RequestParams requestParams, @PathParam("assetId") String assetId, GeoJSONGeometry location);++ /**+ * Updated the attribute "location" of the public asset+ * <p>+ * Allows un-authenticated 'public' users to update public assets for a realm.+ */+ @POST+ @Path("public/{assetId}/updateLocation")+ @Consumes(APPLICATION_JSON)+ @SuccessStatusCode(204)+ @SuppressWarnings("unusable-by-js")+ void updatePublicAssetLocation(@BeanParam RequestParams requestParams, @PathParam("assetId") String assetId, GeoJSONGeometry location);}diff --git a/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java b/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.javaindex 7a98f0bc6..9191ca98b 100644--- a/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java+++ b/model/src/main/java/org/openremote/model/geo/GeoJSONGeometry.java@@ -21,10 +21,13 @@ package org.openremote.model.geo;import org.openremote.model.AbstractTypeHolder;import org.openremote.model.value.ArrayValue;+import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Values;public class GeoJSONGeometry extends AbstractTypeHolder {+ public GeoJSONGeometry() {}+public GeoJSONGeometry(String type) {super(type);}@@ -32,12 +35,10 @@ public class GeoJSONGeometry extends AbstractTypeHolder {/*** Of type "Point".*/- public GeoJSONGeometry(double[] coordinates) {+ public GeoJSONGeometry(ObjectValue coordinates) {this("Point");- ArrayValue array = Values.createArray();- for (int i = 0; i < coordinates.length; i++) {- array.set(i, coordinates[i]);- }- objectValue.put("coordinates", array);++ objectValue.put("latitude", coordinates.getNumber("latitude").orElse(0d));+ objectValue.put("longitude", coordinates.getNumber("longitude").orElse(0d));}}