]> git.basschouten.com Git - openhab-addons.git/blob
6f6e60a44f8e20628db13fe2c38c9d89ca6d547c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.tradfri.internal.handler;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.CHANNEL_POWER;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.tradfri.internal.model.TradfriPlugData;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.RefreshType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.JsonElement;
29
30 /**
31  * The {@link TradfriPlugHandler} is responsible for handling commands for individual plugs.
32  *
33  * @author Kai Kreuzer - Initial contribution
34  */
35 @NonNullByDefault
36 public class TradfriPlugHandler extends TradfriThingHandler {
37
38     private final Logger logger = LoggerFactory.getLogger(TradfriPlugHandler.class);
39
40     public TradfriPlugHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     public void onUpdate(JsonElement data) {
46         if (active && !(data.isJsonNull())) {
47             TradfriPlugData state = new TradfriPlugData(data);
48             updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
49
50             updateState(CHANNEL_POWER, state.getOnOffState() ? OnOffType.ON : OnOffType.OFF);
51             updateDeviceProperties(state);
52         }
53     }
54
55     private void setState(OnOffType onOff) {
56         TradfriPlugData data = new TradfriPlugData();
57         data.setOnOffState(onOff == OnOffType.ON);
58         set(data.getJsonString());
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         if (active) {
64             if (command instanceof RefreshType) {
65                 logger.debug("Refreshing channel {}", channelUID);
66                 coapClient.asyncGet(this);
67                 return;
68             }
69
70             switch (channelUID.getId()) {
71                 case CHANNEL_POWER:
72                     if (command instanceof OnOffType) {
73                         setState(((OnOffType) command));
74                     } else {
75                         logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POWER);
76                     }
77                     break;
78                 default:
79                     logger.error("Unknown channel UID {}", channelUID);
80             }
81         }
82     }
83 }