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
Cleanup and bug fixes in preparation for sharing the container between tests
Rich Turner4cc14f8e6f4a
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.javaindex 0e578cb94..4ec07da28 100644--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java@@ -209,7 +209,6 @@ public abstract class AbstractProtocol implements Protocol {if (protocolInfo != null && protocolInfo.currentConnectionStatus != ConnectionStatus.DISABLED) {LOG.finer("Unlinking protocol configuration from protocol '" + getProtocolName() + "': " + protocolConfiguration);doUnlinkProtocolConfiguration(agent, protocolConfiguration);- updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISCONNECTED);}linkedProtocolConfigurations.remove(protocolConfiguration.getReferenceOrThrow());});@@ -254,7 +253,7 @@ public abstract class AbstractProtocol implements Protocol {withLock(getProtocolName() + "::unlinkAttributes", () -> {LinkedProtocolInfo protocolInfo = linkedProtocolConfigurations.get(protocolConfiguration.getReferenceOrThrow());- if (protocolInfo.currentConnectionStatus == ConnectionStatus.DISABLED) {+ if (protocolInfo != null && protocolInfo.currentConnectionStatus == ConnectionStatus.DISABLED) {LOG.info("Protocol configuration is disabled so not unlinking attributes '" + getProtocolName() + "': " + protocolConfiguration);return;}diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.javaindex fa937ec0d..be4395988 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java@@ -163,7 +163,6 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implementsAttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();Pair<VelbusNetwork, Consumer<ConnectionStatus>> velbusNetworkAndConsumer = networkConfigurationMap.remove(protocolRef);- updateStatus(protocolRef, ConnectionStatus.DISCONNECTED);if (velbusNetworkAndConsumer != null) {velbusNetworkAndConsumer.key.removeConnectionStatusConsumer(velbusNetworkAndConsumer.value);diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.javaindex 341ce34be..b3724c939 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java@@ -28,7 +28,9 @@ import org.openremote.model.value.Value;import java.util.*;import java.util.concurrent.Future;+import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledFuture;+import java.util.concurrent.TimeUnit;import java.util.function.Consumer;import static org.openremote.agent.protocol.velbus.AbstractVelbusProtocol.LOG;@@ -44,10 +46,10 @@ public class VelbusNetwork {protected VelbusDevice[] devices = new VelbusDevice[254];protected VelbusDevice[] subAddressDevices = new VelbusDevice[254];protected ScheduledFuture queueProcessingTask;- protected ProtocolExecutorService executorService;+ protected ScheduledExecutorService executorService;protected final List<Consumer<ConnectionStatus>> connectionStatusConsumers = new ArrayList<>();- public VelbusNetwork(IoClient<VelbusPacket> client, ProtocolExecutorService executorService, Integer timeInjectionIntervalSeconds) {+ public VelbusNetwork(IoClient<VelbusPacket> client, ScheduledExecutorService executorService, Integer timeInjectionIntervalSeconds) {this.client = client;this.executorService = executorService;this.timeInjectionIntervalSeconds = timeInjectionIntervalSeconds;@@ -55,7 +57,7 @@ public class VelbusNetwork {client.addMessageConsumer(this::onPacketReceived);onConnectionStatusChanged(getConnectionStatus());if (timeInjectionIntervalSeconds != null) {- timeInjector = getExecutorService().scheduleWithFixedDelay(this::doTimeInjection, timeInjectionIntervalSeconds * 1000, timeInjectionIntervalSeconds * 1000);+ timeInjector = getExecutorService().scheduleWithFixedDelay(this::doTimeInjection, timeInjectionIntervalSeconds, timeInjectionIntervalSeconds, TimeUnit.SECONDS);}}@@ -71,7 +73,7 @@ public class VelbusNetwork {}}- public ProtocolExecutorService getExecutorService() {+ public ScheduledExecutorService getExecutorService() {return this.executorService;}@@ -264,7 +266,8 @@ public class VelbusNetwork {queueProcessingTask = getExecutorService().scheduleWithFixedDelay(this::doSendPacket,0,- DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS+ DELAY_BETWEEN_PACKET_WRITES_MILLISECONDS,+ TimeUnit.MILLISECONDS);}@@ -287,7 +290,7 @@ public class VelbusNetwork {scheduledTasks.removeIf(Future::isDone);if (getConnectionStatus() == ConnectionStatus.CONNECTED) {- ScheduledFuture future = getExecutorService().schedule(runnable, delayMillis);+ ScheduledFuture future = getExecutorService().schedule(runnable, delayMillis, TimeUnit.MILLISECONDS);scheduledTasks.add(future);return future;}diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.javaindex cfa1963ba..5b05a708c 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/device/VelbusDevice.java@@ -6,6 +6,7 @@ import org.openremote.model.value.Value;import java.util.*;import java.util.concurrent.Future;+import java.util.concurrent.TimeUnit;import java.util.function.Consumer;import java.util.stream.Collectors;@@ -110,7 +111,7 @@ public class VelbusDevice {}LOG.info("Initialisation starting: " + getBaseAddress());- initialisationTask = velbusNetwork.getExecutorService().scheduleWithFixedDelay(this::doInitialisation, 0, INITIALISATION_TIMEOUT_SECONDS * 1000);+ initialisationTask = velbusNetwork.getExecutorService().scheduleWithFixedDelay(this::doInitialisation, 0, INITIALISATION_TIMEOUT_SECONDS, TimeUnit.SECONDS);}}diff --git a/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.java b/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.javaindex acc9ded46..f3acd724b 100644--- a/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.java+++ b/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.java@@ -90,7 +90,7 @@ public class EventServiceImpl implements EventService {@Overridepublic <E extends SharedEvent> void unsubscribe(Class<E> eventClass) {- CancelEventSubscription<E> cancellation = new CancelEventSubscription<>(eventClass);+ CancelEventSubscription cancellation = new CancelEventSubscription(eventClass);final String key = cancellation.getEventType();if (activeSubscriptions.containsKey(key)) {DomGlobal.clearInterval(activeSubscriptions.get(key));diff --git a/container/src/main/java/org/openremote/container/security/IdentityProvider.java b/container/src/main/java/org/openremote/container/security/IdentityProvider.javaindex 198b86309..15c572657 100644--- a/container/src/main/java/org/openremote/container/security/IdentityProvider.java+++ b/container/src/main/java/org/openremote/container/security/IdentityProvider.java@@ -20,6 +20,7 @@package org.openremote.container.security;import io.undertow.servlet.api.DeploymentInfo;+import org.openremote.container.Container;/*** SPI for implementations used by {@link IdentityService}.@@ -29,11 +30,11 @@ public interface IdentityProvider {String SETUP_ADMIN_PASSWORD = "SETUP_ADMIN_PASSWORD";String SETUP_ADMIN_PASSWORD_DEFAULT = "secret";- void init();+ void init(Container container) throws Exception;- void start();+ void start(Container container) throws Exception;- void stop();+ void stop(Container container) throws Exception;void secureDeployment(DeploymentInfo deploymentInfo);}diff --git a/container/src/main/java/org/openremote/container/security/IdentityService.java b/container/src/main/java/org/openremote/container/security/IdentityService.javaindex ca3685260..9452dd91c 100644--- a/container/src/main/java/org/openremote/container/security/IdentityService.java+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java@@ -44,18 +44,10 @@ public abstract class IdentityService implements ContainerService {public static final int PRIORITY = PersistenceService.PRIORITY - 10;private static final Logger LOG = Logger.getLogger(IdentityService.class.getName());-- public static final String IDENTITY_NETWORK_SECURE = "IDENTITY_NETWORK_SECURE";- public static final boolean IDENTITY_NETWORK_SECURE_DEFAULT = false;- public static final String IDENTITY_NETWORK_HOST = "IDENTITY_NETWORK_HOST";- public static final String IDENTITY_NETWORK_HOST_DEFAULT = "localhost";- public static final String IDENTITY_NETWORK_WEBSERVER_PORT = "IDENTITY_NETWORK_WEBSERVER_PORT";- public static final int IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT = 8080;public static final String IDENTITY_PROVIDER = "IDENTITY_PROVIDER";public static final String IDENTITY_PROVIDER_DEFAULT = "keycloak";// The externally visible address of this installation- protected UriBuilder externalServerUri;protected IdentityProvider identityProvider;protected boolean devMode;@@ -66,39 +58,21 @@ public abstract class IdentityService implements ContainerService {@Overridepublic void init(Container container) throws Exception {- boolean identityNetworkSecure = getBoolean(container.getConfig(), IDENTITY_NETWORK_SECURE, IDENTITY_NETWORK_SECURE_DEFAULT);- String identityNetworkHost = getString(container.getConfig(), IDENTITY_NETWORK_HOST, IDENTITY_NETWORK_HOST_DEFAULT);- int identityNetworkPort = getInteger(container.getConfig(), IDENTITY_NETWORK_WEBSERVER_PORT, IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT);devMode = container.isDevMode();- externalServerUri = UriBuilder.fromUri("")- .scheme(identityNetworkSecure ? "https" : "http")- .host(identityNetworkHost);-- // Only set the port if it's not the default protocol port- if (identityNetworkPort != 80 && identityNetworkPort != 443) {- externalServerUri = externalServerUri.port(identityNetworkPort);- }-- LOG.info("External system base URL: " + externalServerUri.build());-String identityProviderType = getString(container.getConfig(), IDENTITY_PROVIDER, IDENTITY_PROVIDER_DEFAULT);identityProvider = createIdentityProvider(container, identityProviderType);- identityProvider.init();+ identityProvider.init(container);}@Overridepublic void start(Container container) throws Exception {- identityProvider.start();+ identityProvider.start(container);}@Overridepublic void stop(Container container) throws Exception {- identityProvider.stop();- }-- public UriBuilder getExternalServerUri() {- return externalServerUri.clone();+ identityProvider.stop(container);}public void secureDeployment(DeploymentInfo deploymentInfo) {@@ -144,4 +118,4 @@ public abstract class IdentityService implements ContainerService {* To configure the {@link IdentityProvider}, subclasses should override {@link #init(Container)}.*/abstract public IdentityProvider createIdentityProvider(Container container, String type);-}\ No newline at end of file+}diff --git a/container/src/main/java/org/openremote/container/security/basic/BasicIdentityProvider.java b/container/src/main/java/org/openremote/container/security/basic/BasicIdentityProvider.javaindex 4eed5308d..eba4898d6 100644--- a/container/src/main/java/org/openremote/container/security/basic/BasicIdentityProvider.java+++ b/container/src/main/java/org/openremote/container/security/basic/BasicIdentityProvider.java@@ -33,6 +33,7 @@ import io.undertow.server.handlers.form.FormParserFactory;import io.undertow.servlet.api.AuthMethodConfig;import io.undertow.servlet.api.DeploymentInfo;import io.undertow.servlet.api.LoginConfig;+import org.openremote.container.Container;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.security.IdentityProvider;import org.openremote.model.Constants;@@ -124,9 +125,10 @@ public abstract class BasicIdentityProvider implements IdentityProvider {}private static final Logger LOG = Logger.getLogger(BasicIdentityProvider.class.getName());- final protected PersistenceService persistenceService;+ protected PersistenceService persistenceService;- public BasicIdentityProvider(PersistenceService persistenceService) {+ @Override+ public void init(Container container) {this.persistenceService = persistenceService;// Add schema and scripts for the PUBLIC realm to replicate keycloak user tablesthis.persistenceService.getDefaultSchemaLocations().add(@@ -136,17 +138,12 @@ public abstract class BasicIdentityProvider implements IdentityProvider {}@Override- public void init() {-- }-- @Override- public void start() {+ public void start(Container container) {}@Override- public void stop() {+ public void stop(Container container) {}diff --git a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.javaindex c4c9742db..12f8298f5 100644--- a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java+++ b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java@@ -47,7 +47,6 @@ import javax.ws.rs.core.Response;import javax.ws.rs.core.UriBuilder;import java.util.Arrays;import java.util.List;-import java.util.concurrent.ExecutorService;import java.util.concurrent.TimeUnit;import java.util.logging.Level;import java.util.logging.Logger;@@ -55,8 +54,7 @@ import java.util.logging.Logger;import static java.util.concurrent.TimeUnit.MINUTES;import static javax.ws.rs.core.Response.Status.Family.REDIRECTION;import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;-import static org.openremote.container.util.MapAccess.getInteger;-import static org.openremote.container.util.MapAccess.getString;+import static org.openremote.container.util.MapAccess.*;import static org.openremote.container.web.WebClient.getTarget;import static org.openremote.container.web.WebService.pathStartsWithHandler;import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;@@ -143,19 +141,25 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {public static final int IDENTITY_SESSION_MAX_MINUTES_DEFAULT = 60 * 24; // 1 daypublic static final String IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES = "IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES";public static final int IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES_DEFAULT = 60 * 24 * 14; // 14 days+ public static final String IDENTITY_NETWORK_SECURE = "IDENTITY_NETWORK_SECURE";+ public static final boolean IDENTITY_NETWORK_SECURE_DEFAULT = false;+ public static final String IDENTITY_NETWORK_HOST = "IDENTITY_NETWORK_HOST";+ public static final String IDENTITY_NETWORK_HOST_DEFAULT = "localhost";+ public static final String IDENTITY_NETWORK_WEBSERVER_PORT = "IDENTITY_NETWORK_WEBSERVER_PORT";+ public static final int IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT = 8080;private static final Logger LOG = Logger.getLogger(KeycloakIdentityProvider.class.getName());// Each realm in Keycloak has a client application with this identifierfinal protected String clientId;// The externally visible address of this installation- final protected UriBuilder externalServerUri;+ protected UriBuilder externalServerUri;// The (internal) URI where Keycloak can be found- final protected UriBuilder keycloakServiceUri;+ protected UriBuilder keycloakServiceUri;// Configuration options for new realms- final protected int sessionTimeoutSeconds;- final protected int sessionMaxSeconds;- final protected int sessionOfflineTimeoutSeconds;+ protected int sessionTimeoutSeconds;+ protected int sessionMaxSeconds;+ protected int sessionOfflineTimeoutSeconds;// This will pass authentication ("NOT ATTEMPTED" state), but later fail any role authorizationfinal protected KeycloakDeployment notAuthenticatedKeycloakDeployment = new KeycloakDeployment();// The client we use to access Keycloak@@ -168,10 +172,26 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {// Optional reverse proxy that listens to AUTH_PATH and forwards requests to Keycloakprotected HttpHandler authProxyHandler;- @SuppressWarnings("deprecation")- protected KeycloakIdentityProvider(String clientId, UriBuilder externalServerUri, ExecutorService executorService, Container container) {+ protected KeycloakIdentityProvider(String clientId) {this.clientId = clientId;- this.externalServerUri = externalServerUri;+ }++ @Override+ public void init(Container container) {+ boolean identityNetworkSecure = getBoolean(container.getConfig(), IDENTITY_NETWORK_SECURE, IDENTITY_NETWORK_SECURE_DEFAULT);+ String identityNetworkHost = getString(container.getConfig(), IDENTITY_NETWORK_HOST, IDENTITY_NETWORK_HOST_DEFAULT);+ int identityNetworkPort = getInteger(container.getConfig(), IDENTITY_NETWORK_WEBSERVER_PORT, IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT);++ externalServerUri = UriBuilder.fromUri("")+ .scheme(identityNetworkSecure ? "https" : "http")+ .host(identityNetworkHost);++ // Only set the port if it's not the default protocol port+ if (identityNetworkPort != 80 && identityNetworkPort != 443) {+ externalServerUri = externalServerUri.port(identityNetworkPort);+ }++ LOG.info("External system base URL: " + externalServerUri.build());sessionMaxSeconds = getInteger(container.getConfig(), IDENTITY_SESSION_MAX_MINUTES, IDENTITY_SESSION_MAX_MINUTES_DEFAULT) * 60;if (sessionMaxSeconds < 60) {@@ -195,6 +215,7 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {LOG.info("Keycloak service URL: " + keycloakServiceUri.build());+// This client sets a custom Host header on outgoing requests, acting like a reverse proxy that "preserves"// the Host header. Keycloak will verify token issuer name based on this, so it must match the external host// and port that was used to obtain the token.@@ -254,21 +275,17 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {}};- }-- @Override- public void init() {// TODO Not a great way to block startup while we wait for other services (Hystrix?)waitForKeycloak();LOG.info("Keycloak identity provider available: " + keycloakServiceUri.build());}@Override- public void start() {+ public void start(Container container) {}@Override- public void stop() {+ public void stop(Container container) {if (httpClient != null)httpClient.close();}@@ -419,6 +436,5 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {/*** There must be _some_ valid redirect URIs for the application or authentication will not be possible.*/- abstract protected void addClientRedirectUris(String client, List<String> redirectUrls);-+ abstract protected void addClientRedirectUris(String client, List<String> redirectUrls, boolean devMode);}diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentHealthStatusProvider.java b/manager/src/main/java/org/openremote/manager/agent/AgentHealthStatusProvider.javaindex fe0785b61..9d215703b 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentHealthStatusProvider.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentHealthStatusProvider.java@@ -20,12 +20,11 @@package org.openremote.manager.agent;import org.openremote.container.Container;-import org.openremote.container.ContainerService;import org.openremote.container.ContainerHealthStatusProvider;+import org.openremote.container.ContainerService;import org.openremote.model.asset.Asset;import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.asset.agent.ProtocolConfiguration;-import org.openremote.model.attribute.AttributeRef;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Value;import org.openremote.model.value.Values;@@ -141,8 +140,10 @@ public class AgentHealthStatusProvider implements ContainerHealthStatusProvider"status",protocolConfigInfo.getValue().value == null ? null : protocolConfigInfo.getValue().value.name());- List<AttributeRef> linkedAttributes = agentService.linkedAttributes.get(protocolConfigInfo.getKey());- protocol.put("linkedAttributes", linkedAttributes != null ? linkedAttributes.size() : 0);+ long attributeCount = !agentService.linkedAttributes.containsKey(protocolConfigInfo.getKey())+ ? 0+ : agentService.linkedAttributes.get(protocolConfigInfo.getKey()).size();+ protocol.put("linkedAttributes", attributeCount);agentValue.put(protocolConfigInfo.getKey().getAttributeName(), protocol);});diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.javaindex 57a8edbe6..3d3f2f9c1 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java@@ -105,10 +105,11 @@ public class AgentService extends RouteBuilder implements ContainerService, Asseprotected final Map<AttributeRef, Pair<AssetAttribute, ConnectionStatus>> protocolConfigurations = new HashMap<>();protected final Map<String, List<Consumer<PersistenceEvent<Asset>>>> childAssetSubscriptions = new HashMap<>();protected final Map<String, Protocol> protocols = new HashMap<>();- protected final Map<AttributeRef, List<AttributeRef>> linkedAttributes = new HashMap<>();+ protected final Map<AttributeRef, List<AssetAttribute>> linkedAttributes = new HashMap<>();protected LocalAgentConnector localAgentConnector;protected Map<String, Asset> agentMap;protected ParseContext jsonPathParser;+ protected boolean initDone;/*** It's important that {@link Protocol}s have a lower priority than this service so they are fully initialized@@ -130,6 +131,10 @@ public class AgentService extends RouteBuilder implements ContainerService, AssegatewayService = container.getService(GatewayService.class);localAgentConnector = new LocalAgentConnector(this);+ if (initDone) {+ return;+ }+clientEventService.addSubscriptionAuthorizer((auth, subscription) ->subscription.isEventType(AgentStatusEvent.class)&& identityService.getIdentityProvider()@@ -152,6 +157,8 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse.build().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL));++ initDone = true;}@Override@@ -193,6 +200,14 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse@Overridepublic void stop(Container container) throws Exception {+ agentMap.values().forEach(agent ->+ unlinkProtocolConfigurations(+ agent,+ agent.getAttributesStream()+ .filter(ProtocolConfiguration::isProtocolConfiguration)+ .collect(Collectors.toList())));+ agentMap.clear();+ protocols.clear();}@Override@@ -627,27 +642,12 @@ public class AgentService extends RouteBuilder implements ContainerService, AssewithLock(getClass().getSimpleName() + "::unlinkProtocolConfigurations", () -> configurations.forEach(configuration -> {AttributeRef protocolAttributeRef = configuration.getReferenceOrThrow();- // Get all assets that have attributes that use this protocol configuration- List<Asset> assets = assetStorageService.findAll(- new AssetQuery()- .attributeMeta(- new RefPredicate(- MetaItemType.AGENT_LINK,- protocolAttributeRef.getEntityId(),- protocolAttributeRef.getAttributeName()- )- )- );+ // Unlink all linked attributes for this protocol configuration+ List<AssetAttribute> protocolLinkedAttributes = linkedAttributes.remove(protocolAttributeRef);- assets.forEach(- asset ->- getGroupedAgentLinkAttributes(- asset.getAttributesStream(),- assetAttribute -> getAgentLink(assetAttribute)- .map(attributeRef -> attributeRef.equals(protocolAttributeRef))- .orElse(false)- ).forEach(this::unlinkAttributes)- );+ if (protocolLinkedAttributes != null && !protocolLinkedAttributes.isEmpty()) {+ unlinkAttributes(configuration, protocolLinkedAttributes);+ }Protocol protocol = getProtocol(configuration);@@ -658,8 +658,6 @@ public class AgentService extends RouteBuilder implements ContainerService, AsseLOG.log(Level.SEVERE, "Protocol threw an exception during protocol configuration unlinking", e);}- // Set status to disconnected- publishProtocolConnectionStatus(protocolAttributeRef, DISCONNECTED);protocolConfigurations.remove(protocolAttributeRef);// Check if there are any remaining configs for the agent@@ -677,6 +675,10 @@ public class AgentService extends RouteBuilder implements ContainerService, AsseLOG.info("Agent protocol status updated to " + connectionStatus + ": " + protocolRef);protocolDeploymentInfo.value = connectionStatus;+ if (!agentMap.containsKey(protocolRef.getEntityId())) {+ return;+ }+// Notify clientsclientEventService.publishEvent(new AgentStatusEvent(@@ -711,17 +713,13 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn;}- attributes.removeIf(attr ->- linkedAttributes.values().stream()- .anyMatch(linkedAttributes -> linkedAttributes.contains(attr.getReferenceOrThrow())));-linkedAttributes.compute(protocolConfiguration.getReferenceOrThrow(),(protocolRef, linkedAttrs) -> {if (linkedAttrs == null) {linkedAttrs = new ArrayList<>(attributes.size());}- linkedAttrs.addAll(attributes.stream().map(AssetAttribute::getReferenceOrThrow).collect(Collectors.toList()));+ linkedAttrs.addAll(attributes);return linkedAttrs;});@@ -738,7 +736,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asseprotected void unlinkAttributes(AssetAttribute protocolConfiguration, Collection<AssetAttribute> attributes) {withLock(getClass().getSimpleName() + "::unlinkAttributes", () -> {- LOG.fine("Unlinking all attributes that use protocol attribute: " + protocolConfiguration);+ LOG.fine("Unlinking attributes that use protocol attribute: " + protocolConfiguration);Protocol protocol = getProtocol(protocolConfiguration);if (protocol == null) {@@ -749,7 +747,7 @@ public class AgentService extends RouteBuilder implements ContainerService, AsselinkedAttributes.computeIfPresent(protocolConfiguration.getReferenceOrThrow(),(protocolRef, linkedAttrs) -> {- linkedAttrs.removeAll(attributes.stream().map(AssetAttribute::getReferenceOrThrow).collect(Collectors.toList()));+ linkedAttrs.removeIf(attr -> attributes.stream().anyMatch(a -> a.getReferenceOrThrow().equals(attr.getReferenceOrThrow())));return linkedAttrs.isEmpty() ? null : linkedAttrs;});diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 6066a6b34..988600b85 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -327,7 +327,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServiccontainer.getService(ManagerWebService.class).getApiSingletons().add(new ConsoleResourceImpl(container.getService(TimerService.class),identityService,- this)+ this,+ clientEventService));container.getService(MessageBrokerService.class).getContext().addRoutes(this);@@ -700,6 +701,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// Update all empty attribute timestamps with server-time (a caller which doesn't have a// reliable time source such as a browser should clear the timestamp when setting an attribute// value).+asset.getAttributesStream().forEach(attribute -> {Optional<Long> timestamp = attribute.getValueTimestamp();if (!timestamp.isPresent() || timestamp.get() <= 0) {diff --git a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.javaindex 1c3dcab6b..70c924591 100644--- a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java@@ -22,10 +22,12 @@ package org.openremote.manager.asset.console;import org.openremote.container.timer.TimerService;import org.openremote.container.util.UniqueIdentifierGenerator;import org.openremote.manager.asset.AssetStorageService;+import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.ValidationFailure;import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetEvent;import org.openremote.model.asset.AssetType;import org.openremote.model.asset.UserAsset;import org.openremote.model.console.ConsoleConfiguration;@@ -50,9 +52,22 @@ public class ConsoleResourceImpl extends ManagerWebResource implements ConsoleReprotected Map<String, String> realmConsoleParentMap = new HashMap<>();protected AssetStorageService assetStorageService;- public ConsoleResourceImpl(TimerService timerService, ManagerIdentityService identityService, AssetStorageService assetStorageService) {+ public ConsoleResourceImpl(TimerService timerService, ManagerIdentityService identityService, AssetStorageService assetStorageService, ClientEventService clientEventService) {super(timerService, identityService);this.assetStorageService = assetStorageService;++ // Subscribe for asset events+ clientEventService.addInternalSubscription(+ AssetEvent.class,+ null,+ this::onAssetChange);+ }++ protected void onAssetChange(AssetEvent event) {+ // Remove any parent console asset mapping if the asset gets deleted+ if (event.getCause() == AssetEvent.Cause.DELETE) {+ realmConsoleParentMap.values().remove(event.getEntityId());+ }}@Overridediff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.javaindex 20fd26b24..bf333cc73 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java@@ -1,15 +1,18 @@package org.openremote.manager.datapoint;import org.hibernate.Session;+import org.hibernate.exception.ConstraintViolationException;import org.hibernate.jdbc.AbstractReturningWork;import org.openremote.container.Container;import org.openremote.container.ContainerService;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.timer.TimerService;+import org.openremote.container.util.MapAccess;import org.openremote.manager.asset.AssetProcessingException;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.asset.AssetUpdateProcessor;import org.openremote.manager.concurrent.ManagerExecutorService;+import org.openremote.manager.notification.NotificationProcessingException;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.asset.Asset;@@ -26,8 +29,10 @@ import org.openremote.model.value.Value;import org.openremote.model.value.ValueType;import org.openremote.model.value.Values;import org.postgresql.util.PGInterval;+import org.postgresql.util.PGobject;import javax.persistence.EntityManager;+import javax.persistence.PersistenceException;import javax.persistence.TypedQuery;import java.sql.*;import java.time.Duration;@@ -44,6 +49,7 @@ import java.util.stream.Collectors;import static java.time.temporal.ChronoUnit.DAYS;import static java.util.stream.Collectors.groupingBy;import static java.util.stream.Collectors.toList;+import static org.openremote.container.util.MapAccess.getInteger;/*** Store and retrieve datapoints for asset attributes and periodically purge data points based on@@ -54,7 +60,7 @@ import static java.util.stream.Collectors.toList;public class AssetDatapointService implements ContainerService, AssetUpdateProcessor {public static final String DATA_POINTS_MAX_AGE_DAYS = "DATA_POINTS_MAX_AGE_DAYS";- public static final String DATA_POINTS_MAX_AGE_DAYS_DEFAULT = "31";+ public static final int DATA_POINTS_MAX_AGE_DAYS_DEFAULT = 31;private static final Logger LOG = Logger.getLogger(AssetDatapointService.class.getName());public static final int PRIORITY = AssetStorageService.PRIORITY + 100;protected PersistenceService persistenceService;@@ -85,9 +91,7 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce));- maxDatapointAgeDays = Integer.parseInt(- container.getConfig().getOrDefault(DATA_POINTS_MAX_AGE_DAYS, DATA_POINTS_MAX_AGE_DAYS_DEFAULT)- );+ maxDatapointAgeDays = getInteger(container.getConfig(), DATA_POINTS_MAX_AGE_DAYS, DATA_POINTS_MAX_AGE_DAYS_DEFAULT);if (maxDatapointAgeDays <= 0) {LOG.warning(DATA_POINTS_MAX_AGE_DAYS + " value is not a valid value so data points won't be auto purged");@@ -117,12 +121,34 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProceAsset asset,AssetAttribute attribute,Source source) throws AssetProcessingException {+if (attribute.isStoreDatapoints()&& attribute.getStateEvent().isPresent()&& attribute.getStateEvent().get().getValue().isPresent()) { // Don't store datapoints with null value++ // Perform upsert on datapoint (datapoint isn't immutable then really and tied to postgresql but prevents entire attribute event from failing)LOG.finest("Storing datapoint for: " + attribute);- AssetDatapoint assetDatapoint = new AssetDatapoint(attribute.getStateEvent().get());- em.persist(assetDatapoint);++ PGobject pgJsonValue = new PGobject();+ pgJsonValue.setType("jsonb");+ try {+ pgJsonValue.setValue(attribute.getValue().map(Value::toJson).orElse(null));+ } catch (SQLException e) {+ throw new AssetProcessingException(AssetProcessingException.Reason.STATE_STORAGE_FAILED, "Failed to insert or update asset data point for attribute: " + attribute);+ }++ em.unwrap(Session.class).doWork(connection -> {+ PreparedStatement st = connection.prepareStatement("INSERT INTO asset_datapoint (entity_id, attribute_name, value, timestamp) \n" ++ "VALUES (?, ?, ?, ?)\n" ++ "ON CONFLICT (entity_id, attribute_name, timestamp) DO UPDATE \n" ++ " SET value = excluded.value");++ st.setString(1, asset.getId());+ st.setString(2, attribute.name);+ st.setObject(3, pgJsonValue);+ st.setTimestamp(4, attribute.getValueTimestamp().map(java.sql.Timestamp::new).orElse(null));+ st.executeUpdate();+ });}return false;}diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex 04df7773e..79cb9f27e 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -28,7 +28,6 @@ import org.openremote.container.message.MessageBrokerService;import org.openremote.container.security.AuthContext;import org.openremote.container.timer.TimerService;import org.openremote.container.web.ConnectionConstants;-import org.openremote.manager.concurrent.ManagerExecutorService;import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.mqtt.MqttBrokerService;import org.openremote.manager.security.ManagerIdentityService;@@ -42,10 +41,7 @@ import org.openremote.model.util.TextUtil;import javax.websocket.CloseReason;import javax.websocket.Session;-import java.util.Collection;-import java.util.HashMap;-import java.util.Map;-import java.util.UUID;+import java.util.*;import java.util.concurrent.CopyOnWriteArraySet;import java.util.function.Consumer;import java.util.logging.Level;@@ -116,6 +112,7 @@ public class ClientEventService implements ProtocolClientEventService {public static final int PRIORITY = ManagerWebService.PRIORITY - 200;private static final Logger LOG = Logger.getLogger(ClientEventService.class.getName());public static final String WEBSOCKET_EVENTS = "events";+ protected static final String INTERNAL_SESSION_KEY = "ClientEventServiceInternal";// TODO: Some of these options should be configurable depending on expected load etc.public static final String CLIENT_EVENT_TOPIC = "seda://ClientEventTopic?multipleConsumers=true&concurrentConsumers=1&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&limitConcurrentConsumers=false&size=1000";@@ -130,6 +127,7 @@ public class ClientEventService implements ProtocolClientEventService {protected ManagerIdentityService identityService;protected EventSubscriptions eventSubscriptions;protected GatewayService gatewayService;+ protected Set<EventSubscription<?>> pendingInternalSubscriptions;protected boolean stopped;@Override@@ -145,8 +143,7 @@ public class ClientEventService implements ProtocolClientEventService {gatewayService = container.getService(GatewayService.class);eventSubscriptions = new EventSubscriptions(- container.getService(TimerService.class),- container.getService(ManagerExecutorService.class)+ container.getService(TimerService.class));messageBrokerService.getContext().getTypeConverterRegistry().addTypeConverters(@@ -207,8 +204,6 @@ public class ClientEventService implements ProtocolClientEventService {exchange.getIn().setBody(exchange.getIn().getBody(EventSubscription.class));} else if (bodyStr.startsWith(CancelEventSubscription.MESSAGE_PREFIX)) {exchange.getIn().setBody(exchange.getIn().getBody(CancelEventSubscription.class));- } else if (bodyStr.startsWith(RenewEventSubscriptions.MESSAGE_PREFIX)) {- exchange.getIn().setBody(exchange.getIn().getBody(RenewEventSubscriptions.class));} else if (bodyStr.startsWith(SharedEvent.MESSAGE_PREFIX)) {exchange.getIn().setBody(exchange.getIn().getBody(SharedEvent.class));}@@ -249,14 +244,6 @@ public class ClientEventService implements ProtocolClientEventService {eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));}).stop()- .when(body().isInstanceOf(RenewEventSubscriptions.class))- .process(exchange -> {- String sessionKey = getSessionKey(exchange);- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);- boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId());- eventSubscriptions.update(sessionKey, restrictedUser,exchange.getIn().getBody(RenewEventSubscriptions.class).getSubscriptionIds());- })- .stop().when(body().isInstanceOf(SharedEvent.class)).choice().when(header(HEADER_CONNECTION_TYPE).isNotNull()) // Inbound messages from clients@@ -275,6 +262,38 @@ public class ClientEventService implements ProtocolClientEventService {.end();}});++ // Add pending internal subscriptions+ if (!pendingInternalSubscriptions.isEmpty()) {+ pendingInternalSubscriptions.forEach(subscription ->+ eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, false, subscription));+ }++ pendingInternalSubscriptions = null;+ }++ /**+ * Make an internal subscription to {@link SharedEvent}s sent on the client event bus+ */+ public <T extends SharedEvent> String addInternalSubscription(Class<T> eventClass, EventFilter<T> filter, Consumer<T> eventConsumer) {+ return addInternalSubscription(Integer.toString(Objects.hash(eventClass, filter, eventConsumer)), eventClass, filter, eventConsumer);+ }+ public <T extends SharedEvent> String addInternalSubscription(String subscriptionId, Class<T> eventClass, EventFilter<T> filter, Consumer<T> eventConsumer) {+ EventSubscription<T> subscription = new EventSubscription<T>(eventClass, filter, subscriptionId, eventConsumer);+ if (eventSubscriptions == null) {+ // Not initialised yet+ if (pendingInternalSubscriptions == null) {+ pendingInternalSubscriptions = new HashSet<>();+ }+ pendingInternalSubscriptions.add(subscription);+ } else {+ eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, false, subscription);+ }+ return subscriptionId;+ }++ public void cancelInternalSubscription(String subscriptionId) {+ eventSubscriptions.cancel(INTERNAL_SESSION_KEY, new CancelEventSubscription(subscriptionId));}@Override@@ -330,7 +349,9 @@ public class ClientEventService implements ProtocolClientEventService {keycloakIdentityProvider.createClient(clientCredentials.getRealm(), clientRepresentation);User serviceUser = keycloakIdentityProvider.getClientServiceUser(clientCredentials.getRealm(), clientCredentials.getClientId());- keycloakIdentityProvider.updateRoles(clientCredentials.getRealm(), serviceUser.getId(), clientCredentials.getRoles());+ if (clientCredentials.getRoles() != null && clientCredentials.getRoles().length > 0) {+ keycloakIdentityProvider.updateRoles(clientCredentials.getRealm(), serviceUser.getId(), clientCredentials.getRoles());+ }}@Override@@ -358,14 +379,14 @@ public class ClientEventService implements ProtocolClientEventService {.anyMatch(authorizer -> authorizer.apply(authContext, subscription));}- public void publishEvent(SharedEvent event) {+ public <T extends SharedEvent> void publishEvent(T event) {publishEvent(true, event);}/*** @param accessRestricted <code>true</code> if this event can be received by restricted user sessions.*/- public void publishEvent(boolean accessRestricted, SharedEvent event) {+ public <T extends SharedEvent> void publishEvent(boolean accessRestricted, T event) {// Only publish if service is not stoppedif (stopped) {return;@@ -385,6 +406,10 @@ public class ClientEventService implements ProtocolClientEventService {if (messageBrokerService != null && messageBrokerService.getProducerTemplate() != null) {LOG.fine("Sending to session '" + sessionKey + "': " + data);SessionInfo sessionInfo = sessionKeyInfoMap.get(sessionKey);+ if (sessionInfo == null) {+ LOG.info("Cannot send to requested session it doesn't exist or is disconnected");+ return;+ }if (sessionInfo.connectionType.equals(HEADER_CONNECTION_TYPE_WEBSOCKET)) {messageBrokerService.getProducerTemplate().sendBodyAndHeader("websocket://" + WEBSOCKET_EVENTS,@@ -412,10 +437,6 @@ public class ClientEventService implements ProtocolClientEventService {sessionInfo.closeRunnable.run();}- public EventSubscriptions getEventSubscriptions() {- return eventSubscriptions;- }-protected void passToInterceptors(Exchange exchange) {// Pass to each interceptor and stop if any interceptor marks the exchange as stop routingif (exchangeInterceptors.stream().anyMatch(interceptor -> {diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.javaindex 1fbc3814a..285e29e1d 100644--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java@@ -24,7 +24,6 @@ import org.apache.camel.Message;import org.apache.camel.impl.DefaultMessage;import org.openremote.container.timer.TimerService;import org.openremote.container.web.ConnectionConstants;-import org.openremote.manager.concurrent.ManagerExecutorService;import org.openremote.model.event.TriggeredEventSubscription;import org.openremote.model.event.shared.CancelEventSubscription;import org.openremote.model.event.shared.EventSubscription;@@ -58,7 +57,7 @@ public class EventSubscriptions {);}- public void createOrUpdate(boolean restrictedUser, EventSubscription eventSubscription) {+ public void createOrUpdate(boolean restrictedUser, EventSubscription<?> eventSubscription) {if (TextUtil.isNullOrEmpty(eventSubscription.getSubscriptionId())) {cancelByType(eventSubscription.getEventType());@@ -114,22 +113,12 @@ public class EventSubscriptions {}}- public EventSubscriptions(TimerService timerService, ManagerExecutorService executorService) {+ public EventSubscriptions(TimerService timerService) {LOG.info("Starting background task checking for expired event subscriptions from clients");this.timerService = timerService;- // This puts a burden on clients and generates noise; subscriptions are removed when the socket is closed- // so clients should actively add/remove subscriptions as they require rather than let them expire and/or- // have to renew them continually-// executorService.scheduleAtFixedRate(() -> {-// synchronized (this.sessionSubscriptionIdMap) {-// for (SessionSubscriptions subscriptions : sessionSubscriptionIdMap.values()) {-// subscriptions.removeExpired();-// }-// }-// }, 5000, 1000);}- public void createOrUpdate(String sessionKey, boolean restrictedUser, EventSubscription subscription) {+ public void createOrUpdate(String sessionKey, boolean restrictedUser, EventSubscription<?> subscription) {synchronized (this.sessionSubscriptionIdMap) {// TODO Check if the user can actually subscribe to the events it wants, how do we do that?LOG.fine("For session '" + sessionKey + "', creating/updating: " + subscription);@@ -151,14 +140,18 @@ public class EventSubscriptions {public void cancel(String sessionKey, CancelEventSubscription subscription) {synchronized (this.sessionSubscriptionIdMap) {- if (!this.sessionSubscriptionIdMap.containsKey(sessionKey))+ if (!this.sessionSubscriptionIdMap.containsKey(sessionKey)) {return;+ }+ if (subscription.getEventType() == null && subscription.getSubscriptionId() == null) {+ return;+ }LOG.fine("For session '" + sessionKey + "', cancelling: " + subscription);SessionSubscriptions sessionSubscriptions = this.sessionSubscriptionIdMap.get(sessionKey);- if (TextUtil.isNullOrEmpty(subscription.getSubscriptionId())) {- sessionSubscriptions.cancelByType(subscription.getEventType());- } else {+ if (!TextUtil.isNullOrEmpty(subscription.getSubscriptionId())) {sessionSubscriptions.cancelById(subscription.getSubscriptionId());+ } else {+ sessionSubscriptions.cancelByType(subscription.getEventType());}}}@@ -208,11 +201,14 @@ public class EventSubscriptions {msg.setHeader(ConnectionConstants.SESSION_KEY, sessionKey);messageList.add(msg);} else {- sessionSubscription.subscription.getInternalConsumer().accept(triggeredEventSubscription);+ if (triggeredEventSubscription.getEvents() != null) {+ triggeredEventSubscription.getEvents().forEach(ev ->+ sessionSubscription.subscription.getInternalConsumer().accept(ev));+ }}}}}return messageList;}-}\ No newline at end of file+}diff --git a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.javaindex a1a49ca01..da2d88cf6 100644--- a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java+++ b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java@@ -104,14 +104,6 @@ public class EventTypeConverters implements TypeConverters {return UnauthorizedEventSubscription.MESSAGE_PREFIX + Container.JSON.writeValueAsString(unauthorizedEventSubscription);}- @Converter- public RenewEventSubscriptions readRenewEventSubscriptions(String string, Exchange exchange) throws Exception {- if (!string.startsWith(RenewEventSubscriptions.MESSAGE_PREFIX))- return null;- string = string.substring(RenewEventSubscriptions.MESSAGE_PREFIX.length());- return Container.JSON.readValue(string, RenewEventSubscriptions.class);- }-@Converterpublic EventRequestResponseWrapper readRequestResponse(String string, Exchange exchange) throws Exception {if (!string.startsWith(EventRequestResponseWrapper.MESSAGE_PREFIX))diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.javaindex 056bbd2eb..00dc8b058 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.java@@ -19,13 +19,12 @@*/package org.openremote.manager.gateway;-import org.openremote.agent.protocol.websocket.WebsocketIoClient;import org.openremote.container.timer.TimerService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.asset.agent.ConnectionStatus;-import org.openremote.model.gateway.GatewayConnection;import org.openremote.model.gateway.GatewayClientResource;+import org.openremote.model.gateway.GatewayConnection;import org.openremote.model.http.RequestParams;import javax.ws.rs.WebApplicationException;diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.javaindex 032ab065e..40dd0fc63 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java@@ -227,27 +227,19 @@ public class GatewayClientService extends RouteBuilder implements ContainerServiclient.addMessageConsumer(message -> onCentralManagerMessage(connection, message));// Subscribe to Asset and attribute events of local realm and pass through to connected manager- clientEventService.getEventSubscriptions().createOrUpdate(- getClientSessionKey(connection),- false,- new EventSubscription<>(- AssetEvent.class,- new AssetFilter<AssetEvent>().setRealm(connection.getLocalRealm()),- triggeredEventSubscription ->- triggeredEventSubscription.getEvents()- .forEach(event ->- sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, event)))));-- clientEventService.getEventSubscriptions().createOrUpdate(- getClientSessionKey(connection),- false,- new EventSubscription<>(- AttributeEvent.class,- new AssetFilter<AttributeEvent>().setRealm(connection.getLocalRealm()),- triggeredEventSubscription ->- triggeredEventSubscription.getEvents()- .forEach(event ->- sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, event)))));+ clientEventService.addInternalSubscription(+ getClientSessionKey(connection)+"Asset",+ AssetEvent.class,+ new AssetFilter<AssetEvent>().setRealm(connection.getLocalRealm()),+ assetEvent ->+ sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, assetEvent)));++ clientEventService.addInternalSubscription(+ getClientSessionKey(connection)+"Attribute",+ AttributeEvent.class,+ new AssetFilter<AttributeEvent>().setRealm(connection.getLocalRealm()),+ attributeEvent ->+ sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, attributeEvent)));client.connect();return client;@@ -275,7 +267,8 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi}if (connection != null) {- clientEventService.getEventSubscriptions().cancelAll(getClientSessionKey(connection));+ clientEventService.cancelInternalSubscription(getClientSessionKey(connection)+"Asset");+ clientEventService.cancelInternalSubscription(getClientSessionKey(connection)+"Attribute");}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.javaindex 4d17d73df..d6323152f 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java@@ -38,7 +38,7 @@ import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.event.shared.CancelEventSubscription;import org.openremote.model.event.shared.EventSubscription;-import org.openremote.model.event.shared.RenewEventSubscriptions;+import org.openremote.model.util.TextUtil;import org.openremote.model.value.Value;import org.openremote.model.value.Values;@@ -147,32 +147,26 @@ public class EventInterceptHandler extends AbstractInterceptHandler {subscriptionId = connection.assetAttributeSubscriptions.remove(attributeRef);}}- if (subscriptionId != null) { //renew subscription- RenewEventSubscriptions renewEventSubscriptions = new RenewEventSubscriptions(new String[]{subscriptionId});- Map<String, Object> headers = prepareHeaders(connection);- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, renewEventSubscriptions, headers);- } else {- AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm).setAssetIds(assetId);- EventSubscription<AttributeEvent> subscription = new EventSubscription<>(- AttributeEvent.class,- attributeAssetFilter,- String.valueOf(connection.getNextSubscriptionId())- );- if (attributeRef == null) { //attribute specific- connection.assetSubscriptions.put(assetId, subscription.getSubscriptionId());+ AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm).setAssetIds(assetId);+ EventSubscription<AttributeEvent> subscription = new EventSubscription<>(+ AttributeEvent.class,+ attributeAssetFilter,+ TextUtil.isNullOrEmpty(subscriptionId) ? String.valueOf(connection.getNextSubscriptionId()) : subscriptionId);++ if (attributeRef == null) { //attribute specific+ connection.assetSubscriptions.put(assetId, subscription.getSubscriptionId());+ } else {+ attributeAssetFilter.setAttributeNames(attributeRef.getAttributeName());+ if (isValueSubscription) {+ connection.assetAttributeValueSubscriptions.put(attributeRef, subscription.getSubscriptionId());} else {- attributeAssetFilter.setAttributeNames(attributeRef.getAttributeName());- if (isValueSubscription) {- connection.assetAttributeValueSubscriptions.put(attributeRef, subscription.getSubscriptionId());- } else {- connection.assetAttributeSubscriptions.put(attributeRef, subscription.getSubscriptionId());- }+ connection.assetAttributeSubscriptions.put(attributeRef, subscription.getSubscriptionId());}-- Map<String, Object> headers = prepareHeaders(connection);- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);}++ Map<String, Object> headers = prepareHeaders(connection);+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);} else {throw new IllegalStateException("Connection with clientId " + interceptSubscribeMessage.getClientID() + " not found.");}@@ -197,7 +191,7 @@ public class EventInterceptHandler extends AbstractInterceptHandler {}if (subscriptionId != null) {Map<String, Object> headers = prepareHeaders(connection);- CancelEventSubscription<AttributeEvent> cancelEventSubscription = new CancelEventSubscription<>(AttributeEvent.class, subscriptionId);+ CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(AttributeEvent.class, subscriptionId);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);}}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.javaindex 67740eda6..e5cf4477b 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java@@ -125,6 +125,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse// here means we can quickly insert facts into newly started enginesprotected Set<AssetState> assetStates = new HashSet<>();protected String configEventExpires;+ protected boolean initDone;@Overridepublic int getPriority() {@@ -145,6 +146,10 @@ public class RulesService extends RouteBuilder implements ContainerService, AsseassetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class);clientEventService = container.getService(ClientEventService.class);+ if (initDone) {+ return;+ }+clientEventService.addSubscriptionAuthorizer((auth, subscription) -> {if (subscription.isEventType(RulesEngineStatusEvent.class) || subscription.isEventType(RulesetChangedEvent.class)) {@@ -182,6 +187,8 @@ public class RulesService extends RouteBuilder implements ContainerService, Assecontainer.getService(ManagerIdentityService.class)));++ initDone = true;}@Override@@ -191,7 +198,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.routeId("RulesetPersistenceChanges").filter(isPersistenceEventForEntityType(Ruleset.class)).process(exchange -> {- PersistenceEvent persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);+ PersistenceEvent<?> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);processRulesetChange((Ruleset) persistenceEvent.getEntity(), persistenceEvent.getCause());});@@ -201,7 +208,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.routeId("RuleEngineTenantChanges").filter(isPersistenceEventForEntityType(Tenant.class)).process(exchange -> {- PersistenceEvent persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);+ PersistenceEvent<?> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);Tenant tenant = (Tenant) persistenceEvent.getEntity();processTenantChange(tenant, persistenceEvent.getCause());});@@ -293,7 +300,6 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse}}- // TODO: Do rule engines need to be stopped on shutdownassetEngines.forEach((assetId, rulesEngine) -> rulesEngine.stop(true));assetEngines.clear();tenantEngines.forEach((realm, rulesEngine) -> rulesEngine.stop(true));@@ -303,7 +309,13 @@ public class RulesService extends RouteBuilder implements ContainerService, AsseglobalEngine.stop(true);globalEngine = null;}++ assetStates.clear();});++ for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {+ geofenceAssetAdapter.stop(container);+ }}@Overridediff --git a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.javaindex f96d4183f..ce161f651 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java@@ -24,7 +24,6 @@ import org.openremote.container.Container;import org.openremote.container.security.AuthContext;import org.openremote.container.security.basic.BasicIdentityProvider;import org.openremote.container.security.basic.PasswordStorage;-import org.openremote.manager.persistence.ManagerPersistenceService;import org.openremote.model.event.shared.TenantFilter;import org.openremote.model.query.UserQuery;import org.openremote.model.query.filter.TenantPredicate;@@ -44,18 +43,19 @@ import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;public class ManagerBasicIdentityProvider extends BasicIdentityProvider implements ManagerIdentityProvider {private static final Logger LOG = Logger.getLogger(ManagerBasicIdentityProvider.class.getName());- final protected ManagerIdentityService identityService;- final protected String adminPassword;+ protected ManagerIdentityService identityService;+ protected String adminPassword;- public ManagerBasicIdentityProvider(Container container) {- super(container.getService(ManagerPersistenceService.class));+ @Override+ public void init(Container container) {+ super.init(container);this.identityService = container.getService(ManagerIdentityService.class);adminPassword = container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT);}@Override- public void start() {- super.start();+ public void start(Container container) {+ super.start(container);// Create master tenant and admin userif (!tenantExists(MASTER_REALM)) {diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.javaindex 2e0a8c916..878c9ff2e 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java@@ -47,7 +47,6 @@ public class ManagerIdentityService extends IdentityService {container.getService(ManagerWebService.class).getApiSingletons().add(new UserResourceImpl(container.getService(TimerService.class), this));-}public ManagerIdentityProvider getIdentityProvider() {@@ -60,11 +59,11 @@ public class ManagerIdentityService extends IdentityService {switch (identityProviderType.toLowerCase(Locale.ROOT)) {case "keycloak":LOG.info("Enabling Keycloak identity provider");- this.identityProvider = new ManagerKeycloakIdentityProvider(getExternalServerUri(), container);+ this.identityProvider = new ManagerKeycloakIdentityProvider();break;case "basic":LOG.info("Enabling basic identity provider");- this.identityProvider = new ManagerBasicIdentityProvider(container);+ this.identityProvider = new ManagerBasicIdentityProvider();break;default:throw new UnsupportedOperationException("Unknown identity provider: " + identityProviderType);diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 7ee8673e3..1d519b68c 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -20,8 +20,8 @@package org.openremote.manager.security;import org.apache.camel.ExchangePattern;-import org.keycloak.admin.client.resource.*;import org.keycloak.admin.client.resource.UserResource;+import org.keycloak.admin.client.resource.*;import org.keycloak.common.enums.SslRequired;import org.keycloak.representations.idm.*;import org.openremote.container.Container;@@ -35,7 +35,6 @@ import org.openremote.container.timer.TimerService;import org.openremote.container.web.ClientRequestInfo;import org.openremote.container.web.WebService;import org.openremote.manager.apps.ConsoleAppService;-import org.openremote.manager.concurrent.ManagerExecutorService;import org.openremote.manager.event.ClientEventService;import org.openremote.model.Constants;import org.openremote.model.asset.AssetTreeModifiedEvent;@@ -53,8 +52,6 @@ import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;-import static org.openremote.container.security.IdentityService.IDENTITY_NETWORK_HOST;-import static org.openremote.container.security.IdentityService.IDENTITY_NETWORK_HOST_DEFAULT;import static org.openremote.container.util.JsonUtil.convert;import static org.openremote.container.util.MapAccess.getBoolean;import static org.openremote.container.util.MapAccess.getString;@@ -75,32 +72,38 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider impublic static final String DEFAULT_REALM_KEYCLOAK_THEME = "DEFAULT_REALM_KEYCLOAK_THEME";public static final String DEFAULT_REALM_KEYCLOAK_THEME_DEFAULT = "openremote";- final boolean devMode;- final protected PersistenceService persistenceService;- final protected TimerService timerService;- final protected MessageBrokerService messageBrokerService;- final protected ClientEventService clientEventService;- final protected ConsoleAppService consoleAppService;- final protected String keycloakAdminPassword;- final protected Container container;+ protected PersistenceService persistenceService;+ protected TimerService timerService;+ protected MessageBrokerService messageBrokerService;+ protected ClientEventService clientEventService;+ protected ConsoleAppService consoleAppService;+ protected String keycloakAdminPassword;+ protected Container container;- public ManagerKeycloakIdentityProvider(UriBuilder externalServerUri, Container container) {- super(KEYCLOAK_CLIENT_ID, externalServerUri, container.getService(ManagerExecutorService.class), container);+ public ManagerKeycloakIdentityProvider() {+ super(KEYCLOAK_CLIENT_ID);+ }+ @Override+ public void init(Container container) {+ super.init(container);this.container = container;this.keycloakAdminPassword = container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT);- this.devMode = container.isDevMode();this.timerService = container.getService(TimerService.class);this.persistenceService = container.getService(PersistenceService.class);this.messageBrokerService = container.getService(MessageBrokerService.class);this.clientEventService = container.getService(ClientEventService.class);this.consoleAppService = container.getService(ConsoleAppService.class);+ }+ @Override+ public void start(Container container) {+ super.start(container);enableAuthProxy(container.getService(WebService.class));}@Override- protected void addClientRedirectUris(String client, List<String> redirectUrls) {+ protected void addClientRedirectUris(String client, List<String> redirectUrls, boolean devMode) {if (devMode) {// Allow any redirect URIs in dev moderedirectUrls.add("*");@@ -159,6 +162,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imResponse response = realmResource.users().create(convert(Container.JSON, UserRepresentation.class, user));+ response.close();if (!response.getStatusInfo().equals(Response.Status.CREATED)) {throw new WebApplicationException(Response.status(response.getStatus())@@ -166,7 +170,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im.build());} else {- response.close();user = getUserByUsername(realm, user.getUsername().toLowerCase());//Username is stored in lowercase in Keycloakif (user != null && !TextUtil.isNullOrEmpty(password)) {CredentialRepresentation credentials = new CredentialRepresentation();@@ -253,13 +256,13 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imList<RoleRepresentation> availableRoles = roleMappingResource.clientLevel(client.getId()).listAvailable();// Get newly defined roles- List<RoleRepresentation> addRoles = Arrays.stream(roles)+ List<RoleRepresentation> addRoles = roles == null ? Collections.emptyList() : Arrays.stream(roles).filter(cr -> clientMappedRoles.stream().noneMatch(r -> r.getName().equals(cr.getValue()))).map(cr -> availableRoles.stream().filter(r -> r.getName().equals(cr.getValue())).findFirst().orElse(null)).collect(Collectors.toList());// Remove obsolete roles- List<RoleRepresentation> removeRoles = clientMappedRoles.stream()+ List<RoleRepresentation> removeRoles = roles == null ? clientMappedRoles : clientMappedRoles.stream().filter(r -> Arrays.stream(roles).noneMatch(cr -> cr.getValue().equals(r.getName()))).collect(Collectors.toList());@@ -547,7 +550,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}public void createOpenRemoteClientApplication(String realm) {- createClient(realm, createDefaultClientRepresentation(realm, KEYCLOAK_CLIENT_ID, "OpenRemote", devMode));+ createClient(realm, createDefaultClientRepresentation(realm, KEYCLOAK_CLIENT_ID, "OpenRemote", container.isDevMode()));}/**@@ -579,11 +582,11 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imList<String> redirectUris = new ArrayList<>();try {for (String consoleName : consoleAppService.getInstalled()) {- addClientRedirectUris(consoleName, redirectUris);+ addClientRedirectUris(consoleName, redirectUris, devMode);}} catch (Exception exception) {LOG.log(Level.WARNING, exception.getMessage(), exception);- addClientRedirectUris(realm, redirectUris);+ addClientRedirectUris(realm, redirectUris, devMode);}client.setRedirectUris(redirectUris);diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.javaindex 0276724db..b5845bcc0 100644--- a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java@@ -335,7 +335,7 @@ public abstract class AbstractManagerSetup implements Setup {);}- public class Scene {+ public static class Scene {final String attributeName;final String attributeLabel;@@ -394,7 +394,8 @@ public abstract class AbstractManagerSetup implements Setup {}}- protected Asset createDemoApartmentSceneAgent(Asset apartment, Scene[] scenes, Asset... rooms) {+ public static Asset createDemoApartmentScenes(AssetStorageService assetStorageService, Asset apartment, Scene[] scenes, Asset... rooms) {+Asset agent = new Asset("Scene Agent", AGENT, apartment);for (Scene scene : scenes) {agent.addAttributes(scene.createMacroAttribute(apartment, rooms));@@ -404,11 +405,13 @@ public abstract class AbstractManagerSetup implements Setup {}addDemoApartmentSceneEnableDisableTimer(apartment, agent, scenes);-+ agent = assetStorageService.merge(agent);+ linkDemoApartmentWithSceneAgent(apartment, agent, scenes);+ apartment = assetStorageService.merge(apartment);return agent;}- protected void addDemoApartmentSceneEnableDisableTimer(Asset apartment, Asset agent, Scene[] scenes) {+ protected static void addDemoApartmentSceneEnableDisableTimer(Asset apartment, Asset agent, Scene[] scenes) {AssetAttribute enableAllMacro = initProtocolConfiguration(new AssetAttribute("enableSceneTimer"), MacroProtocol.PROTOCOL_NAME).addMeta(new MetaItem(LABEL, Values.create("Enable scene timer")));for (Scene scene : scenes) {@@ -440,7 +443,7 @@ public abstract class AbstractManagerSetup implements Setup {agent.addAttributes(disableAllMacro);}- protected void linkDemoApartmentWithSceneAgent(Asset apartment, Asset agent, Scene[] scenes) {+ protected static void linkDemoApartmentWithSceneAgent(Asset apartment, Asset agent, Scene[] scenes) {for (Scene scene : scenes) {apartment.addAttributes(new AssetAttribute(scene.attributeName, AttributeValueType.STRING, Values.create(AttributeExecuteStatus.READY.name()))diff --git a/manager/src/main/java/org/openremote/manager/setup/SetupService.java b/manager/src/main/java/org/openremote/manager/setup/SetupService.javaindex 444689d67..1a899e83b 100644--- a/manager/src/main/java/org/openremote/manager/setup/SetupService.java+++ b/manager/src/main/java/org/openremote/manager/setup/SetupService.java@@ -114,16 +114,12 @@ public class SetupService implements ContainerService {} catch (Exception ex) {throw new RuntimeException("Error setting up application", ex);}-- // When setup is complete, initialize the clock again - effectively set the PSEUDO clock to- // wall clock time. If the PSEUDO clock is enabled in tests, we must advance time after setup- // imports asset data. The protocols which are started after setup can trigger asset attribute- // events, and event source time must be later than asset state time.- container.getService(TimerService.class).getClock().init();}@Overridepublic void stop(Container container) {+ setupTasks = null;+ setupList.clear();}public <S extends Setup> S getTaskOfType(Class<S> setupType) {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 39e783762..b9270ee9d 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@@ -46,6 +46,7 @@ import java.time.ZonedDateTime;import java.util.Arrays;import java.util.List;+import static org.openremote.manager.datapoint.AssetDatapointService.DATA_POINTS_MAX_AGE_DAYS_DEFAULT;import static org.openremote.model.asset.AssetType.*;import static org.openremote.model.asset.agent.ProtocolConfiguration.initProtocolConfiguration;import static org.openremote.model.attribute.AttributeValueType.*;@@ -61,12 +62,9 @@ public class ManagerDemoSetup extends AbstractManagerSetup {public static final int DEMO_RULE_STATES_SMART_OFFICE = 1;public static final int DEMO_RULE_STATES_SMART_BUILDING = DEMO_RULE_STATES_APARTMENT_1 + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;public static final int DEMO_RULE_STATES_SMART_CITY = 47;- public static final int DEMO_RULE_STATES_CUSTOMER_A = DEMO_RULE_STATES_SMART_BUILDING;- public static final int DEMO_RULE_STATES_GLOBAL = DEMO_RULE_STATES_CUSTOMER_A + DEMO_RULE_STATES_SMART_OFFICE + DEMO_RULE_STATES_SMART_CITY;+ public static final int DEMO_RULE_STATES_GLOBAL = DEMO_RULE_STATES_SMART_BUILDING + DEMO_RULE_STATES_SMART_OFFICE + DEMO_RULE_STATES_SMART_CITY;public static final int DEMO_RULE_STATES_APARTMENT_1_WITH_SCENES = DEMO_RULE_STATES_APARTMENT_1 + 28;- public static final int DEMO_RULE_STATES_SMART_HOME_WITH_SCENES = DEMO_RULE_STATES_APARTMENT_1_WITH_SCENES + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;- public static final int DEMO_RULE_STATES_CUSTOMER_A_WITH_SCENES = DEMO_RULE_STATES_SMART_HOME_WITH_SCENES;- public static final int DEMO_RULE_STATES_GLOBAL_WITH_SCENES = DEMO_RULE_STATES_CUSTOMER_A_WITH_SCENES;+ public static final int DEMO_RULE_STATES_SMART_BUILDING_WITH_SCENES = DEMO_RULE_STATES_APARTMENT_1_WITH_SCENES + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;public static GeoJSONPoint SMART_OFFICE_LOCATION = new GeoJSONPoint(5.460315214821094, 51.44541688237109);public static GeoJSONPoint SMART_BUILDING_LOCATION = new GeoJSONPoint(5.454027, 51.446308);public static GeoJSONPoint SMART_CITY_LOCATION = new GeoJSONPoint(5.3814711, 51.4484647);@@ -75,6 +73,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {public static GeoJSONPoint AREA_3_LOCATION = new GeoJSONPoint(5.487478, 51.446979);public static final String agentProtocolConfigName = "simulator123";public static final String thingLightToggleAttributeName = "light1Toggle";+ public static final Scene[] DEMO_APARTMENT_SCENES = new Scene[] {+ new Scene("morningScene", "Morning scene", "MORNING", "0 0 7 ? *", false, 21d),+ new Scene("dayScene", "Day scene", "DAY", "0 30 8 ? *", true, 15d),+ new Scene("eveningScene", "Evening scene", "EVENING", "0 30 17 ? *", false, 22d),+ new Scene("nightScene", "Night scene", "NIGHT", "0 0 22 ? *", true, 19d)+ };final protected boolean importDemoScenes;public String smartOfficeId;public String groundFloorId;@@ -83,6 +87,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {public String thingId;public String smartBuildingId;public String apartment1Id;+ public String apartment1SceneAgentId;public String apartment1ServiceAgentId;public String apartment1LivingroomId = UniqueIdentifierGenerator.generateId("apartment1LivingroomId");public String apartment1KitchenId;@@ -187,7 +192,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {Values.create(true)),new MetaItem(DATA_POINTS_MAX_AGE_DAYS,- Values.create(7)+ Values.create(DATA_POINTS_MAX_AGE_DAYS_DEFAULT*7)),new MetaItem(AGENT_LINK,@@ -262,112 +267,6 @@ public class ManagerDemoSetup extends AbstractManagerSetup {thing = assetStorageService.merge(thing);thingId = thing.getId();- // Some sample datapoints- final Asset finalThing = assetStorageService.find(thingId, true);- ZonedDateTime now = LocalDateTime.now().atZone(ZoneId.systemDefault());-- AssetAttribute light1PowerConsumptionAttribute = thing.getAttribute("light1PowerConsumption")- .orElseThrow(() -> new RuntimeException("Invalid test data"));-- persistenceService.doTransaction(em -> {- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(0.11), now.minusDays(80).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(1.22), now.minusDays(40).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(2.33), now.minusDays(20).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(3.44), now.minusDays(10).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(4.55), now.minusDays(8).toEpochSecond() * 1000);-- light1PowerConsumptionAttribute.setValue(Values.create(5.66), now.minusDays(6).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(6.77), now.minusDays(3).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(7.88), now.minusDays(1).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(8.99), now.minusHours(10).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(9.11), now.minusHours(5).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(10.22), now.minusHours(2).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(11.33), now.minusHours(1).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(11.44), now.minusMinutes(30).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(12.00), now.minusMinutes(5).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(12.11), now.minusSeconds(5).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);-- light1PowerConsumptionAttribute.setValue(Values.create(12.22), now.minusSeconds(1).toEpochSecond() * 1000);- assetDatapointService.processAssetUpdate(em,- finalThing,- light1PowerConsumptionAttribute,- AttributeEvent.Source.SENSOR);- });-// ################################ Demo assets for 'building' realm ###################################Asset smartBuilding = new Asset();@@ -566,21 +465,9 @@ public class ManagerDemoSetup extends AbstractManagerSetup {apartment1Id = apartment1.getId();if (importDemoScenes) {- Scene[] scenes = new Scene[]{- new Scene("morningScene", "Morning scene", "MORNING", "0 0 7 ? *", false, 21d),- new Scene("dayScene", "Day scene", "DAY", "0 30 8 ? *", true, 15d),- new Scene("eveningScene", "Evening scene", "EVENING", "0 30 17 ? *", false, 22d),- new Scene("nightScene", "Night scene", "NIGHT", "0 0 22 ? *", true, 19d)- };-- Asset demoApartmentSceneAgent = createDemoApartmentSceneAgent(- apartment1, scenes, apartment1Livingroom, apartment1Kitchen, apartment1Hallway- );-- demoApartmentSceneAgent = assetStorageService.merge(demoApartmentSceneAgent);-- linkDemoApartmentWithSceneAgent(apartment1, demoApartmentSceneAgent, scenes);- apartment1 = assetStorageService.merge(apartment1);+ Asset demoApartment1SceneAgent = createDemoApartmentScenes(+ assetStorageService, apartment1, DEMO_APARTMENT_SCENES, apartment1Livingroom, apartment1Kitchen, apartment1Hallway);+ apartment1SceneAgentId = demoApartment1SceneAgent.getId();}Asset apartment2 = new Asset("Apartment 2", RESIDENCE, smartBuilding);@@ -860,11 +747,6 @@ public class ManagerDemoSetup extends AbstractManagerSetup {peopleCounter3Asset = assetStorageService.merge(peopleCounter3Asset);peopleCounter3AssetId = peopleCounter3Asset.getId();- AssetRuleset peopleCounter3Rules = new AssetRuleset(- peopleCounter3Asset.getId(), "PeopleCounter 3 Rules", GROOVY, IOUtils.toString(getClass().getResource("/demo/rules/DemoSmartCityCamera.groovy"), StandardCharsets.UTF_8)- );- peopleCounter3Rules = rulesetStorageService.merge(peopleCounter3Rules);-Asset lightController_3Asset = createDemoLightControllerAsset("LightController 3", assetArea3, new GeoJSONPoint(5.487478, 51.446979));lightController_3Asset = assetStorageService.merge(lightController_3Asset);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 76e3ca1dc..1097312d5 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@@ -30,6 +30,8 @@ import java.io.InputStream;import java.nio.charset.StandardCharsets;import java.util.logging.Logger;+import static org.openremote.model.rules.Ruleset.Lang.GROOVY;+public class RulesDemoSetup extends AbstractManagerSetup {private static final Logger LOG = Logger.getLogger(RulesDemoSetup.class.getName());@@ -105,5 +107,14 @@ public class RulesDemoSetup extends AbstractManagerSetup {).setAccessPublicRead(true);tenantBuildingRulesetId = rulesetStorageService.merge(ruleset).getId();}++ // People counter+ try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoSmartCityCamera.groovy")) {+ String rules = IOUtils.toString(inputStream, StandardCharsets.UTF_8);+ Ruleset ruleset = new AssetRuleset(+ managerDemoSetup.peopleCounter3AssetId, "PeopleCounter 3 Rules", GROOVY, rules+ );+ rulesetStorageService.merge(ruleset);+ }}}diff --git a/manager/src/main/java/org/openremote/manager/system/SslHealthStatusProvider.java b/manager/src/main/java/org/openremote/manager/system/SslHealthStatusProvider.javaindex 8ef4aae2f..2f425e4fd 100644--- a/manager/src/main/java/org/openremote/manager/system/SslHealthStatusProvider.java+++ b/manager/src/main/java/org/openremote/manager/system/SslHealthStatusProvider.java@@ -36,7 +36,7 @@ import java.util.logging.Level;import java.util.logging.Logger;import static java.time.temporal.ChronoUnit.DAYS;-import static org.openremote.container.security.IdentityService.*;+import static org.openremote.container.security.keycloak.KeycloakIdentityProvider.*;import static org.openremote.container.util.MapAccess.getBoolean;import static org.openremote.container.util.MapAccess.getInteger;diff --git a/model/src/main/java/org/openremote/model/event/shared/CancelEventSubscription.java b/model/src/main/java/org/openremote/model/event/shared/CancelEventSubscription.javaindex 5f02e3265..803c4c7ac 100644--- a/model/src/main/java/org/openremote/model/event/shared/CancelEventSubscription.java+++ b/model/src/main/java/org/openremote/model/event/shared/CancelEventSubscription.java@@ -26,7 +26,7 @@ import org.openremote.model.event.Event;* type of event it doesn't want to receive anymore or the subscription ID to remove* a specific subscriber.*/-public class CancelEventSubscription<E extends SharedEvent> {+public class CancelEventSubscription {public static final String MESSAGE_PREFIX = "UNSUBSCRIBE:";@@ -37,17 +37,21 @@ public class CancelEventSubscription<E extends SharedEvent> {protected CancelEventSubscription() {}+ public <E extends SharedEvent> CancelEventSubscription(Class<E> eventClass) {+ this(Event.getEventType(eventClass), null);+ }++ public <E extends SharedEvent> CancelEventSubscription(Class<E> eventClass, String subscriptionId) {+ this(Event.getEventType(eventClass), subscriptionId);+ }+public CancelEventSubscription(String eventType, String subscriptionId) {this.eventType = eventType;this.subscriptionId = subscriptionId;}- public CancelEventSubscription(Class<E> eventClass) {- this(Event.getEventType(eventClass), null);- }-- public CancelEventSubscription(Class<E> eventClass, String subscriptionId) {- this(Event.getEventType(eventClass), subscriptionId);+ public CancelEventSubscription(String subscriptionId) {+ this.subscriptionId = subscriptionId;}public String getEventType() {diff --git a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.javaindex ae6045e41..1742b42ef 100644--- a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java+++ b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java@@ -52,7 +52,7 @@ public class EventSubscription<E extends SharedEvent> {* Optional only set when an internal subscription is made*/@JsonIgnore- protected Consumer<TriggeredEventSubscription<E>> internalConsumer;+ protected Consumer<E> internalConsumer;protected EventSubscription() {}@@ -74,7 +74,7 @@ public class EventSubscription<E extends SharedEvent> {this.filter = filter;}- public EventSubscription(Class<E> eventClass, EventFilter<E> filter, Consumer<TriggeredEventSubscription<E>> internalConsumer) {+ public EventSubscription(Class<E> eventClass, EventFilter<E> filter, Consumer<E> internalConsumer) {this.eventType = Event.getEventType(eventClass);this.filter = filter;this.internalConsumer = internalConsumer;@@ -86,7 +86,7 @@ public class EventSubscription<E extends SharedEvent> {this.subscriptionId = subscriptionId;}- public EventSubscription(Class<E> eventClass, EventFilter<E> filter, String subscriptionId, Consumer<TriggeredEventSubscription<E>> internalConsumer) {+ public EventSubscription(Class<E> eventClass, EventFilter<E> filter, String subscriptionId, Consumer<E> internalConsumer) {this.eventType = Event.getEventType(eventClass);this.filter = filter;this.subscriptionId = subscriptionId;@@ -113,11 +113,11 @@ public class EventSubscription<E extends SharedEvent> {return Event.getEventType(eventClass).equals(getEventType());}- public Consumer<TriggeredEventSubscription<E>> getInternalConsumer() {+ public Consumer<E> getInternalConsumer() {return internalConsumer;}- public void setInternalConsumer(Consumer<TriggeredEventSubscription<E>> internalConsumer) {+ public void setInternalConsumer(Consumer<E> internalConsumer) {this.internalConsumer = internalConsumer;}diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.tsindex 7c302dbb3..965a7b338 100644--- a/ui/component/core/src/event.ts+++ b/ui/component/core/src/event.ts@@ -644,7 +644,7 @@ export class WebSocketEventProvider extends EventProviderImpl {if (!this._webSocket) {return;}- const cancelSubscription: CancelEventSubscription<SharedEvent> = {+ const cancelSubscription: CancelEventSubscription = {subscriptionId: subscriptionId};this._webSocket.send(UNSUBSCRIBE_MESSAGE_PREFIX + JSON.stringify(cancelSubscription));