]> git.basschouten.com Git - openhab-addons.git/blob
b591fd3705f8f6ddc09ec3ac2f6e22fa09ec76e4
[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.DateTimeType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.types.State;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link WemoMotionHandler} is responsible for handling commands for
34  * a WeMo Motion.
35  *
36  * @author Jacob Laursen - Initial contribution
37  */
38 @NonNullByDefault
39 public class WemoMotionHandler extends WemoHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(WemoMotionHandler.class);
42     private final Map<String, String> stateMap = new ConcurrentHashMap<String, String>();
43
44     public WemoMotionHandler(Thing thing, UpnpIOService upnpIOService, UpnpService upnpService,
45             WemoHttpCall wemoHttpCaller) {
46         super(thing, upnpIOService, upnpService, wemoHttpCaller);
47     }
48
49     @Override
50     public void initialize() {
51         logger.debug("Initializing WemoMotionHandler for thing '{}'", thing.getUID());
52         updateStatus(ThingStatus.UNKNOWN);
53         super.initialize();
54     }
55
56     @Override
57     public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
58         logger.debug("Received pair '{}':'{}' (service '{}') for thing '{}'",
59                 new Object[] { variable, value, service, this.getThing().getUID() });
60
61         updateStatus(ThingStatus.ONLINE);
62
63         if (!"BinaryState".equals(variable)) {
64             return;
65         }
66
67         String oldValue = this.stateMap.get(variable);
68         if (variable != null && value != null) {
69             this.stateMap.put(variable, value);
70         }
71
72         if (value != null && value.length() == 1) {
73             String binaryState = stateMap.get("BinaryState");
74             if (binaryState != null) {
75                 if (oldValue == null || !oldValue.equals(binaryState)) {
76                     State state = "0".equals(binaryState) ? OnOffType.OFF : OnOffType.ON;
77                     logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
78                     updateState(WemoBindingConstants.CHANNEL_MOTION_DETECTION, state);
79                     if (OnOffType.ON.equals(state)) {
80                         State lastMotionDetected = new DateTimeType();
81                         updateState(WemoBindingConstants.CHANNEL_LAST_MOTION_DETECTED, lastMotionDetected);
82                     }
83                 }
84             }
85         }
86     }
87 }