[insteon] set network to ONLINE after driver is initialized (#13030)

* [insteon] set network to ONLINE after driver is initialized

Signed-off-by: Rob Nielsen <rob.nielsen@yahoo.com>
This commit is contained in:
robnielsen 2022-07-26 12:49:05 -05:00 committed by GitHub
parent 7e26b1149e
commit 24e9228699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 37 deletions

View File

@ -117,6 +117,7 @@ public class InsteonBinding {
private PortListener portListener = new PortListener(); private PortListener portListener = new PortListener();
private int devicePollIntervalMilliseconds = 300000; private int devicePollIntervalMilliseconds = 300000;
private int deadDeviceTimeout = -1; private int deadDeviceTimeout = -1;
private boolean driverInitialized = false;
private int messagesReceived = 0; private int messagesReceived = 0;
private boolean isActive = false; // state of binding private boolean isActive = false; // state of binding
private int x10HouseUnit = -1; private int x10HouseUnit = -1;
@ -167,6 +168,10 @@ public class InsteonBinding {
return driver; return driver;
} }
public boolean isDriverInitialized() {
return driverInitialized;
}
public boolean startPolling() { public boolean startPolling() {
logger.debug("starting to poll {}", driver.getPortName()); logger.debug("starting to poll {}", driver.getPortName());
driver.start(); driver.start();
@ -519,6 +524,8 @@ public class InsteonBinding {
if (!missing.isEmpty()) { if (!missing.isEmpty()) {
handler.addMissingDevices(missing); handler.addMissingDevices(missing);
} }
driverInitialized = true;
} }
@Override @Override

View File

@ -46,6 +46,7 @@ import org.slf4j.LoggerFactory;
*/ */
@NonNullByDefault @NonNullByDefault
public class InsteonNetworkHandler extends BaseBridgeHandler { public class InsteonNetworkHandler extends BaseBridgeHandler {
private static final int DRIVER_INITIALIZED_TIME_IN_SECONDS = 1;
private static final int LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS = 600; private static final int LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS = 600;
private static final int RETRY_DELAY_IN_SECONDS = 30; private static final int RETRY_DELAY_IN_SECONDS = 30;
private static final int SETTLE_TIME_IN_SECONDS = 5; private static final int SETTLE_TIME_IN_SECONDS = 5;
@ -54,6 +55,7 @@ public class InsteonNetworkHandler extends BaseBridgeHandler {
private @Nullable InsteonBinding insteonBinding; private @Nullable InsteonBinding insteonBinding;
private @Nullable InsteonDeviceDiscoveryService insteonDeviceDiscoveryService; private @Nullable InsteonDeviceDiscoveryService insteonDeviceDiscoveryService;
private @Nullable ScheduledFuture<?> driverInitializedJob = null;
private @Nullable ScheduledFuture<?> pollingJob = null; private @Nullable ScheduledFuture<?> pollingJob = null;
private @Nullable ScheduledFuture<?> reconnectJob = null; private @Nullable ScheduledFuture<?> reconnectJob = null;
private @Nullable ScheduledFuture<?> settleJob = null; private @Nullable ScheduledFuture<?> settleJob = null;
@ -78,57 +80,76 @@ public class InsteonNetworkHandler extends BaseBridgeHandler {
logger.debug("Starting Insteon bridge"); logger.debug("Starting Insteon bridge");
InsteonNetworkConfiguration config = getConfigAs(InsteonNetworkConfiguration.class); InsteonNetworkConfiguration config = getConfigAs(InsteonNetworkConfiguration.class);
scheduler.execute(() -> { SerialPortManager serialPortManager = this.serialPortManager;
SerialPortManager serialPortManager = this.serialPortManager; if (serialPortManager == null) {
if (serialPortManager == null) { String msg = "Initialization failed, serial port manager is null.";
String msg = "Initialization failed, serial port manager is null."; logger.warn(msg);
logger.warn(msg);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
return; return;
} }
insteonBinding = new InsteonBinding(this, config, serialPortManager, scheduler); insteonBinding = new InsteonBinding(this, config, serialPortManager, scheduler);
updateStatus(ThingStatus.UNKNOWN); updateStatus(ThingStatus.UNKNOWN);
// hold off on starting to poll until devices that already are defined as things are added. // hold off on starting to poll until devices that already are defined as things are added.
// wait SETTLE_TIME_IN_SECONDS to start then check every second afterwards until it has been at // wait SETTLE_TIME_IN_SECONDS to start then check every second afterwards until it has been at
// least SETTLE_TIME_IN_SECONDS since last device was created. // least SETTLE_TIME_IN_SECONDS since last device was created.
settleJob = scheduler.scheduleWithFixedDelay(() -> { settleJob = scheduler.scheduleWithFixedDelay(() -> {
// check to see if it has been at least SETTLE_TIME_IN_SECONDS since last device was created // check to see if it has been at least SETTLE_TIME_IN_SECONDS since last device was created
if (System.currentTimeMillis() - lastInsteonDeviceCreatedTimestamp > SETTLE_TIME_IN_SECONDS * 1000) { if (System.currentTimeMillis() - lastInsteonDeviceCreatedTimestamp > SETTLE_TIME_IN_SECONDS * 1000) {
// settle time has expired start polling // settle time has expired start polling
InsteonBinding insteonBinding = this.insteonBinding; InsteonBinding insteonBinding = this.insteonBinding;
if (insteonBinding != null && insteonBinding.startPolling()) { if (insteonBinding != null && insteonBinding.startPolling()) {
pollingJob = scheduler.scheduleWithFixedDelay(() -> { pollingJob = scheduler.scheduleWithFixedDelay(() -> {
insteonBinding.logDeviceStatistics(); insteonBinding.logDeviceStatistics();
}, 0, LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS, TimeUnit.SECONDS); }, 0, LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS, TimeUnit.SECONDS);
insteonBinding.setIsActive(true); // wait until driver is initialized before setting network to ONLINE
driverInitializedJob = scheduler.scheduleWithFixedDelay(() -> {
if (insteonBinding.isDriverInitialized()) {
logger.debug("driver is initialized");
updateStatus(ThingStatus.ONLINE); insteonBinding.setIsActive(true);
} else {
String msg = "Initialization failed, unable to start the Insteon bridge with the port '"
+ config.getPort() + "'.";
logger.warn(msg);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg); updateStatus(ThingStatus.ONLINE);
}
ScheduledFuture<?> settleJob = this.settleJob; ScheduledFuture<?> driverInitializedJob = this.driverInitializedJob;
if (settleJob != null) { if (driverInitializedJob != null) {
settleJob.cancel(false); driverInitializedJob.cancel(false);
} this.driverInitializedJob = null;
}
} else {
logger.debug("driver is not initialized yet");
}
}, 0, DRIVER_INITIALIZED_TIME_IN_SECONDS, TimeUnit.SECONDS);
} else {
String msg = "Initialization failed, unable to start the Insteon bridge with the port '"
+ config.getPort() + "'.";
logger.warn(msg);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
}
ScheduledFuture<?> settleJob = this.settleJob;
if (settleJob != null) {
settleJob.cancel(false);
this.settleJob = null; this.settleJob = null;
} }
}, SETTLE_TIME_IN_SECONDS, 1, TimeUnit.SECONDS); }
}); }, SETTLE_TIME_IN_SECONDS, 1, TimeUnit.SECONDS);
} }
@Override @Override
public void dispose() { public void dispose() {
logger.debug("Shutting down Insteon bridge"); logger.debug("Shutting down Insteon bridge");
ScheduledFuture<?> driverInitializedJob = this.driverInitializedJob;
if (driverInitializedJob != null) {
driverInitializedJob.cancel(true);
this.driverInitializedJob = null;
}
ScheduledFuture<?> pollingJob = this.pollingJob; ScheduledFuture<?> pollingJob = this.pollingJob;
if (pollingJob != null) { if (pollingJob != null) {
pollingJob.cancel(true); pollingJob.cancel(true);
@ -172,8 +193,8 @@ public class InsteonNetworkHandler extends BaseBridgeHandler {
ScheduledFuture<?> reconnectJob = this.reconnectJob; ScheduledFuture<?> reconnectJob = this.reconnectJob;
if (reconnectJob != null) { if (reconnectJob != null) {
reconnectJob.cancel(false); reconnectJob.cancel(false);
this.reconnectJob = null;
} }
this.reconnectJob = null;
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Port disconnected."); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Port disconnected.");
} }