From 168d1fec39a55c22dadfca74dc2a5d25396d3faa Mon Sep 17 00:00:00 2001 From: Markus Michels Date: Fri, 19 Jan 2024 15:58:18 -0500 Subject: [PATCH] =?UTF-8?q?[shelly]=C2=A0improved=20logging=20on=20WebSock?= =?UTF-8?q?et=20connection=20problems=20(#16303)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve logging on WebSocket connection problems Signed-off-by: Markus Michels Signed-off-by: Ciprian Pascu --- .../shelly/internal/api2/Shelly2ApiRpc.java | 14 ++++++---- .../shelly/internal/api2/ShellyBluApi.java | 27 +++++++++---------- .../internal/handler/ShellyBaseHandler.java | 2 +- .../handler/ShellyBluSensorHandler.java | 4 ++- .../resources/OH-INF/i18n/shelly.properties | 7 ++--- .../main/resources/scripts/oh-blu-scanner.js | 4 +-- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java index df0a0602e93..5b654e685c4 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java @@ -672,7 +672,7 @@ public class Shelly2ApiRpc extends Shelly2ApiClient implements ShellyApiInterfac getThing().requestUpdates(1, true); // refresh config break; case SHELLY2_EVENT_SLEEP: - logger.debug("{}: Device went to sleep mode", thingName); + logger.debug("{}: Connection terminated, e.g. device in sleep mode", thingName); break; case SHELLY2_EVENT_WIFICONNFAILED: logger.debug("{}: WiFi connect failed, check setup, reason {}", thingName, @@ -700,10 +700,14 @@ public class Shelly2ApiRpc extends Shelly2ApiClient implements ShellyApiInterfac } @Override - public void onClose(int statusCode, String reason) { + public void onClose(int statusCode, String description) { try { - logger.debug("{}: WebSocket connection closed, status = {}/{}", thingName, statusCode, getString(reason)); - if (statusCode == StatusCode.ABNORMAL && !discovery && getProfile().alwaysOn) { // e.g. device rebooted + String reason = getString(description); + logger.debug("{}: WebSocket connection closed, status = {}/{}", thingName, statusCode, reason); + if ("Bye".equalsIgnoreCase(reason)) { + logger.debug("{}: Device went to sleep mode", thingName); + } else if (statusCode == StatusCode.ABNORMAL && !discovery && getProfile().alwaysOn) { + // e.g. device rebooted thingOffline("WebSocket connection closed abnormal"); } } catch (ShellyApiException e) { @@ -714,7 +718,7 @@ public class Shelly2ApiRpc extends Shelly2ApiClient implements ShellyApiInterfac @Override public void onError(Throwable cause) { - logger.debug("{}: WebSocket error", thingName); + logger.debug("{}: WebSocket error: {}", thingName, cause.getMessage()); if (thing != null && thing.getProfile().alwaysOn) { thingOffline("WebSocket error"); } diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java index 1778a2184b4..3758cee1812 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java @@ -18,9 +18,9 @@ import static org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.*; import static org.openhab.binding.shelly.internal.util.ShellyUtils.*; import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.shelly.internal.api.ShellyApiException; import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile; @@ -50,25 +50,24 @@ import org.slf4j.LoggerFactory; * * @author Markus Michels - Initial contribution */ +@NonNullByDefault public class ShellyBluApi extends Shelly2ApiRpc { private static final Logger logger = LoggerFactory.getLogger(ShellyBluApi.class); private boolean connected = false; // true = BLU devices has connected private ShellySettingsStatus deviceStatus = new ShellySettingsStatus(); private int lastPid = -1; - private static final Map MAP_INPUT_EVENT_TYPE = new HashMap<>(); - static { - MAP_INPUT_EVENT_TYPE.put(SHELLY2_EVENT_1PUSH, SHELLY_BTNEVENT_1SHORTPUSH); - MAP_INPUT_EVENT_TYPE.put(SHELLY2_EVENT_2PUSH, SHELLY_BTNEVENT_2SHORTPUSH); - MAP_INPUT_EVENT_TYPE.put(SHELLY2_EVENT_3PUSH, SHELLY_BTNEVENT_3SHORTPUSH); - MAP_INPUT_EVENT_TYPE.put(SHELLY2_EVENT_LPUSH, SHELLY_BTNEVENT_LONGPUSH); - MAP_INPUT_EVENT_TYPE.put(SHELLY2_EVENT_LSPUSH, SHELLY_BTNEVENT_LONGSHORTPUSH); - MAP_INPUT_EVENT_TYPE.put(SHELLY2_EVENT_SLPUSH, SHELLY_BTNEVENT_SHORTLONGPUSH); - MAP_INPUT_EVENT_TYPE.put("1", SHELLY_BTNEVENT_1SHORTPUSH); - MAP_INPUT_EVENT_TYPE.put("2", SHELLY_BTNEVENT_2SHORTPUSH); - MAP_INPUT_EVENT_TYPE.put("3", SHELLY_BTNEVENT_3SHORTPUSH); - MAP_INPUT_EVENT_TYPE.put("4", SHELLY_BTNEVENT_LONGPUSH); - } + private static final Map MAP_INPUT_EVENT_TYPE = Map.of( // + SHELLY2_EVENT_1PUSH, SHELLY_BTNEVENT_1SHORTPUSH, // + SHELLY2_EVENT_2PUSH, SHELLY_BTNEVENT_2SHORTPUSH, // + SHELLY2_EVENT_3PUSH, SHELLY_BTNEVENT_3SHORTPUSH, // + SHELLY2_EVENT_LPUSH, SHELLY_BTNEVENT_LONGPUSH, // + SHELLY2_EVENT_LSPUSH, SHELLY_BTNEVENT_LONGSHORTPUSH, // + SHELLY2_EVENT_SLPUSH, SHELLY_BTNEVENT_SHORTLONGPUSH, // + "1", SHELLY_BTNEVENT_1SHORTPUSH, // + "2", SHELLY_BTNEVENT_2SHORTPUSH, // + "3", SHELLY_BTNEVENT_3SHORTPUSH, // + "4", SHELLY_BTNEVENT_LONGPUSH); /** * Regular constructor - called by Thing handler diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java index 4e0a4a1f141..003a20eadd6 100755 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java @@ -340,7 +340,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler ? tmpPrf.settings.sleepMode.period * 60 // minutes : tmpPrf.settings.sleepMode.period * 3600; // hours tmpPrf.updatePeriod += 60; // give 1min extra - } else if ((tmpPrf.settings.coiot != null) && tmpPrf.settings.coiot.updatePeriod != null) { + } else if (tmpPrf.settings.coiot != null && tmpPrf.settings.coiot.updatePeriod != null) { // Derive from CoAP update interval, usually 2*15+10s=40sec -> 70sec tmpPrf.updatePeriod = Math.max(UPDATE_SETTINGS_INTERVAL_SECONDS, 2 * getInteger(tmpPrf.settings.coiot.updatePeriod)) + 10; diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBluSensorHandler.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBluSensorHandler.java index 3325cc8673b..d6c10719791 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBluSensorHandler.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBluSensorHandler.java @@ -20,6 +20,7 @@ import static org.openhab.core.thing.Thing.PROPERTY_MODEL_ID; import java.util.Map; import java.util.TreeMap; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile; @@ -37,6 +38,7 @@ import org.slf4j.LoggerFactory; * * @author Markus Michels - Initial contribution */ +@NonNullByDefault public class ShellyBluSensorHandler extends ShellyBaseHandler { private static final Logger logger = LoggerFactory.getLogger(ShellyBluSensorHandler.class); @@ -52,7 +54,7 @@ public class ShellyBluSensorHandler extends ShellyBaseHandler { super.initialize(); } - public static void addBluThing(String gateway, Shelly2NotifyEvent e, ShellyThingTable thingTable) { + public static void addBluThing(String gateway, Shelly2NotifyEvent e, @Nullable ShellyThingTable thingTable) { String model = substringBefore(getString(e.data.name), "-").toUpperCase(); String mac = e.data.addr.replaceAll(":", ""); String ttype = ""; diff --git a/bundles/org.openhab.binding.shelly/src/main/resources/OH-INF/i18n/shelly.properties b/bundles/org.openhab.binding.shelly/src/main/resources/OH-INF/i18n/shelly.properties index 94797887498..ff383047286 100644 --- a/bundles/org.openhab.binding.shelly/src/main/resources/OH-INF/i18n/shelly.properties +++ b/bundles/org.openhab.binding.shelly/src/main/resources/OH-INF/i18n/shelly.properties @@ -101,9 +101,6 @@ thing-type.shelly.shellyplussmoke.description = Shelly Plus Smoke - Smoke Detect thing-type.shelly.shellypluswdus.description = Shelly Wall Dimmer US Device thing-type.shelly.shellyplus10v.description = Shelly Plus Dimmer 10V -# Wall displays -thing-type.shelly.shellywalldisplay.description = Shelly Wall Display with sensors and input/output - # Plus Mini Devices thing-type.shelly.shellyplusmini1.description = Shelly Plus Mini 1 - Single Relay Switch thing-type.shelly.shellyplusminipm.description = Shelly Plus Mini PM - Power Meter @@ -125,8 +122,8 @@ thing-type.shelly.shellyblubutton.description = Shelly BLU Button 1 thing-type.shelly.shellybludw.description = Shelly BLU Door/Window Sensor thing-type.shelly.shellyblumotion.description = Shelly BLU Motion Sensor - # Wall Displays - thing-type.shelly.shellywalldisplay.description = Shelly Wall Display with sensors and input/output +# Wall Displays +thing-type.shelly.shellywalldisplay.description = Shelly Wall Display with sensors and input/output # thing config - shellydevice thing-type.config.shelly.deviceIp.label = IP Address diff --git a/bundles/org.openhab.binding.shelly/src/main/resources/scripts/oh-blu-scanner.js b/bundles/org.openhab.binding.shelly/src/main/resources/scripts/oh-blu-scanner.js index 3dd38932ad7..005f85860d2 100644 --- a/bundles/org.openhab.binding.shelly/src/main/resources/scripts/oh-blu-scanner.js +++ b/bundles/org.openhab.binding.shelly/src/main/resources/scripts/oh-blu-scanner.js @@ -1,6 +1,6 @@ /* - * This script uses the BLE scan functionality in scripting to pass scan reults to openHAB - * Supported BLU Devices: SBBT , SBDW + * This script uses the BLE scan functionality in scripting to pass scan results to openHAB + * Supported BLU Devices: BLU Button 1, BLU Door/Window, BLU Motion * Version 0.2 */