From 24e92286995f63a0d4c8ca22b3b37cc42ebf3325 Mon Sep 17 00:00:00 2001 From: robnielsen Date: Tue, 26 Jul 2022 12:49:05 -0500 Subject: [PATCH] [insteon] set network to ONLINE after driver is initialized (#13030) * [insteon] set network to ONLINE after driver is initialized Signed-off-by: Rob Nielsen --- .../insteon/internal/InsteonBinding.java | 7 ++ .../handler/InsteonNetworkHandler.java | 95 +++++++++++-------- 2 files changed, 65 insertions(+), 37 deletions(-) diff --git a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java index fa21489efeb..fc655359912 100644 --- a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java +++ b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java @@ -117,6 +117,7 @@ public class InsteonBinding { private PortListener portListener = new PortListener(); private int devicePollIntervalMilliseconds = 300000; private int deadDeviceTimeout = -1; + private boolean driverInitialized = false; private int messagesReceived = 0; private boolean isActive = false; // state of binding private int x10HouseUnit = -1; @@ -167,6 +168,10 @@ public class InsteonBinding { return driver; } + public boolean isDriverInitialized() { + return driverInitialized; + } + public boolean startPolling() { logger.debug("starting to poll {}", driver.getPortName()); driver.start(); @@ -519,6 +524,8 @@ public class InsteonBinding { if (!missing.isEmpty()) { handler.addMissingDevices(missing); } + + driverInitialized = true; } @Override diff --git a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java index 56ef95c93e4..0a5ed92f7aa 100644 --- a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java +++ b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java @@ -46,6 +46,7 @@ import org.slf4j.LoggerFactory; */ @NonNullByDefault 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 RETRY_DELAY_IN_SECONDS = 30; private static final int SETTLE_TIME_IN_SECONDS = 5; @@ -54,6 +55,7 @@ public class InsteonNetworkHandler extends BaseBridgeHandler { private @Nullable InsteonBinding insteonBinding; private @Nullable InsteonDeviceDiscoveryService insteonDeviceDiscoveryService; + private @Nullable ScheduledFuture driverInitializedJob = null; private @Nullable ScheduledFuture pollingJob = null; private @Nullable ScheduledFuture reconnectJob = null; private @Nullable ScheduledFuture settleJob = null; @@ -78,57 +80,76 @@ public class InsteonNetworkHandler extends BaseBridgeHandler { logger.debug("Starting Insteon bridge"); InsteonNetworkConfiguration config = getConfigAs(InsteonNetworkConfiguration.class); - scheduler.execute(() -> { - SerialPortManager serialPortManager = this.serialPortManager; - if (serialPortManager == null) { - String msg = "Initialization failed, serial port manager is null."; - logger.warn(msg); + SerialPortManager serialPortManager = this.serialPortManager; + if (serialPortManager == null) { + String msg = "Initialization failed, serial port manager is null."; + logger.warn(msg); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg); - return; - } - insteonBinding = new InsteonBinding(this, config, serialPortManager, scheduler); - updateStatus(ThingStatus.UNKNOWN); + return; + } + insteonBinding = new InsteonBinding(this, config, serialPortManager, scheduler); + updateStatus(ThingStatus.UNKNOWN); - // 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 - // least SETTLE_TIME_IN_SECONDS since last device was created. - settleJob = scheduler.scheduleWithFixedDelay(() -> { - // 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) { - // settle time has expired start polling - InsteonBinding insteonBinding = this.insteonBinding; - if (insteonBinding != null && insteonBinding.startPolling()) { - pollingJob = scheduler.scheduleWithFixedDelay(() -> { - insteonBinding.logDeviceStatistics(); - }, 0, LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS, TimeUnit.SECONDS); + // 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 + // least SETTLE_TIME_IN_SECONDS since last device was created. + settleJob = scheduler.scheduleWithFixedDelay(() -> { + // 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) { + // settle time has expired start polling + InsteonBinding insteonBinding = this.insteonBinding; + if (insteonBinding != null && insteonBinding.startPolling()) { + pollingJob = scheduler.scheduleWithFixedDelay(() -> { + insteonBinding.logDeviceStatistics(); + }, 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); - } else { - String msg = "Initialization failed, unable to start the Insteon bridge with the port '" - + config.getPort() + "'."; - logger.warn(msg); + insteonBinding.setIsActive(true); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg); - } + updateStatus(ThingStatus.ONLINE); - ScheduledFuture settleJob = this.settleJob; - if (settleJob != null) { - settleJob.cancel(false); - } + ScheduledFuture driverInitializedJob = this.driverInitializedJob; + if (driverInitializedJob != null) { + 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; } - }, SETTLE_TIME_IN_SECONDS, 1, TimeUnit.SECONDS); - }); + } + }, SETTLE_TIME_IN_SECONDS, 1, TimeUnit.SECONDS); } @Override public void dispose() { logger.debug("Shutting down Insteon bridge"); + ScheduledFuture driverInitializedJob = this.driverInitializedJob; + if (driverInitializedJob != null) { + driverInitializedJob.cancel(true); + this.driverInitializedJob = null; + } + ScheduledFuture pollingJob = this.pollingJob; if (pollingJob != null) { pollingJob.cancel(true); @@ -172,8 +193,8 @@ public class InsteonNetworkHandler extends BaseBridgeHandler { ScheduledFuture reconnectJob = this.reconnectJob; if (reconnectJob != null) { reconnectJob.cancel(false); + this.reconnectJob = null; } - this.reconnectJob = null; } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Port disconnected."); }