]> git.basschouten.com Git - openhab-addons.git/commitdiff
[keba] Robustness improvements on communication error (#10399)
authorMikeTheTux <44850211+MikeTheTux@users.noreply.github.com>
Sat, 17 Apr 2021 21:52:33 +0000 (23:52 +0200)
committerGitHub <noreply@github.com>
Sat, 17 Apr 2021 21:52:33 +0000 (23:52 +0200)
Signed-off-by: Michael Weger <weger.michael@gmx.net>
bundles/org.openhab.binding.keba/src/main/java/org/openhab/binding/keba/internal/handler/KeContactHandler.java

index af2b999a91516932bc7ea88c339a387589e14db0..5317db71ce78ebd7df406151b4fe2c6bf5a54829 100644 (file)
@@ -181,51 +181,61 @@ public class KeContactHandler extends BaseThingHandler {
             long stamp = System.currentTimeMillis();
             if (!isKebaReachable()) {
                 logger.debug("isKebaReachable() timed out after '{}' milliseconds", System.currentTimeMillis() - stamp);
-                transceiver.unRegisterHandler(getHandler());
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+                        "An timeout occurred while polling the charging station");
             } else {
-                if (getThing().getStatus() == ThingStatus.ONLINE) {
-                    ByteBuffer response = cache.get(CACHE_REPORT_1);
-                    if (response != null) {
-                        onData(response);
-                    }
+                ByteBuffer response = cache.get(CACHE_REPORT_1);
+                if (response == null) {
+                    logger.debug("Missing response from Keba station for 'report 1'");
+                } else {
+                    onData(response);
+                }
 
-                    Thread.sleep(REPORT_INTERVAL);
+                Thread.sleep(REPORT_INTERVAL);
 
-                    response = cache.get(CACHE_REPORT_2);
-                    if (response != null) {
-                        onData(response);
-                    }
+                response = cache.get(CACHE_REPORT_2);
+                if (response == null) {
+                    logger.debug("Missing response from Keba station for 'report 2'");
+                } else {
+                    onData(response);
+                }
 
-                    Thread.sleep(REPORT_INTERVAL);
+                Thread.sleep(REPORT_INTERVAL);
 
-                    response = cache.get(CACHE_REPORT_3);
-                    if (response != null) {
-                        onData(response);
-                    }
+                response = cache.get(CACHE_REPORT_3);
+                if (response == null) {
+                    logger.debug("Missing response from Keba station for 'report 3'");
+                } else {
+                    onData(response);
+                }
 
-                    if (isReport100needed) {
-                        Thread.sleep(REPORT_INTERVAL);
+                if (isReport100needed) {
+                    Thread.sleep(REPORT_INTERVAL);
 
-                        response = cache.get(CACHE_REPORT_100);
-                        if (response != null) {
-                            onData(response);
-                        }
-                        isReport100needed = false;
+                    response = cache.get(CACHE_REPORT_100);
+                    if (response == null) {
+                        logger.debug("Missing response from Keba station for 'report 100'");
+                    } else {
+                        onData(response);
                     }
+                    isReport100needed = false;
                 }
             }
-        } catch (NumberFormatException | IOException e) {
-            logger.debug("An exception occurred while polling the KEBA KeContact '{}': {}", getThing().getUID(),
+        } catch (IOException e) {
+            logger.debug("An error occurred while polling the KEBA KeContact '{}': {}", getThing().getUID(),
                     e.getMessage(), e);
-            Thread.currentThread().interrupt();
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                    "An exception occurred while while polling the charging station");
+                    "An error occurred while polling the charging station");
         } catch (InterruptedException e) {
             logger.debug("Polling job has been interrupted for handler of thing '{}'.", getThing().getUID());
         }
     }
 
     protected void onData(ByteBuffer byteBuffer) {
+        if (getThing().getStatus() != ThingStatus.ONLINE) {
+            updateStatus(ThingStatus.ONLINE);
+        }
+
         String response = new String(byteBuffer.array(), 0, byteBuffer.limit());
         response = StringUtils.chomp(response);