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
Finished ios implementation for geofence provider.(Needs refactoring) Also added rules for geofences
Michael Rademaker84a1300ec7d0
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.javaindex 24489ce48..05cbf0fbe 100644--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java@@ -23,9 +23,7 @@ import com.vividsolutions.jts.geom.Point;import org.openremote.container.ContainerService;import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetAttribute;-import org.openremote.model.asset.AssetMeta;import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.attribute.MetaItem;import java.util.function.Predicate;diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex 72fe54d59..03aad88ba 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -637,10 +637,14 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour.orElse(AssetAttribute.createWithDescriptor(AttributeType.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)));+ ArrayValue arrayValue = location.getObjectValue().getArray("coordinates").orElse(null);+ if (arrayValue != null) {+ locationAttribute.setValue(currentCoordinates.put("latitude", arrayValue.getNumber(0).orElse(0d)).put("longitude", arrayValue.getNumber(1).orElse(0d)));- asset.replaceAttribute(locationAttribute);+ asset.replaceAttribute(locationAttribute);+ } else {+ asset.removeAttribute(locationAttribute.name);+ }asset = assetStorageService.merge(asset);} catch (IllegalStateException ex) {diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 976748c54..289edcbbe 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -45,8 +45,8 @@ import org.openremote.manager.security.UserConfiguration;import org.openremote.model.Constants;import org.openremote.model.ValidationFailure;import org.openremote.model.asset.*;-import org.openremote.model.attribute.AttributeType;import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeType;import org.openremote.model.calendar.CalendarEvent;import org.openremote.model.calendar.RecurrenceRule;import org.openremote.model.event.shared.TenantFilter;@@ -130,9 +130,9 @@ public class AssetStorageService extends RouteBuilder implements ContainerServiccontainer.getService(WebService.class).getApiSingletons().add(new ConsoleResourceImpl(container.getService(TimerService.class),- identityService,- this)- );+ identityService,+ this)+ );container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);}@@ -595,15 +595,9 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicStringBuilder sb = new StringBuilder();AssetQuery.Include include = query.select.include;boolean recursive = query.select.recursive;- boolean includeMainProperties = include == ALL ||- include == AssetQuery.Include.ALL_EXCEPT_PATH ||- include == ALL_EXCEPT_PATH_AND_ATTRIBUTES;sb.append("select A.ID as ID, A.NAME as NAME, A.ACCESS_PUBLIC_READ as ACCESS_PUBLIC_READ");-- if (query.orderBy != null || query.location != null || includeMainProperties) {- sb.append(", A.CREATED_ON AS CREATED_ON, A.ASSET_TYPE AS ASSET_TYPE, A.PARENT_ID AS PARENT_ID, A.REALM_ID AS REALM_ID");- }+ sb.append(", A.CREATED_ON AS CREATED_ON, A.ASSET_TYPE AS ASSET_TYPE, A.PARENT_ID AS PARENT_ID, A.REALM_ID AS REALM_ID");if (include == AssetQuery.Include.ONLY_ID_AND_NAME) {return sb.toString();@@ -1184,7 +1178,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbreak;}} else if (attributePredicate.value instanceof AssetQuery.ObjectValueKeyPredicate) {- ObjectValueKeyPredicate keyPredicate = (ObjectValueKeyPredicate)attributePredicate.value;+ ObjectValueKeyPredicate keyPredicate = (ObjectValueKeyPredicate) attributePredicate.value;if (keyPredicate.negated) {attributeBuilder.append(" and NOT(AX.VALUE #> '{value}' ?? ? ) ");} else {diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex e80ceccda..db6336d73 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -214,16 +214,19 @@ public class AssetQueryPredicate implements Predicate<AssetState> {protected Predicate<Coordinate> asPredicate(LocationPredicate predicate) {return coordinate -> {if (predicate instanceof RadialLocationPredicate) {- return false;+// return false;++ //TODO: remove Hibernate spatial+ //Find Geo Library with proper license to do this stuff!// TODO: Fix this-// RadialLocationPredicate radialLocationPredicate = (RadialLocationPredicate) predicate;-// GeometricShapeFactory shapeFactory = new GeometricShapeFactory();-// shapeFactory.setCentre(new Coordinate(radialLocationPredicate.lat, radialLocationPredicate.lng));-// shapeFactory.setSize(radialLocationPredicate.radius * 2);-// Polygon circle = shapeFactory.createCircle();-// GeometryFactory geometryFactory = new GeometryFactory();-// Point point = geometryFactory.createPoint(coordinate);-// return point.within(circle);+ RadialLocationPredicate radialLocationPredicate = (RadialLocationPredicate) predicate;+ GeometricShapeFactory shapeFactory = new GeometricShapeFactory();+ shapeFactory.setCentre(new Coordinate(radialLocationPredicate.lat, radialLocationPredicate.lng));+ shapeFactory.setSize(radialLocationPredicate.radius * 2);+ Polygon circle = shapeFactory.createCircle();+ GeometryFactory geometryFactory = new GeometryFactory();+ Point point = geometryFactory.createPoint(coordinate);+ return point.within(circle);} else if (predicate instanceof RectangularLocationPredicate) {// Again this is a euclidean plane so doesn't work perfectly for WGS lat/lng - the bigger the rectangle to less accurate it is)diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.javaindex beb05cbad..a646cc739 100644--- a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java@@ -652,6 +652,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {AttributeType.LOCATION.getType()).setMeta(new MetaItem(RULE_STATE)));+ console.setAccessPublicRead(true);console = assetStorageService.merge(console);consoleId = console.getId();diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/RulesDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/RulesDemoSetup.javaindex 68d540ae6..9c6116829 100644--- a/manager/src/main/java/org/openremote/manager/setup/builtin/RulesDemoSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/RulesDemoSetup.java@@ -38,6 +38,7 @@ public class RulesDemoSetup extends AbstractManagerSetup {}public Long apartmentActionsRulesetId;+ public Long smartOfficeRulesetId;@Overridepublic void onStart() throws Exception {@@ -94,5 +95,13 @@ public class RulesDemoSetup extends AbstractManagerSetup {);apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();}++ try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/ConsoleLocationPredicate.groovy")) {+ String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));+ Ruleset ruleset = new AssetRuleset(+ "Demo Console Location", managerDemoSetup.smartOfficeId, rules, Ruleset.Lang.GROOVY+ );+ smartOfficeRulesetId = rulesetStorageService.merge(ruleset).getId();+ }}}diff --git a/manager/src/main/resources/demo/rules/ConsoleLocationPredicate.groovy b/manager/src/main/resources/demo/rules/ConsoleLocationPredicate.groovynew file mode 100644index 000000000..c9998d61e--- /dev/null+++ b/manager/src/main/resources/demo/rules/ConsoleLocationPredicate.groovy@@ -0,0 +1,116 @@+package org.openremote.test.rules++import org.openremote.manager.rules.RulesBuilder+import org.openremote.model.asset.AssetQuery+import org.openremote.model.asset.AssetType+import org.openremote.model.asset.BaseAssetQuery+import org.openremote.model.notification.AlertNotification++RulesBuilder rules = binding.rules++rules.add()+ .name("Geofence - Airport Schiphol")+ .when(+ { facts ->+ def consoles = facts.matchAssetState(new AssetQuery()+ .type(AssetType.CONSOLE)+ .location(new BaseAssetQuery.RadialLocationPredicate(4000, 4.761891d, 52.309197d)))+ .collect()++ consoles.size() > 0+ })+ .then(+ { facts ->++ AlertNotification alert = new AlertNotification(+ title: "Welcome in The Netherlands",+ message: "Have a look at the UNaLab program"+ )+ alert.addLinkAction("Yes Please", "https://www.eindhoven.nl/en/unalab-program")+ alert.addLinkAction("Not now", "#")++ })++rules.add()+ .name("Geofence - Eindhoven Airport")+ .when(+ { facts ->+ def consoles = facts.matchFirstAssetState(new AssetQuery()+ .type(AssetType.CONSOLE)+ .location(new BaseAssetQuery.RadialLocationPredicate(1000, 5.391885d, 51.458181d)))+ .collect()++ consoles.size() > 0+ })+ .then(+ { facts ->+ AlertNotification alert = new AlertNotification(+ title: "Welcome in Eindhoven",+ message: "Have a look at the UNaLab program"+ )+ alert.addLinkAction("Yes Please", "https://www.eindhoven.nl/en/unalab-program")+ alert.addLinkAction("Not now", "#")+ })++rules.add()+ .name("Geofence - van Abbe Museum")+ .when(+ { facts ->+ def consoles = facts.matchFirstAssetState(new AssetQuery()+ .type(AssetType.CONSOLE)+ .location(new BaseAssetQuery.RadialLocationPredicate(75, 5.481743d, 51.434402d)))+ .collect()++ consoles.size() > 0+ })+ .then(+ { facts ->+ AlertNotification alert = new AlertNotification(+ title: "Welcome in UNaLab consortium meeting",+ message: "Have a look at the program for our meeting in Eindhoven"+ )+ alert.addLinkAction("Yes Please", "https://www.eindhoven.nl/en/unalab-program")+ alert.addLinkAction("Not now", "#")+ })++rules.add()+ .name("Geofence - Keizersgracht")+ .when(+ { facts ->+ def consoles = facts.matchFirstAssetState(new AssetQuery()+ .type(AssetType.CONSOLE)+ .location(new BaseAssetQuery.RadialLocationPredicate(100, 5.475926d, 51.437987d)))+ .collect()++ consoles.size() > 0+ })+ .then(+ { facts ->+ AlertNotification alert = new AlertNotification(+ title: "Hello, you are now at Keizersgracht",+ message: "How do you like this tour so far?"+ )+ alert.addLinkAction("Rate it", "https://www.eindhoven.nl/en/unalab-survey-2")+ alert.addLinkAction("Not now", "#")+ })++rules.add()+ .name("Geofence - Cafe Centraal")+ .when(+ { facts ->+ def consoles = facts.matchFirstAssetState(new AssetQuery()+ .type(AssetType.CONSOLE)+ .location(new BaseAssetQuery.RadialLocationPredicate(100, 5.478801d, 51.439351d)))+ .collect()++ consoles.size() > 0+ })+ .then(+ { facts ->+ AlertNotification alert = new AlertNotification(+ title: "Welcome at Cafe Centraal",+ message: "Now you have visited the NBS sites in Eindhoven, let us know your favorite project."+ )+ alert.addLinkAction("Fill in the survey", "https://www.eindhoven.nl/en/unalab-program#survey")+ alert.addLinkAction("Not now", "#")+ })diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.javaindex ee88b136e..efa8ff3c2 100644--- a/model/src/main/java/org/openremote/model/asset/Asset.java+++ b/model/src/main/java/org/openremote/model/asset/Asset.java@@ -632,6 +632,7 @@ public class Asset implements IdentifiableEntity {// FUNCTIONAL METHODS BELOW// ---------------------------------------------------+ //TODO return GeoJSONPoint and remove getPoint/*** Complies to the GeoJSON specification RFC 7946, which means the first element in the array is longitude* and the second element is the latitude