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
restored view code
Michael Rademaker51da79764d07
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 b91f31d76..fe371d91b 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,10 +204,9 @@ public abstract class AbstractAssetActivity<Vview.getAssetNavigation().setEnabled(assetId != null);view.setName(asset.getName());view.setCreatedOn(asset.getCreatedOn());- //TODO location update-// view.setLocation(asset.getCoordinates());-// view.showDroppedPin(asset.getGeoFeature(20));-// view.flyTo(asset.getCoordinates());+ view.setLocation(asset.getCoordinates());+ view.showDroppedPin(asset.getGeoFeature(20));+ view.flyTo(asset.getCoordinates());view.setAccessPublicRead(asset.isAccessPublicRead());}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 b340194f7..f78b44262 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@@ -462,10 +462,9 @@ public class AssetEditActivityif (selectedCoordinates != null) {view.flyTo(selectedCoordinates);}- //TODO location update- /*else if (asset.getCoordinates() != null) {+ else if (asset.getCoordinates() != null) {view.flyTo(asset.getCoordinates());- }*/+ }}@Override@@ -663,8 +662,7 @@ public class AssetEditActivityasset.setType(view.getType());}if (selectedCoordinates != null) {- //TODO location update- //asset.setCoordinates(selectedCoordinates);+ asset.setCoordinates(selectedCoordinates);}}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 1bfbd2449..f979f7b6a 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,8 +164,7 @@ public class AssetViewActivity@Overridepublic void centerMap() {- //TODO location update- //view.flyTo(asset.getCoordinates());+ view.flyTo(asset.getCoordinates());}@Overridediff --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 924672166..348aef649 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@@ -152,12 +152,11 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapVsuper.onStop();view.setPresenter(null);- //TODO location update-// if (asset != null && asset.hasGeoFeature() || dashboardAttributes.size() > 0) {-// environment.getEventService().unsubscribe(-// AttributeEvent.class-// );-// }+ if (asset != null && asset.hasGeoFeature() || dashboardAttributes.size() > 0) {+ environment.getEventService().unsubscribe(+ AttributeEvent.class+ );+ }}@Override@@ -173,14 +172,13 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapVnew AssetViewPlace(assetId)));showAssetOnMap();- //TODO location update-// if (asset.hasGeoFeature() || dashboardAttributes.size() > 0) {-// showAssetInfoItems();-// environment.getEventService().subscribe(-// AttributeEvent.class,-// new AttributeEvent.EntityIdFilter(asset.getId())-// );-// }+ if (asset.hasGeoFeature() || dashboardAttributes.size() > 0) {+ showAssetInfoItems();+ environment.getEventService().subscribe(+ AttributeEvent.class,+ new AttributeEvent.EntityIdFilter(asset.getId())+ );+ }});} else if (realmId != null) {// TODO: Tenant map not implemented@@ -188,12 +186,11 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapV}protected void showAssetOnMap() {- //TODO location update-// if (asset.hasGeoFeature()) {-// GeoJSON geoFeature = asset.getGeoFeature(30);-// view.showDroppedPin(geoFeature);-// view.flyTo(asset.getCoordinates());-// }+ if (asset.hasGeoFeature()) {+ GeoJSON geoFeature = asset.getGeoFeature(30);+ view.showDroppedPin(geoFeature);+ view.flyTo(asset.getCoordinates());+ }}protected void showAssetInfoItems() {@@ -208,15 +205,14 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapV).sorted(Comparator.comparing(MapInfoItem::getLabel)).collect(Collectors.toList());- //TODO location update-// if (asset.hasGeoFeature()) {-// infoItems.add(0, new MapInfoItem(-// "map-marker",-// environment.getMessages().location(),-// null,-// Values.create(asset.getCoordinatesLabel())-// ));-// }+ if (asset.hasGeoFeature()) {+ infoItems.add(0, new MapInfoItem(+ "map-marker",+ environment.getMessages().location(),+ null,+ Values.create(asset.getCoordinatesLabel())+ ));+ }view.showInfoItems(infoItems);}diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.javaindex e0f2388fa..0a27064d9 100644--- a/model/src/main/java/org/openremote/model/asset/Asset.java+++ b/model/src/main/java/org/openremote/model/asset/Asset.java@@ -24,13 +24,19 @@ import org.hibernate.annotations.Check;import org.hibernate.annotations.Formula;import org.openremote.model.IdentifiableEntity;import org.openremote.model.attribute.Attribute;+import org.openremote.model.geo.GeoJSON;+import org.openremote.model.geo.GeoJSONFeature;+import org.openremote.model.geo.GeoJSONGeometry;import org.openremote.model.util.ObservableList;+import org.openremote.model.util.TextUtil;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Values;import javax.persistence.*;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size;+import java.math.BigDecimal;+import java.math.RoundingMode;import java.util.Arrays;import java.util.Date;import java.util.List;@@ -42,6 +48,7 @@ import java.util.stream.Stream;import static org.openremote.model.Constants.PERSISTENCE_JSON_OBJECT_TYPE;import static org.openremote.model.Constants.PERSISTENCE_UNIQUE_ID_GENERATOR;import static org.openremote.model.asset.AssetAttribute.*;+import static org.openremote.model.attribute.AttributeDescriptorImpl.LOCATION;// @formatter:off@@ -609,6 +616,63 @@ public class Asset implements IdentifiableEntity {// FUNCTIONAL METHODS BELOW// ---------------------------------------------------+ public double[] 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;+ } else {+ return null;+ }+ }++ public void setCoordinates(double[] 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);++ 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.+ * -+ */+ public String getCoordinatesLabel() {+ return+ new BigDecimal(getCoordinates()[1]).setScale(5, RoundingMode.HALF_UP) + " " ++ new BigDecimal(getCoordinates()[0]).setScale(5, RoundingMode.HALF_UP) + " Lat|Lng";+ }++ public boolean hasGeoFeature() {+ return getCoordinates() != null && getCoordinates().length == 2;+ }++ public GeoJSON getGeoFeature(int maxNameLength) {+ if (!hasGeoFeature())+ return GeoJSON.EMPTY_FEATURE_COLLECTION;+ return new GeoJSON("FeatureCollection").setFeatures(+ new GeoJSONFeature("Feature")+ .setProperty("id", getId())+ .setProperty("title", TextUtil.ellipsize(getName(), maxNameLength))+ .setGeometry(new GeoJSONGeometry(getCoordinates()))+ );+ }+public static boolean isAssetNameEqualTo(Asset asset, String name) {return asset != null && asset.getName().equals(name);}diff --git a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovyindex 5b8d82824..76840ee56 100644--- a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy@@ -335,7 +335,8 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrtestAsset.setAttributes(new AssetAttribute("attribute1", AttributeType.STRING, Values.create("Test")))- testAsset.setLocation(new GeometryFactory().createPoint(new Coordinate(1d, 2d)))+ double[] coordinates = [1d, 2d]+ testAsset.setCoordinates(coordinates)authenticatedTestResource.postAsset(testAsset)then: "the asset should be stored in the test protocol"@@ -344,9 +345,8 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrassert testServerProtocol.resource1.postedAssets.get(0).name == "Test Asset"assert testServerProtocol.resource1.postedAssets.get(0).hasTypeWellKnown()assert testServerProtocol.resource1.postedAssets.get(0).wellKnownType == AssetType.THING- //TODO location update-// assert testServerProtocol.resource1.postedAssets.get(0).coordinates[0] == 1d-// assert testServerProtocol.resource1.postedAssets.get(0).coordinates[1] == 2d+ assert testServerProtocol.resource1.postedAssets.get(0).coordinates[0] == 1d+ assert testServerProtocol.resource1.postedAssets.get(0).coordinates[1] == 2d}when: "the authenticated test resource is used to get an asset"@@ -355,9 +355,8 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrthen: "the asset should match the posted asset"assert asset.name == testAsset.nameassert asset.wellKnownType == testAsset.wellKnownType- //TODO location update-// assert asset.coordinates[0] == testAsset.coordinates[0]-// assert asset.coordinates[1] == testAsset.coordinates[1]+ assert asset.coordinates[0] == testAsset.coordinates[0]+ assert asset.coordinates[1] == testAsset.coordinates[1]when: "the un-authenticated test resource is used to post an asset"testResource.postAsset(testAsset)@@ -395,7 +394,8 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrtestAsset.setAttributes(new AssetAttribute("attribute2", AttributeType.STRING, Values.create("Test")))- testAsset.setLocation(new GeometryFactory().createPoint(new Coordinate(3d, 4d)))+ coordinates = [3d, 4d]+ testAsset.setCoordinates(coordinates)authenticatedTestResource.postAsset(testAsset)then: "the asset should be stored in the test protocol"@@ -404,9 +404,8 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrassert testServerProtocol.resource1.postedAssets.get(1).name == "Test Asset 2"assert testServerProtocol.resource1.postedAssets.get(1).hasTypeWellKnown()assert testServerProtocol.resource1.postedAssets.get(1).wellKnownType == AssetType.THING- //TODO location update-// assert testServerProtocol.resource1.postedAssets.get(1).coordinates[0] == 3d-// assert testServerProtocol.resource1.postedAssets.get(1).coordinates[1] == 4d+ assert testServerProtocol.resource1.postedAssets.get(1).coordinates[0] == 3d+ assert testServerProtocol.resource1.postedAssets.get(1).coordinates[1] == 4d}when: "the new test resource is used to get an asset"@@ -415,9 +414,8 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTrthen: "the asset should match the posted asset"assert asset.name == testAsset.nameassert asset.wellKnownType == testAsset.wellKnownType- //TODO location update-// assert asset.coordinates[0] == testAsset.coordinates[0]-// assert asset.coordinates[1] == testAsset.coordinates[1]+ assert asset.coordinates[0] == testAsset.coordinates[0]+ assert asset.coordinates[1] == testAsset.coordinates[1]when: "a protocol configuration is deleted"agent.removeAttribute("protocolConfig")