]> git.basschouten.com Git - openhab-addons.git/blob
5b41ba856fe5f9a9b8e5b65aef45ff220f301ffd
[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.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.tradfri.internal.TradfriCoapClient;
20 import org.openhab.binding.tradfri.internal.model.TradfriControllerData;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.JsonElement;
32
33 /**
34  * The {@link TradfriControllerHandler} is responsible for handling commands for individual controllers.
35  *
36  * @author Christoph Weitkamp - Initial contribution
37  */
38 @NonNullByDefault
39 public class TradfriControllerHandler extends TradfriThingHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(TradfriControllerHandler.class);
42
43     // keeps track of the current state for handling of increase/decrease
44     private @Nullable TradfriControllerData state;
45
46     public TradfriControllerHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void onUpdate(JsonElement data) {
52         if (active && !(data.isJsonNull())) {
53             state = new TradfriControllerData(data);
54             updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
55
56             final TradfriControllerData state = this.state;
57
58             if (state == null) {
59                 return;
60             }
61             DecimalType batteryLevel = state.getBatteryLevel();
62             if (batteryLevel != null) {
63                 updateState(CHANNEL_BATTERY_LEVEL, batteryLevel);
64             }
65
66             OnOffType batteryLow = state.getBatteryLow();
67             if (batteryLow != null) {
68                 updateState(CHANNEL_BATTERY_LOW, batteryLow);
69             }
70
71             updateDeviceProperties(state);
72
73             logger.debug(
74                     "Updating thing for controllerId {} to state {batteryLevel: {}, batteryLow: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
75                     state.getDeviceId(), batteryLevel, batteryLow, state.getFirmwareVersion(), state.getModelId(),
76                     state.getVendor());
77         }
78     }
79
80     @Override
81     public void handleCommand(ChannelUID channelUID, Command command) {
82         if (active) {
83             if (command instanceof RefreshType) {
84                 TradfriCoapClient coapClient = this.coapClient;
85                 if (coapClient != null) {
86                     logger.debug("Refreshing channel {}", channelUID);
87                     coapClient.asyncGet(this);
88                 } else {
89                     logger.debug("coapClient is null!");
90                 }
91                 return;
92             }
93
94             logger.debug("The controller is a read-only device and cannot handle commands.");
95         }
96     }
97 }