Skip to content

Commit

Permalink
[remoteopenhab] New settings to setup the remote server accessibility…
Browse files Browse the repository at this point in the history
… check (#9311)

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored Dec 9, 2020
1 parent 27a8455 commit 3bd17f4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
18 changes: 10 additions & 8 deletions bundles/org.openhab.binding.remoteopenhab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@
<description>The token to use when the remote openHAB server is setup to require authorization to run its REST API.</description>
<advanced>true</advanced>
</parameter>

<parameter name="accessibilityInterval" type="integer" min="0" step="1" unit="min">
<label>Accessibility Interval</label>
<description>Minutes between checking the remote server accessibility. 0 to disable the check. Default is 3.</description>
<default>3</default>
<advanced>true</advanced>
</parameter>

<parameter name="aliveInterval" type="integer" min="0" step="1" unit="min">
<label>Alive Interval</label>
<description>Number of last minutes to take into account to determine whether the remote server is alive. 0 to
disable this feature. Default is 5.</description>
<default>5</default>
<advanced>true</advanced>
</parameter>
</config-description>
</bridge-type>

Expand Down

0 comments on commit 3bd17f4

Please sign in to comment.