Skip to content

Commit

Permalink
[velux] Bridge status shows offline when communication fails (#13406)
Browse files Browse the repository at this point in the history
* [velux] dynamically update bridge on-/off- line state

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
  • Loading branch information
andrewfg authored Sep 25, 2022
1 parent b7cbf2b commit 79309cb
Showing 1 changed file with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.velux.internal.handler;

import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -160,6 +162,8 @@ public class VeluxBridgeHandler extends ExtendedBaseBridgeHandler implements Vel
*/
private VeluxBridgeConfiguration veluxBridgeConfiguration = new VeluxBridgeConfiguration();

private Duration offlineDelay = Duration.ofMinutes(5);

/*
* ************************
* ***** Constructors *****
Expand Down Expand Up @@ -283,6 +287,17 @@ public void initialize() {
logger.trace("initialize(): initialize bridge configuration parameters.");
veluxBridgeConfiguration = new VeluxBinding(getConfigAs(VeluxBridgeConfiguration.class)).checked();

/*
* When a binding call to the hub fails with a communication error, it will retry the call for a maximum of
* veluxBridgeConfiguration.retries times, where the interval between retry attempts increases on each attempt
* calculated as veluxBridgeConfiguration.refreshMSecs * 2^retry (i.e. 1, 2, 4, 8, 16, 32 etc.) so a complete
* retry series takes (veluxBridgeConfiguration.refreshMSecs * ((2^(veluxBridgeConfiguration.retries + 1)) - 1)
* milliseconds. So we have to let this full retry series to have been tried (and failed), before we consider
* the thing to be actually offline.
*/
offlineDelay = Duration.ofMillis(
((long) Math.pow(2, veluxBridgeConfiguration.retries + 1) - 1) * veluxBridgeConfiguration.refreshMSecs);

scheduler.execute(() -> {
disposing = false;
initializeSchedulerJob();
Expand Down Expand Up @@ -812,10 +827,28 @@ private synchronized void handleCommandCommsJob(ChannelUID channelUID, Command c
postCommand(channelUID, newValue);
}
}

Instant lastCommunication = Instant.ofEpochMilli(thisBridge.lastCommunication());
Instant lastSuccessfulCommunication = Instant.ofEpochMilli(thisBridge.lastSuccessfulCommunication());
boolean lastCommunicationSucceeded = lastSuccessfulCommunication.equals(lastCommunication);
ThingStatus thingStatus = getThing().getStatus();

if (lastCommunicationSucceeded) {
if (thingStatus == ThingStatus.OFFLINE || thingStatus == ThingStatus.UNKNOWN) {
updateStatus(ThingStatus.ONLINE);
}
} else {
if ((thingStatus == ThingStatus.ONLINE || thingStatus == ThingStatus.UNKNOWN)
&& lastSuccessfulCommunication.plus(offlineDelay).isBefore(lastCommunication)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
}
}

ThingProperty.setValue(this, VeluxBindingConstants.PROPERTY_BRIDGE_TIMESTAMP_ATTEMPT,
new java.util.Date(thisBridge.lastCommunication()).toString());
lastCommunication.toString());
ThingProperty.setValue(this, VeluxBindingConstants.PROPERTY_BRIDGE_TIMESTAMP_SUCCESS,
new java.util.Date(thisBridge.lastSuccessfulCommunication()).toString());
lastSuccessfulCommunication.toString());

logger.trace("handleCommandCommsJob({}) done.", Thread.currentThread());
}

Expand Down

0 comments on commit 79309cb

Please sign in to comment.