use of org.openremote.model.PersistenceEvent in project openremote by openremote.
the class RulesService method configure.
@SuppressWarnings("unchecked")
@Override
public void configure() throws Exception {
// If any ruleset was modified in the database then check its' status and undeploy, deploy, or update it
from(PERSISTENCE_TOPIC).routeId("RulesetPersistenceChanges").filter(isPersistenceEventForEntityType(Ruleset.class)).filter(isNotForGateway(gatewayService)).process(exchange -> {
PersistenceEvent<?> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
processRulesetChange((Ruleset) persistenceEvent.getEntity(), persistenceEvent.getCause());
});
// If any tenant was modified in the database then check its' status and undeploy, deploy or update any
// associated rulesets
from(PERSISTENCE_TOPIC).routeId("RuleEngineTenantChanges").filter(isPersistenceEventForEntityType(Tenant.class)).filter(isNotForGateway(gatewayService)).process(exchange -> {
PersistenceEvent<?> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
Tenant tenant = (Tenant) persistenceEvent.getEntity();
processTenantChange(tenant, persistenceEvent.getCause());
});
// If any asset was modified in the database, detect changed attributes
from(PERSISTENCE_TOPIC).routeId("RuleEngineAssetChanges").filter(isPersistenceEventForEntityType(Asset.class)).process(exchange -> {
PersistenceEvent<Asset<?>> persistenceEvent = (PersistenceEvent<Asset<?>>) exchange.getIn().getBody(PersistenceEvent.class);
final Asset<?> eventAsset = persistenceEvent.getEntity();
processAssetChange(eventAsset, persistenceEvent);
});
}
use of org.openremote.model.PersistenceEvent in project openremote by openremote.
the class PushNotificationHandler method configure.
@Override
public void configure() throws Exception {
// If any console asset was modified in the database, detect push provider changes
from(PersistenceService.PERSISTENCE_TOPIC).routeId("PushNotificationAssetChanges").filter(PersistenceService.isPersistenceEventForEntityType(ConsoleAsset.class)).filter(isNotForGateway(gatewayService)).process(exchange -> {
@SuppressWarnings("unchecked") PersistenceEvent<ConsoleAsset> persistenceEvent = (PersistenceEvent<ConsoleAsset>) exchange.getIn().getBody(PersistenceEvent.class);
final ConsoleAsset asset = persistenceEvent.getEntity();
processConsoleAssetChange(asset, persistenceEvent);
});
}
use of org.openremote.model.PersistenceEvent in project openremote by openremote.
the class MqttBrokerService method configure.
@SuppressWarnings("unchecked")
@Override
public void configure() throws Exception {
from(PERSISTENCE_TOPIC).routeId("UserPersistenceChanges").filter(isPersistenceEventForEntityType(User.class)).process(exchange -> {
PersistenceEvent<User> persistenceEvent = (PersistenceEvent<User>) exchange.getIn().getBody(PersistenceEvent.class);
User user = persistenceEvent.getEntity();
if (!user.isServiceAccount()) {
return;
}
boolean forceDisconnect = persistenceEvent.getCause() == PersistenceEvent.Cause.DELETE;
if (persistenceEvent.getCause() == PersistenceEvent.Cause.UPDATE) {
// Force disconnect if certain properties have changed
forceDisconnect = Arrays.stream(persistenceEvent.getPropertyNames()).anyMatch((propertyName) -> (propertyName.equals("enabled") && !user.getEnabled()) || propertyName.equals("username"));
}
if (forceDisconnect) {
// Find existing connection for this user
Arrays.stream(getConnections()).filter(connection -> user.getUsername().equals(connection.getUsername())).findFirst().ifPresent(connection -> {
LOG.info("User modified or deleted so forcing connected client to disconnect: connection=" + connection);
forceDisconnect(connection.getClientId());
});
}
});
}
use of org.openremote.model.PersistenceEvent in project openremote by openremote.
the class GatewayService method processGatewayChange.
protected void processGatewayChange(GatewayAsset gateway, PersistenceEvent<Asset<?>> persistenceEvent) {
switch(persistenceEvent.getCause()) {
case CREATE:
createUpdateGatewayServiceUser(gateway);
synchronized (gatewayConnectorMap) {
GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);
gatewayConnectorMap.put(gateway.getId().toLowerCase(Locale.ROOT), connector);
}
break;
case UPDATE:
// Check if this gateway has a connector
GatewayConnector connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));
if (connector == null) {
break;
}
connector.gateway = gateway;
// Check if disabled
boolean isNowDisabled = gateway.getDisabled().orElse(false);
if (isNowDisabled) {
connector.sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
}
connector.setDisabled(isNowDisabled);
int attributeIndex = persistenceEvent.getPropertyNames() != null ? IntStream.range(0, persistenceEvent.getPropertyNames().length).filter(i -> "attributes".equals(persistenceEvent.getPropertyNames()[i])).findFirst().orElse(-1) : -1;
if (attributeIndex >= 0) {
// Check if disabled attribute has changed
AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");
boolean wasDisabled = oldAttributes.getValue(GatewayAsset.DISABLED).orElse(false);
if (wasDisabled != isNowDisabled) {
createUpdateGatewayServiceUser(gateway);
}
}
break;
case DELETE:
// Check if this gateway has a connector
connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));
if (connector == null) {
break;
}
synchronized (gatewayConnectorMap) {
connector = gatewayConnectorMap.remove(gateway.getId().toLowerCase(Locale.ROOT));
if (connector != null) {
connector.disconnect();
}
}
removeGatewayServiceUser(gateway);
break;
}
}
Aggregations