2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wemo.internal.handler;
15 import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
16 import static org.openhab.binding.wemo.internal.WemoUtil.*;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.jupnp.UpnpService;
24 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.io.transport.upnp.UpnpIOService;
27 import org.openhab.core.library.types.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.ThingStatusInfo;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.RefreshType;
39 import org.openhab.core.types.State;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * {@link WemoLightHandler} is the handler for a WeMo light, responsible for handling commands and state updates for the
45 * different channels of a WeMo light.
47 * @author Hans-Jörg Merk - Initial contribution
50 public class WemoLightHandler extends WemoBaseThingHandler {
52 private final Logger logger = LoggerFactory.getLogger(WemoLightHandler.class);
54 private final Object jobLock = new Object();
56 private @Nullable WemoBridgeHandler wemoBridgeHandler;
58 private @Nullable String wemoLightID;
60 private int currentBrightness;
63 * Set dimming stepsize to 5%
65 private static final int DIM_STEPSIZE = 5;
68 * The default refresh initial delay in Seconds.
70 private static final int DEFAULT_REFRESH_INITIAL_DELAY = 15;
72 private @Nullable ScheduledFuture<?> pollingJob;
74 public WemoLightHandler(Thing thing, UpnpIOService upnpIOService, UpnpService upnpService,
75 WemoHttpCall wemoHttpcaller) {
76 super(thing, upnpIOService, upnpService, wemoHttpcaller);
78 logger.debug("Creating a WemoLightHandler for thing '{}'", getThing().getUID());
82 public void initialize() {
84 // initialize() is only called if the required parameter 'deviceID' is available
85 wemoLightID = (String) getConfig().get(DEVICE_ID);
87 final Bridge bridge = getBridge();
88 if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
89 addSubscription(BRIDGEEVENT);
90 pollingJob = scheduler.scheduleWithFixedDelay(this::poll, DEFAULT_REFRESH_INITIAL_DELAY,
91 DEFAULT_REFRESH_INTERVAL_SECONDS, TimeUnit.SECONDS);
92 updateStatus(ThingStatus.UNKNOWN);
94 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE);
99 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
100 if (bridgeStatusInfo.getStatus().equals(ThingStatus.ONLINE)) {
101 updateStatus(ThingStatus.ONLINE);
103 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
104 ScheduledFuture<?> job = this.pollingJob;
105 if (job != null && !job.isCancelled()) {
108 this.pollingJob = null;
113 public void dispose() {
114 logger.debug("WemoLightHandler disposed.");
116 ScheduledFuture<?> job = this.pollingJob;
117 if (job != null && !job.isCancelled()) {
120 this.pollingJob = null;
124 private synchronized @Nullable WemoBridgeHandler getWemoBridgeHandler() {
125 Bridge bridge = getBridge();
126 if (bridge == null) {
127 logger.warn("Required bridge not defined for device {}.", wemoLightID);
130 ThingHandler handler = bridge.getHandler();
131 if (handler instanceof WemoBridgeHandler) {
132 this.wemoBridgeHandler = (WemoBridgeHandler) handler;
134 logger.debug("No available bridge handler found for {} bridge {} .", wemoLightID, bridge.getUID());
137 return this.wemoBridgeHandler;
140 private void poll() {
141 synchronized (jobLock) {
142 if (pollingJob == null) {
146 logger.debug("Polling job");
147 // Check if the Wemo device is set in the UPnP service registry
148 if (!isUpnpDeviceRegistered()) {
149 logger.debug("UPnP device {} not yet registered", getUDN());
150 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
151 "@text/config-status.pending.device-not-registered [\"" + getUDN() + "\"]");
155 } catch (Exception e) {
156 logger.debug("Exception during poll: {}", e.getMessage(), e);
162 public void handleCommand(ChannelUID channelUID, Command command) {
163 String wemoURL = getWemoURL(BASICACTION);
164 if (wemoURL == null) {
165 logger.debug("Failed to send command '{}' for device '{}': URL cannot be created", command,
166 getThing().getUID());
169 if (command instanceof RefreshType) {
172 } catch (Exception e) {
173 logger.debug("Exception during poll", e);
176 Configuration configuration = getConfig();
177 configuration.get(DEVICE_ID);
179 WemoBridgeHandler wemoBridge = getWemoBridgeHandler();
180 if (wemoBridge == null) {
181 logger.debug("wemoBridgeHandler not found, cannot handle command");
184 String devUDN = "uuid:" + wemoBridge.getThing().getConfiguration().get(UDN).toString();
185 logger.trace("WeMo Bridge to send command to : {}", devUDN);
188 String capability = null;
189 switch (channelUID.getId()) {
190 case CHANNEL_BRIGHTNESS:
191 capability = "10008";
192 if (command instanceof PercentType) {
193 int newBrightness = ((PercentType) command).intValue();
194 logger.trace("wemoLight received Value {}", newBrightness);
195 int value1 = Math.round(newBrightness * 255 / 100);
196 value = value1 + ":0";
197 currentBrightness = newBrightness;
198 } else if (command instanceof OnOffType) {
199 switch (command.toString()) {
207 } else if (command instanceof IncreaseDecreaseType) {
209 switch (command.toString()) {
211 currentBrightness = currentBrightness + DIM_STEPSIZE;
212 newBrightness = Math.round(currentBrightness * 255 / 100);
213 if (newBrightness > 255) {
216 value = newBrightness + ":0";
219 currentBrightness = currentBrightness - DIM_STEPSIZE;
220 newBrightness = Math.round(currentBrightness * 255 / 100);
221 if (newBrightness < 0) {
224 value = newBrightness + ":0";
230 capability = "10006";
231 switch (command.toString()) {
242 if (capability != null && value != null) {
243 String soapHeader = "\"urn:Belkin:service:bridge:1#SetDeviceStatus\"";
244 String content = "<?xml version=\"1.0\"?>"
245 + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
246 + "<s:Body>" + "<u:SetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">"
247 + "<DeviceStatusList>"
248 + "<?xml version="1.0" encoding="UTF-8"?><DeviceStatus><DeviceID>"
250 + "</DeviceID><IsGroupAction>NO</IsGroupAction><CapabilityID>"
251 + capability + "</CapabilityID><CapabilityValue>" + value
252 + "</CapabilityValue></DeviceStatus>" + "</DeviceStatusList>"
253 + "</u:SetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
255 wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
256 if ("10008".equals(capability)) {
257 OnOffType binaryState = null;
258 binaryState = "0".equals(value) ? OnOffType.OFF : OnOffType.ON;
259 updateState(CHANNEL_STATE, binaryState);
261 updateStatus(ThingStatus.ONLINE);
263 } catch (Exception e) {
264 logger.warn("Failed to send command '{}' for device '{}': {}", command, getThing().getUID(),
266 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
272 public @Nullable String getUDN() {
273 WemoBridgeHandler wemoBridge = getWemoBridgeHandler();
274 if (wemoBridge == null) {
275 logger.debug("wemoBridgeHandler not found");
278 return (String) wemoBridge.getThing().getConfiguration().get(UDN);
282 * The {@link getDeviceState} is used for polling the actual state of a WeMo Light and updating the according
285 public void getDeviceState() {
286 logger.debug("Request actual state for LightID '{}'", wemoLightID);
287 String wemoURL = getWemoURL(BRIDGEACTION);
288 if (wemoURL == null) {
289 logger.debug("Failed to get actual state for device '{}': URL cannot be created", getThing().getUID());
293 String soapHeader = "\"urn:Belkin:service:bridge:1#GetDeviceStatus\"";
294 String content = "<?xml version=\"1.0\"?>"
295 + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
296 + "<s:Body>" + "<u:GetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceIDs>"
297 + wemoLightID + "</DeviceIDs>" + "</u:GetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";
299 String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
300 wemoCallResponse = unescapeXml(wemoCallResponse);
301 String response = substringBetween(wemoCallResponse, "<CapabilityValue>", "</CapabilityValue>");
302 logger.trace("wemoNewLightState = {}", response);
303 String[] splitResponse = response.split(",");
304 if (splitResponse[0] != null) {
305 OnOffType binaryState = null;
306 binaryState = "0".equals(splitResponse[0]) ? OnOffType.OFF : OnOffType.ON;
307 updateState(CHANNEL_STATE, binaryState);
309 if (splitResponse[1] != null) {
310 String splitBrightness[] = splitResponse[1].split(":");
311 if (splitBrightness[0] != null) {
312 int newBrightnessValue = Integer.valueOf(splitBrightness[0]);
313 int newBrightness = Math.round(newBrightnessValue * 100 / 255);
314 logger.trace("newBrightness = {}", newBrightness);
315 State newBrightnessState = new PercentType(newBrightness);
316 updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
317 currentBrightness = newBrightness;
320 updateStatus(ThingStatus.ONLINE);
321 } catch (Exception e) {
322 logger.debug("Could not retrieve new Wemo light state for '{}':", getThing().getUID(), e);
323 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
328 public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
329 logger.trace("Received pair '{}':'{}' (service '{}') for thing '{}'",
330 new Object[] { variable, value, service, this.getThing().getUID() });
331 String capabilityId = substringBetween(value, "<CapabilityId>", "</CapabilityId>");
332 String newValue = substringBetween(value, "<Value>", "</Value>");
333 switch (capabilityId) {
335 OnOffType binaryState = null;
336 binaryState = "0".equals(newValue) ? OnOffType.OFF : OnOffType.ON;
337 updateState(CHANNEL_STATE, binaryState);
340 String splitValue[] = newValue.split(":");
341 if (splitValue[0] != null) {
342 int newBrightnessValue = Integer.valueOf(splitValue[0]);
343 int newBrightness = Math.round(newBrightnessValue * 100 / 255);
344 State newBrightnessState = new PercentType(newBrightness);
345 updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
346 currentBrightness = newBrightness;