[wemo] Refactor duplicated code (#12101)

* Extract getHost() and getUDN() to common handler base class.
* Delete AbstractWemoHandler as it serves no purpose.
* Extract isUpnpDeviceRegistered() to base class.
* Fix typo.

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-01-24 21:26:35 +01:00 committed by GitHub
parent ea21f2ff47
commit bb9cb906d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 137 additions and 374 deletions

View File

@ -111,7 +111,7 @@ public class WemoBindingConstants {
public static final String UDN = "udn";
public static final String DEVICE_ID = "deviceID";
public static final String POLLINGINTERVALL = "pollingInterval";
public static final int DEFAULT_REFRESH_INTERVALL_SECONDS = 60;
public static final int DEFAULT_REFRESH_INTERVAL_SECONDS = 60;
public static final int SUBSCRIPTION_DURATION_SECONDS = 600;
public static final int LINK_DISCOVERY_SERVICE_INITIAL_DELAY = 5;
public static final String HTTP_CALL_CONTENT_HEADER = "text/xml; charset=utf-8";

View File

@ -1,32 +0,0 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.wemo.internal.handler;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.BaseThingHandler;
/**
* @author Stefan Triller - Initial contribution
*/
@NonNullByDefault
public abstract class AbstractWemoHandler extends BaseThingHandler {
protected WemoHttpCall wemoHttpCaller;
public AbstractWemoHandler(Thing thing, WemoHttpCall wemoHttpCaller) {
super(thing);
this.wemoHttpCaller = wemoHttpCaller;
}
}

View File

@ -0,0 +1,99 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.wemo.internal.handler;
import java.net.URL;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.WemoBindingConstants;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
/**
* {@link WemoBaseThingHandler} provides a base implementation for the
* concrete WeMo handlers for each thing type.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class WemoBaseThingHandler extends BaseThingHandler implements UpnpIOParticipant {
protected @Nullable UpnpIOService service;
protected WemoHttpCall wemoHttpCaller;
protected String host = "";
public WemoBaseThingHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing);
this.service = upnpIOService;
this.wemoHttpCaller = wemoHttpCaller;
}
@Override
public void initialize() {
// can be overridden by subclasses
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
// can be overridden by subclasses
}
@Override
public void onStatusChanged(boolean status) {
// can be overridden by subclasses
}
@Override
public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
// can be overridden by subclasses
}
@Override
public void onServiceSubscribed(@Nullable String service, boolean succeeded) {
// can be overridden by subclasses
}
@Override
public @Nullable String getUDN() {
return (String) this.getThing().getConfiguration().get(WemoBindingConstants.UDN);
}
protected boolean isUpnpDeviceRegistered() {
UpnpIOService service = this.service;
if (service != null) {
return service.isRegistered(this);
}
return false;
}
protected 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 java.io.StringReader;
import java.net.URL;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collections;
@ -34,7 +33,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
@ -63,7 +61,7 @@ import org.xml.sax.InputSource;
* @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
*/
@NonNullByDefault
public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoCoffeeHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoCoffeeHandler.class);
@ -72,21 +70,12 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
private final Object upnpLock = new Object();
private final Object jobLock = new Object();
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall;
private String host = "";
private Map<String, Boolean> subscriptionState = new HashMap<>();
private @Nullable ScheduledFuture<?> pollingJob;
public WemoCoffeeHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller);
this.wemoCall = wemoHttpCaller;
this.service = upnpIOService;
super(thing, upnpIOService, wemoHttpCaller);
logger.debug("Creating a WemoCoffeeHandler for thing '{}'", getThing().getUID());
}
@ -102,7 +91,7 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
@ -195,7 +184,7 @@ 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>"
+ "</u:SetAttributes>" + "</s:Body>" + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
updateState(CHANNEL_STATE, OnOffType.ON);
State newMode = new StringType("Brewing");
@ -279,19 +268,6 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
}
}
private boolean isUpnpDeviceRegistered() {
UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
@Override
public String getUDN() {
return (String) this.getThing().getConfiguration().get(UDN);
}
/**
* The {@link updateWemoState} polls the actual state of a WeMo CoffeeMaker.
*/
@ -315,7 +291,7 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
String action = "GetAttributes";
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -474,23 +450,4 @@ public class WemoCoffeeHandler extends AbstractWemoHandler implements UpnpIOPart
logger.trace("New attribute brewed '{}' received", dateTimeState);
return dateTimeState;
}
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
public void onStatusChanged(boolean status) {
}
}

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.WemoUtil.*;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -27,7 +26,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.StringType;
@ -49,7 +47,7 @@ import org.slf4j.LoggerFactory;
* @author Hans-Jörg Merk - Initial contribution;
*/
@NonNullByDefault
public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoCrockpotHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoCrockpotHandler.class);
@ -60,21 +58,12 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall;
private String host = "";
private Map<String, Boolean> subscriptionState = new HashMap<>();
private @Nullable ScheduledFuture<?> pollingJob;
public WemoCrockpotHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller);
this.wemoCall = wemoHttpCaller;
this.service = upnpIOService;
super(thing, upnpIOService, wemoHttpCaller);
logger.debug("Creating a WemoCrockpotHandler for thing '{}'", getThing().getUID());
}
@ -90,7 +79,7 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
@ -186,7 +175,7 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
+ "<s:Body>" + "<u:SetCrockpotState xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<mode>"
+ mode + "</mode>" + "<time>" + time + "</time>" + "</u:SetCrockpotState>" + "</s:Body>"
+ "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
@ -264,19 +253,6 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
}
}
private boolean isUpnpDeviceRegistered() {
UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
@Override
public String getUDN() {
return (String) this.getThing().getConfiguration().get(UDN);
}
/**
* The {@link updateWemoState} polls the actual state of a WeMo device and
* calls {@link onValueReceived} to update the statemap and channels..
@ -302,7 +278,7 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
String action = "GetCrockpotState";
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -345,23 +321,4 @@ public class WemoCrockpotHandler extends AbstractWemoHandler implements UpnpIOPa
}
updateStatus(ThingStatus.ONLINE);
}
@Override
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.WemoUtil.*;
import java.net.URL;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collections;
@ -30,7 +29,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
@ -55,7 +53,7 @@ import org.slf4j.LoggerFactory;
* @author Hans-Jörg Merk - Initial contribution
*/
@NonNullByDefault
public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoDimmerHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoDimmerHandler.class);
@ -66,12 +64,6 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall;
private String host = "";
private Map<String, Boolean> subscriptionState = new HashMap<>();
private @Nullable ScheduledFuture<?> pollingJob;
@ -85,10 +77,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
private static final int DIM_STEPSIZE = 5;
public WemoDimmerHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller);
this.service = upnpIOService;
this.wemoCall = wemoHttpCaller;
super(thing, upnpIOService, wemoHttpCaller);
logger.debug("Creating a WemoDimmerHandler for thing '{}'", getThing().getUID());
}
@ -104,7 +93,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
@ -477,19 +466,6 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
}
}
private boolean isUpnpDeviceRegistered() {
UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
@Override
public String getUDN() {
return (String) this.getThing().getConfiguration().get(UDN);
}
/**
* The {@link updateWemoState} polls the actual state of a WeMo device and
* calls {@link onValueReceived} to update the statemap and channels..
@ -517,7 +493,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
try {
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -546,7 +522,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
content = createStateRequestContent(action, actionService);
try {
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -613,7 +589,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
+ "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:basicevent:1\">" + "<" + argument
+ ">" + value + "</" + argument + ">" + "</u:" + action + ">" + "</s:Body>" + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
@ -648,7 +624,7 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
+ "<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
+ "</u:SetBinaryState>" + "</s:Body>" + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
@ -661,23 +637,4 @@ public class WemoDimmerHandler extends AbstractWemoHandler implements UpnpIOPart
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
public void onStatusChanged(boolean status) {
}
}

View File

@ -17,7 +17,6 @@ import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collections;
@ -34,7 +33,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
@ -63,7 +61,7 @@ import org.slf4j.LoggerFactory;
* @author Mihir Patil - Added standby switch
*/
@NonNullByDefault
public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoHandler.class);
@ -76,21 +74,12 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall;
private Map<String, Boolean> subscriptionState = new HashMap<>();
private @Nullable ScheduledFuture<?> pollingJob;
private String host = "";
public WemoHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller);
this.service = upnpIOService;
this.wemoCall = wemoHttpCaller;
super(thing, upnpIOService, wemoHttpCaller);
logger.debug("Creating a WemoHandler for thing '{}'", getThing().getUID());
}
@ -106,7 +95,7 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
@ -186,7 +175,7 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
boolean binaryState = OnOffType.ON.equals(command) ? true : false;
String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"";
String content = createBinaryStateContent(binaryState);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
@ -407,34 +396,6 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
}
}
private boolean isUpnpDeviceRegistered() {
UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
@Override
public String getUDN() {
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
* calls {@link onValueReceived} to update the statemap and channels..
@ -467,7 +428,7 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
try {
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -489,8 +450,4 @@ public class WemoHandler extends AbstractWemoHandler implements UpnpIOParticipan
logger.error("Failed to get actual state for device '{}': {}", getThing().getUID(), e.getMessage());
}
}
@Override
public void onStatusChanged(boolean status) {
}
}

View File

@ -17,7 +17,6 @@ import static org.openhab.binding.wemo.internal.WemoUtil.*;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -33,7 +32,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
@ -61,7 +59,7 @@ import org.xml.sax.SAXException;
* @author Hans-Jörg Merk - Initial contribution;
*/
@NonNullByDefault
public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoHolmesHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoHolmesHandler.class);
@ -75,21 +73,12 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
private final Map<String, String> stateMap = Collections.synchronizedMap(new HashMap<>());
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall;
private String host = "";
private Map<String, Boolean> subscriptionState = new HashMap<>();
private @Nullable ScheduledFuture<?> pollingJob;
public WemoHolmesHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
super(thing, wemoHttpCaller);
this.service = upnpIOService;
this.wemoCall = wemoHttpCaller;
super(thing, upnpIOService, wemoHttpCaller);
logger.debug("Creating a WemoHolmesHandler for thing '{}'", getThing().getUID());
}
@ -105,7 +94,7 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
@ -280,7 +269,7 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
+ "<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>"
+ "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
@ -357,19 +346,6 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
}
}
private boolean isUpnpDeviceRegistered() {
UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
@Override
public String getUDN() {
return (String) this.getThing().getConfiguration().get(UDN);
}
/**
* The {@link updateWemoState} polls the actual state of a WeMo device and
* calls {@link onValueReceived} to update the statemap and channels..
@ -395,7 +371,7 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
String action = "GetAttributes";
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -611,23 +587,4 @@ public class WemoHolmesHandler extends AbstractWemoHandler implements UpnpIOPart
}
updateStatus(ThingStatus.ONLINE);
}
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
public void onStatusChanged(boolean status) {
}
}

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.WemoUtil.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
@ -25,7 +24,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
@ -50,7 +48,7 @@ import org.slf4j.LoggerFactory;
* @author Hans-Jörg Merk - Initial contribution
*/
@NonNullByDefault
public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoLightHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoLightHandler.class);
@ -61,16 +59,10 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
private @Nullable WemoBridgeHandler wemoBridgeHandler;
private @Nullable UpnpIOService service;
private String host = "";
private @Nullable String wemoLightID;
private int currentBrightness;
private WemoHttpCall wemoCall;
/**
* Set dimming stepsize to 5%
*/
@ -86,10 +78,7 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
private @Nullable ScheduledFuture<?> pollingJob;
public WemoLightHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpcaller) {
super(thing, wemoHttpcaller);
this.service = upnpIOService;
this.wemoCall = wemoHttpcaller;
super(thing, upnpIOService, wemoHttpcaller);
logger.debug("Creating a WemoLightHandler for thing '{}'", getThing().getUID());
}
@ -107,7 +96,7 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, DEFAULT_REFRESH_INITIAL_DELAY,
DEFAULT_REFRESH_INTERVALL_SECONDS, TimeUnit.SECONDS);
DEFAULT_REFRESH_INTERVAL_SECONDS, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE);
@ -288,7 +277,7 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
+ "&lt;/CapabilityValue&gt;&lt;/DeviceStatus&gt;" + "</DeviceStatusList>"
+ "</u:SetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -348,7 +337,7 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
+ "<s:Body>" + "<u:GetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceIDs>"
+ wemoLightID + "</DeviceIDs>" + "</u:GetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -411,10 +400,6 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
}
}
@Override
public void onStatusChanged(boolean status) {
}
private synchronized void addSubscription() {
synchronized (upnpLock) {
UpnpIOService localService = service;
@ -454,27 +439,4 @@ public class WemoLightHandler extends AbstractWemoHandler implements UpnpIOParti
}
}
}
private boolean isUpnpDeviceRegistered() {
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 java.io.StringReader;
import java.net.URL;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
@ -29,7 +28,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.wemo.internal.http.WemoHttpCall;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.ChannelUID;
@ -54,7 +52,7 @@ import org.xml.sax.InputSource;
* @author Hans-Jörg Merk - Initial contribution
*/
@NonNullByDefault
public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParticipant {
public class WemoMakerHandler extends WemoBaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(WemoMakerHandler.class);
@ -62,19 +60,10 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
private final Object jobLock = new Object();
private @Nullable UpnpIOService service;
private WemoHttpCall wemoCall;
private String host = "";
private @Nullable ScheduledFuture<?> pollingJob;
public WemoMakerHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpcaller) {
super(thing, wemoHttpcaller);
this.service = upnpIOService;
this.wemoCall = wemoHttpcaller;
super(thing, upnpIOService, wemoHttpcaller);
logger.debug("Creating a WemoMakerHandler for thing '{}'", getThing().getUID());
}
@ -90,7 +79,7 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
localService.registerParticipant(this);
}
host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVALL_SECONDS,
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
} else {
@ -169,7 +158,7 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
boolean binaryState = OnOffType.ON.equals(command) ? true : false;
String soapHeader = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"";
String content = createBinaryStateContent(binaryState);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null && logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
logger.trace("wemoCall with soapHeader '{}' for device '{}'", soapHeader, getThing().getUID());
@ -184,19 +173,6 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
}
}
private boolean isUpnpDeviceRegistered() {
UpnpIOService localService = service;
if (localService != null) {
return localService.isRegistered(this);
}
return false;
}
@Override
public String getUDN() {
return (String) this.getThing().getConfiguration().get(UDN);
}
/**
* The {@link updateWemoState} polls the actual state of a WeMo Maker.
*/
@ -220,7 +196,7 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
String action = "GetAttributes";
String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
String content = createStateRequestContent(action, actionService);
String wemoCallResponse = wemoCall.executeCall(wemoURL, soapHeader, content);
String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (logger.isTraceEnabled()) {
logger.trace("wemoCall to URL '{}' for device '{}'", wemoURL, getThing().getUID());
@ -292,31 +268,4 @@ public class WemoMakerHandler extends AbstractWemoHandler implements UpnpIOParti
logger.error("Failed to get attributes for device '{}'", getThing().getUID(), e);
}
}
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
public void onStatusChanged(boolean status) {
}
@Override
public void onServiceSubscribed(@Nullable String service, boolean succeeded) {
}
@Override
public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
}
}