]> git.basschouten.com Git - openhab-addons.git/commitdiff
[insteon] set network to ONLINE after driver is initialized (#13030)
authorrobnielsen <rob.nielsen@yahoo.com>
Tue, 26 Jul 2022 17:49:05 +0000 (12:49 -0500)
committerGitHub <noreply@github.com>
Tue, 26 Jul 2022 17:49:05 +0000 (19:49 +0200)
* [insteon] set network to ONLINE after driver is initialized

Signed-off-by: Rob Nielsen <rob.nielsen@yahoo.com>
bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java
bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java

index fa21489efeb476aeb9225d3110ed8cdaa3a81777..fc655359912edba8aa75b38b6c510eb77da5c636 100644 (file)
@@ -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
index 56ef95c93e456ee6a4bba2a44d63c466123c30f2..0a5ed92f7aa2092b8924b40d1c5bab073ad8f889 100644 (file)
@@ -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);
-
-            // 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);
-
-                        updateStatus(ThingStatus.ONLINE);
-                    } 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);
-                    }
+            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);
+
+                    // wait until driver is initialized before setting network to ONLINE
+                    driverInitializedJob = scheduler.scheduleWithFixedDelay(() -> {
+                        if (insteonBinding.isDriverInitialized()) {
+                            logger.debug("driver is initialized");
+
+                            insteonBinding.setIsActive(true);
+
+                            updateStatus(ThingStatus.ONLINE);
+
+                            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.");
             }