]> git.basschouten.com Git - openhab-addons.git/blob
e028e7568d6f29749ca5c6d61086555f7425e9bb
[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.openhab.binding.wemo.internal.InsightParser;
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.library.types.QuantityType;
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 WemoInsightHandler} is responsible for handling commands for
34  * a WeMo Insight Switch.
35  *
36  * @author Jacob Laursen - Initial contribution
37  */
38 @NonNullByDefault
39 public class WemoInsightHandler extends WemoHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(WemoInsightHandler.class);
42     private final Map<String, String> stateMap = new ConcurrentHashMap<String, String>();
43
44     public WemoInsightHandler(Thing thing, UpnpIOService upnpIOService, WemoHttpCall wemoHttpCaller) {
45         super(thing, upnpIOService, wemoHttpCaller);
46     }
47
48     @Override
49     public void onValueReceived(@Nullable String variable, @Nullable String value, @Nullable String service) {
50         logger.debug("Received pair '{}':'{}' (service '{}') for thing '{}'",
51                 new Object[] { variable, value, service, this.getThing().getUID() });
52
53         updateStatus(ThingStatus.ONLINE);
54
55         if (!"BinaryState".equals(variable) && !"InsightParams".equals(variable)) {
56             return;
57         }
58
59         if (variable != null && value != null) {
60             this.stateMap.put(variable, value);
61         }
62
63         if (value != null && value.length() > 1) {
64             String insightParams = stateMap.get(variable);
65
66             if (insightParams != null) {
67                 InsightParser parser = new InsightParser(insightParams);
68                 Map<String, State> results = parser.parse();
69                 results.forEach((channel, state) -> {
70                     logger.trace("New InsightParam {} '{}' for device '{}' received", channel, state,
71                             getThing().getUID());
72                     updateState(channel, state);
73                 });
74
75                 // Update helper channel onStandBy by checking if currentPower > standByLimit.
76                 var standByLimit = (QuantityType<?>) results.get(WemoBindingConstants.CHANNEL_STANDBYLIMIT);
77                 if (standByLimit != null) {
78                     var currentPower = (QuantityType<?>) results.get(WemoBindingConstants.CHANNEL_CURRENTPOWER);
79                     if (currentPower != null) {
80                         updateState(WemoBindingConstants.CHANNEL_ONSTANDBY,
81                                 OnOffType.from(currentPower.intValue() <= standByLimit.intValue()));
82                     }
83                 }
84             }
85         }
86     }
87 }