[asuswrt] Fix reconnect when offline (#18813)

Signed-off-by: Jeff James <jeff@james-online.com>
This commit is contained in:
jsjames
2025-06-20 05:49:22 +02:00
committed by GitHub
parent 41dd287597
commit 71c1e47d4c
3 changed files with 25 additions and 23 deletions
@@ -37,9 +37,10 @@ public class AsuswrtBindingSettings {
public static final Boolean HTTP_SSL_TRUST_ALL = true; // trust all ssl-certs
public static final Integer COOKIE_LIFETIME_S = 3600; // lifetime of login-cookie
public static final Integer POLLING_INTERVAL_S_MIN = 5; // minimum polling interval
public static final Integer POLLING_INTERVAL_S_DEFAULT = 20; // default polling interval
public static final Integer RECONNECT_INTERVAL_S = 30; // interval trying try to reconnect to router
public static final Integer POLLING_INTERVAL_MIN_S = 5; // minimum polling interval
public static final Integer POLLING_INTERVAL_DEFAULT_S = 20; // default polling interval
public static final Integer RECONNECT_BACKOFF_START_S = 30;
public static final Integer RECONNECT_BACKOFF_MAX_S = 3000;
public static final Integer DISCOVERY_TIMEOUT_S = 10; // discovery service timeout in s
public static final Integer DISCOVERY_AUTOREMOVE_S = 1800; // discovery service remove things after x seconds
@@ -36,7 +36,6 @@ public class AsuswrtConfiguration {
public String username = "";
public String password = "";
public int pollingInterval = 20;
public int reconnectInterval = 60;
public int discoveryInterval = 3600;
public int httpPort = 80;
public int httpsPort = 443;
@@ -75,6 +75,8 @@ public class AsuswrtRouter extends BaseBridgeHandler {
private final HttpClient httpClient;
private final String uid;
private int backoffDuration = RECONNECT_BACKOFF_START_S;
public AsuswrtErrorHandler errorHandler;
public AsuswrtRouter(Bridge bridge, HttpClient httpClient) {
@@ -128,15 +130,13 @@ public class AsuswrtRouter extends BaseBridgeHandler {
}
public void startPollingJob() {
int pollingInterval = AsuswrtUtils.getValueOrDefault(config.pollingInterval, POLLING_INTERVAL_S_DEFAULT);
TimeUnit timeUnit = TimeUnit.SECONDS;
int pollingInterval = AsuswrtUtils.getValueOrDefault(config.pollingInterval, POLLING_INTERVAL_DEFAULT_S);
if (pollingInterval > 0) {
if (pollingInterval < POLLING_INTERVAL_S_MIN) {
pollingInterval = POLLING_INTERVAL_S_MIN;
}
logger.trace("({}) start polling scheduler with interval {} {}", getUID(), pollingInterval, timeUnit);
pollingInterval = Math.max(pollingInterval, POLLING_INTERVAL_MIN_S);
logger.trace("({}) start polling scheduler with interval {} {}", getUID(), pollingInterval,
TimeUnit.SECONDS);
pollingJob = scheduler.scheduleWithFixedDelay(this::pollingJobAction, pollingInterval, pollingInterval,
timeUnit);
TimeUnit.SECONDS);
} else {
stopScheduler(pollingJob);
}
@@ -149,26 +149,26 @@ public class AsuswrtRouter extends BaseBridgeHandler {
}
protected void startReconnectScheduler() {
int pollingInterval = config.reconnectInterval;
TimeUnit timeUnit = TimeUnit.SECONDS;
if (pollingInterval < RECONNECT_INTERVAL_S) {
pollingInterval = RECONNECT_INTERVAL_S;
}
logger.trace("({}) start reconnect scheduler in {} {}", getUID(), pollingInterval, timeUnit);
reconnectJob = scheduler.schedule(this::reconnectJobAction, pollingInterval, timeUnit);
backoffDuration = RECONNECT_BACKOFF_START_S;
logger.debug("({}) will attempt reconnect in {} {}", getUID(), backoffDuration, TimeUnit.SECONDS);
reconnectJob = scheduler.schedule(this::reconnectJobAction, backoffDuration, TimeUnit.SECONDS);
}
protected void reconnectJobAction() {
connect();
if (!connect()) {
backoffDuration = Math.min(backoffDuration * 2, RECONNECT_BACKOFF_MAX_S);
logger.debug("({}) will attempt reconnect in {} {}", getUID(), backoffDuration, TimeUnit.SECONDS);
reconnectJob = scheduler.schedule(this::reconnectJobAction, backoffDuration, TimeUnit.SECONDS);
}
}
protected void startDiscoveryScheduler() {
int pollingInterval = config.discoveryInterval;
TimeUnit timeUnit = TimeUnit.SECONDS;
if (config.autoDiscoveryEnabled && pollingInterval > 0) {
logger.trace("{} starting bridge discovery sheduler with interval {} {}", getUID(), pollingInterval,
timeUnit);
discoveryJob = scheduler.scheduleWithFixedDelay(discoveryService::startScan, 0, pollingInterval, timeUnit);
TimeUnit.SECONDS);
discoveryJob = scheduler.scheduleWithFixedDelay(discoveryService::startScan, 0, pollingInterval,
TimeUnit.SECONDS);
} else {
stopScheduler(discoveryJob);
}
@@ -194,7 +194,7 @@ public class AsuswrtRouter extends BaseBridgeHandler {
* Connects to the router and sets the states.
*/
@SuppressWarnings("null")
protected void connect() {
protected boolean connect() {
connector.login();
if (connector.cookieStore.cookieIsSet()) {
stopScheduler(reconnectJob);
@@ -202,8 +202,10 @@ public class AsuswrtRouter extends BaseBridgeHandler {
devicePropertiesChanged(deviceInfo);
setState(ThingStatus.ONLINE);
startPollingJob();
return true;
} else {
setState(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, errorHandler.getErrorMessage());
return false;
}
}