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