]> git.basschouten.com Git - openhab-addons.git/commitdiff
[miio] Avoid excessive MessageSenderThread (#11455)
authorMarcel <marcel@verpaalen.com>
Wed, 27 Oct 2021 19:18:54 +0000 (21:18 +0200)
committerGitHub <noreply@github.com>
Wed, 27 Oct 2021 19:18:54 +0000 (21:18 +0200)
* [miio] Avoid excessive MessageSenderThread

Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java
bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java

index b97d68cd10d867d969ce2dea2dac9a7346db697f..2dcf5d54e3a0e529b1b237457f6a5d481e79aa6d 100644 (file)
@@ -90,6 +90,7 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
     protected @Nullable MiIoAsyncCommunication miioCom;
     protected CloudConnector cloudConnector;
     protected String cloudServer = "";
+    protected String deviceId = "";
     protected int lastId;
 
     protected Map<Integer, String> cmds = new ConcurrentHashMap<>();
@@ -153,6 +154,7 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
             return;
         }
         this.cloudServer = configuration.cloudServer;
+        this.deviceId = configuration.deviceId;
         isIdentified = false;
         deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
         deviceVariables.put(PROPERTY_DID, configuration.deviceId);
@@ -372,63 +374,62 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
         if (configuration.host.isBlank()) {
             return null;
         }
-        @Nullable
-        String deviceId = configuration.deviceId;
+        if (deviceId.isBlank() && !getCloudServer().isBlank()) {
+            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
+                    "Cloud communication requires defined deviceId in the config");
+            return null;
+        }
         if (deviceId.length() == 8 && deviceId.matches("^.*[a-zA-Z]+.*$")) {
             logger.warn(
                     "As per openHAB version 3.2 the deviceId is no longer a string with hexadecimals, instead it is a string with the numeric respresentation of the deviceId. If you continue seeing this message, update deviceId in your thing configuration. Expected change for thing '{}': Update current deviceId: '{}' to '{}'",
                     getThing().getUID(), deviceId, Utils.fromHEX(deviceId));
-            deviceId = "";
-        }
-        try {
-            if (!deviceId.isBlank() && tokenCheckPass(configuration.token)) {
-                final MiIoAsyncCommunication miioCom = new MiIoAsyncCommunication(configuration.host, token, deviceId,
-                        lastId, configuration.timeout, cloudConnector);
-                if (getCloudServer().isBlank()) {
-                    logger.debug("Ping Mi deviceId '{}' at {}", deviceId, configuration.host);
-                    Message miIoResponse = miioCom.sendPing(configuration.host);
-                    if (miIoResponse != null) {
-                        logger.debug("Ping response from deviceId '{}' at {}. Time stamp: {}, OH time {}, delta {}",
-                                Utils.fromHEX(Utils.getHex(miIoResponse.getDeviceId())), configuration.host,
-                                miIoResponse.getTimestamp(), LocalDateTime.now(), miioCom.getTimeDelta());
-                        miioCom.registerListener(this);
-                        this.miioCom = miioCom;
-                        return miioCom;
-                    } else {
-                        miioCom.close();
-                    }
-                } else {
-                    miioCom.registerListener(this);
-                    this.miioCom = miioCom;
-                    return miioCom;
-                }
+            if (getCloudServer().isBlank()) {
+                deviceId = "";
             } else {
-                logger.debug("No deviceId defined. Retrieving Mi deviceId");
-                final MiIoAsyncCommunication miioCom = new MiIoAsyncCommunication(configuration.host, token, "", lastId,
-                        configuration.timeout, cloudConnector);
-                Message miIoResponse = miioCom.sendPing(configuration.host);
+                final String id = Utils.fromHEX(deviceId);
+                deviceId = id;
+                miIoScheduler.execute(() -> updateDeviceIdConfig(id));
+            }
+        }
+
+        if (!deviceId.isBlank() && (tokenCheckPass(configuration.token) || !getCloudServer().isBlank())) {
+            final MiIoAsyncCommunication miioComF = new MiIoAsyncCommunication(configuration.host, token, deviceId,
+                    lastId, configuration.timeout, cloudConnector);
+            miioComF.registerListener(this);
+            this.miioCom = miioComF;
+            return miioComF;
+        } else {
+            logger.debug("No deviceId defined. Retrieving Mi deviceId");
+            final MiIoAsyncCommunication miioComF = new MiIoAsyncCommunication(configuration.host, token, "", lastId,
+                    configuration.timeout, cloudConnector);
+            try {
+                Message miIoResponse = miioComF.sendPing(configuration.host);
                 if (miIoResponse != null) {
                     deviceId = Utils.fromHEX(Utils.getHex(miIoResponse.getDeviceId()));
                     logger.debug("Ping response from deviceId '{}' at {}. Time stamp: {}, OH time {}, delta {}",
                             deviceId, configuration.host, miIoResponse.getTimestamp(), LocalDateTime.now(),
-                            miioCom.getTimeDelta());
-                    miioCom.setDeviceId(deviceId);
+                            miioComF.getTimeDelta());
+                    miioComF.setDeviceId(deviceId);
                     logger.debug("Using retrieved Mi deviceId: {}", deviceId);
-                    updateDeviceIdConfig(deviceId);
-                    miioCom.registerListener(this);
-                    this.miioCom = miioCom;
-                    return miioCom;
+                    miioComF.registerListener(this);
+                    this.miioCom = miioComF;
+                    final String id = deviceId;
+                    miIoScheduler.execute(() -> updateDeviceIdConfig(id));
+                    return miioComF;
                 } else {
-                    miioCom.close();
+                    miioComF.close();
+                    logger.debug("Ping response from deviceId '{}' at {} FAILED", configuration.deviceId,
+                            configuration.host);
+                    disconnectedNoResponse();
+                    return null;
                 }
+            } catch (IOException e) {
+                miioComF.close();
+                logger.debug("Could not connect to {} at {}", getThing().getUID().toString(), configuration.host);
+                disconnected(e.getMessage());
+                return null;
             }
-            logger.debug("Ping response from deviceId '{}' at {} FAILED", configuration.deviceId, configuration.host);
-            disconnectedNoResponse();
-            return null;
-        } catch (IOException e) {
-            logger.debug("Could not connect to {} at {}", getThing().getUID().toString(), configuration.host);
-            disconnected(e.getMessage());
-            return null;
+
         }
     }
 
index 95be2fba35aa850531c57396d174f13361523310..15c710300bba9c76cfb4953aa282e406fb62892b 100644 (file)
@@ -93,7 +93,6 @@ public class MiIoAsyncCommunication {
         this.timeout = timeout;
         this.cloudConnector = cloudConnector;
         setId(id);
-        startReceiver();
     }
 
     protected List<MiIoMessageListener> getListeners() {
@@ -249,7 +248,7 @@ public class MiIoAsyncCommunication {
     public synchronized void startReceiver() {
         MessageSenderThread senderThread = this.senderThread;
         if (senderThread == null || !senderThread.isAlive()) {
-            senderThread = new MessageSenderThread(deviceId);
+            senderThread = new MessageSenderThread(deviceId.isBlank() ? "?" + ip : deviceId);
             senderThread.start();
             this.senderThread = senderThread;
         }
@@ -435,7 +434,7 @@ public class MiIoAsyncCommunication {
         if (socket == null || socket.isClosed()) {
             socket = new DatagramSocket();
             socket.setSoTimeout(timeout);
-            logger.debug("Opening socket on port: {} ", socket.getLocalPort());
+            logger.debug("Opening socket on port: {} ({} {})", socket.getLocalPort(), deviceId, ip);
             this.socket = socket;
             return socket;
         } else {
@@ -497,6 +496,10 @@ public class MiIoAsyncCommunication {
 
     public void setDeviceId(String deviceId) {
         this.deviceId = deviceId;
+        MessageSenderThread senderThread = this.senderThread;
+        if (senderThread != null) {
+            senderThread.setName("OH-binding-miio-MessageSenderThread-" + deviceId);
+        }
     }
 
     public int getQueueLength() {