]> git.basschouten.com Git - openhab-addons.git/blob
cd54b98236a09c00b23c2571c682dff96fce1641
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.wemo.internal.handler;
14
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.jupnp.UpnpService;
21 import org.openhab.binding.wemo.internal.WemoBindingConstants;
22 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
23 import org.openhab.core.io.transport.upnp.UpnpIOService;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.State;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link WemoSwitchHandler} is responsible for handling commands for
33  * a WeMo device supporting a binary switch: Socket or Light Switch.
34  *
35  * @author Jacob Laursen - Initial contribution
36  */
37 @NonNullByDefault
38 public class WemoSwitchHandler extends WemoHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(WemoSwitchHandler.class);
41     private final Map<String, String> stateMap = new ConcurrentHashMap<String, String>();
42
43     public WemoSwitchHandler(Thing thing, UpnpIOService upnpIOService, UpnpService upnpService,
44             WemoHttpCall wemoHttpCaller) {
45         super(thing, upnpIOService, upnpService, wemoHttpCaller);
46     }
47
48     @Override
49     public void initialize() {
50         logger.debug("Initializing WemoSwitchHandler for thing '{}'", thing.getUID());
51         updateStatus(ThingStatus.UNKNOWN);
52         super.initialize();
53     }
54
55     @Override
56     public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
57         logger.debug("Received pair '{}':'{}' (service '{}') for thing '{}'",
58                 new Object[] { variable, value, service, this.getThing().getUID() });
59
60         updateStatus(ThingStatus.ONLINE);
61
62         if (!"BinaryState".equals(variable)) {
63             return;
64         }
65
66         String oldValue = this.stateMap.get(variable);
67         if (variable != null && value != null) {
68             this.stateMap.put(variable, value);
69         }
70
71         if (value != null && value.length() == 1) {
72             String binaryState = stateMap.get("BinaryState");
73             if (binaryState != null) {
74                 if (oldValue == null || !oldValue.equals(binaryState)) {
75                     State state = "0".equals(binaryState) ? OnOffType.OFF : OnOffType.ON;
76                     logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
77                     updateState(WemoBindingConstants.CHANNEL_STATE, state);
78                 }
79             }
80         }
81     }
82 }