]> git.basschouten.com Git - openhab-addons.git/commitdiff
[myStrom] Request info is not supported by the first generation of plug (#11854)
authorFredo70 <fchastagnol@fredoware.ch>
Tue, 28 Dec 2021 17:42:16 +0000 (18:42 +0100)
committerGitHub <noreply@github.com>
Tue, 28 Dec 2021 17:42:16 +0000 (18:42 +0100)
* Request info is not supported by the first generation of plug

closes #10432
Signed-off-by: Frederic Chastagnol <fchastagnol@fredoware.ch>
bundles/org.openhab.binding.mystrom/src/main/java/org/openhab/binding/mystrom/internal/AbstractMyStromHandler.java
bundles/org.openhab.binding.mystrom/src/main/java/org/openhab/binding/mystrom/internal/MyStromBulbHandler.java

index 1be559b58b42badf32ce70b3f669eb4b55b7cb97..101a409e0a005a7f592f72e7e0be596db19b9f03 100644 (file)
@@ -46,8 +46,11 @@ import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
 import org.openhab.core.thing.binding.BaseThingHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
 
 /**
  * The {@link AbstractMyStromHandler} is responsible for handling commands, which are
@@ -64,6 +67,7 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
     protected String hostname = "";
     protected String mac = "";
 
+    private final Logger logger = LoggerFactory.getLogger(AbstractMyStromHandler.class);
     private @Nullable ScheduledFuture<?> pollingJob;
     protected final Gson gson = new Gson();
 
@@ -91,28 +95,33 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
         super.dispose();
     }
 
-    private void updateProperties() throws MyStromException {
-        String json = sendHttpRequest(HttpMethod.GET, "/api/v1/info", null);
-        MyStromDeviceInfo deviceInfo = gson.fromJson(json, MyStromDeviceInfo.class);
-        if (deviceInfo == null) {
-            throw new MyStromException("Cannot retrieve device info from myStrom device " + getThing().getUID());
+    private void updateProperties() {
+        try {
+            String json = sendHttpRequest(HttpMethod.GET, "/api/v1/info", null);
+            MyStromDeviceInfo deviceInfo = gson.fromJson(json, MyStromDeviceInfo.class);
+            if (deviceInfo == null) {
+                throw new MyStromException("Cannot retrieve device info from myStrom device " + getThing().getUID());
+            }
+            this.mac = deviceInfo.mac;
+            Map<String, String> properties = editProperties();
+            properties.put(PROPERTY_MAC, deviceInfo.mac);
+            properties.put(PROPERTY_VERSION, deviceInfo.version);
+            properties.put(PROPERTY_TYPE, Long.toString(deviceInfo.type));
+            properties.put(PROPERTY_SSID, deviceInfo.ssid);
+            properties.put(PROPERTY_IP, deviceInfo.ip);
+            properties.put(PROPERTY_MASK, deviceInfo.mask);
+            properties.put(PROPERTY_GW, deviceInfo.gw);
+            properties.put(PROPERTY_DNS, deviceInfo.dns);
+            properties.put(PROPERTY_STATIC, Boolean.toString(deviceInfo.staticState));
+            properties.put(PROPERTY_CONNECTED, Boolean.toString(deviceInfo.connected));
+            Calendar calendar = Calendar.getInstance();
+            DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM,
+                    Locale.getDefault());
+            properties.put(PROPERTY_LAST_REFRESH, formatter.format(calendar.getTime()));
+            updateProperties(properties);
+        } catch (JsonSyntaxException | MyStromException ex) {
+            logger.debug("Updating properties failed: ", ex);
         }
-        this.mac = deviceInfo.mac;
-        Map<String, String> properties = editProperties();
-        properties.put(PROPERTY_MAC, deviceInfo.mac);
-        properties.put(PROPERTY_VERSION, deviceInfo.version);
-        properties.put(PROPERTY_TYPE, Long.toString(deviceInfo.type));
-        properties.put(PROPERTY_SSID, deviceInfo.ssid);
-        properties.put(PROPERTY_IP, deviceInfo.ip);
-        properties.put(PROPERTY_MASK, deviceInfo.mask);
-        properties.put(PROPERTY_GW, deviceInfo.gw);
-        properties.put(PROPERTY_DNS, deviceInfo.dns);
-        properties.put(PROPERTY_STATIC, Boolean.toString(deviceInfo.staticState));
-        properties.put(PROPERTY_CONNECTED, Boolean.toString(deviceInfo.connected));
-        Calendar calendar = Calendar.getInstance();
-        DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.getDefault());
-        properties.put(PROPERTY_LAST_REFRESH, formatter.format(calendar.getTime()));
-        updateProperties(properties);
     }
 
     /**
@@ -147,6 +156,7 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
     private void initializeInternal() {
         try {
             updateProperties();
+            checkRequiredInfo();
             updateStatus(ThingStatus.ONLINE);
             MyStromConfiguration config = getConfigAs(MyStromConfiguration.class);
             pollingJob = scheduler.scheduleWithFixedDelay(this::pollDevice, 0, config.refresh, TimeUnit.SECONDS);
@@ -155,5 +165,8 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
         }
     }
 
+    protected void checkRequiredInfo() throws MyStromException {
+    }
+
     protected abstract void pollDevice();
 }
index 259136291438e43af42cf57aaa9575c30947bdbb..012f69610fb86ff9e0c79595cba6316c54542634 100644 (file)
@@ -154,6 +154,13 @@ public class MyStromBulbHandler extends AbstractMyStromHandler {
         }
     }
 
+    @Override
+    protected void checkRequiredInfo() throws MyStromException {
+        if (mac.isBlank()) {
+            throw new MyStromException("Cannot retrieve MAC info from myStrom device " + getThing().getUID());
+        }
+    }
+
     private @Nullable Map<String, MyStromDeviceSpecificInfo> getReport() {
         try {
             String returnContent = sendHttpRequest(HttpMethod.GET, "/api/v1/device", null);