use of org.openremote.manager.rules.geofence.GeofenceAssetAdapter in project openremote by openremote.
the class RulesService method start.
@Override
public void start(Container container) throws Exception {
if (!geofenceAssetAdapters.isEmpty()) {
LOG.info("GeoefenceAssetAdapters found: " + geofenceAssetAdapters.size());
locationPredicateRulesConsumer = this::onEngineLocationRulesChanged;
for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {
geofenceAssetAdapter.start(container);
}
}
LOG.info("Deploying global rulesets");
rulesetStorageService.findAll(GlobalRuleset.class, new RulesetQuery().setEnabledOnly(true).setFullyPopulate(true)).forEach(this::deployGlobalRuleset);
LOG.info("Deploying tenant rulesets");
tenants = Arrays.stream(identityService.getIdentityProvider().getTenants()).filter(Tenant::getEnabled).toArray(Tenant[]::new);
rulesetStorageService.findAll(TenantRuleset.class, new RulesetQuery().setEnabledOnly(true).setFullyPopulate(true)).stream().filter(rd -> Arrays.stream(tenants).anyMatch(tenant -> rd.getRealm().equals(tenant.getRealm()))).forEach(this::deployTenantRuleset);
LOG.info("Deploying asset rulesets");
// Group by asset ID then tenant and check tenant is enabled
// noinspection ResultOfMethodCallIgnored
deployAssetRulesets(rulesetStorageService.findAll(AssetRuleset.class, new RulesetQuery().setEnabledOnly(true).setFullyPopulate(true))).count();
LOG.info("Loading all assets with fact attributes to initialize state of rules engines");
Stream<Pair<Asset<?>, Stream<Attribute<?>>>> stateAttributes = findRuleStateAttributes();
// Push each attribute as an asset update through the rule engine chain
// that will ensure the insert only happens to the engines in scope
stateAttributes.forEach(pair -> {
Asset<?> asset = pair.key;
pair.value.forEach(ruleAttribute -> {
AssetState<?> assetState = new AssetState<>(asset, ruleAttribute, Source.INTERNAL);
updateAssetState(assetState);
});
});
// Start the engines
if (globalEngine != null) {
globalEngine.start();
}
tenantEngines.values().forEach(RulesEngine::start);
assetEngines.values().forEach(RulesEngine::start);
startDone = true;
preInitassetStates.forEach(this::doProcessAssetUpdate);
preInitassetStates.clear();
}
use of org.openremote.manager.rules.geofence.GeofenceAssetAdapter in project openremote by openremote.
the class RulesService method processModifiedGeofences.
protected void processModifiedGeofences() {
withLock(getClass().getSimpleName() + "::processModifiedGeofences", () -> {
LOG.finest("Processing geofence modifications: modified asset geofence count=" + assetsWithModifiedLocationPredicates.size());
try {
// Find all location predicates associated with modified assets and pass through to the geofence adapters
List<RulesEngine.AssetStateLocationPredicates> assetLocationPredicates = new ArrayList<>(assetsWithModifiedLocationPredicates.size());
assetsWithModifiedLocationPredicates.forEach(assetId -> {
RulesEngine.AssetStateLocationPredicates locationPredicates = new RulesEngine.AssetStateLocationPredicates(assetId, new HashSet<>());
engineAssetLocationPredicateMap.forEach((rulesEngine, engineAssetStateLocationPredicates) -> engineAssetStateLocationPredicates.stream().filter(assetStateLocationPredicates -> assetStateLocationPredicates.getAssetId().equals(assetId)).findFirst().ifPresent(assetStateLocationPredicate -> {
locationPredicates.getLocationPredicates().addAll(assetStateLocationPredicate.getLocationPredicates());
}));
assetLocationPredicates.add(locationPredicates);
});
for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {
LOG.finest("Passing modified geofences to adapter: " + geofenceAssetAdapter.getName());
geofenceAssetAdapter.processLocationPredicates(assetLocationPredicates);
if (assetLocationPredicates.isEmpty()) {
LOG.finest("All modified geofences handled");
break;
}
}
} catch (Exception e) {
LOG.log(SEVERE, "Exception thrown by geofence adapter whilst processing location predicates", e);
} finally {
// Clear modified assets ready for next batch
assetsWithModifiedLocationPredicates.clear();
}
});
}
use of org.openremote.manager.rules.geofence.GeofenceAssetAdapter in project openremote by openremote.
the class RulesService method stop.
@Override
public void stop(Container container) throws Exception {
withLock(getClass().getSimpleName() + "::stop", () -> {
for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {
try {
geofenceAssetAdapter.stop(container);
} catch (Exception e) {
LOG.log(SEVERE, "Exception thrown whilst stopping geofence adapter", e);
}
}
assetEngines.forEach((assetId, rulesEngine) -> rulesEngine.stop(true));
assetEngines.clear();
tenantEngines.forEach((realm, rulesEngine) -> rulesEngine.stop(true));
tenantEngines.clear();
if (globalEngine != null) {
globalEngine.stop(true);
globalEngine = null;
}
assetStates.clear();
});
for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {
geofenceAssetAdapter.stop(container);
}
}
use of org.openremote.manager.rules.geofence.GeofenceAssetAdapter in project openremote by openremote.
the class RulesService method init.
@Override
public void init(Container container) throws Exception {
executorService = container.getExecutorService();
timerService = container.getService(TimerService.class);
persistenceService = container.getService(PersistenceService.class);
rulesetStorageService = container.getService(RulesetStorageService.class);
identityService = container.getService(ManagerIdentityService.class);
notificationService = container.getService(NotificationService.class);
assetStorageService = container.getService(AssetStorageService.class);
assetProcessingService = container.getService(AssetProcessingService.class);
assetDatapointService = container.getService(AssetDatapointService.class);
assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class);
clientEventService = container.getService(ClientEventService.class);
gatewayService = container.getService(GatewayService.class);
if (initDone) {
return;
}
clientEventService.addSubscriptionAuthorizer((realm, auth, subscription) -> {
if (subscription.isEventType(RulesEngineStatusEvent.class) || subscription.isEventType(RulesetChangedEvent.class)) {
if (auth == null) {
return false;
}
if (auth.isSuperUser()) {
return true;
}
// Regular user must have role
if (!auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), auth.getClientId())) {
return false;
}
boolean isRestrictedUser = identityService.getIdentityProvider().isRestrictedUser(auth);
return !isRestrictedUser;
}
return false;
});
ServiceLoader.load(GeofenceAssetAdapter.class).forEach(geofenceAssetAdapter -> {
LOG.fine("Adding GeofenceAssetAdapter: " + geofenceAssetAdapter.getClass().getName());
geofenceAssetAdapters.add(geofenceAssetAdapter);
});
geofenceAssetAdapters.addAll(container.getServices(GeofenceAssetAdapter.class));
geofenceAssetAdapters.sort(Comparator.comparingInt(GeofenceAssetAdapter::getPriority));
container.getService(MessageBrokerService.class).getContext().addRoutes(this);
configEventExpires = getString(container.getConfig(), RULE_EVENT_EXPIRES, RULE_EVENT_EXPIRES_DEFAULT);
container.getService(ManagerWebService.class).addApiSingleton(new FlowResourceImpl(container.getService(TimerService.class), container.getService(ManagerIdentityService.class)));
initDone = true;
}
Aggregations