Skip to content

Commit

Permalink
[wemo] Refactor duplicated code (openhab#12101)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Signed-off-by: Andras Uhrin <[email protected]>
  • Loading branch information
jlaur authored and andrasU committed Nov 12, 2022
1 parent bbac71b commit ea06060
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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;
Expand All @@ -34,7 +33,6 @@
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;
Expand Down Expand Up @@ -63,7 +61,7 @@
* @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);

Expand All @@ -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());
}
Expand All @@ -102,7 +91,7 @@ public void initialize() {
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 {
Expand Down Expand Up @@ -195,7 +184,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
+ "&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");
Expand Down Expand Up @@ -279,19 +268,6 @@ private synchronized void removeSubscription() {
}
}

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.
*/
Expand All @@ -315,7 +291,7 @@ protected void updateWemoState() {
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());
Expand Down Expand Up @@ -474,23 +450,4 @@ protected void updateWemoState() {
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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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;
Expand All @@ -27,7 +26,6 @@
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;
Expand All @@ -49,7 +47,7 @@
* @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);

Expand All @@ -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());
}
Expand All @@ -90,7 +79,7 @@ public void initialize() {
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 {
Expand Down Expand Up @@ -186,7 +175,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
+ "<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());
Expand Down Expand Up @@ -264,19 +253,6 @@ private synchronized void removeSubscription() {
}
}

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..
Expand All @@ -302,7 +278,7 @@ protected void updateWemoState() {
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());
Expand Down Expand Up @@ -345,23 +321,4 @@ protected void updateWemoState() {
}
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 "";
}
}
Loading

0 comments on commit ea06060

Please sign in to comment.