Skip to content

armgit / core

Public Imported from GitHub
0

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

ORConsole GeofenceAssetAdapter testing

Rich Turner6343d16e6389
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
index 58ecf1b69..37d99c250 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
@@ -53,6 +53,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.logging.Level.FINEST;
+import static java.util.logging.Level.SEVERE;
import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;
@@ -429,10 +430,11 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
case INSERT:
case UPDATE:
- geofenceAdapterName
- .map(name -> assetIdGeofenceAdapterMap.put(asset.getId(), geofenceAssetAdapters.get(name)))
- .orElseGet(() -> assetIdGeofenceAdapterMap.remove(asset.getId()));
-
+ if (geofenceAdapterName.isPresent()) {
+ assetIdGeofenceAdapterMap.put(asset.getId(), geofenceAssetAdapters.get(geofenceAdapterName.get()));
+ } else {
+ assetIdGeofenceAdapterMap.remove(asset.getId());
+ }
break;
case DELETE:
@@ -876,13 +878,14 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
assetLocationPredicates.add(locationPredicates);
}
- );
+ );
LOG.info("Processing asset geofence updates for adapter '" + geofenceAdapter.getName() + "': asset count=" + assetLocationPredicates.size());
geofenceAdapter.processLocationPredicates(assetLocationPredicates, isFirstRun);
}
- );
-
+ );
+ } catch (Exception e) {
+ LOG.log(SEVERE, "Exception thrown by geofence adapter whilst processing location predicates", e);
} finally {
// Clear modified assets ready for next batch
modifiedAssetLocationPredicates.clear();
diff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
index 2890f7a8c..47c6d6e1d 100644
--- a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
@@ -47,7 +47,7 @@ import java.util.*;
public class ORConsoleGeofenceAssetAdapter extends GeofenceAssetAdapter {
public static final String NAME = "ORConsole";
- public static final String POST_URL_TEMPLATE = "/asset/location/";
+ public static final String LOCATION_POST_URL_FORMAT_TEMPLATE = "/%1$s/asset/public/%2$s/updateLocation";
protected Map<String, RulesEngine.AssetStateLocationPredicates> assetLocationPredicatesMap = new HashMap<>();
protected NotificationService notificationService;
@@ -65,29 +65,33 @@ public class ORConsoleGeofenceAssetAdapter extends GeofenceAssetAdapter {
@Override
public void processLocationPredicates(List<RulesEngine.AssetStateLocationPredicates> modifiedAssetLocationPredicates, boolean initialising) {
- Set<AssetState> notifyAssets = new HashSet<>(modifiedAssetLocationPredicates.size());
-
modifiedAssetLocationPredicates.forEach(assetStateLocationPredicates -> {
assetStateLocationPredicates
.getLocationPredicates()
.removeIf(locationPredicate ->
!(locationPredicate instanceof BaseAssetQuery.RadialLocationPredicate));
- if (initialising) {
- assetLocationPredicatesMap.put(assetStateLocationPredicates.getAssetState().getId(),
- assetStateLocationPredicates);
- } else {
+ if (!initialising) {
+ Set<AssetState> notifyAssets = new HashSet<>(modifiedAssetLocationPredicates.size());
+
RulesEngine.AssetStateLocationPredicates existingPredicates = assetLocationPredicatesMap.get(
assetStateLocationPredicates.getAssetState().getId());
- if (!existingPredicates.getLocationPredicates().isEmpty() || !assetStateLocationPredicates.getLocationPredicates().isEmpty()) {
+ if ((existingPredicates == null || existingPredicates.getLocationPredicates().isEmpty()) && !assetStateLocationPredicates.getLocationPredicates().isEmpty()) {
// We're not comparing before and after state as RulesService has done that although it could be
// that rectangular location predicates have changed but this will do for now
notifyAssets.add(assetStateLocationPredicates.getAssetState());
}
+
+ notifyAssets.forEach(assetState -> notifyAssetGeofencesChanged(assetState.getId()));
}
- });
- notifyAssets.forEach(assetState -> notifyAssetGeofencesChanged(assetState.getId()));
+ if (assetStateLocationPredicates.getLocationPredicates().isEmpty()) {
+ assetLocationPredicatesMap.remove(assetStateLocationPredicates.getAssetState().getId());
+ } else {
+ assetLocationPredicatesMap.put(assetStateLocationPredicates.getAssetState().getId(),
+ assetStateLocationPredicates);
+ }
+ });
}
@Override
@@ -107,7 +111,7 @@ public class ORConsoleGeofenceAssetAdapter extends GeofenceAssetAdapter {
protected GeofenceDefinition locationPredicateToGeofenceDefinition(AssetState assetState, BaseAssetQuery.LocationPredicate locationPredicate) {
BaseAssetQuery.RadialLocationPredicate radialLocationPredicate = (BaseAssetQuery.RadialLocationPredicate) locationPredicate;
String id = assetState.getId() + "_" + Integer.toString(radialLocationPredicate.hashCode());
- String postUrl = "/" + assetState.getTenantRealm() + POST_URL_TEMPLATE + assetState.getId();
+ String postUrl = String.format(LOCATION_POST_URL_FORMAT_TEMPLATE, assetState.getTenantRealm(), assetState.getId());
return new GeofenceDefinition(id,
radialLocationPredicate.getLat(),
radialLocationPredicate.getLng(),
@@ -117,5 +121,6 @@ public class ORConsoleGeofenceAssetAdapter extends GeofenceAssetAdapter {
protected void notifyAssetGeofencesChanged(String assetId) {
// TODO: implement geofence push notification
+ notificationService.findDeviceToken()
}
}
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.java
index 2145d5335..fa472fa7e 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
@@ -627,7 +627,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment3Livingroom = assetStorageService.merge(apartment3Livingroom);
apartment3LivingroomId = apartment3Livingroom.getId();
- Asset console = new Asset("Demo Android Console", CONSOLE, lobby)
+ Asset console = new Asset("Demo Android Console", CONSOLE, groundFloor)
.addAttributes(
new AssetAttribute(AttributeDescriptorImpl.LOCATION.getName(),
AttributeDescriptorImpl.LOCATION.getType())
diff --git a/test/src/test/groovy/org/openremote/test/rules/geofence/BasicGeofenceProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/rules/geofence/BasicGeofenceProcessingTest.groovy
index 495894237..e379b5010 100644
--- a/test/src/test/groovy/org/openremote/test/rules/geofence/BasicGeofenceProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/geofence/BasicGeofenceProcessingTest.groovy
@@ -9,24 +9,27 @@ import org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapter
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.setup.builtin.ManagerDemoSetup
+import org.openremote.model.asset.Asset
+import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetMeta
-import org.openremote.model.asset.BaseAssetQuery
+import org.openremote.model.attribute.AttributeDescriptorImpl
import org.openremote.model.attribute.MetaItem
import org.openremote.model.rules.AssetRuleset
import org.openremote.model.rules.GlobalRuleset
import org.openremote.model.rules.Ruleset
+import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import org.openremote.test.rules.BasicRulesImport
-import spock.lang.Ignore
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
import static org.openremote.manager.rules.RulesetDeployment.Status.DEPLOYED
import static org.openremote.manager.setup.builtin.ManagerDemoSetup.*
+import static org.openremote.model.asset.AssetMeta.*
+import static org.openremote.model.asset.AssetType.CONSOLE
class BasicGeofenceProcessingTest extends Specification implements ManagerContainerTrait {
- @Ignore
def "Check tracking of location predicate rules"() {
given: "expected conditions"
def conditions = new PollingConditions(timeout: 10, initialDelay: 0.5, delay: 0.5)
@@ -69,28 +72,38 @@ class BasicGeofenceProcessingTest extends Specification implements ManagerContai
assert geofences[0].lat == 50
assert geofences[0].lng == 100
assert geofences[0].id == managerDemoSetup.consoleId + "_-174661203"
- assert geofences[0].postUrl == "/master/asset/location/" + managerDemoSetup.consoleId
+ assert geofences[0].postUrl == "/master/asset/public/${managerDemoSetup.consoleId}/updateLocation"
}
- when: "another asset's location attribute is marked as RULE_STATE"
- def lobby = assetStorageService.find(managerDemoSetup.lobbyId, true)
- lobby.getAttribute("location").get().addMeta(new MetaItem(AssetMeta.RULE_STATE))
- lobby = assetStorageService.merge(lobby)
+ when: "another console asset is added with its location attribute marked as RULE_STATE and it has a CONSOLE_PROVIDER_GEOFENCE attribute"
+ Asset console2 = new Asset("Demo Android Console 2", CONSOLE)
+ .addAttributes(
+ new AssetAttribute(AttributeDescriptorImpl.LOCATION.getName(),
+ AttributeDescriptorImpl.LOCATION.getType())
+ .setMeta(new MetaItem(RULE_STATE)),
+ new AssetAttribute(AttributeDescriptorImpl.CONSOLE_PROVIDER_GEOFENCE.getName(),
+ AttributeDescriptorImpl.CONSOLE_PROVIDER_GEOFENCE.getType())
+ .setMeta(
+ new MetaItem(GEOFENCE_ADAPTER, Values.create("ORConsole")),
+ new MetaItem(ACCESS_PUBLIC_READ)
+ )
+ )
+ console2.setParentId(managerDemoSetup.lobbyId)
+ console2 = assetStorageService.merge(console2)
+ def console2Id = console2.getId()
- then: "the geofence adapter should be notified of the new lobby asset with a location predicate"
+ then: "the geofence adapter should be notified of the new console asset with the existing location predicate"
conditions.eventually {
- assert geofenceChanges != null
- assert !geofenceInit
- assert geofenceChanges.size() == 1
- def lobbyGeofences = geofenceChanges.find({it.assetState.id == managerDemoSetup.lobbyId}).locationPredicates
- assert lobbyGeofences.size() == 1
- BaseAssetQuery.RadialLocationPredicate locPredicate = lobbyGeofences.find {
- it instanceof BaseAssetQuery.RadialLocationPredicate && it.centrePoint[0] == 100 && it.centrePoint[1] == 50 && it.radius == 100
- }
- assert locPredicate != null
+ def geofences = geofenceAdapter.getAssetGeofences(console2Id)
+ assert geofences.size() == 1
+ assert geofences[0].radius == 100
+ assert geofences[0].lat == 50
+ assert geofences[0].lng == 100
+ assert geofences[0].id == console2Id + "_-174661203"
+ assert geofences[0].postUrl == "/master/asset/public/${console2Id}/updateLocation"
}
- when: "a new ruleset is deployed with multiple location predicate rules on the lobby asset"
+ when: "a new ruleset is deployed with multiple location predicate rules (including a duplicate and a rectangular predicate) on the lobby asset"
def lobbyRuleset = new AssetRuleset(
"Lobby location predicates",
managerDemoSetup.lobbyId,
@@ -111,72 +124,80 @@ class BasicGeofenceProcessingTest extends Specification implements ManagerContai
})
}
- then: "the mock geofence adapter should have been notified of the new location predicates for the demo thing and lobby assets"
+ then: "the geofence adapter should have been notified of the new radial location predicates for the console assets (but shouldn't contain the rectangular or duplicate predicates)"
conditions.eventually {
- assert geofenceChanges != null
- assert !geofenceInit
- assert geofenceChanges.size() == 2
- Set<BaseAssetQuery.LocationPredicate> thingGeofences = geofenceChanges.find {
- it.assetState.id == managerDemoSetup.thingId
- }.locationPredicates
- assert thingGeofences.size() == 3
- BaseAssetQuery.RadialLocationPredicate rad1Predicate = thingGeofences.find {
- it instanceof BaseAssetQuery.RadialLocationPredicate && it.centrePoint[0] == 100 && it.centrePoint[1] == 50 && it.radius == 100
- }
- BaseAssetQuery.RadialLocationPredicate rad2Predicate = thingGeofences.find {
- it instanceof BaseAssetQuery.RadialLocationPredicate && it.centrePoint[0] == 50 && it.centrePoint[1] == 0 && it.radius == 200
+ def console1Geofences = geofenceAdapter.getAssetGeofences(managerDemoSetup.consoleId)
+ def console2Geofences = geofenceAdapter.getAssetGeofences(console2Id)
+ assert console1Geofences.size() == 1
+ assert console2Geofences.size() == 2
+ assert console1Geofences[0].radius == 100
+ assert console1Geofences[0].lat == 50
+ assert console1Geofences[0].lng == 100
+ assert console1Geofences[0].id == managerDemoSetup.consoleId + "_-174661203"
+ assert console1Geofences[0].postUrl == "/master/asset/public/${managerDemoSetup.consoleId}/updateLocation"
+
+ def console2Geofence1 = console2Geofences.find {
+ it.radius == 100 &&
+ it.lat == 50 &&
+ it.lng == 100 &&
+ it.id == console2Id + "_-174661203" &&
+ it.postUrl == "/master/asset/public/${console2Id}/updateLocation"
}
- BaseAssetQuery.RectangularLocationPredicate rect1Predicate = thingGeofences.find {
- it instanceof BaseAssetQuery.RectangularLocationPredicate && it.centrePoint[0] == 75 && it.centrePoint[1] == 25 && it.lngMax == 100 && it.latMax == 50
+ assert console2Geofence1 != null
+
+ def console2Geofence2 = console2Geofences.find {
+ it.radius == 50 &&
+ it.lat == 0 &&
+ it.lng == -60 &&
+ it.id == console2Id + "_-1397479941" &&
+ it.postUrl == "/master/asset/public/${console2Id}/updateLocation"
}
- assert rad1Predicate != null
- assert rad2Predicate != null
- assert rect1Predicate != null
- def lobbyGeofences = geofenceChanges.find({it.assetState.id == managerDemoSetup.lobbyId}).locationPredicates
- assert lobbyGeofences.size() == 3
- assert lobbyGeofences.any {it == rad1Predicate}
- assert lobbyGeofences.any {it == rad2Predicate}
- assert lobbyGeofences.any {it == rect1Predicate}
+ assert console2Geofence2 != null
}
+
when: "a location predicate ruleset is removed"
rulesetStorageService.delete(GlobalRuleset.class, rulesImport.globalRuleset3Id)
- then: "the mock geofence adapter should be notified that the demo thing and lobby asset's rules with location predicates have changed"
+ then: "the geofence adapter should be notified and update to reflect the changes"
conditions.eventually {
- assert geofenceChanges != null
- assert geofenceChanges.size() == 2
- Set<BaseAssetQuery.LocationPredicate> thingGeofences = geofenceChanges.find {
- it.assetState.id == managerDemoSetup.thingId
- }.locationPredicates
- assert thingGeofences.size() == 2
- BaseAssetQuery.RadialLocationPredicate rad2Predicate = thingGeofences.find {
- it instanceof BaseAssetQuery.RadialLocationPredicate && it.centrePoint[0] == 50 && it.centrePoint[1] == 0 && it.radius == 200
+ def console1Geofences = geofenceAdapter.getAssetGeofences(managerDemoSetup.consoleId)
+ def console2Geofences = geofenceAdapter.getAssetGeofences(console2Id)
+ assert console1Geofences.size() == 0
+ assert console2Geofences.size() == 2
+
+ def console2Geofence1 = console2Geofences.find {
+ it.radius == 100 &&
+ it.lat == 50 &&
+ it.lng == 100 &&
+ it.id == console2Id + "_-174661203" &&
+ it.postUrl == "/master/asset/public/${console2Id}/updateLocation"
}
- BaseAssetQuery.RectangularLocationPredicate rect1Predicate = thingGeofences.find {
- it instanceof BaseAssetQuery.RectangularLocationPredicate && it.centrePoint[0] == 75 && it.centrePoint[1] == 25 && it.lngMax == 100 && it.latMax == 50
+ assert console2Geofence1 != null
+
+ def console2Geofence2 = console2Geofences.find {
+ it.radius == 50 &&
+ it.lat == 0 &&
+ it.lng == -60 &&
+ it.id == console2Id + "_-1397479941" &&
+ it.postUrl == "/master/asset/public/${console2Id}/updateLocation"
}
- assert rad2Predicate != null
- assert rect1Predicate != null
- def lobbyGeofences = geofenceChanges.find({it.assetState.id == managerDemoSetup.lobbyId}).locationPredicates
- assert lobbyGeofences.size() == 2
- assert lobbyGeofences.any {it == rad2Predicate}
- assert lobbyGeofences.any {it == rect1Predicate}
+ assert console2Geofence2 != null
}
- when: "the RULE_STATE meta is removed from the lobby asset's location attribute"
- lobby.getAttribute("location").get().getMeta().removeIf({it.name.orElse(null) == AssetMeta.RULE_STATE.urn})
- lobby = assetStorageService.merge(lobby)
+ when: "the RULE_STATE meta is removed from the second console asset's location attribute"
+ console2.getAttribute(AttributeDescriptorImpl.LOCATION.getName()).get().getMeta().removeIf({it.name.orElse(null) == AssetMeta.RULE_STATE.urn})
+ console2 = assetStorageService.merge(console2)
- then: "the mock geofence adapter should be notified that the lobby asset no longer has any rules with location predicates"
+ then: "the geofence adapter should be notified that the console asset no longer has any rules with location predicates"
conditions.eventually {
- assert geofenceChanges != null
- assert !geofenceInit
- assert geofenceChanges.size() == 1
- def lobbyGeofences = geofenceChanges.find({it.assetState.id == managerDemoSetup.lobbyId}).locationPredicates
- assert lobbyGeofences.size() == 0
+ def console2Geofences = geofenceAdapter.getAssetGeofences(console2Id)
+ assert console2Geofences.size() == 0
+ assert geofenceAdapter.assetLocationPredicatesMap.isEmpty()
}
+ // TODO: Verify the RulesResource code path
+
cleanup: "the server should be stopped"
stopContainer(container)
}
diff --git a/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate.groovy b/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate.groovy
index 7a004d364..21695abb1 100644
--- a/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate.groovy
+++ b/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate.groovy
@@ -7,7 +7,7 @@ import org.openremote.model.asset.BaseAssetQuery
RulesBuilder rules = binding.rules
rules.add()
- .name("Location predicate")
+ .name("Radial location predicate")
.when(
{ facts ->
facts.matchFirstAssetState(new AssetQuery().location(new BaseAssetQuery.RadialLocationPredicate(100, 50, 100))).isPresent() &&
@@ -15,5 +15,5 @@ rules.add()
})
.then(
{ facts ->
- facts.put("Location", "fired")
+ facts.put("RadialLocation", "fired")
})
diff --git a/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate2.groovy b/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate2.groovy
index 4545c5720..7b8e849c8 100644
--- a/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate2.groovy
+++ b/test/src/test/resources/org/openremote/test/rules/BasicLocationPredicate2.groovy
@@ -7,17 +7,29 @@ import org.openremote.model.asset.BaseAssetQuery
RulesBuilder rules = binding.rules
rules.add()
- .name("Radial location predicate")
+ .name("Another radial location predicate")
.when(
{ facts ->
- facts.matchFirstAssetState(new AssetQuery().location(new BaseAssetQuery.RadialLocationPredicate(200, 0, 50))).isPresent() &&
+ facts.matchFirstAssetState(new AssetQuery().location(new BaseAssetQuery.RadialLocationPredicate(50, 0, -60))).isPresent() &&
!facts.matchFirst("RadialLocation").isPresent()
})
.then(
{ facts ->
- facts.put("RadialLocation", "fired")
+ facts.put("RadialLocation2", "fired")
})
+rules.add()
+ .name("Duplicate radial location predicate")
+ .when(
+ { facts ->
+ facts.matchFirstAssetState(new AssetQuery().location(new BaseAssetQuery.RadialLocationPredicate(100, 50, 100))).isPresent() &&
+ !facts.matchFirst("Location").isPresent()
+ })
+ .then(
+ { facts ->
+ facts.put("Location", "fired")
+ })
+
rules.add()
.name("Rectangular location predicate")
.when(