Skip to content

armgit / core

Public Imported from GitHub
0

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

Gateway improvements/fixes

Rich Turner813d5d151a26
diff --git a/container/src/main/java/org/openremote/container/util/Util.java b/container/src/main/java/org/openremote/container/util/Util.java
index 94435628a..2f6e7559e 100644
--- a/container/src/main/java/org/openremote/container/util/Util.java
+++ b/container/src/main/java/org/openremote/container/util/Util.java
@@ -140,7 +140,7 @@ public class Util {
}
long adjustedMax = Math.max(maxDelay - jitterMargin, jitterMargin);
- long delay = Math.min(initialDelay * (long)Math.pow(2, retries), adjustedMax);
+ long delay = Math.max(initialDelay, Math.min(initialDelay * (long)Math.pow(2, retries), adjustedMax));
delay += Math.random()*jitterMargin;
retries++;
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
index 1aad8c09f..c17e041be 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
@@ -28,6 +28,7 @@ import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.query.AssetQuery;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.value.Values;
import java.util.*;
@@ -40,13 +41,14 @@ import java.util.logging.Logger;
import java.util.stream.Collectors;
import static org.openremote.model.query.AssetQuery.Select.selectExcludeAll;
+import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
/**
* Handles all communication between a gateway representation in the local manager and the actual gateway
*/
public class GatewayConnector {
- private static final Logger LOG = Logger.getLogger(GatewayConnector.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(GATEWAY, GatewayConnector.class.getName());
public static long SYNC_TIMEOUT_MILLIS = 10000; // How long to wait for a response before resending request
public static long ASSET_CRUD_TIMEOUT_MILLIS = 10000; // How long to wait for a response when merging an asset before throwing an exception
public static int MAX_SYNC_RETRIES = 5;
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
index 1bd84e0d5..dee258a23 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
@@ -44,6 +44,7 @@ import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.gateway.GatewayDisconnectEvent;
import org.openremote.model.query.AssetQuery;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Values;
@@ -63,12 +64,13 @@ import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_
import static org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType;
import static org.openremote.manager.event.ClientEventService.getClientId;
import static org.openremote.manager.event.ClientEventService.getSessionKey;
+import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
public class GatewayService extends RouteBuilder implements ContainerService, AssetUpdateProcessor {
public static final int GATEWAY_SERVICE_PRIORITY = DEFAULT_PRIORITY - 10;
public static final String GATEWAY_CLIENT_ID_PREFIX = "gateway-";
- private static final Logger LOG = Logger.getLogger(GatewayService.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(GATEWAY, GatewayService.class.getName());
protected AssetStorageService assetStorageService;
protected AssetProcessingService assetProcessingService;
protected ManagerIdentityService identityService;
@@ -239,7 +241,11 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
// This is a change to a locally registered gateway
if (attribute.getNameOrThrow().equals("disabled")) {
boolean disabled = attribute.getValueAsBoolean().orElse(false);
- connector.setDisabled(disabled);
+ boolean isAlreadyDisabled = asset.getAttribute("disabled").flatMap(AssetAttribute::getValueAsBoolean).orElse(false);
+ if (disabled != isAlreadyDisabled) {
+ connector.setDisabled(disabled);
+ updateGatewayClient(connector, !disabled);
+ }
}
} else {
String gatewayId = assetIdGatewayIdMap.get(asset.getId());
@@ -254,6 +260,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
LOG.info("Gateway is not connected so attribute event for descendant asset will be dropped (Asset ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);
throw new AssetProcessingException(AssetProcessingException.Reason.GATEWAY_DISCONNECTED, "Gateway is not connected: Gateway ID=" + connector.gatewayId);
}
+ LOG.fine("Attribute event for a gateway descendant asset being forwarded to the gateway (Asset ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);
connector.sendMessageToGateway(
new AttributeEvent(
asset.getId(),
@@ -385,6 +392,8 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
if (connector.isDisabled()) {
LOG.warning("Gateway is currently disabled so will be ignored: Gateway ID=" + gatewayId);
+ // Should not have got passed keycloak ensure keycloak client is disabled
+ updateGatewayClient(connector, false);
clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
disconnectRunnable.run();
return;
@@ -430,6 +439,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
// Check if disabled
boolean isNowDisabled = gateway.getAttribute("disabled").flatMap(AssetAttribute::getValueAsBoolean).orElse(false);
connector.setDisabled(isNowDisabled);
+ updateGatewayClient(connector, !isNowDisabled);
break;
case DELETE:
// Check if this gateway has a connector
@@ -513,6 +523,15 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
}
}
+ protected void updateGatewayClient(GatewayConnector connector, boolean enabled) {
+ final ClientsResource clientsResource = identityProvider.getRealms(getClientRequestInfo()).realm(connector.getRealm()).clients();
+ clientsResource.findByClientId(GATEWAY_CLIENT_ID_PREFIX + connector.gatewayId).stream().findFirst().ifPresent(
+ clientRepresentation -> {
+ clientRepresentation.setEnabled(enabled);
+ clientsResource.get(clientRepresentation.getId()).update(clientRepresentation);
+ });
+ }
+
protected void destroyGatewayClient(Asset gateway) {
String id = gateway.getAttribute("clientId").flatMap(AssetAttribute::getValueAsString).orElse(null);
if (TextUtil.isNullOrEmpty(id)) {
diff --git a/model/src/main/java/org/openremote/model/syslog/SyslogCategory.java b/model/src/main/java/org/openremote/model/syslog/SyslogCategory.java
index 19e326b2d..4d1d87cfe 100644
--- a/model/src/main/java/org/openremote/model/syslog/SyslogCategory.java
+++ b/model/src/main/java/org/openremote/model/syslog/SyslogCategory.java
@@ -56,6 +56,12 @@ public enum SyslogCategory {
"Protocol",
true,
"Protocol"
+ ),
+ GATEWAY(
+ "Gateway",
+ true,
+ "GatewayService",
+ "GatewayConnector"
);
protected final String categoryLabel;