[myStrom] Request info is not supported by the first generation of plug (#11854)

* Request info is not supported by the first generation of plug

closes #10432
Signed-off-by: Frederic Chastagnol <fchastagnol@fredoware.ch>
This commit is contained in:
Fredo70 2021-12-28 18:42:16 +01:00 committed by GitHub
parent b6af3aba9f
commit 96a8f942b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 21 deletions

View 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();
}

View 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);