2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tradfri.internal.handler;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
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;
31 import com.google.gson.JsonElement;
34 * The {@link TradfriControllerHandler} is responsible for handling commands for individual controllers.
36 * @author Christoph Weitkamp - Initial contribution
39 public class TradfriControllerHandler extends TradfriThingHandler {
41 private final Logger logger = LoggerFactory.getLogger(TradfriControllerHandler.class);
43 // keeps track of the current state for handling of increase/decrease
44 private @Nullable TradfriControllerData state;
46 public TradfriControllerHandler(Thing thing) {
51 public void onUpdate(JsonElement data) {
52 if (active && !(data.isJsonNull())) {
53 state = new TradfriControllerData(data);
54 updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
56 final TradfriControllerData state = this.state;
61 DecimalType batteryLevel = state.getBatteryLevel();
62 if (batteryLevel != null) {
63 updateState(CHANNEL_BATTERY_LEVEL, batteryLevel);
66 OnOffType batteryLow = state.getBatteryLow();
67 if (batteryLow != null) {
68 updateState(CHANNEL_BATTERY_LOW, batteryLow);
71 updateDeviceProperties(state);
74 "Updating thing for controllerId {} to state {batteryLevel: {}, batteryLow: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
75 state.getDeviceId(), batteryLevel, batteryLow, state.getFirmwareVersion(), state.getModelId(),
81 public void handleCommand(ChannelUID channelUID, Command command) {
83 if (command instanceof RefreshType) {
84 TradfriCoapClient coapClient = this.coapClient;
85 if (coapClient != null) {
86 logger.debug("Refreshing channel {}", channelUID);
87 coapClient.asyncGet(this);
89 logger.debug("coapClient is null!");
94 logger.debug("The controller is a read-only device and cannot handle commands.");