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