From 3bd17f47881d4602698c950efcb1a397b7a72d7e Mon Sep 17 00:00:00 2001 From: lolodomo Date: Wed, 9 Dec 2020 23:52:41 +0100 Subject: [PATCH] [remoteopenhab] New settings to setup the remote server accessibility check (#9311) Signed-off-by: Laurent Garnier --- .../README.md | 18 ++++++++------- .../RemoteopenhabServerConfiguration.java | 2 ++ .../handler/RemoteopenhabBridgeHandler.java | 22 +++++++++++++------ .../resources/OH-INF/thing/thing-types.xml | 15 +++++++++++++ 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.remoteopenhab/README.md b/bundles/org.openhab.binding.remoteopenhab/README.md index 977f2daf02c6e..07bef25df914c 100644 --- a/bundles/org.openhab.binding.remoteopenhab/README.md +++ b/bundles/org.openhab.binding.remoteopenhab/README.md @@ -36,14 +36,16 @@ The binding has no configuration options, all configuration is done at Thing lev The `server` thing has the following configuration parameters: -| Parameter | Required | Description | -|--------------------|----------|-----------------------------------------------------------------------------------------------------------| -| host | yes | The host name or IP address of the remote openHAB server. | -| useHttps | no | Set to true if you want to use HTTPS to communicate with the remote openHAB server. Default is false. | -| port | yes | The HTTP port to use to communicate with the remote openHAB server. Default is 8080. | -| trustedCertificate | no | Set to true if you want to use HTTPS even without a valid SSL certificate provided by your remote server. | -| restPath | yes | The subpath of the REST API on the remote openHAB server. Default is /rest | -| token | no | The token to use when the remote openHAB server is setup to require authorization to run its REST API. | +| Parameter | Required | Description | +|-----------------------|----------|-----------------------------------------------------------------------------------------------------------| +| host | yes | The host name or IP address of the remote openHAB server. | +| useHttps | no | Set to true if you want to use HTTPS to communicate with the remote openHAB server. Default is false. | +| port | yes | The HTTP port to use to communicate with the remote openHAB server. Default is 8080. | +| trustedCertificate | no | Set to true if you want to use HTTPS even without a valid SSL certificate provided by your remote server. | +| restPath | yes | The subpath of the REST API on the remote openHAB server. Default is /rest | +| token | no | The token to use when the remote openHAB server is setup to require authorization to run its REST API. | +| accessibilityInterval | no | Minutes between checking the remote server accessibility. 0 to disable the check. Default is 3. | +| aliveInterval | no | Number of last minutes to take into account to determine whether the remote server is alive. 0 to disable this feature. Default is 5. | The `thing` thing has the following configuration parameters: diff --git a/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/config/RemoteopenhabServerConfiguration.java b/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/config/RemoteopenhabServerConfiguration.java index cbb0c8242ee37..edfefdd7ad96a 100644 --- a/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/config/RemoteopenhabServerConfiguration.java +++ b/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/config/RemoteopenhabServerConfiguration.java @@ -33,4 +33,6 @@ public class RemoteopenhabServerConfiguration { public boolean trustedCertificate = false; public String restPath = "/rest"; public String token = ""; + public int accessibilityInterval = 3; + public int aliveInterval = 5; } diff --git a/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/handler/RemoteopenhabBridgeHandler.java b/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/handler/RemoteopenhabBridgeHandler.java index bc3a199dfbfa9..06040dfaf0bf3 100644 --- a/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/handler/RemoteopenhabBridgeHandler.java +++ b/bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/handler/RemoteopenhabBridgeHandler.java @@ -98,7 +98,6 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; private static final DateTimeFormatter FORMATTER_DATE = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN); - private static final long CONNECTION_TIMEOUT_MILLIS = TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES); private static final int MAX_STATE_SIZE_FOR_LOGGING = 50; private final Logger logger = LoggerFactory.getLogger(RemoteopenhabBridgeHandler.class); @@ -177,7 +176,10 @@ public void initialize() { updateStatus(ThingStatus.UNKNOWN); - startCheckConnectionJob(); + scheduler.submit(this::checkConnection); + if (config.accessibilityInterval > 0) { + startCheckConnectionJob(config.accessibilityInterval, config.aliveInterval); + } } @Override @@ -354,19 +356,25 @@ public void checkConnection() { } } - private void startCheckConnectionJob() { + private void startCheckConnectionJob(int accessibilityInterval, int aliveInterval) { ScheduledFuture localCheckConnectionJob = checkConnectionJob; if (localCheckConnectionJob == null || localCheckConnectionJob.isCancelled()) { checkConnectionJob = scheduler.scheduleWithFixedDelay(() -> { long millisSinceLastEvent = System.currentTimeMillis() - restClient.getLastEventTimestamp(); - if (millisSinceLastEvent > CONNECTION_TIMEOUT_MILLIS) { - logger.debug("Check: Maybe disconnected from streaming events, millisSinceLastEvent={}", + if (aliveInterval == 0 || restClient.getLastEventTimestamp() == 0) { + logger.debug("Time to check server accessibility"); + checkConnection(); + } else if (millisSinceLastEvent > (aliveInterval * 60000)) { + logger.debug( + "Time to check server accessibility (maybe disconnected from streaming events, millisSinceLastEvent={})", millisSinceLastEvent); checkConnection(); } else { - logger.debug("Check: Receiving streaming events, millisSinceLastEvent={}", millisSinceLastEvent); + logger.debug( + "Bypass server accessibility check (receiving streaming events, millisSinceLastEvent={})", + millisSinceLastEvent); } - }, 0, CONNECTION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); + }, accessibilityInterval, accessibilityInterval, TimeUnit.MINUTES); } } diff --git a/bundles/org.openhab.binding.remoteopenhab/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.remoteopenhab/src/main/resources/OH-INF/thing/thing-types.xml index 762bba8de9ca6..f992004b880f3 100644 --- a/bundles/org.openhab.binding.remoteopenhab/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.remoteopenhab/src/main/resources/OH-INF/thing/thing-types.xml @@ -53,6 +53,21 @@ The token to use when the remote openHAB server is setup to require authorization to run its REST API. true + + + + Minutes between checking the remote server accessibility. 0 to disable the check. Default is 3. + 3 + true + + + + + Number of last minutes to take into account to determine whether the remote server is alive. 0 to + disable this feature. Default is 5. + 5 + true +