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