[wemo] make UPnP more robust and fix polling/portscan (#12001)

* [wemo] make UPnP more robust
* [wemo] change thing status updates

Signed-off-by: Hans-Jörg Merk <github@hmerk.de>
This commit is contained in:
Hans-Jörg Merk 2022-01-23 22:49:52 +01:00 committed by GitHub
parent d4fb20d529
commit d9c31e626a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 1838 additions and 1116 deletions

View File

@ -116,6 +116,15 @@ public class WemoBindingConstants {
public static final int LINK_DISCOVERY_SERVICE_INITIAL_DELAY = 5; public static final int LINK_DISCOVERY_SERVICE_INITIAL_DELAY = 5;
public static final String HTTP_CALL_CONTENT_HEADER = "text/xml; charset=utf-8"; public static final String HTTP_CALL_CONTENT_HEADER = "text/xml; charset=utf-8";
public static final String BASICACTION = "basicevent";
public static final String BASICEVENT = "basicevent1";
public static final String BRIDGEACTION = "bridge";
public static final String BRIDGEEVENT = "bridge1";
public static final String DEVICEACTION = "deviceevent";
public static final String DEVICEEVENT = "deviceevent1";
public static final String INSIGHTACTION = "insight";
public static final String INSIGHTEVENT = "insight1";
public static final Set<ThingTypeUID> SUPPORTED_BRIDGE_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE); public static final Set<ThingTypeUID> SUPPORTED_BRIDGE_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
public static final Set<ThingTypeUID> SUPPORTED_LIGHT_THING_TYPES = Collections.singleton(THING_TYPE_MZ100); public static final Set<ThingTypeUID> SUPPORTED_LIGHT_THING_TYPES = Collections.singleton(THING_TYPE_MZ100);

View File

@ -64,7 +64,7 @@ public class WemoHandlerFactory extends BaseThingHandlerFactory {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = WemoBindingConstants.SUPPORTED_THING_TYPES; public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = WemoBindingConstants.SUPPORTED_THING_TYPES;
private UpnpIOService upnpIOService; private final UpnpIOService upnpIOService;
private @Nullable WemoHttpCallFactory wemoHttpCallFactory; private @Nullable WemoHttpCallFactory wemoHttpCallFactory;
@Override @Override
@ -103,14 +103,14 @@ public class WemoHandlerFactory extends BaseThingHandlerFactory {
WemoBridgeHandler handler = new WemoBridgeHandler((Bridge) thing); WemoBridgeHandler handler = new WemoBridgeHandler((Bridge) thing);
registerDeviceDiscoveryService(handler, wemoHttpcaller); registerDeviceDiscoveryService(handler, wemoHttpcaller);
return handler; return handler;
} else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MAKER)) {
logger.debug("Creating a WemoMakerHandler for thing '{}' with UDN '{}'", thing.getUID(),
thing.getConfiguration().get(UDN));
return new WemoMakerHandler(thing, upnpIOService, wemoHttpcaller);
} else if (WemoBindingConstants.SUPPORTED_DEVICE_THING_TYPES.contains(thing.getThingTypeUID())) { } else if (WemoBindingConstants.SUPPORTED_DEVICE_THING_TYPES.contains(thing.getThingTypeUID())) {
logger.debug("Creating a WemoHandler for thing '{}' with UDN '{}'", thing.getUID(), logger.debug("Creating a WemoHandler for thing '{}' with UDN '{}'", thing.getUID(),
thing.getConfiguration().get(UDN)); thing.getConfiguration().get(UDN));
return new WemoHandler(thing, upnpIOService, wemoHttpcaller); return new WemoHandler(thing, upnpIOService, wemoHttpcaller);
} else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MAKER)) {
logger.debug("Creating a WemoMakerHandler for thing '{}' with UDN '{}'", thing.getUID(),
thing.getConfiguration().get(UDN));
return new WemoMakerHandler(thing, upnpIOService, wemoHttpcaller);
} else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_COFFEE)) { } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_COFFEE)) {
logger.debug("Creating a WemoCoffeeHandler for thing '{}' with UDN '{}'", thing.getUID(), logger.debug("Creating a WemoCoffeeHandler for thing '{}' with UDN '{}'", thing.getUID(),
thing.getConfiguration().get(UDN)); thing.getConfiguration().get(UDN));

View File

@ -13,7 +13,6 @@
package org.openhab.binding.wemo.internal; package org.openhab.binding.wemo.internal;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@ -23,6 +22,9 @@ import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.io.net.http.HttpUtil; import org.openhab.core.io.net.http.HttpUtil;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/** /**
* {@link WemoUtil} implements some helper functions. * {@link WemoUtil} implements some helper functions.
@ -123,11 +125,10 @@ public class WemoUtil {
return unescapedOutput.toString(); return unescapedOutput.toString();
} }
public static @Nullable String getWemoURL(URL descriptorURL, String actionService) { public static @Nullable String getWemoURL(String host, String actionService) {
int portCheckStart = 49151; int portCheckStart = 49151;
int portCheckStop = 49157; int portCheckStop = 49157;
String port = null; String port = null;
String host = substringBetween(descriptorURL.toString(), "://", ":");
for (int i = portCheckStart; i < portCheckStop; i++) { for (int i = portCheckStart; i < portCheckStop; i++) {
if (serviceAvailableFunction.apply(host, i)) { if (serviceAvailableFunction.apply(host, i)) {
port = String.valueOf(i); port = String.valueOf(i);
@ -155,4 +156,30 @@ public class WemoUtil {
entities.put("quot", "\""); entities.put("quot", "\"");
return entities; return entities;
} }
public static String createBinaryStateContent(boolean binaryState) {
String binary = binaryState == true ? "1" : "0";
String content = "<?xml version=\"1.0\"?>"
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<BinaryState>"
+ binary + "</BinaryState>" + "</u:SetBinaryState>" + "</s:Body>" + "</s:Envelope>";
return content;
}
public static String createStateRequestContent(String action, String actionService) {
String content = "<?xml version=\"1.0\"?>"
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:"
+ action + ">" + "</s:Body>" + "</s:Envelope>";
return content;
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}
} }

View File

@ -25,7 +25,6 @@ import org.openhab.binding.wemo.internal.WemoBindingConstants;
import org.openhab.core.config.discovery.DiscoveryResult; import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant; import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID; import org.openhab.core.thing.ThingUID;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
@ -34,7 +33,7 @@ import org.slf4j.LoggerFactory;
/** /**
* The {@link WemoDiscoveryParticipant} is responsible for discovering new and * The {@link WemoDiscoveryParticipant} is responsible for discovering new and
* removed Wemo devices. It uses the central {@link UpnpDiscoveryService}. * removed Wemo devices.
* *
* @author Hans-Jörg Merk - Initial contribution * @author Hans-Jörg Merk - Initial contribution
* @author Kai Kreuzer - some refactoring for performance and simplification * @author Kai Kreuzer - some refactoring for performance and simplification

View File

@ -65,8 +65,9 @@ public class WemoDiscoveryService extends AbstractDiscoveryService {
@Override @Override
protected void startScan() { protected void startScan() {
logger.debug("Starting UPnP RootDevice search..."); logger.debug("Starting UPnP RootDevice search...");
if (upnpService != null) { UpnpService localService = upnpService;
upnpService.getControlPoint().search(new RootDeviceHeader()); if (localService != null) {
localService.getControlPoint().search(new RootDeviceHeader());
} else { } else {
logger.debug("upnpService not set"); logger.debug("upnpService not set");
} }

View File

@ -58,7 +58,8 @@ public class WemoBridgeHandler extends BaseBridgeHandler {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
logger.debug("Cannot initalize WemoBridgeHandler. UDN not set."); logger.debug("Cannot initalize WemoBridgeHandler. UDN not set.");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
} }
} }

View File

@ -16,7 +16,6 @@ import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
import static org.openhab.binding.wemo.internal.WemoUtil.*; import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.time.Instant; import java.time.Instant;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@ -51,10 +50,8 @@ import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@ -72,31 +69,18 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_COFFEE); public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_COFFEE);
private Map<String, Boolean> subscriptionState = new HashMap<>(); private final Object upnpLock = new Object();
private final Object jobLock = new Object();
private UpnpIOService service; private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall; private WemoHttpCall wemoCall;
private @Nullable ScheduledFuture<?> refreshJob; private String host = "";
private final Runnable refreshRunnable = new Runnable() { private Map<String, Boolean> subscriptionState = new HashMap<>();
@Override private @Nullable ScheduledFuture<?> pollingJob;
public void run() {
try {
if (!isUpnpDeviceRegistered()) {
logger.debug("WeMo UPnP device {} not yet registered", getUDN());
}
updateWemoState();
onSubscription();
} catch (Exception e) {
logger.debug("Exception during poll", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
}
}
};
public WemoCoffeeHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) { public WemoCoffeeHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller); super(thing, wemoHttpCaller);
@ -104,19 +88,26 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
this.wemoCall = wemoHttpCaller; this.wemoCall = wemoHttpCaller;
this.service = upnpIOService; this.service = upnpIOService;
logger.debug("Creating a WemoCoffeeHandler V0.4 for thing '{}'", getThing().getUID()); logger.debug("Creating a WemoCoffeeHandler for thing '{}'", getThing().getUID());
} }
@Override @Override
public void initialize() { public void initialize() {
Configuration configuration = getConfig(); Configuration configuration = getConfig();
if (configuration.get("udn") != null) { if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoCoffeeHandler for UDN '{}'", configuration.get("udn")); logger.debug("Initializing WemoCoffeeHandler for UDN '{}'", configuration.get(UDN));
onSubscription(); UpnpIOService localService = service;
onUpdate(); if (localService != null) {
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
logger.debug("Cannot initalize WemoCoffeeHandler. UDN not set."); logger.debug("Cannot initalize WemoCoffeeHandler. UDN not set.");
} }
} }
@ -124,19 +115,61 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
@Override @Override
public void dispose() { public void dispose() {
logger.debug("WeMoCoffeeHandler disposed."); logger.debug("WeMoCoffeeHandler disposed.");
ScheduledFuture<?> job = this.pollingJob;
ScheduledFuture<?> job = refreshJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
removeSubscription(); removeSubscription();
} }
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
synchronized (upnpLock) {
subscriptionState = new HashMap<>();
}
return;
}
updateStatus(ThingStatus.ONLINE);
updateWemoState();
addSubscription();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
}
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID); String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to send command '{}' for device '{}': IP address missing", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to send command '{}' for device '{}': URL cannot be created", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
if (command instanceof RefreshType) { if (command instanceof RefreshType) {
try { try {
updateWemoState(); updateWemoState();
@ -162,15 +195,19 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
+ "&lt;attribute&gt;&lt;name&gt;Cleaning&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;</attributeList>" + "&lt;attribute&gt;&lt;name&gt;Cleaning&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;</attributeList>"
+ "</u:SetAttributes>" + "</s:Body>" + "</s:Envelope>"; + "</u:SetAttributes>" + "</s:Body>" + "</s:Envelope>";
URL descriptorURL = service.getDescriptorURL(this); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoURL = getWemoURL(descriptorURL, "basicevent"); if (wemoCallResponse != null) {
updateState(CHANNEL_STATE, OnOffType.ON);
if (wemoURL != null) { State newMode = new StringType("Brewing");
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); updateState(CHANNEL_COFFEEMODE, newMode);
if (wemoCallResponse != null) { if (logger.isTraceEnabled()) {
updateState(CHANNEL_STATE, OnOffType.ON); logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
State newMode = new StringType("Brewing"); logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader,
updateState(CHANNEL_COFFEEMODE, newMode); getThing().getUID());
logger.trace("wemoCall with content '{}' for device '{}'", content,
getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse,
getThing().getUID());
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -179,7 +216,8 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
} }
} }
// if command.equals(OnOffType.OFF) we do nothing because WeMo Coffee Maker cannot be switched off // if command.equals(OnOffType.OFF) we do nothing because WeMo Coffee Maker cannot be switched
// off
// remotely // remotely
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} }
@ -200,53 +238,53 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
// We can subscribe to GENA events, but there is no usefull response right now. // We can subscribe to GENA events, but there is no usefull response right now.
} }
private synchronized void onSubscription() { private synchronized void addSubscription() {
if (service.isRegistered(this)) { synchronized (upnpLock) {
logger.debug("Checking WeMo GENA subscription for '{}'", this); UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Checking WeMo GENA subscription for '{}'", getThing().getUID());
String subscription = "deviceevent1"; String subscription = DEVICEEVENT;
if (subscriptionState.get(subscription) == null) { if (subscriptionState.get(subscription) == null) {
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription); logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
service.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS); subscription);
subscriptionState.put(subscription, true); localService.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS);
subscriptionState.put(subscription, true);
}
} else {
logger.debug(
"Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
getThing().getUID());
}
} }
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
this);
} }
} }
private synchronized void removeSubscription() { private synchronized void removeSubscription() {
logger.debug("Removing WeMo GENA subscription for '{}'", this); logger.debug("Removing WeMo GENA subscription for '{}'", getThing().getUID());
synchronized (upnpLock) {
if (service.isRegistered(this)) { UpnpIOService localService = service;
String subscription = "deviceevent1"; if (localService != null) {
if (subscriptionState.get(subscription) != null) { if (localService.isRegistered(this)) {
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription); String subscription = DEVICEEVENT;
service.removeSubscription(this, subscription); if (subscriptionState.get(subscription) != null) {
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription);
localService.removeSubscription(this, subscription);
}
subscriptionState = new HashMap<>();
localService.unregisterParticipant(this);
}
} }
subscriptionState = new HashMap<>();
service.unregisterParticipant(this);
}
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("pollingInterval");
if (refreshConfig != null) {
refreshInterval = ((BigDecimal) refreshConfig).intValue();
logger.debug("Setting WemoCoffeeHandler refreshInterval to '{}' seconds", refreshInterval);
}
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, 0, refreshInterval, TimeUnit.SECONDS);
} }
} }
private boolean isUpnpDeviceRegistered() { private boolean isUpnpDeviceRegistered() {
return service.isRegistered(this); UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
} }
@Override @Override
@ -258,154 +296,163 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
* The {@link updateWemoState} polls the actual state of a WeMo CoffeeMaker. * The {@link updateWemoState} polls the actual state of a WeMo CoffeeMaker.
*/ */
protected void updateWemoState() { protected void updateWemoState() {
String action = "GetAttributes"; String localHost = getHost();
String actionService = "deviceevent"; if (localHost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
String content = "<?xml version=\"1.0\"?>" "@text/config-status.error.missing-ip");
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" return;
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:" }
+ action + ">" + "</s:Body>" + "</s:Envelope>"; String actionService = DEVICEACTION;
String wemoURL = getWemoURL(host, actionService);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
URL descriptorURL = service.getDescriptorURL(this); String action = "GetAttributes";
String wemoURL = getWemoURL(descriptorURL, actionService); String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
}
try {
String stringParser = substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>");
if (wemoURL != null) { // Due to Belkins bad response formatting, we need to run this twice.
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); stringParser = unescapeXml(stringParser);
if (wemoCallResponse != null) { stringParser = unescapeXml(stringParser);
try {
String stringParser = substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>");
// Due to Belkins bad response formatting, we need to run this twice. logger.trace("CoffeeMaker response '{}' for device '{}' received", stringParser,
stringParser = unescapeXml(stringParser); getThing().getUID());
stringParser = unescapeXml(stringParser);
logger.trace("CoffeeMaker response '{}' for device '{}' received", stringParser, stringParser = "<data>" + stringParser + "</data>";
getThing().getUID());
stringParser = "<data>" + stringParser + "</data>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// see
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc = db.parse(is);
// see NodeList nodes = doc.getElementsByTagName("attribute");
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
Document doc = db.parse(is); // iterate the attributes
NodeList nodes = doc.getElementsByTagName("attribute"); for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
// iterate the attributes NodeList deviceIndex = element.getElementsByTagName("name");
for (int i = 0; i < nodes.getLength(); i++) { Element line = (Element) deviceIndex.item(0);
Element element = (Element) nodes.item(i); String attributeName = getCharacterDataFromElement(line);
logger.trace("attributeName: {}", attributeName);
NodeList deviceIndex = element.getElementsByTagName("name"); NodeList deviceID = element.getElementsByTagName("value");
Element line = (Element) deviceIndex.item(0); line = (Element) deviceID.item(0);
String attributeName = getCharacterDataFromElement(line); String attributeValue = getCharacterDataFromElement(line);
logger.trace("attributeName: {}", attributeName); logger.trace("attributeValue: {}", attributeValue);
NodeList deviceID = element.getElementsByTagName("value"); switch (attributeName) {
line = (Element) deviceID.item(0); case "Mode":
String attributeValue = getCharacterDataFromElement(line); State newMode = new StringType("Brewing");
logger.trace("attributeValue: {}", attributeValue); State newAttributeValue;
switch (attributeName) { switch (attributeValue) {
case "Mode": case "0":
State newMode = new StringType("Brewing"); updateState(CHANNEL_STATE, OnOffType.ON);
State newAttributeValue; newMode = new StringType("Refill");
updateState(CHANNEL_COFFEEMODE, newMode);
switch (attributeValue) { break;
case "0": case "1":
updateState(CHANNEL_STATE, OnOffType.ON); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("Refill"); newMode = new StringType("PlaceCarafe");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "1": case "2":
updateState(CHANNEL_STATE, OnOffType.OFF); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("PlaceCarafe"); newMode = new StringType("RefillWater");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "2": case "3":
updateState(CHANNEL_STATE, OnOffType.OFF); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("RefillWater"); newMode = new StringType("Ready");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "3": case "4":
updateState(CHANNEL_STATE, OnOffType.OFF); updateState(CHANNEL_STATE, OnOffType.ON);
newMode = new StringType("Ready"); newMode = new StringType("Brewing");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "4": case "5":
updateState(CHANNEL_STATE, OnOffType.ON); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("Brewing"); newMode = new StringType("Brewed");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "5": case "6":
updateState(CHANNEL_STATE, OnOffType.OFF); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("Brewed"); newMode = new StringType("CleaningBrewing");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "6": case "7":
updateState(CHANNEL_STATE, OnOffType.OFF); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("CleaningBrewing"); newMode = new StringType("CleaningSoaking");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "7": case "8":
updateState(CHANNEL_STATE, OnOffType.OFF); updateState(CHANNEL_STATE, OnOffType.OFF);
newMode = new StringType("CleaningSoaking"); newMode = new StringType("BrewFailCarafeRemoved");
updateState(CHANNEL_COFFEEMODE, newMode); updateState(CHANNEL_COFFEEMODE, newMode);
break; break;
case "8": }
updateState(CHANNEL_STATE, OnOffType.OFF); break;
newMode = new StringType("BrewFailCarafeRemoved"); case "ModeTime":
updateState(CHANNEL_COFFEEMODE, newMode); newAttributeValue = new DecimalType(attributeValue);
break; updateState(CHANNEL_MODETIME, newAttributeValue);
} break;
break; case "TimeRemaining":
case "ModeTime": newAttributeValue = new DecimalType(attributeValue);
newAttributeValue = new DecimalType(attributeValue); updateState(CHANNEL_TIMEREMAINING, newAttributeValue);
updateState(CHANNEL_MODETIME, newAttributeValue); break;
break; case "WaterLevelReached":
case "TimeRemaining": newAttributeValue = new DecimalType(attributeValue);
newAttributeValue = new DecimalType(attributeValue); updateState(CHANNEL_WATERLEVELREACHED, newAttributeValue);
updateState(CHANNEL_TIMEREMAINING, newAttributeValue); break;
break; case "CleanAdvise":
case "WaterLevelReached": newAttributeValue = "0".equals(attributeValue) ? OnOffType.OFF : OnOffType.ON;
newAttributeValue = new DecimalType(attributeValue); updateState(CHANNEL_CLEANADVISE, newAttributeValue);
updateState(CHANNEL_WATERLEVELREACHED, newAttributeValue); break;
break; case "FilterAdvise":
case "CleanAdvise": newAttributeValue = "0".equals(attributeValue) ? OnOffType.OFF : OnOffType.ON;
newAttributeValue = attributeValue.equals("0") ? OnOffType.OFF : OnOffType.ON; updateState(CHANNEL_FILTERADVISE, newAttributeValue);
updateState(CHANNEL_CLEANADVISE, newAttributeValue); break;
break; case "Brewed":
case "FilterAdvise": newAttributeValue = getDateTimeState(attributeValue);
newAttributeValue = attributeValue.equals("0") ? OnOffType.OFF : OnOffType.ON; if (newAttributeValue != null) {
updateState(CHANNEL_FILTERADVISE, newAttributeValue); updateState(CHANNEL_BREWED, newAttributeValue);
break; }
case "Brewed": break;
newAttributeValue = getDateTimeState(attributeValue); case "LastCleaned":
if (newAttributeValue != null) { newAttributeValue = getDateTimeState(attributeValue);
updateState(CHANNEL_BREWED, newAttributeValue); if (newAttributeValue != null) {
} updateState(CHANNEL_LASTCLEANED, newAttributeValue);
break; }
case "LastCleaned": break;
newAttributeValue = getDateTimeState(attributeValue);
if (newAttributeValue != null) {
updateState(CHANNEL_LASTCLEANED, newAttributeValue);
}
break;
}
} }
} catch (Exception e) {
logger.error("Failed to parse attributeList for WeMo CoffeMaker '{}'", this.getThing().getUID(),
e);
} }
} catch (Exception e) {
logger.error("Failed to parse attributeList for WeMo CoffeMaker '{}'", this.getThing().getUID(), e);
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -428,13 +475,19 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
return dateTimeState; return dateTimeState;
} }
public static String getCharacterDataFromElement(Element e) { public String getHost() {
Node child = e.getFirstChild(); String localHost = host;
if (child instanceof CharacterData) { if (!localHost.isEmpty()) {
CharacterData cd = (CharacterData) child; return localHost;
return cd.getData();
} }
return "?"; UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
} }
@Override @Override

View File

@ -15,7 +15,6 @@ package org.openhab.binding.wemo.internal.handler;
import static org.openhab.binding.wemo.internal.WemoBindingConstants.*; import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
import static org.openhab.binding.wemo.internal.WemoUtil.*; import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -56,23 +55,20 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_CROCKPOT); public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_CROCKPOT);
private final Map<String, Boolean> subscriptionState = new HashMap<>(); private final Object upnpLock = new Object();
private final Object jobLock = new Object();
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>()); private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private UpnpIOService service; private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall; private WemoHttpCall wemoCall;
private @Nullable ScheduledFuture<?> refreshJob; private String host = "";
private final Runnable refreshRunnable = () -> { private Map<String, Boolean> subscriptionState = new HashMap<>();
updateWemoState();
if (!isUpnpDeviceRegistered()) { private @Nullable ScheduledFuture<?> pollingJob;
logger.debug("WeMo UPnP device {} not yet registered", getUDN());
} else {
onSubscription();
}
};
public WemoCrockpotHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) { public WemoCrockpotHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller); super(thing, wemoHttpCaller);
@ -87,13 +83,19 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
public void initialize() { public void initialize() {
Configuration configuration = getConfig(); Configuration configuration = getConfig();
if (configuration.get("udn") != null) { if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoCrockpotHandler for UDN '{}'", configuration.get("udn")); logger.debug("Initializing WemoCrockpotHandler for UDN '{}'", configuration.get(UDN));
service.registerParticipant(this); UpnpIOService localService = service;
onSubscription(); if (localService != null) {
onUpdate(); localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
logger.debug("Cannot initalize WemoCrockpotHandler. UDN not set."); logger.debug("Cannot initalize WemoCrockpotHandler. UDN not set.");
} }
} }
@ -101,18 +103,60 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
@Override @Override
public void dispose() { public void dispose() {
logger.debug("WeMoCrockpotHandler disposed."); logger.debug("WeMoCrockpotHandler disposed.");
ScheduledFuture<?> job = this.pollingJob;
ScheduledFuture<?> job = refreshJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
removeSubscription(); removeSubscription();
} }
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
synchronized (upnpLock) {
subscriptionState = new HashMap<>();
}
return;
}
updateStatus(ThingStatus.ONLINE);
updateWemoState();
addSubscription();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
}
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID); String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to send command '{}' for device '{}': IP address missing", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to send command '{}' for device '{}': URL cannot be created", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
String mode = "0"; String mode = "0";
String time = null; String time = null;
@ -142,12 +186,12 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
+ "<s:Body>" + "<u:SetCrockpotState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<mode>" + "<s:Body>" + "<u:SetCrockpotState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<mode>"
+ mode + "</mode>" + "<time>" + time + "</time>" + "</u:SetCrockpotState>" + "</s:Body>" + mode + "</mode>" + "<time>" + time + "</time>" + "</u:SetCrockpotState>" + "</s:Body>"
+ "</s:Envelope>"; + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
URL descriptorURL = service.getDescriptorURL(this); if (wemoCallResponse != null && logger.isTraceEnabled()) {
String wemoURL = getWemoURL(descriptorURL, "basicevent"); logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoURL != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
logger.debug("Failed to send command '{}' for device '{}':", command, getThing().getUID(), e); logger.debug("Failed to send command '{}' for device '{}':", command, getThing().getUID(), e);
@ -177,54 +221,55 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
} }
} }
private synchronized void onSubscription() { private synchronized void addSubscription() {
if (service.isRegistered(this)) { synchronized (upnpLock) {
logger.debug("Checking WeMo GENA subscription for '{}'", this); UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Checking WeMo GENA subscription for '{}'", getThing().getUID());
String subscription = "basicevent1"; String subscription = BASICEVENT;
if (subscriptionState.get(subscription) == null) { if (subscriptionState.get(subscription) == null) {
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription); logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
service.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS); subscription);
subscriptionState.put(subscription, true); localService.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS);
subscriptionState.put(subscription, true);
}
} else {
logger.debug(
"Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
getThing().getUID());
}
} }
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
this);
} }
} }
private synchronized void removeSubscription() { private synchronized void removeSubscription() {
logger.debug("Removing WeMo GENA subscription for '{}'", this); synchronized (upnpLock) {
UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Removing WeMo GENA subscription for '{}'", getThing().getUID());
String subscription = BASICEVENT;
if (service.isRegistered(this)) { if (subscriptionState.get(subscription) != null) {
String subscription = "basicevent1"; logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription);
localService.removeSubscription(this, subscription);
if (subscriptionState.get(subscription) != null) { }
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription); subscriptionState.remove(subscription);
service.removeSubscription(this, subscription); localService.unregisterParticipant(this);
}
} }
subscriptionState.remove(subscription);
service.unregisterParticipant(this);
}
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("refresh");
refreshInterval = refreshConfig == null ? DEFAULT_REFRESH_INTERVALL_SECONDS
: ((BigDecimal) refreshConfig).intValue();
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, 0, refreshInterval, TimeUnit.SECONDS);
} }
} }
private boolean isUpnpDeviceRegistered() { private boolean isUpnpDeviceRegistered() {
return service.isRegistered(this); UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
} }
@Override @Override
@ -238,52 +283,61 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
* *
*/ */
protected void updateWemoState() { protected void updateWemoState() {
String action = "GetCrockpotState"; String localHost = getHost();
String actionService = "basicevent"; if (localHost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
String content = "<?xml version=\"1.0\"?>" "@text/config-status.error.missing-ip");
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" return;
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:" }
+ action + ">" + "</s:Body>" + "</s:Envelope>"; String actionService = BASICEVENT;
String wemoURL = getWemoURL(localHost, actionService);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
URL descriptorURL = service.getDescriptorURL(this); String action = "GetCrockpotState";
String wemoURL = getWemoURL(descriptorURL, actionService); String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
if (wemoURL != null) { String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); if (wemoCallResponse != null) {
if (wemoCallResponse != null) { if (logger.isTraceEnabled()) {
logger.trace("State response '{}' for device '{}' received", wemoCallResponse, getThing().getUID()); logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
String mode = substringBetween(wemoCallResponse, "<mode>", "</mode>"); logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
String time = substringBetween(wemoCallResponse, "<time>", "</time>"); logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
String coockedTime = substringBetween(wemoCallResponse, "<coockedTime>", "</coockedTime>"); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
State newMode = new StringType(mode);
State newCoockedTime = DecimalType.valueOf(coockedTime);
switch (mode) {
case "0":
newMode = new StringType("OFF");
break;
case "50":
newMode = new StringType("WARM");
State warmTime = DecimalType.valueOf(time);
updateState(CHANNEL_WARMCOOKTIME, warmTime);
break;
case "51":
newMode = new StringType("LOW");
State lowTime = DecimalType.valueOf(time);
updateState(CHANNEL_LOWCOOKTIME, lowTime);
break;
case "52":
newMode = new StringType("HIGH");
State highTime = DecimalType.valueOf(time);
updateState(CHANNEL_HIGHCOOKTIME, highTime);
break;
}
updateState(CHANNEL_COOKMODE, newMode);
updateState(CHANNEL_COOKEDTIME, newCoockedTime);
} }
String mode = substringBetween(wemoCallResponse, "<mode>", "</mode>");
String time = substringBetween(wemoCallResponse, "<time>", "</time>");
String coockedTime = substringBetween(wemoCallResponse, "<coockedTime>", "</coockedTime>");
State newMode = new StringType(mode);
State newCoockedTime = DecimalType.valueOf(coockedTime);
switch (mode) {
case "0":
newMode = new StringType("OFF");
break;
case "50":
newMode = new StringType("WARM");
State warmTime = DecimalType.valueOf(time);
updateState(CHANNEL_WARMCOOKTIME, warmTime);
break;
case "51":
newMode = new StringType("LOW");
State lowTime = DecimalType.valueOf(time);
updateState(CHANNEL_LOWCOOKTIME, lowTime);
break;
case "52":
newMode = new StringType("HIGH");
State highTime = DecimalType.valueOf(time);
updateState(CHANNEL_HIGHCOOKTIME, highTime);
break;
}
updateState(CHANNEL_COOKMODE, newMode);
updateState(CHANNEL_COOKEDTIME, newCoockedTime);
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
logger.debug("Failed to get actual state for device '{}': {}", getThing().getUID(), e.getMessage(), e); logger.debug("Failed to get actual state for device '{}': {}", getThing().getUID(), e.getMessage(), e);
@ -295,4 +349,19 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
@Override @Override
public void onStatusChanged(boolean status) { public void onStatusChanged(boolean status) {
} }
public String getHost() {
String localHost = host;
if (!localHost.isEmpty()) {
return localHost;
}
UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
}
} }

View File

@ -15,7 +15,6 @@ package org.openhab.binding.wemo.internal.handler;
import static org.openhab.binding.wemo.internal.WemoBindingConstants.*; import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
import static org.openhab.binding.wemo.internal.WemoUtil.*; import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.time.Instant; import java.time.Instant;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@ -62,12 +61,21 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DIMMER); public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DIMMER);
private Map<String, Boolean> subscriptionState = new HashMap<>(); private final Object upnpLock = new Object();
private Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>()); private final Object jobLock = new Object();
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private @Nullable UpnpIOService service;
private UpnpIOService service;
private WemoHttpCall wemoCall; private WemoHttpCall wemoCall;
private String host = "";
private Map<String, Boolean> subscriptionState = new HashMap<>();
private @Nullable ScheduledFuture<?> pollingJob;
private int currentBrightness; private int currentBrightness;
private int currentNightModeBrightness; private int currentNightModeBrightness;
private @Nullable String currentNightModeState; private @Nullable String currentNightModeState;
@ -76,23 +84,6 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
*/ */
private static final int DIM_STEPSIZE = 5; private static final int DIM_STEPSIZE = 5;
private @Nullable ScheduledFuture<?> refreshJob;
private Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
try {
if (!isUpnpDeviceRegistered()) {
logger.debug("WeMo UPnP device {} not yet registered", getUDN());
}
updateWemoState();
onSubscription();
} catch (Exception e) {
logger.debug("Exception during poll : {}", e.getMessage(), e);
}
}
};
public WemoDimmerHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) { public WemoDimmerHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller); super(thing, wemoHttpCaller);
@ -105,12 +96,20 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
@Override @Override
public void initialize() { public void initialize() {
Configuration configuration = getConfig(); Configuration configuration = getConfig();
if (configuration.get("udn") != null) {
logger.debug("Initializing WemoDimmerHandler for UDN '{}'", configuration.get("udn")); if (configuration.get(UDN) != null) {
service.registerParticipant(this); logger.debug("Initializing WemoDimmerHandler for UDN '{}'", configuration.get(UDN));
onSubscription(); UpnpIOService localService = service;
onUpdate(); if (localService != null) {
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
logger.debug("Cannot initalize WemoDimmerHandler. UDN not set."); logger.debug("Cannot initalize WemoDimmerHandler. UDN not set.");
} }
} }
@ -119,15 +118,42 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
public void dispose() { public void dispose() {
logger.debug("WeMoDimmerHandler disposed."); logger.debug("WeMoDimmerHandler disposed.");
ScheduledFuture<?> job = refreshJob; ScheduledFuture<?> job = this.pollingJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
removeSubscription(); removeSubscription();
} }
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
synchronized (upnpLock) {
subscriptionState = new HashMap<>();
}
return;
}
updateStatus(ThingStatus.ONLINE);
updateWemoState();
addSubscription();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
}
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID); logger.trace("Command '{}' received for channel '{}'", command, channelUID);
@ -161,7 +187,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
value = String.valueOf(newBrightness); value = String.valueOf(newBrightness);
currentBrightness = newBrightness; currentBrightness = newBrightness;
argument = "brightness"; argument = "brightness";
if (value.equals("0")) { if ("0".equals(value)) {
value = "1"; value = "1";
argument = "brightness"; argument = "brightness";
setBinaryState(action, argument, "1"); setBinaryState(action, argument, "1");
@ -195,7 +221,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
break; break;
} }
argument = "brightness"; argument = "brightness";
if (value.equals("0")) { if ("0".equals(value)) {
value = "1"; value = "1";
argument = "brightness"; argument = "brightness";
setBinaryState(action, argument, "1"); setBinaryState(action, argument, "1");
@ -251,12 +277,12 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
} }
} }
if (faderSeconds != null && faderEnabled != null) { if (faderSeconds != null && faderEnabled != null) {
if (command.equals(OnOffType.ON)) { if (OnOffType.ON.equals(command)) {
value = "<BinaryState></BinaryState>" + "<Duration></Duration>" + "<EndAction></EndAction>" value = "<BinaryState></BinaryState>" + "<Duration></Duration>" + "<EndAction></EndAction>"
+ "<brightness></brightness>" + "<fader>" + faderSeconds + ":" + timeStamp + ":" + "<brightness></brightness>" + "<fader>" + faderSeconds + ":" + timeStamp + ":"
+ faderEnabled + ":0:0</fader>" + "<UDN></UDN>"; + faderEnabled + ":0:0</fader>" + "<UDN></UDN>";
updateState(CHANNEL_STATE, OnOffType.ON); updateState(CHANNEL_STATE, OnOffType.ON);
} else if (command.equals(OnOffType.OFF)) { } else if (OnOffType.OFF.equals(command)) {
value = "<BinaryState></BinaryState>" + "<Duration></Duration>" + "<EndAction></EndAction>" value = "<BinaryState></BinaryState>" + "<Duration></Duration>" + "<EndAction></EndAction>"
+ "<brightness></brightness>" + "<fader>" + faderSeconds + ":-1:" + faderEnabled + "<brightness></brightness>" + "<fader>" + faderSeconds + ":-1:" + faderEnabled
+ ":0:0</fader>" + "<UDN></UDN>"; + ":0:0</fader>" + "<UDN></UDN>";
@ -268,10 +294,10 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
action = "ConfigureNightMode"; action = "ConfigureNightMode";
argument = "NightModeConfiguration"; argument = "NightModeConfiguration";
String nightModeBrightness = String.valueOf(currentNightModeBrightness); String nightModeBrightness = String.valueOf(currentNightModeBrightness);
if (command.equals(OnOffType.ON)) { if (OnOffType.ON.equals(command)) {
value = "&lt;startTime&gt;0&lt;/startTime&gt; \\n&lt;nightMode&gt;1&lt;/nightMode&gt; \\n&lt;endTime&gt;23400&lt;/endTime&gt; \\n&lt;nightModeBrightness&gt;" value = "&lt;startTime&gt;0&lt;/startTime&gt; \\n&lt;nightMode&gt;1&lt;/nightMode&gt; \\n&lt;endTime&gt;23400&lt;/endTime&gt; \\n&lt;nightModeBrightness&gt;"
+ nightModeBrightness + "&lt;/nightModeBrightness&gt; \\n"; + nightModeBrightness + "&lt;/nightModeBrightness&gt; \\n";
} else if (command.equals(OnOffType.OFF)) { } else if (OnOffType.OFF.equals(command)) {
value = "&lt;startTime&gt;0&lt;/startTime&gt; \\n&lt;nightMode&gt;0&lt;/nightMode&gt; \\n&lt;endTime&gt;23400&lt;/endTime&gt; \\n&lt;nightModeBrightness&gt;" value = "&lt;startTime&gt;0&lt;/startTime&gt; \\n&lt;nightMode&gt;0&lt;/nightMode&gt; \\n&lt;endTime&gt;23400&lt;/endTime&gt; \\n&lt;nightModeBrightness&gt;"
+ nightModeBrightness + "&lt;/nightModeBrightness&gt; \\n"; + nightModeBrightness + "&lt;/nightModeBrightness&gt; \\n";
} }
@ -338,7 +364,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
switch (variable) { switch (variable) {
case "BinaryState": case "BinaryState":
if (oldBinaryState == null || !oldBinaryState.equals(value)) { if (oldBinaryState == null || !oldBinaryState.equals(value)) {
State state = value.equals("0") ? OnOffType.OFF : OnOffType.ON; State state = "0".equals(value) ? OnOffType.OFF : OnOffType.ON;
logger.debug("State '{}' for device '{}' received", state, getThing().getUID()); logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
updateState(CHANNEL_BRIGHTNESS, state); updateState(CHANNEL_BRIGHTNESS, state);
if (state.equals(OnOffType.OFF)) { if (state.equals(OnOffType.OFF)) {
@ -352,7 +378,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
State newBrightnessState = new PercentType(newBrightnessValue); State newBrightnessState = new PercentType(newBrightnessValue);
String binaryState = this.stateMap.get("BinaryState"); String binaryState = this.stateMap.get("BinaryState");
if (binaryState != null) { if (binaryState != null) {
if (binaryState.equals("1")) { if ("1".equals(binaryState)) {
updateState(CHANNEL_BRIGHTNESS, newBrightnessState); updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
} }
} }
@ -385,7 +411,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
} }
break; break;
case "nightMode": case "nightMode":
State nightModeState = value.equals("0") ? OnOffType.OFF : OnOffType.ON; State nightModeState = "0".equals(value) ? OnOffType.OFF : OnOffType.ON;
currentNightModeState = value; currentNightModeState = value;
logger.debug("nightModeState '{}' for device '{}' received", nightModeState, getThing().getUID()); logger.debug("nightModeState '{}' for device '{}' received", nightModeState, getThing().getUID());
updateState(CHANNEL_NIGHTMODE, nightModeState); updateState(CHANNEL_NIGHTMODE, nightModeState);
@ -413,53 +439,50 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
updateState(CHANNEL_NIGHTMODEBRIGHTNESS, nightModeBrightnessState); updateState(CHANNEL_NIGHTMODEBRIGHTNESS, nightModeBrightnessState);
break; break;
} }
} }
} }
private synchronized void onSubscription() { private synchronized void addSubscription() {
if (service.isRegistered(this)) { UpnpIOService localService = service;
logger.debug("Checking WeMo GENA subscription for '{}'", this); if (localService != null) {
String subscription = "basicevent1"; if (localService.isRegistered(this)) {
if (subscriptionState.get(subscription) == null) { logger.debug("Checking WeMo GENA subscription for '{}'", getThing().getUID());
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription); String subscription = BASICEVENT;
service.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS); if (subscriptionState.get(subscription) == null) {
subscriptionState.put(subscription, true); logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
subscription);
localService.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS);
subscriptionState.put(subscription, true);
}
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
getThing().getUID());
} }
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
this);
} }
} }
private synchronized void removeSubscription() { private synchronized void removeSubscription() {
logger.debug("Removing WeMo GENA subscription for '{}'", this); logger.debug("Removing WeMo GENA subscription for '{}'", getThing().getUID());
if (service.isRegistered(this)) { UpnpIOService localService = service;
String subscription = "basicevent1"; if (localService != null) {
if (subscriptionState.get(subscription) != null) { if (localService.isRegistered(this)) {
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription); String subscription = BASICEVENT;
service.removeSubscription(this, subscription); if (subscriptionState.get(subscription) != null) {
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription);
localService.removeSubscription(this, subscription);
}
subscriptionState = new HashMap<>();
localService.unregisterParticipant(this);
} }
subscriptionState = new HashMap<>();
service.unregisterParticipant(this);
}
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("refresh");
if (refreshConfig != null) {
refreshInterval = ((BigDecimal) refreshConfig).intValue();
}
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, 10, refreshInterval, TimeUnit.SECONDS);
} }
} }
private boolean isUpnpDeviceRegistered() { private boolean isUpnpDeviceRegistered() {
return service.isRegistered(this); UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
} }
@Override @Override
@ -473,83 +496,84 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
* *
*/ */
protected void updateWemoState() { protected void updateWemoState() {
String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
String action = "GetBinaryState"; String action = "GetBinaryState";
String variable = null; String variable = null;
String actionService = "basicevent"; String actionService = BASICACTION;
String value = null; String value = null;
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = "<?xml version=\"1.0\"?>" String content = createStateRequestContent(action, actionService);
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:"
+ action + ">" + "</s:Body>" + "</s:Envelope>";
try { try {
URL descriptorURL = service.getDescriptorURL(this); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoURL = getWemoURL(descriptorURL, "basicevent"); if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
if (wemoURL != null) { logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoCallResponse != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("State response '{}' for device '{}' received", wemoCallResponse, getThing().getUID()); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
value = substringBetween(wemoCallResponse, "<BinaryState>", "</BinaryState>");
variable = "BinaryState";
logger.trace("New state '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<brightness>", "</brightness>");
variable = "brightness";
logger.trace("New brightness '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<fader>", "</fader>");
variable = "fader";
logger.trace("New fader value '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
} }
value = substringBetween(wemoCallResponse, "<BinaryState>", "</BinaryState>");
variable = "BinaryState";
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<brightness>", "</brightness>");
variable = "brightness";
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<fader>", "</fader>");
variable = "fader";
this.onValueReceived(variable, value, actionService + "1");
updateStatus(ThingStatus.ONLINE);
} }
} catch (Exception e) { } catch (Exception e) {
logger.debug("Failed to get actual state for device '{}': {}", getThing().getUID(), e.getMessage()); logger.debug("Failed to get actual state for device '{}': {}", getThing().getUID(), e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} }
updateStatus(ThingStatus.ONLINE);
action = "GetNightModeConfiguration"; action = "GetNightModeConfiguration";
variable = null; variable = null;
value = null; value = null;
soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
content = "<?xml version=\"1.0\"?>" content = createStateRequestContent(action, actionService);
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:"
+ action + ">" + "</s:Body>" + "</s:Envelope>";
try { try {
URL descriptorURL = service.getDescriptorURL(this); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoURL = getWemoURL(descriptorURL, "basicevent"); if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
if (wemoURL != null) { logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoCallResponse != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("GetNightModeConfiguration response '{}' for device '{}' received", wemoCallResponse, logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
getThing().getUID());
value = substringBetween(wemoCallResponse, "<startTime>", "</startTime>");
variable = "startTime";
logger.trace("New startTime '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<endTime>", "</endTime>");
variable = "endTime";
logger.trace("New endTime '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<nightMode>", "</nightMode>");
variable = "nightMode";
logger.trace("New nightMode state '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<nightModeBrightness>", "</nightModeBrightness>");
variable = "nightModeBrightness";
logger.trace("New nightModeBrightness '{}' for device '{}' received", value, getThing().getUID());
this.onValueReceived(variable, value, actionService + "1");
} }
value = substringBetween(wemoCallResponse, "<startTime>", "</startTime>");
variable = "startTime";
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<endTime>", "</endTime>");
variable = "endTime";
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<nightMode>", "</nightMode>");
variable = "nightMode";
this.onValueReceived(variable, value, actionService + "1");
value = substringBetween(wemoCallResponse, "<nightModeBrightness>", "</nightModeBrightness>");
variable = "nightModeBrightness";
this.onValueReceived(variable, value, actionService + "1");
updateStatus(ThingStatus.ONLINE);
} }
} catch (Exception e) { } catch (Exception e) {
logger.debug("Failed to get actual NightMode state for device '{}': {}", getThing().getUID(), logger.debug("Failed to get actual NightMode state for device '{}': {}", getThing().getUID(),
e.getMessage()); e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} }
updateStatus(ThingStatus.ONLINE);
} }
public @Nullable State getDateTimeState(String attributeValue) { public @Nullable State getDateTimeState(String attributeValue) {
@ -568,6 +592,20 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
} }
public void setBinaryState(String action, String argument, String value) { public void setBinaryState(String action, String argument, String value) {
String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to set binary state for device '{}': IP address missing", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to set binary state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\""; String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"";
String content = "<?xml version=\"1.0\"?>" String content = "<?xml version=\"1.0\"?>"
@ -575,11 +613,12 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<" + argument + "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<" + argument
+ ">" + value + "</" + argument + ">" + "</u:" + action + ">" + "</s:Body>" + "</s:Envelope>"; + ">" + value + "</" + argument + ">" + "</u:" + action + ">" + "</s:Body>" + "</s:Envelope>";
URL descriptorURL = service.getDescriptorURL(this); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoURL = getWemoURL(descriptorURL, "basicevent"); if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
if (wemoURL != null) { logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
} }
} catch (Exception e) { } catch (Exception e) {
logger.debug("Failed to set binaryState '{}' for device '{}': {}", value, getThing().getUID(), logger.debug("Failed to set binaryState '{}' for device '{}': {}", value, getThing().getUID(),
@ -589,26 +628,55 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
} }
public void setTimerStart(String action, String argument, String value) { public void setTimerStart(String action, String argument, String value) {
String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to set timerStart for device '{}': IP address missing", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to set timerStart for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\""; String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"";
String content = "<?xml version=\"1.0\"?>" String content = "<?xml version=\"1.0\"?>"
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + value + "<s:Body>" + "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + value
+ "</u:SetBinaryState>" + "</s:Body>" + "</s:Envelope>"; + "</u:SetBinaryState>" + "</s:Body>" + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
URL descriptorURL = service.getDescriptorURL(this); if (wemoCallResponse != null && logger.isTraceEnabled()) {
String wemoURL = getWemoURL(descriptorURL, "basicevent"); logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoURL != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
} }
} catch (Exception e) { } catch (Exception e) {
logger.debug("Failed to set binaryState '{}' for device '{}': {}", value, getThing().getUID(), logger.debug("Failed to set timerStart '{}' for device '{}': {}", value, getThing().getUID(),
e.getMessage()); e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} }
} }
public String getHost() {
String localHost = host;
if (!localHost.isEmpty()) {
return localHost;
}
UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
}
@Override @Override
public void onStatusChanged(boolean status) { public void onStatusChanged(boolean status) {
} }

View File

@ -71,32 +71,20 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
.of(THING_TYPE_SOCKET, THING_TYPE_INSIGHT, THING_TYPE_LIGHTSWITCH, THING_TYPE_MOTION) .of(THING_TYPE_SOCKET, THING_TYPE_INSIGHT, THING_TYPE_LIGHTSWITCH, THING_TYPE_MOTION)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
private Map<String, Boolean> subscriptionState = new HashMap<>(); private final Object upnpLock = new Object();
private final Object jobLock = new Object();
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>()); private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
protected UpnpIOService service; private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall; private WemoHttpCall wemoCall;
private @Nullable ScheduledFuture<?> refreshJob; private Map<String, Boolean> subscriptionState = new HashMap<>();
private final Runnable refreshRunnable = new Runnable() { private @Nullable ScheduledFuture<?> pollingJob;
@Override private String host = "";
public void run() {
try {
if (!isUpnpDeviceRegistered()) {
logger.debug("WeMo UPnP device {} not yet registered", getUDN());
}
updateWemoState();
onSubscription();
} catch (Exception e) {
logger.debug("Exception during poll", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
}
}
};
public WemoHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) { public WemoHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller); super(thing, wemoHttpCaller);
@ -111,63 +99,100 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
public void initialize() { public void initialize() {
Configuration configuration = getConfig(); Configuration configuration = getConfig();
if (configuration.get("udn") != null) { if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoHandler for UDN '{}'", configuration.get("udn")); logger.debug("Initializing WemoHandler for UDN '{}'", configuration.get(UDN));
service.registerParticipant(this); UpnpIOService localService = service;
onSubscription(); if (localService != null) {
onUpdate(); localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
logger.debug("Cannot initalize WemoHandler. UDN not set."); logger.debug("Cannot initalize WemoHandler. UDN not set.");
} }
} }
@Override @Override
public void dispose() { public void dispose() {
logger.debug("WeMoHandler disposed."); logger.debug("WemoHandler disposed for thing {}", getThing().getUID());
ScheduledFuture<?> job = refreshJob; ScheduledFuture<?> job = this.pollingJob;
if (job != null && !job.isCancelled()) { if (job != null) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
removeSubscription(); removeSubscription();
} }
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
synchronized (upnpLock) {
subscriptionState = new HashMap<>();
}
return;
}
updateStatus(ThingStatus.ONLINE);
updateWemoState();
addSubscription();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
}
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID); String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to send command '{}' for device '{}': IP address missing", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to send command '{}' for device '{}': URL cannot be created", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
if (command instanceof RefreshType) { if (command instanceof RefreshType) {
try { try {
updateWemoState(); updateWemoState();
} catch (Exception e) { } catch (Exception e) {
logger.debug("Exception during poll", e); logger.debug("Exception during poll", e);
} }
} else if (channelUID.getId().equals(CHANNEL_STATE)) { } else if (CHANNEL_STATE.equals(channelUID.getId())) {
if (command instanceof OnOffType) { if (command instanceof OnOffType) {
try { try {
String binaryState = null; boolean binaryState = OnOffType.ON.equals(command) ? true : false;
if (command.equals(OnOffType.ON)) {
binaryState = "1";
} else if (command.equals(OnOffType.OFF)) {
binaryState = "0";
}
String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\""; String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"";
String content = createBinaryStateContent(binaryState);
String content = "<?xml version=\"1.0\"?>" String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" if (wemoCallResponse != null && logger.isTraceEnabled()) {
+ "<s:Body>" + "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">" logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
+ "<BinaryState>" + binaryState + "</BinaryState>" + "</u:SetBinaryState>" + "</s:Body>" logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
+ "</s:Envelope>"; logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse,
URL descriptorURL = service.getDescriptorURL(this); getThing().getUID());
String wemoURL = getWemoURL(descriptorURL, "basicevent");
if (wemoURL != null) {
wemoCall.executeCall(wemoURL, soapHeader, content);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to send command '{}' for device '{}': {}", command, getThing().getUID(), logger.error("Failed to send command '{}' for device '{}': {}", command, getThing().getUID(),
@ -195,19 +220,23 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
if (!"BinaryState".equals(variable) && !"InsightParams".equals(variable)) {
return;
}
String oldValue = this.stateMap.get(variable);
if (variable != null && value != null) { if (variable != null && value != null) {
this.stateMap.put(variable, value); this.stateMap.put(variable, value);
} }
if (getThing().getThingTypeUID().getId().equals("insight")) { if (value != null && value.length() > 1) {
String insightParams = stateMap.get("InsightParams"); String insightParams = stateMap.get(variable);
if (insightParams != null) { if (insightParams != null) {
String[] splitInsightParams = insightParams.split("\\|"); String[] splitInsightParams = insightParams.split("\\|");
if (splitInsightParams[0] != null) { if (splitInsightParams[0] != null) {
OnOffType binaryState = null; OnOffType binaryState = "0".equals(splitInsightParams[0]) ? OnOffType.OFF : OnOffType.ON;
binaryState = splitInsightParams[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState, logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState,
getThing().getUID()); getThing().getUID());
updateState(CHANNEL_STATE, binaryState); updateState(CHANNEL_STATE, binaryState);
@ -278,106 +307,112 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
getThing().getUID()); getThing().getUID());
updateState(CHANNEL_ENERGYTOTAL, energyTotal); updateState(CHANNEL_ENERGYTOTAL, energyTotal);
BigDecimal standByLimitMW = new BigDecimal(splitInsightParams[10]); if (splitInsightParams.length > 10 && splitInsightParams[10] != null) {
State standByLimit = new QuantityType<>( BigDecimal standByLimitMW = new BigDecimal(splitInsightParams[10]);
standByLimitMW.divide(new BigDecimal(1000), 0, RoundingMode.HALF_UP), Units.WATT); // recalculate State standByLimit = new QuantityType<>(
// mW to W standByLimitMW.divide(new BigDecimal(1000), 0, RoundingMode.HALF_UP), Units.WATT); // recalculate
logger.trace("New InsightParam standByLimit '{}' for device '{}' received", standByLimit, // mW to W
getThing().getUID()); logger.trace("New InsightParam standByLimit '{}' for device '{}' received", standByLimit,
updateState(CHANNEL_STANDBYLIMIT, standByLimit); getThing().getUID());
updateState(CHANNEL_STANDBYLIMIT, standByLimit);
if (currentMW.divide(new BigDecimal(1000), 0, RoundingMode.HALF_UP).intValue() > standByLimitMW if (currentMW.divide(new BigDecimal(1000), 0, RoundingMode.HALF_UP).intValue() > standByLimitMW
.divide(new BigDecimal(1000), 0, RoundingMode.HALF_UP).intValue()) { .divide(new BigDecimal(1000), 0, RoundingMode.HALF_UP).intValue()) {
updateState(CHANNEL_ONSTANDBY, OnOffType.OFF); updateState(CHANNEL_ONSTANDBY, OnOffType.OFF);
} else { } else {
updateState(CHANNEL_ONSTANDBY, OnOffType.ON); updateState(CHANNEL_ONSTANDBY, OnOffType.ON);
}
} }
} }
} else { } else if (value != null && value.length() == 1) {
String binaryState = stateMap.get("BinaryState"); String binaryState = stateMap.get("BinaryState");
if (binaryState != null) { if (binaryState != null) {
State state = binaryState.equals("0") ? OnOffType.OFF : OnOffType.ON; if (oldValue == null || !oldValue.equals(binaryState)) {
logger.debug("State '{}' for device '{}' received", state, getThing().getUID()); State state = "0".equals(binaryState) ? OnOffType.OFF : OnOffType.ON;
if (getThing().getThingTypeUID().getId().equals("motion")) { logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
updateState(CHANNEL_MOTIONDETECTION, state); if ("motion".equals(getThing().getThingTypeUID().getId())) {
if (state.equals(OnOffType.ON)) { updateState(CHANNEL_MOTIONDETECTION, state);
State lastMotionDetected = new DateTimeType(); if (OnOffType.ON.equals(state)) {
updateState(CHANNEL_LASTMOTIONDETECTED, lastMotionDetected); State lastMotionDetected = new DateTimeType();
updateState(CHANNEL_LASTMOTIONDETECTED, lastMotionDetected);
}
} else {
updateState(CHANNEL_STATE, state);
} }
} else {
updateState(CHANNEL_STATE, state);
} }
} }
} }
} }
private synchronized void onSubscription() { private synchronized void addSubscription() {
if (service.isRegistered(this)) { synchronized (upnpLock) {
logger.debug("Checking WeMo GENA subscription for '{}'", this); UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Checking WeMo GENA subscription for '{}'", getThing().getUID());
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); ThingTypeUID thingTypeUID = thing.getThingTypeUID();
String subscription = "basicevent1"; String subscription = BASICEVENT;
if (subscriptionState.get(subscription) == null) { if (subscriptionState.get(subscription) == null) {
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription); logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
service.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS); subscription);
subscriptionState.put(subscription, true); localService.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS);
} subscriptionState.put(subscription, true);
}
if (thingTypeUID.equals(THING_TYPE_INSIGHT)) { if (THING_TYPE_INSIGHT.equals(thingTypeUID)) {
subscription = "insight1"; subscription = INSIGHTEVENT;
if (subscriptionState.get(subscription) == null) { if (subscriptionState.get(subscription) == null) {
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
subscription); subscription);
service.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS); localService.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS);
subscriptionState.put(subscription, true); subscriptionState.put(subscription, true);
}
}
} else {
logger.debug(
"Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
getThing().getUID());
} }
} }
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
this);
} }
} }
private synchronized void removeSubscription() { private synchronized void removeSubscription() {
logger.debug("Removing WeMo GENA subscription for '{}'", this); synchronized (upnpLock) {
UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Removing WeMo GENA subscription for '{}'", getThing().getUID());
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
String subscription = BASICEVENT;
if (service.isRegistered(this)) { if (subscriptionState.get(subscription) != null) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription);
String subscription = "basicevent1"; localService.removeSubscription(this, subscription);
}
if (subscriptionState.get(subscription) != null) { if (THING_TYPE_INSIGHT.equals(thingTypeUID)) {
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription); subscription = INSIGHTEVENT;
service.removeSubscription(this, subscription); if (subscriptionState.get(subscription) != null) {
} logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription);
localService.removeSubscription(this, subscription);
if (thingTypeUID.equals(THING_TYPE_INSIGHT)) { }
subscription = "insight1"; }
if (subscriptionState.get(subscription) != null) { subscriptionState = new HashMap<>();
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription); localService.unregisterParticipant(this);
service.removeSubscription(this, subscription);
} }
} }
subscriptionState = new HashMap<>();
service.unregisterParticipant(this);
}
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("refresh");
if (refreshConfig != null) {
refreshInterval = ((BigDecimal) refreshConfig).intValue();
}
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, 0, refreshInterval, TimeUnit.SECONDS);
} }
} }
private boolean isUpnpDeviceRegistered() { private boolean isUpnpDeviceRegistered() {
return service.isRegistered(this); UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
} }
@Override @Override
@ -385,46 +420,69 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
return (String) this.getThing().getConfiguration().get(UDN); return (String) this.getThing().getConfiguration().get(UDN);
} }
public String getHost() {
String localHost = host;
if (!localHost.isEmpty()) {
return localHost;
}
UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
}
/** /**
* The {@link updateWemoState} polls the actual state of a WeMo device and * The {@link updateWemoState} polls the actual state of a WeMo device and
* calls {@link onValueReceived} to update the statemap and channels.. * calls {@link onValueReceived} to update the statemap and channels..
* *
*/ */
protected void updateWemoState() { protected void updateWemoState() {
String actionService = BASICACTION;
String localhost = getHost();
if (localhost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String action = "GetBinaryState"; String action = "GetBinaryState";
String variable = "BinaryState"; String variable = "BinaryState";
String actionService = "basicevent";
String value = null; String value = null;
if ("insight".equals(getThing().getThingTypeUID().getId())) {
if (getThing().getThingTypeUID().getId().equals("insight")) {
action = "GetInsightParams"; action = "GetInsightParams";
variable = "InsightParams"; variable = "InsightParams";
actionService = "insight"; actionService = INSIGHTACTION;
}
String wemoURL = getWemoURL(localhost, actionService);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
} }
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = "<?xml version=\"1.0\"?>" String content = createStateRequestContent(action, actionService);
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:"
+ action + ">" + "</s:Body>" + "</s:Envelope>";
try { try {
URL descriptorURL = service.getDescriptorURL(this); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoURL = getWemoURL(descriptorURL, actionService); if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
if (wemoURL != null) { logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoCallResponse != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("State response '{}' for device '{}' received", wemoCallResponse, getThing().getUID()); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
if (variable.equals("InsightParams")) { }
value = substringBetween(wemoCallResponse, "<InsightParams>", "</InsightParams>"); if ("InsightParams".equals(variable)) {
} else { value = substringBetween(wemoCallResponse, "<InsightParams>", "</InsightParams>");
value = substringBetween(wemoCallResponse, "<BinaryState>", "</BinaryState>"); } else {
} value = substringBetween(wemoCallResponse, "<BinaryState>", "</BinaryState>");
if (value.length() != 0) { }
logger.trace("New state '{}' for device '{}' received", value, getThing().getUID()); if (value.length() != 0) {
this.onValueReceived(variable, value, actionService + "1"); logger.trace("New state '{}' for device '{}' received", value, getThing().getUID());
} this.onValueReceived(variable, value, actionService + "1");
} }
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -17,7 +17,6 @@ import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -49,10 +48,8 @@ import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -72,22 +69,21 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
private static final int FILTER_LIFE_DAYS = 330; private static final int FILTER_LIFE_DAYS = 330;
private static final int FILTER_LIFE_MINS = FILTER_LIFE_DAYS * 24 * 60; private static final int FILTER_LIFE_MINS = FILTER_LIFE_DAYS * 24 * 60;
private final Map<String, Boolean> subscriptionState = new HashMap<>();
private final Object upnpLock = new Object();
private final Object jobLock = new Object();
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>()); private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private UpnpIOService service; private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall; private WemoHttpCall wemoCall;
private @Nullable ScheduledFuture<?> refreshJob; private String host = "";
private final Runnable refreshRunnable = () -> { private Map<String, Boolean> subscriptionState = new HashMap<>();
if (!isUpnpDeviceRegistered()) {
logger.debug("WeMo UPnP device {} not yet registered", getUDN()); private @Nullable ScheduledFuture<?> pollingJob;
} else {
updateWemoState();
onSubscription();
}
};
public WemoHolmesHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) { public WemoHolmesHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller); super(thing, wemoHttpCaller);
@ -102,13 +98,19 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
public void initialize() { public void initialize() {
Configuration configuration = getConfig(); Configuration configuration = getConfig();
if (configuration.get("udn") != null) { if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoHolmesHandler for UDN '{}'", configuration.get("udn")); logger.debug("Initializing WemoHolmesHandler for UDN '{}'", configuration.get(UDN));
service.registerParticipant(this); UpnpIOService localService = service;
onSubscription(); if (localService != null) {
onUpdate(); localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
logger.debug("Cannot initalize WemoHolmesHandler. UDN not set."); logger.debug("Cannot initalize WemoHolmesHandler. UDN not set.");
} }
} }
@ -117,18 +119,60 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
public void dispose() { public void dispose() {
logger.debug("WemoHolmesHandler disposed."); logger.debug("WemoHolmesHandler disposed.");
ScheduledFuture<?> job = refreshJob; ScheduledFuture<?> job = this.pollingJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
removeSubscription(); removeSubscription();
} }
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
synchronized (upnpLock) {
subscriptionState = new HashMap<>();
}
return;
}
updateStatus(ThingStatus.ONLINE);
updateWemoState();
addSubscription();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
}
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID); String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to send command '{}' for device '{}': IP address missing", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, DEVICEACTION);
if (wemoURL == null) {
logger.error("Failed to send command '{}' for device '{}': URL cannot be created", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
String attribute = null; String attribute = null;
String value = null; String value = null;
@ -236,12 +280,12 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
+ "<attributeList>&lt;attribute&gt;&lt;name&gt;" + attribute + "&lt;/name&gt;&lt;value&gt;" + value + "<attributeList>&lt;attribute&gt;&lt;name&gt;" + attribute + "&lt;/name&gt;&lt;value&gt;" + value
+ "&lt;/value&gt;&lt;/attribute&gt;</attributeList>" + "</u:SetAttributes>" + "</s:Body>" + "&lt;/value&gt;&lt;/attribute&gt;</attributeList>" + "</u:SetAttributes>" + "</s:Body>"
+ "</s:Envelope>"; + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
URL descriptorURL = service.getDescriptorURL(this); if (wemoCallResponse != null && logger.isTraceEnabled()) {
String wemoURL = getWemoURL(descriptorURL, "deviceevent"); logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoURL != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
logger.debug("Failed to send command '{}' for device '{}':", command, getThing().getUID(), e); logger.debug("Failed to send command '{}' for device '{}':", command, getThing().getUID(), e);
@ -270,54 +314,55 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
} }
} }
private synchronized void onSubscription() { private synchronized void addSubscription() {
if (service.isRegistered(this)) { synchronized (upnpLock) {
logger.debug("Checking WeMo GENA subscription for '{}'", this); UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Checking WeMo GENA subscription for '{}'", getThing().getUID());
String subscription = "basicevent1"; String subscription = BASICEVENT;
if (subscriptionState.get(subscription) == null) { if (subscriptionState.get(subscription) == null) {
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription); logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
service.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS); subscription);
subscriptionState.put(subscription, true); localService.addSubscription(this, subscription, SUBSCRIPTION_DURATION_SECONDS);
subscriptionState.put(subscription, true);
}
} else {
logger.debug(
"Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
getThing().getUID());
}
} }
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
this);
} }
} }
private synchronized void removeSubscription() { private synchronized void removeSubscription() {
logger.debug("Removing WeMo GENA subscription for '{}'", this); synchronized (upnpLock) {
UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Removing WeMo GENA subscription for '{}'", getThing().getUID());
String subscription = BASICEVENT;
if (service.isRegistered(this)) { if (subscriptionState.get(subscription) != null) {
String subscription = "basicevent1"; logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription);
localService.removeSubscription(this, subscription);
if (subscriptionState.get(subscription) != null) { }
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), subscription); subscriptionState.remove(subscription);
service.removeSubscription(this, subscription); localService.unregisterParticipant(this);
}
} }
subscriptionState.remove(subscription);
service.unregisterParticipant(this);
}
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("refresh");
refreshInterval = refreshConfig == null ? DEFAULT_REFRESH_INTERVALL_SECONDS
: ((BigDecimal) refreshConfig).intValue();
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, 0, refreshInterval, TimeUnit.SECONDS);
} }
} }
private boolean isUpnpDeviceRegistered() { private boolean isUpnpDeviceRegistered() {
return service.isRegistered(this); UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
} }
@Override @Override
@ -331,164 +376,77 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
* *
*/ */
protected void updateWemoState() { protected void updateWemoState() {
String action = "GetAttributes"; String localHost = getHost();
String actionService = "deviceevent"; if (localHost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
String content = "<?xml version=\"1.0\"?>" "@text/config-status.error.missing-ip");
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" return;
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:" }
+ action + ">" + "</s:Body>" + "</s:Envelope>"; String actionService = DEVICEACTION;
String wemoURL = getWemoURL(localHost, actionService);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
URL descriptorURL = service.getDescriptorURL(this); String action = "GetAttributes";
String wemoURL = getWemoURL(descriptorURL, actionService); String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
}
if (wemoURL != null) { String stringParser = substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>");
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
logger.trace("State response '{}' for device '{}' received", wemoCallResponse, getThing().getUID());
String stringParser = substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>"); // Due to Belkins bad response formatting, we need to run this twice.
stringParser = unescapeXml(stringParser);
stringParser = unescapeXml(stringParser);
// Due to Belkins bad response formatting, we need to run this twice. logger.trace("AirPurifier response '{}' for device '{}' received", stringParser, getThing().getUID());
stringParser = unescapeXml(stringParser);
stringParser = unescapeXml(stringParser);
logger.trace("AirPurifier response '{}' for device '{}' received", stringParser, stringParser = "<data>" + stringParser + "</data>";
getThing().getUID());
stringParser = "<data>" + stringParser + "</data>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// see
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc = db.parse(is);
// see NodeList nodes = doc.getElementsByTagName("attribute");
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
Document doc = db.parse(is); // iterate the attributes
NodeList nodes = doc.getElementsByTagName("attribute"); for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
// iterate the attributes NodeList deviceIndex = element.getElementsByTagName("name");
for (int i = 0; i < nodes.getLength(); i++) { Element line = (Element) deviceIndex.item(0);
Element element = (Element) nodes.item(i); String attributeName = getCharacterDataFromElement(line);
logger.trace("attributeName: {}", attributeName);
NodeList deviceIndex = element.getElementsByTagName("name"); NodeList deviceID = element.getElementsByTagName("value");
Element line = (Element) deviceIndex.item(0); line = (Element) deviceID.item(0);
String attributeName = getCharacterDataFromElement(line); String attributeValue = getCharacterDataFromElement(line);
logger.trace("attributeName: {}", attributeName); logger.trace("attributeValue: {}", attributeValue);
NodeList deviceID = element.getElementsByTagName("value"); State newMode = new StringType();
line = (Element) deviceID.item(0); switch (attributeName) {
String attributeValue = getCharacterDataFromElement(line); case "Mode":
logger.trace("attributeValue: {}", attributeValue); if ("purifier".equals(getThing().getThingTypeUID().getId())) {
State newMode = new StringType();
switch (attributeName) {
case "Mode":
if ("purifier".equals(getThing().getThingTypeUID().getId())) {
switch (attributeValue) {
case "0":
newMode = new StringType("OFF");
break;
case "1":
newMode = new StringType("LOW");
break;
case "2":
newMode = new StringType("MED");
break;
case "3":
newMode = new StringType("HIGH");
break;
case "4":
newMode = new StringType("AUTO");
break;
}
updateState(CHANNEL_PURIFIERMODE, newMode);
} else {
switch (attributeValue) {
case "0":
newMode = new StringType("OFF");
break;
case "1":
newMode = new StringType("FROSTPROTECT");
break;
case "2":
newMode = new StringType("HIGH");
break;
case "3":
newMode = new StringType("LOW");
break;
case "4":
newMode = new StringType("ECO");
break;
}
updateState(CHANNEL_HEATERMODE, newMode);
}
break;
case "Ionizer":
switch (attributeValue) {
case "0":
newMode = OnOffType.OFF;
break;
case "1":
newMode = OnOffType.ON;
break;
}
updateState(CHANNEL_IONIZER, newMode);
break;
case "AirQuality":
switch (attributeValue) {
case "0":
newMode = new StringType("POOR");
break;
case "1":
newMode = new StringType("MODERATE");
break;
case "2":
newMode = new StringType("GOOD");
break;
}
updateState(CHANNEL_AIRQUALITY, newMode);
break;
case "FilterLife":
int filterLife = Integer.valueOf(attributeValue);
if ("purifier".equals(getThing().getThingTypeUID().getId())) {
filterLife = Math.round((filterLife / FILTER_LIFE_MINS) * 100);
} else {
filterLife = Math.round((filterLife / 60480) * 100);
}
updateState(CHANNEL_FILTERLIFE, new PercentType(String.valueOf(filterLife)));
break;
case "ExpiredFilterTime":
switch (attributeValue) {
case "0":
newMode = OnOffType.OFF;
break;
case "1":
newMode = OnOffType.ON;
break;
}
updateState(CHANNEL_EXPIREDFILTERTIME, newMode);
break;
case "FilterPresent":
switch (attributeValue) {
case "0":
newMode = OnOffType.OFF;
break;
case "1":
newMode = OnOffType.ON;
break;
}
updateState(CHANNEL_FILTERPRESENT, newMode);
break;
case "FANMode":
switch (attributeValue) { switch (attributeValue) {
case "0": case "0":
newMode = new StringType("OFF"); newMode = new StringType("OFF");
@ -507,48 +465,143 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
break; break;
} }
updateState(CHANNEL_PURIFIERMODE, newMode); updateState(CHANNEL_PURIFIERMODE, newMode);
break; } else {
case "DesiredHumidity":
switch (attributeValue) { switch (attributeValue) {
case "0": case "0":
newMode = new PercentType("45"); newMode = new StringType("OFF");
break; break;
case "1": case "1":
newMode = new PercentType("50"); newMode = new StringType("FROSTPROTECT");
break; break;
case "2": case "2":
newMode = new PercentType("55"); newMode = new StringType("HIGH");
break; break;
case "3": case "3":
newMode = new PercentType("60"); newMode = new StringType("LOW");
break; break;
case "4": case "4":
newMode = new PercentType("100"); newMode = new StringType("ECO");
break; break;
} }
updateState(CHANNEL_DESIREDHUMIDITY, newMode); updateState(CHANNEL_HEATERMODE, newMode);
break; }
case "CurrentHumidity": break;
newMode = new StringType(attributeValue); case "Ionizer":
updateState(CHANNEL_CURRENTHUMIDITY, newMode); switch (attributeValue) {
break; case "0":
case "Temperature": newMode = OnOffType.OFF;
newMode = new StringType(attributeValue); break;
updateState(CHANNEL_CURRENTTEMP, newMode); case "1":
break; newMode = OnOffType.ON;
case "SetTemperature": break;
newMode = new StringType(attributeValue); }
updateState(CHANNEL_TARGETTEMP, newMode); updateState(CHANNEL_IONIZER, newMode);
break; break;
case "AutoOffTime": case "AirQuality":
newMode = new StringType(attributeValue); switch (attributeValue) {
updateState(CHANNEL_AUTOOFFTIME, newMode); case "0":
break; newMode = new StringType("POOR");
case "TimeRemaining": break;
newMode = new StringType(attributeValue); case "1":
updateState(CHANNEL_HEATINGREMAINING, newMode); newMode = new StringType("MODERATE");
break; break;
} case "2":
newMode = new StringType("GOOD");
break;
}
updateState(CHANNEL_AIRQUALITY, newMode);
break;
case "FilterLife":
int filterLife = Integer.valueOf(attributeValue);
if ("purifier".equals(getThing().getThingTypeUID().getId())) {
filterLife = Math.round((filterLife / FILTER_LIFE_MINS) * 100);
} else {
filterLife = Math.round((filterLife / 60480) * 100);
}
updateState(CHANNEL_FILTERLIFE, new PercentType(String.valueOf(filterLife)));
break;
case "ExpiredFilterTime":
switch (attributeValue) {
case "0":
newMode = OnOffType.OFF;
break;
case "1":
newMode = OnOffType.ON;
break;
}
updateState(CHANNEL_EXPIREDFILTERTIME, newMode);
break;
case "FilterPresent":
switch (attributeValue) {
case "0":
newMode = OnOffType.OFF;
break;
case "1":
newMode = OnOffType.ON;
break;
}
updateState(CHANNEL_FILTERPRESENT, newMode);
break;
case "FANMode":
switch (attributeValue) {
case "0":
newMode = new StringType("OFF");
break;
case "1":
newMode = new StringType("LOW");
break;
case "2":
newMode = new StringType("MED");
break;
case "3":
newMode = new StringType("HIGH");
break;
case "4":
newMode = new StringType("AUTO");
break;
}
updateState(CHANNEL_PURIFIERMODE, newMode);
break;
case "DesiredHumidity":
switch (attributeValue) {
case "0":
newMode = new PercentType("45");
break;
case "1":
newMode = new PercentType("50");
break;
case "2":
newMode = new PercentType("55");
break;
case "3":
newMode = new PercentType("60");
break;
case "4":
newMode = new PercentType("100");
break;
}
updateState(CHANNEL_DESIREDHUMIDITY, newMode);
break;
case "CurrentHumidity":
newMode = new StringType(attributeValue);
updateState(CHANNEL_CURRENTHUMIDITY, newMode);
break;
case "Temperature":
newMode = new StringType(attributeValue);
updateState(CHANNEL_CURRENTTEMP, newMode);
break;
case "SetTemperature":
newMode = new StringType(attributeValue);
updateState(CHANNEL_TARGETTEMP, newMode);
break;
case "AutoOffTime":
newMode = new StringType(attributeValue);
updateState(CHANNEL_AUTOOFFTIME, newMode);
break;
case "TimeRemaining":
newMode = new StringType(attributeValue);
updateState(CHANNEL_HEATINGREMAINING, newMode);
break;
} }
} }
} }
@ -559,13 +612,19 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} }
public static String getCharacterDataFromElement(Element e) { public String getHost() {
Node child = e.getFirstChild(); String localHost = host;
if (child instanceof CharacterData) { if (!localHost.isEmpty()) {
CharacterData cd = (CharacterData) child; return localHost;
return cd.getData();
} }
return "?"; UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
} }
@Override @Override

View File

@ -15,7 +15,6 @@ package org.openhab.binding.wemo.internal.handler;
import static org.openhab.binding.wemo.internal.WemoBindingConstants.*; import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
import static org.openhab.binding.wemo.internal.WemoUtil.*; import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -57,15 +56,21 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
private Map<String, Boolean> subscriptionState = new HashMap<>(); private Map<String, Boolean> subscriptionState = new HashMap<>();
private UpnpIOService service; private final Object upnpLock = new Object();
private WemoHttpCall wemoCall; private final Object jobLock = new Object();
private @Nullable WemoBridgeHandler wemoBridgeHandler; private @Nullable WemoBridgeHandler wemoBridgeHandler;
private @Nullable UpnpIOService service;
private String host = "";
private @Nullable String wemoLightID; private @Nullable String wemoLightID;
private int currentBrightness; private int currentBrightness;
private WemoHttpCall wemoCall;
/** /**
* Set dimming stepsize to 5% * Set dimming stepsize to 5%
*/ */
@ -78,31 +83,15 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
*/ */
private static final int DEFAULT_REFRESH_INITIAL_DELAY = 15; private static final int DEFAULT_REFRESH_INITIAL_DELAY = 15;
private @Nullable ScheduledFuture<?> refreshJob; private @Nullable ScheduledFuture<?> pollingJob;
private final Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
try {
if (!isUpnpDeviceRegistered()) {
logger.debug("WeMo UPnP device {} not yet registered", getUDN());
}
getDeviceState();
onSubscription();
} catch (Exception e) {
logger.debug("Exception during poll", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
}
}
};
public WemoLightHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpcaller) { public WemoLightHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpcaller) {
super(thing, wemoHttpcaller); super(thing, wemoHttpcaller);
this.service = upnpIOService; this.service = upnpIOService;
this.wemoCall = wemoHttpcaller; this.wemoCall = wemoHttpcaller;
logger.debug("Creating a WemoLightHandler for thing '{}'", getThing().getUID());
} }
@Override @Override
@ -112,9 +101,14 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
final Bridge bridge = getBridge(); final Bridge bridge = getBridge();
if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) { if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
UpnpIOService localService = service;
if (localService != null) {
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, DEFAULT_REFRESH_INITIAL_DELAY,
DEFAULT_REFRESH_INTERVALL_SECONDS, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
onSubscription();
onUpdate();
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE);
} }
@ -124,15 +118,13 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) { public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
if (bridgeStatusInfo.getStatus().equals(ThingStatus.ONLINE)) { if (bridgeStatusInfo.getStatus().equals(ThingStatus.ONLINE)) {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
onSubscription();
onUpdate();
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE);
ScheduledFuture<?> job = refreshJob; ScheduledFuture<?> job = this.pollingJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
} }
} }
@ -140,11 +132,11 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
public void dispose() { public void dispose() {
logger.debug("WeMoLightHandler disposed."); logger.debug("WeMoLightHandler disposed.");
ScheduledFuture<?> job = refreshJob; ScheduledFuture<?> job = this.pollingJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
removeSubscription(); removeSubscription();
} }
@ -164,8 +156,52 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
return this.wemoBridgeHandler; return this.wemoBridgeHandler;
} }
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
synchronized (upnpLock) {
subscriptionState = new HashMap<>();
}
return;
}
updateStatus(ThingStatus.ONLINE);
getDeviceState();
addSubscription();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
}
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to send command '{}' for device '{}': IP address missing", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to send command '{}' for device '{}': URL cannot be created", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
if (command instanceof RefreshType) { if (command instanceof RefreshType) {
try { try {
getDeviceState(); getDeviceState();
@ -239,27 +275,32 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
break; break;
} }
try { try {
String soapHeader = "\"urn:Belkin:service:bridge:1#SetDeviceStatus\""; if (capability != null && value != null) {
String content = "<?xml version=\"1.0\"?>" String soapHeader = "\"urn:Belkin:service:bridge:1#SetDeviceStatus\"";
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" String content = "<?xml version=\"1.0\"?>"
+ "<s:Body>" + "<u:SetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<DeviceStatusList>" + "<s:Body>" + "<u:SetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">"
+ "&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;DeviceStatus&gt;&lt;DeviceID&gt;" + "<DeviceStatusList>"
+ wemoLightID + "&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;DeviceStatus&gt;&lt;DeviceID&gt;"
+ "&lt;/DeviceID&gt;&lt;IsGroupAction&gt;NO&lt;/IsGroupAction&gt;&lt;CapabilityID&gt;" + wemoLightID
+ capability + "&lt;/CapabilityID&gt;&lt;CapabilityValue&gt;" + value + "&lt;/DeviceID&gt;&lt;IsGroupAction&gt;NO&lt;/IsGroupAction&gt;&lt;CapabilityID&gt;"
+ "&lt;/CapabilityValue&gt;&lt;/DeviceStatus&gt;" + "</DeviceStatusList>" + capability + "&lt;/CapabilityID&gt;&lt;CapabilityValue&gt;" + value
+ "</u:SetDeviceStatus>" + "</s:Body>" + "</s:Envelope>"; + "&lt;/CapabilityValue&gt;&lt;/DeviceStatus&gt;" + "</DeviceStatusList>"
+ "</u:SetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
URL descriptorURL = service.getDescriptorURL(this);
String wemoURL = getWemoURL(descriptorURL, "bridge");
if (wemoURL != null && capability != null && value != null) {
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) { if (wemoCallResponse != null) {
if (capability.equals("10008")) { if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader,
getThing().getUID());
logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse,
getThing().getUID());
}
if ("10008".equals(capability)) {
OnOffType binaryState = null; OnOffType binaryState = null;
binaryState = value.equals("0") ? OnOffType.OFF : OnOffType.ON; binaryState = "0".equals(value) ? OnOffType.OFF : OnOffType.ON;
updateState(CHANNEL_STATE, binaryState); updateState(CHANNEL_STATE, binaryState);
} }
} }
@ -285,7 +326,21 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
* channel states. * channel states.
*/ */
public void getDeviceState() { public void getDeviceState() {
String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
logger.debug("Request actual state for LightID '{}'", wemoLightID); logger.debug("Request actual state for LightID '{}'", wemoLightID);
String wemoURL = getWemoURL(localHost, BRIDGEACTION);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
String soapHeader = "\"urn:Belkin:service:bridge:1#GetDeviceStatus\""; String soapHeader = "\"urn:Belkin:service:bridge:1#GetDeviceStatus\"";
String content = "<?xml version=\"1.0\"?>" String content = "<?xml version=\"1.0\"?>"
@ -293,31 +348,32 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
+ "<s:Body>" + "<u:GetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceIDs>" + "<s:Body>" + "<u:GetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceIDs>"
+ wemoLightID + "</DeviceIDs>" + "</u:GetDeviceStatus>" + "</s:Body>" + "</s:Envelope>"; + wemoLightID + "</DeviceIDs>" + "</u:GetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
URL descriptorURL = service.getDescriptorURL(this); String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoURL = getWemoURL(descriptorURL, "bridge"); if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
if (wemoURL != null) { logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
if (wemoCallResponse != null) { logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
wemoCallResponse = unescapeXml(wemoCallResponse); logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
String response = substringBetween(wemoCallResponse, "<CapabilityValue>", "</CapabilityValue>"); }
logger.trace("wemoNewLightState = {}", response); wemoCallResponse = unescapeXml(wemoCallResponse);
String[] splitResponse = response.split(","); String response = substringBetween(wemoCallResponse, "<CapabilityValue>", "</CapabilityValue>");
if (splitResponse[0] != null) { logger.trace("wemoNewLightState = {}", response);
OnOffType binaryState = null; String[] splitResponse = response.split(",");
binaryState = splitResponse[0].equals("0") ? OnOffType.OFF : OnOffType.ON; if (splitResponse[0] != null) {
updateState(CHANNEL_STATE, binaryState); OnOffType binaryState = null;
} binaryState = "0".equals(splitResponse[0]) ? OnOffType.OFF : OnOffType.ON;
if (splitResponse[1] != null) { updateState(CHANNEL_STATE, binaryState);
String splitBrightness[] = splitResponse[1].split(":"); }
if (splitBrightness[0] != null) { if (splitResponse[1] != null) {
int newBrightnessValue = Integer.valueOf(splitBrightness[0]); String splitBrightness[] = splitResponse[1].split(":");
int newBrightness = Math.round(newBrightnessValue * 100 / 255); if (splitBrightness[0] != null) {
logger.trace("newBrightness = {}", newBrightness); int newBrightnessValue = Integer.valueOf(splitBrightness[0]);
State newBrightnessState = new PercentType(newBrightness); int newBrightness = Math.round(newBrightnessValue * 100 / 255);
updateState(CHANNEL_BRIGHTNESS, newBrightnessState); logger.trace("newBrightness = {}", newBrightness);
currentBrightness = newBrightness; State newBrightnessState = new PercentType(newBrightness);
} updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
currentBrightness = newBrightness;
} }
} }
} }
@ -339,7 +395,7 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
switch (capabilityId) { switch (capabilityId) {
case "10006": case "10006":
OnOffType binaryState = null; OnOffType binaryState = null;
binaryState = newValue.equals("0") ? OnOffType.OFF : OnOffType.ON; binaryState = "0".equals(newValue) ? OnOffType.OFF : OnOffType.ON;
updateState(CHANNEL_STATE, binaryState); updateState(CHANNEL_STATE, binaryState);
break; break;
case "10008": case "10008":
@ -359,51 +415,66 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
public void onStatusChanged(boolean status) { public void onStatusChanged(boolean status) {
} }
private synchronized void onSubscription() { private synchronized void addSubscription() {
if (service.isRegistered(this)) { synchronized (upnpLock) {
logger.debug("Checking WeMo GENA subscription for '{}'", this); UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Checking WeMo GENA subscription for '{}'", getThing().getUID());
if (subscriptionState.get(SUBSCRIPTION) == null) { if (subscriptionState.get(SUBSCRIPTION) == null) {
logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), SUBSCRIPTION); logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(),
service.addSubscription(this, SUBSCRIPTION, SUBSCRIPTION_DURATION_SECONDS); SUBSCRIPTION);
subscriptionState.put(SUBSCRIPTION, true); localService.addSubscription(this, SUBSCRIPTION, SUBSCRIPTION_DURATION_SECONDS);
subscriptionState.put(SUBSCRIPTION, true);
}
} else {
logger.debug(
"Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
getThing().getUID());
}
} }
} else {
logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE",
this);
} }
} }
private synchronized void removeSubscription() { private synchronized void removeSubscription() {
if (service.isRegistered(this)) { synchronized (upnpLock) {
logger.debug("Removing WeMo GENA subscription for '{}'", this); UpnpIOService localService = service;
if (localService != null) {
if (localService.isRegistered(this)) {
logger.debug("Removing WeMo GENA subscription for '{}'", getThing().getUID());
if (subscriptionState.get(SUBSCRIPTION) != null) { if (subscriptionState.get(SUBSCRIPTION) != null) {
logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), SUBSCRIPTION); logger.debug("WeMo {}: Unsubscribing from service {}...", getUDN(), SUBSCRIPTION);
service.removeSubscription(this, SUBSCRIPTION); localService.removeSubscription(this, SUBSCRIPTION);
}
subscriptionState = new HashMap<>();
localService.unregisterParticipant(this);
}
} }
subscriptionState = new HashMap<>();
service.unregisterParticipant(this);
}
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("refresh");
if (refreshConfig != null) {
refreshInterval = ((BigDecimal) refreshConfig).intValue();
}
logger.trace("Start polling job for LightID '{}'", wemoLightID);
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, DEFAULT_REFRESH_INITIAL_DELAY,
refreshInterval, TimeUnit.SECONDS);
} }
} }
private boolean isUpnpDeviceRegistered() { private boolean isUpnpDeviceRegistered() {
return service.isRegistered(this); UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
public String getHost() {
String localHost = host;
if (!localHost.isEmpty()) {
return localHost;
}
UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
} }
} }

View File

@ -16,7 +16,6 @@ import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
import static org.openhab.binding.wemo.internal.WemoUtil.*; import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
@ -43,10 +42,8 @@ import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@ -63,23 +60,15 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_MAKER); public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_MAKER);
private UpnpIOService service; private final Object jobLock = new Object();
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall; private WemoHttpCall wemoCall;
private @Nullable ScheduledFuture<?> refreshJob; private String host = "";
private final Runnable refreshRunnable = new Runnable() { private @Nullable ScheduledFuture<?> pollingJob;
@Override
public void run() {
try {
updateWemoState();
} catch (Exception e) {
logger.debug("Exception during poll", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
}
}
};
public WemoMakerHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpcaller) { public WemoMakerHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpcaller) {
super(thing, wemoHttpcaller); super(thing, wemoHttpcaller);
@ -94,11 +83,19 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
public void initialize() { public void initialize() {
Configuration configuration = getConfig(); Configuration configuration = getConfig();
if (configuration.get("udn") != null) { if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoMakerHandler for UDN '{}'", configuration.get("udn")); logger.debug("Initializing WemoMakerHandler for UDN '{}'", configuration.get(UDN));
onUpdate(); UpnpIOService localService = service;
if (localService != null) {
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/config-status.error.missing-udn");
logger.debug("Cannot initalize WemoMakerHandler. UDN not set."); logger.debug("Cannot initalize WemoMakerHandler. UDN not set.");
} }
} }
@ -107,17 +104,59 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
public void dispose() { public void dispose() {
logger.debug("WeMoMakerHandler disposed."); logger.debug("WeMoMakerHandler disposed.");
ScheduledFuture<?> job = refreshJob; ScheduledFuture<?> job = this.pollingJob;
if (job != null && !job.isCancelled()) { if (job != null && !job.isCancelled()) {
job.cancel(true); job.cancel(true);
} }
refreshJob = null; this.pollingJob = null;
UpnpIOService localService = service;
if (localService != null) {
localService.unregisterParticipant(this);
}
}
private void poll() {
synchronized (jobLock) {
if (pollingJob == null) {
return;
}
try {
logger.debug("Polling job");
host = getHost();
// Check if the Wemo device is set in the UPnP service registry
// If not, set the thing state to ONLINE/CONFIG-PENDING and wait for the next poll
if (!isUpnpDeviceRegistered()) {
logger.debug("UPnP device {} not yet registered", getUDN());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
return;
}
updateStatus(ThingStatus.ONLINE);
updateWemoState();
} catch (Exception e) {
logger.debug("Exception during poll: {}", e.getMessage(), e);
}
}
} }
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID); String localHost = getHost();
if (localHost.isEmpty()) {
logger.error("Failed to send command '{}' for device '{}': IP address missing", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-ip");
return;
}
String wemoURL = getWemoURL(localHost, BASICACTION);
if (wemoURL == null) {
logger.error("Failed to send command '{}' for device '{}': URL cannot be created", command,
getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
if (command instanceof RefreshType) { if (command instanceof RefreshType) {
try { try {
updateWemoState(); updateWemoState();
@ -127,27 +166,16 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
} else if (channelUID.getId().equals(CHANNEL_RELAY)) { } else if (channelUID.getId().equals(CHANNEL_RELAY)) {
if (command instanceof OnOffType) { if (command instanceof OnOffType) {
try { try {
String binaryState = null; boolean binaryState = OnOffType.ON.equals(command) ? true : false;
if (command.equals(OnOffType.ON)) {
binaryState = "1";
} else if (command.equals(OnOffType.OFF)) {
binaryState = "0";
}
String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\""; String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"";
String content = createBinaryStateContent(binaryState);
String content = "<?xml version=\"1.0\"?>" String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" if (wemoCallResponse != null && logger.isTraceEnabled()) {
+ "<s:Body>" + "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">" logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
+ "<BinaryState>" + binaryState + "</BinaryState>" + "</u:SetBinaryState>" + "</s:Body>" logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
+ "</s:Envelope>"; logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse,
URL descriptorURL = service.getDescriptorURL(this); getThing().getUID());
String wemoURL = getWemoURL(descriptorURL, "basicevent");
if (wemoURL != null) {
wemoCall.executeCall(wemoURL, soapHeader, content);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to send command '{}' for device '{}' ", command, getThing().getUID(), e); logger.error("Failed to send command '{}' for device '{}' ", command, getThing().getUID(), e);
@ -156,25 +184,12 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
} }
} }
@SuppressWarnings("unused") private boolean isUpnpDeviceRegistered() {
private synchronized void onSubscription() { UpnpIOService localService = service;
} if (localService != null) {
return localService.isRegistered(this);
@SuppressWarnings("unused")
private synchronized void removeSubscription() {
}
private synchronized void onUpdate() {
ScheduledFuture<?> job = refreshJob;
if (job == null || job.isCancelled()) {
Configuration config = getThing().getConfiguration();
int refreshInterval = DEFAULT_REFRESH_INTERVALL_SECONDS;
Object refreshConfig = config.get("refresh");
if (refreshConfig != null) {
refreshInterval = ((BigDecimal) refreshConfig).intValue();
}
refreshJob = scheduler.scheduleWithFixedDelay(refreshRunnable, 0, refreshInterval, TimeUnit.SECONDS);
} }
return false;
} }
@Override @Override
@ -186,81 +201,91 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
* The {@link updateWemoState} polls the actual state of a WeMo Maker. * The {@link updateWemoState} polls the actual state of a WeMo Maker.
*/ */
protected void updateWemoState() { protected void updateWemoState() {
String action = "GetAttributes"; String localHost = getHost();
String actionService = "deviceevent"; if (localHost.isEmpty()) {
logger.error("Failed to get actual state for device '{}': IP address missing", getThing().getUID());
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\""; updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
String content = "<?xml version=\"1.0\"?>" "@text/config-status.error.missing-ip");
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" return;
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:" }
+ action + ">" + "</s:Body>" + "</s:Envelope>"; String actionService = DEVICEACTION;
String wemoURL = getWemoURL(localHost, actionService);
if (wemoURL == null) {
logger.error("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/config-status.error.missing-url");
return;
}
try { try {
URL descriptorURL = service.getDescriptorURL(this); String action = "GetAttributes";
String wemoURL = getWemoURL(descriptorURL, actionService); String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
logger.trace("wemoCall with content '{}' for device '{}'", content, getThing().getUID());
logger.trace("wemoCall with response '{}' for device '{}'", wemoCallResponse, getThing().getUID());
}
try {
String stringParser = substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>");
logger.trace("Escaped Maker response for device '{}' :", getThing().getUID());
logger.trace("'{}'", stringParser);
if (wemoURL != null) { // Due to Belkins bad response formatting, we need to run this twice.
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content); stringParser = unescapeXml(stringParser);
if (wemoCallResponse != null) { stringParser = unescapeXml(stringParser);
try { logger.trace("Maker response '{}' for device '{}' received", stringParser, getThing().getUID());
String stringParser = substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>");
logger.trace("Escaped Maker response for device '{}' :", getThing().getUID());
logger.trace("'{}'", stringParser);
// Due to Belkins bad response formatting, we need to run this twice. stringParser = "<data>" + stringParser + "</data>";
stringParser = unescapeXml(stringParser);
stringParser = unescapeXml(stringParser);
logger.trace("Maker response '{}' for device '{}' received", stringParser, getThing().getUID());
stringParser = "<data>" + stringParser + "</data>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// see
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc = db.parse(is);
// see NodeList nodes = doc.getElementsByTagName("attribute");
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
Document doc = db.parse(is); // iterate the attributes
NodeList nodes = doc.getElementsByTagName("attribute"); for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
// iterate the attributes NodeList deviceIndex = element.getElementsByTagName("name");
for (int i = 0; i < nodes.getLength(); i++) { Element line = (Element) deviceIndex.item(0);
Element element = (Element) nodes.item(i); String attributeName = getCharacterDataFromElement(line);
logger.trace("attributeName: {}", attributeName);
NodeList deviceIndex = element.getElementsByTagName("name"); NodeList deviceID = element.getElementsByTagName("value");
Element line = (Element) deviceIndex.item(0); line = (Element) deviceID.item(0);
String attributeName = getCharacterDataFromElement(line); String attributeValue = getCharacterDataFromElement(line);
logger.trace("attributeName: {}", attributeName); logger.trace("attributeValue: {}", attributeValue);
NodeList deviceID = element.getElementsByTagName("value"); switch (attributeName) {
line = (Element) deviceID.item(0); case "Switch":
String attributeValue = getCharacterDataFromElement(line); State relayState = "0".equals(attributeValue) ? OnOffType.OFF : OnOffType.ON;
logger.trace("attributeValue: {}", attributeValue); logger.debug("New relayState '{}' for device '{}' received", relayState,
getThing().getUID());
switch (attributeName) { updateState(CHANNEL_RELAY, relayState);
case "Switch": break;
State relayState = attributeValue.equals("0") ? OnOffType.OFF : OnOffType.ON; case "Sensor":
logger.debug("New relayState '{}' for device '{}' received", relayState, State sensorState = "1".equals(attributeValue) ? OnOffType.OFF : OnOffType.ON;
getThing().getUID()); logger.debug("New sensorState '{}' for device '{}' received", sensorState,
updateState(CHANNEL_RELAY, relayState); getThing().getUID());
break; updateState(CHANNEL_SENSOR, sensorState);
case "Sensor": break;
State sensorState = attributeValue.equals("1") ? OnOffType.OFF : OnOffType.ON;
logger.debug("New sensorState '{}' for device '{}' received", sensorState,
getThing().getUID());
updateState(CHANNEL_SENSOR, sensorState);
break;
}
} }
} catch (Exception e) {
logger.error("Failed to parse attributeList for WeMo Maker '{}'", this.getThing().getUID(), e);
} }
} catch (Exception e) {
logger.error("Failed to parse attributeList for WeMo Maker '{}'", this.getThing().getUID(), e);
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -268,13 +293,19 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
} }
} }
public static String getCharacterDataFromElement(Element e) { public String getHost() {
Node child = e.getFirstChild(); String localHost = host;
if (child instanceof CharacterData) { if (!localHost.isEmpty()) {
CharacterData cd = (CharacterData) child; return localHost;
return cd.getData();
} }
return "?"; UpnpIOService localService = service;
if (localService != null) {
URL descriptorURL = localService.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
return "";
} }
@Override @Override

View File

@ -124,3 +124,9 @@ channel-type.wemo.timespan.label = Usage Timespan (s)
channel-type.wemo.timespan.description = Time used to measure average usage channel-type.wemo.timespan.description = Time used to measure average usage
channel-type.wemo.waterLevelReached.label = WaterLevelReached channel-type.wemo.waterLevelReached.label = WaterLevelReached
channel-type.wemo.waterLevelReached.description = Indicates if the WeMo Coffee Maker needs to be refilled channel-type.wemo.waterLevelReached.description = Indicates if the WeMo Coffee Maker needs to be refilled
# Config status messages
config-status.pending.device-not-registered = UPnP device is not registered yet.
config-status.error.missing-udn = UDN of the WeMo device is missing.
config-status.error.missing-ip = IP address of the WeMo device is missing.
config-status.error.missing-url = URL for the WeMo device cannot be created.

View File

@ -17,7 +17,6 @@
<label>Unique Device Name</label> <label>Unique Device Name</label>
<description>The UDN identifies the WeMo Device</description> <description>The UDN identifies the WeMo Device</description>
</parameter> </parameter>
</config-description> </config-description>
</thing-type> </thing-type>
@ -176,7 +175,92 @@
<label>Unique Device Name</label> <label>Unique Device Name</label>
<description>The UDN identifies the WeMo Device</description> <description>The UDN identifies the WeMo Device</description>
</parameter> </parameter>
</config-description>
</thing-type>
<thing-type id="Crockpot">
<label>Crock-Pot Slow Cooker</label>
<description>Crock-Pot Smart Slow Cooker with WeMo</description>
<channels>
<channel id="cookMode" typeId="cookMode"/>
<channel id="warmCookTime" typeId="warmCookTime"/>
<channel id="lowCookTime" typeId="lowCookTime"/>
<channel id="highCookTime" typeId="highCookTime"/>
<channel id="cookedTime" typeId="cookedTime"/>
</channels>
<config-description>
<parameter name="udn" type="text">
<label>Unique Device Name</label>
<description>The UDN identifies the WeMo Device</description>
<required>true</required>
</parameter>
</config-description>
</thing-type>
<thing-type id="Purifier">
<label>Holmes Air Purifier</label>
<description>Holmes Smart Air Purifier with WeMo</description>
<channels>
<channel id="purifierMode" typeId="purifierMode"/>
<channel id="airQuality" typeId="airQuality"/>
<channel id="ionizer" typeId="ionizer"/>
<channel id="filterLife" typeId="filterLife"/>
<channel id="expiredFilterTime" typeId="expiredFilterTime"/>
<channel id="filterPresent" typeId="filterPresent"/>
</channels>
<config-description>
<parameter name="udn" type="text">
<label>Unique Device Name</label>
<description>The UDN identifies the WeMo Device</description>
<required>true</required>
</parameter>
</config-description>
</thing-type>
<thing-type id="Humidifier">
<label>Holmes Humidifier</label>
<description>Holmes Smart Humidifier with WeMo</description>
<channels>
<channel id="humidifierMode" typeId="humidifierMode"/>
<channel id="desiredHumidity" typeId="desiredHumidity"/>
<channel id="currentHumidity" typeId="currentHumidity"/>
<channel id="waterLEvel" typeId="waterLEvel"/>
<channel id="filterLife" typeId="filterLife"/>
<channel id="expiredFilterTime" typeId="expiredFilterTime"/>
</channels>
<config-description>
<parameter name="udn" type="text">
<label>Unique Device Name</label>
<description>The UDN identifies the WeMo Device</description>
<required>true</required>
</parameter>
</config-description>
</thing-type>
<thing-type id="Heater">
<label>Holmes Heater</label>
<description>Holmes Smart Heater with WeMo</description>
<channels>
<channel id="heaterMode" typeId="heaterMode"/>
<channel id="currentTemperature" typeId="currentTemperature"/>
<channel id="targetTemperature" typeId="targetTemperature"/>
<channel id="autoOffTime" typeId="autoOffTime"/>
<channel id="heatingRemaining" typeId="heatingRemaining"/>
</channels>
<config-description>
<parameter name="udn" type="text">
<label>Unique Device Name</label>
<description>The UDN identifies the WeMo Device</description>
<required>true</required>
</parameter>
</config-description> </config-description>
</thing-type> </thing-type>
@ -418,4 +502,187 @@
<description>Allows setting the brightness of Night Mode</description> <description>Allows setting the brightness of Night Mode</description>
</channel-type> </channel-type>
<channel-type id="cookMode">
<item-type>String</item-type>
<label>Cooking Mode</label>
<description>Shows the operation mode of a WeMo CrockPot</description>
<state readOnly="false">
<options>
<option value="OFF">Not cooking</option>
<option value="WARM">Warming</option>
<option value="LOW">Low cooking</option>
<option value="HIGH">High cooking</option>
</options>
</state>
</channel-type>
<channel-type id="warmCookTime">
<item-type>Number</item-type>
<label>WarmCookTime</label>
<description>Shows the timer settings for warm cooking mode</description>
</channel-type>
<channel-type id="lowCookTime">
<item-type>Number</item-type>
<label>LowCookTime</label>
<description>Shows the timer settings for low cooking mode</description>
</channel-type>
<channel-type id="highCookTime">
<item-type>Number</item-type>
<label>HighCookTime</label>
<description>Shows the timer settings for high cooking mode</description>
</channel-type>
<channel-type id="cookedTime">
<item-type>Number</item-type>
<label>CookedTime</label>
<description>Shows the elapsed cooking time</description>
</channel-type>
<channel-type id="purifierMode">
<item-type>String</item-type>
<label>Operation Mode</label>
<description>Shows the operation mode of a WeMo enabled Holmes Air Purifier</description>
<state readOnly="false">
<options>
<option value="OFF">Not Running</option>
<option value="LOW">Running at low level</option>
<option value="MED">Running at medium level</option>
<option value="HIGH">Running at high level</option>
<option value="AUTO">Running in auto mode</option>
</options>
</state>
</channel-type>
<channel-type id="airQiality">
<item-type>String</item-type>
<label>Air Quality</label>
<description>Shows the air quality measured by a WeMo enabled Holmes Air Purifier</description>
<state readOnly="true">
<options>
<option value="POOR"></option>
<option value="MODERATE"></option>
<option value="GOOD"></option>
</options>
</state>
</channel-type>
<channel-type id="ionizer">
<item-type>Switch</item-type>
<label>Ionizer</label>
<description>Switches ionization ON or OFF</description>
</channel-type>
<channel-type id="filterLife">
<item-type>Number</item-type>
<label>Filter Life</label>
<description>Shows the remaining lifetime percentage of the air filter</description>
</channel-type>
<channel-type id="filterExpired">
<item-type>Switch</item-type>
<label>Filter Time expired</label>
<description>Indicates whether the air Filter needs to be replaced</description>
<state readOnly="true"></state>
</channel-type>
<channel-type id="filterPresent">
<item-type>Switch</item-type>
<label>Filter is present</label>
<description>Indicates whether the air Filter is present</description>
<state readOnly="true"></state>
</channel-type>
<channel-type id="humidifierMode">
<item-type>String</item-type>
<label>Operation Mode</label>
<description>Shows the operation mode of a WeMo enabled Holmes Humidifier</description>
<state readOnly="false">
<options>
<option value="OFF">Not Running</option>
<option value="MIN">Running at min level</option>
<option value="LOW">Running at low level</option>
<option value="MED">Running at medium level</option>
<option value="HIGH">Running at high level</option>
<option value="MAX">Running in max level</option>
</options>
</state>
</channel-type>
<channel-type id="currentHumidity">
<item-type>Number</item-type>
<label>Current Humidity</label>
<description>Shows the current humidity of a WeMo enabled Holmes Humidifier</description>
</channel-type>
<channel-type id="desiredHumidity">
<item-type>Number</item-type>
<label>Target Humidity</label>
<description>Shows the target humidity of a WeMo enabled Holmes Humidifier</description>
<state readOnly="false">
<options>
<option value="45"></option>
<option value="50"></option>
<option value="55"></option>
<option value="60"></option>
<option value="100"></option>
</options>
</state>
</channel-type>
<channel-type id="waterLevel">
<item-type>String</item-type>
<label>Water Level</label>
<description>Shows the water levele of a WeMo enabled Holmes Humidifier</description>
<state readOnly="true">
<options>
<option value="EMPTY"></option>
<option value="LOW"></option>
<option value="GOOD"></option>
</options>
</state>
</channel-type>
<channel-type id="heaterMode">
<item-type>String</item-type>
<label>Operation Mode</label>
<description>Shows the operation mode of a WeMo enabled Heater</description>
<state readOnly="false">
<options>
<option value="OFF">Not Running</option>
<option value="FROSTPROTECT">Running at FrostProtect</option>
<option value="HIGH">Running at high level</option>
<option value="LOW">Running at low level</option>
<option value="ECO">Running in Eco mode</option>
</options>
</state>
</channel-type>
<channel-type id="currentTemperature">
<item-type>Number</item-type>
<label>Current Temperature</label>
<description>Shows the current temperature measured by a WeMo enabled Heater</description>
</channel-type>
<channel-type id="targetTemperature">
<item-type>Number</item-type>
<label>Target Temperature</label>
<description>Shows the target temperature for a WeMo enabled Heater</description>
</channel-type>
<channel-type id="autoOffTime">
<item-type>DateTime</item-type>
<label>Auto OFF Time</label>
<description>Time when a WeMo enabled Heater should switch off</description>
<state pattern="%1$tR" readOnly="false"/>
</channel-type>
<channel-type id="heatingRemaining">
<item-type>Number</item-type>
<label>Remaining heating time</label>
<description>Shows the target temperature for a WeMo enabled Heater</description>
<state readOnly="true"></state>
</channel-type>
</thing:thing-descriptions> </thing:thing-descriptions>

View File

@ -170,6 +170,7 @@ public class WemoLightHandlerOSGiTest extends GenericWemoLightOSGiTestParent {
ChannelUID channelUID = new ChannelUID(thingUID, channelID); ChannelUID channelUID = new ChannelUID(thingUID, channelID);
ThingHandler handler = thing.getHandler(); ThingHandler handler = thing.getHandler();
assertNotNull(handler); assertNotNull(handler);
handler.handleCommand(channelUID, command); handler.handleCommand(channelUID, command);
ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);

View File

@ -86,6 +86,7 @@ public class WemoMakerHandlerOSGiTest extends GenericWemoOSGiTest {
ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL); ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
ThingHandler handler = thing.getHandler(); ThingHandler handler = thing.getHandler();
assertNotNull(handler); assertNotNull(handler);
handler.handleCommand(channelUID, command); handler.handleCommand(channelUID, command);
ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
@ -121,6 +122,7 @@ public class WemoMakerHandlerOSGiTest extends GenericWemoOSGiTest {
ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL); ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
ThingHandler handler = thing.getHandler(); ThingHandler handler = thing.getHandler();
assertNotNull(handler); assertNotNull(handler);
handler.handleCommand(channelUID, command); handler.handleCommand(channelUID, command);
ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);