]> git.basschouten.com Git - openhab-addons.git/blob
30a97e21390f3793926e6680e6632703b0c0fc9a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.openhab.binding.wemo.internal.WemoBindingConstants;
21 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
22 import org.openhab.core.io.transport.upnp.UpnpIOService;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.types.State;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link WemoSwitchHandler} is responsible for handling commands for
32  * a WeMo device supporting a binary switch: Socket or Light Switch.
33  *
34  * @author Jacob Laursen - Initial contribution
35  */
36 @NonNullByDefault
37 public class WemoSwitchHandler extends WemoHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(WemoSwitchHandler.class);
40     private final Map<String, String> stateMap = new ConcurrentHashMap<String, String>();
41
42     public WemoSwitchHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
43         super(thing, upnpIOService, wemoHttpCaller);
44     }
45
46     @Override
47     public void initialize() {
48         logger.debug("Initializing WemoSwitchHandler for thing '{}'", thing.getUID());
49         updateStatus(ThingStatus.UNKNOWN);
50         super.initialize();
51     }
52
53     @Override
54     public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
55         logger.debug("Received pair '{}':'{}' (service '{}') for thing '{}'",
56                 new Object[] { variable, value, service, this.getThing().getUID() });
57
58         updateStatus(ThingStatus.ONLINE);
59
60         if (!"BinaryState".equals(variable)) {
61             return;
62         }
63
64         String oldValue = this.stateMap.get(variable);
65         if (variable != null && value != null) {
66             this.stateMap.put(variable, value);
67         }
68
69         if (value != null && value.length() == 1) {
70             String binaryState = stateMap.get("BinaryState");
71             if (binaryState != null) {
72                 if (oldValue == null || !oldValue.equals(binaryState)) {
73                     State state = OnOffType.from(!"0".equals(binaryState));
74                     logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
75                     updateState(WemoBindingConstants.CHANNEL_STATE, state);
76                 }
77             }
78         }
79     }
80 }