return unescapedOutput.toString();
}
- public static @Nullable String getWemoURL(String host, String actionService) {
- int portCheckStart = 49151;
- int portCheckStop = 49157;
- String port = null;
- for (int i = portCheckStart; i < portCheckStop; i++) {
- if (serviceAvailableFunction.apply(host, i)) {
- port = String.valueOf(i);
- break;
- }
- }
- return port == null ? null : "http://" + host + ":" + port + "/upnp/control/" + actionService + "1";
- }
-
private static boolean servicePing(String host, int port) {
try {
HttpUtil.executeUrl("GET", "http://" + host + ":" + port, 250);
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.WemoUtil;
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.ThingStatus;
+import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
protected @Nullable UpnpIOService service;
protected WemoHttpCall wemoHttpCaller;
- protected String host = "";
+ private @Nullable String host;
private Map<String, Instant> subscriptions = new ConcurrentHashMap<String, Instant>();
private @Nullable ScheduledFuture<?> subscriptionRenewalJob;
if (service != null) {
logger.debug("Registering UPnP participant for {}", getThing().getUID());
service.registerParticipant(this);
+ initializeHost();
}
}
subscriptions.clear();
}
- protected String getHost() {
- String localHost = host;
- if (!localHost.isEmpty()) {
- return localHost;
+ public @Nullable String getWemoURL(String actionService) {
+ String host = getHost();
+ if (host == null) {
+ updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+ "@text/config-status.error.missing-ip");
+ return null;
}
- UpnpIOService localService = service;
- if (localService != null) {
- URL descriptorURL = localService.getDescriptorURL(this);
+ int portCheckStart = 49151;
+ int portCheckStop = 49157;
+ String port = null;
+ for (int i = portCheckStart; i < portCheckStop; i++) {
+ if (WemoUtil.serviceAvailableFunction.apply(host, i)) {
+ port = String.valueOf(i);
+ break;
+ }
+ }
+ if (port == null) {
+ updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+ "@text/config-status.error.missing-url");
+ return null;
+ }
+ return "http://" + host + ":" + port + "/upnp/control/" + actionService + "1";
+ }
+
+ private @Nullable String getHost() {
+ if (host != null) {
+ return host;
+ }
+ initializeHost();
+ return host;
+ }
+
+ private void initializeHost() {
+ host = getHostFromService();
+ }
+
+ private @Nullable String getHostFromService() {
+ UpnpIOService service = this.service;
+ if (service != null) {
+ URL descriptorURL = service.getDescriptorURL(this);
if (descriptorURL != null) {
return descriptorURL.getHost();
}
}
- return "";
+ return null;
}
}
if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoCoffeeHandler for UDN '{}'", configuration.get(UDN));
addSubscription(DEVICEEVENT);
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
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()) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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) {
* The {@link updateWemoState} polls the actual state of a WeMo CoffeeMaker.
*/
protected void updateWemoState() {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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 actionService = DEVICEACTION;
- String wemoURL = getWemoURL(host, actionService);
+ String wemoURL = getWemoURL(actionService);
if (wemoURL == null) {
logger.debug("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 {
if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoCrockpotHandler for UDN '{}'", configuration.get(UDN));
addSubscription(BASICEVENT);
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
}
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()) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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";
*
*/
protected void updateWemoState() {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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 actionService = BASICEVENT;
- String wemoURL = getWemoURL(localHost, actionService);
+ String wemoURL = getWemoURL(actionService);
if (wemoURL == null) {
logger.warn("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 {
if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoDimmerHandler for UDN '{}'", configuration.get(UDN));
addSubscription(BASICEVENT);
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
}
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()) {
*
*/
protected void updateWemoState() {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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";
}
public void setBinaryState(String action, String argument, String value) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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 {
}
public void setTimerStart(String action, String argument, String value) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.warn("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 {
* @author Stefan Bußweiler - Added new thing status handling
* @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
* @author Mihir Patil - Added standby switch
+ * @author Jacob Laursen - Refactoring
*/
@NonNullByDefault
public abstract class WemoHandler extends WemoBaseThingHandler {
if (THING_TYPE_INSIGHT.equals(thing.getThingTypeUID())) {
addSubscription(INSIGHTEVENT);
}
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
}
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()) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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) {
*/
protected void updateWemoState() {
String actionService = BASICACTION;
- String localhost = getHost();
- if (localhost.isEmpty()) {
- logger.warn("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 variable = "BinaryState";
String value = null;
variable = "InsightParams";
actionService = INSIGHTACTION;
}
- String wemoURL = getWemoURL(localhost, actionService);
+ String wemoURL = getWemoURL(actionService);
if (wemoURL == null) {
logger.debug("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 + "\"";
if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoHolmesHandler for UDN '{}'", configuration.get(UDN));
addSubscription(BASICEVENT);
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
}
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()) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(DEVICEACTION);
if (wemoURL == null) {
logger.debug("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;
*
*/
protected void updateWemoState() {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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 actionService = DEVICEACTION;
- String wemoURL = getWemoURL(localHost, actionService);
+ String wemoURL = getWemoURL(actionService);
if (wemoURL == null) {
logger.debug("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 {
final Bridge bridge = getBridge();
if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
addSubscription(BRIDGEEVENT);
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, DEFAULT_REFRESH_INITIAL_DELAY,
DEFAULT_REFRESH_INTERVAL_SECONDS, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
}
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()) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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) {
* channel states.
*/
public void getDeviceState() {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
- String wemoURL = getWemoURL(localHost, BRIDGEACTION);
+ String wemoURL = getWemoURL(BRIDGEACTION);
if (wemoURL == null) {
logger.debug("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 {
if (configuration.get(UDN) != null) {
logger.debug("Initializing WemoMakerHandler for UDN '{}'", configuration.get(UDN));
- host = getHost();
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, DEFAULT_REFRESH_INTERVAL_SECONDS,
TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
}
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()) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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);
+ String wemoURL = getWemoURL(BASICACTION);
if (wemoURL == null) {
logger.debug("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) {
* The {@link updateWemoState} polls the actual state of a WeMo Maker.
*/
protected void updateWemoState() {
- String localHost = getHost();
- if (localHost.isEmpty()) {
- logger.warn("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 actionService = DEVICEACTION;
- String wemoURL = getWemoURL(localHost, actionService);
+ String wemoURL = getWemoURL(actionService);
if (wemoURL == null) {
logger.debug("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 {