]> git.basschouten.com Git - openhab-addons.git/blob
f32093f2b7ee8b66c822a0265df3a0ca3f5fd443
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.openhab.binding.tradfri.internal.TradfriCoapClient;
19 import org.openhab.binding.tradfri.internal.model.TradfriSensorData;
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 TradfriSensorHandler} is responsible for handling commands for individual sensors.
34  *
35  * @author Christoph Weitkamp - Initial contribution
36  */
37 @NonNullByDefault
38 public class TradfriSensorHandler extends TradfriThingHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(TradfriSensorHandler.class);
41
42     public TradfriSensorHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public void onUpdate(JsonElement data) {
48         if (active && !(data.isJsonNull())) {
49             TradfriSensorData state = new TradfriSensorData(data);
50             updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
51
52             DecimalType batteryLevel = state.getBatteryLevel();
53             if (batteryLevel != null) {
54                 updateState(CHANNEL_BATTERY_LEVEL, batteryLevel);
55             }
56
57             OnOffType batteryLow = state.getBatteryLow();
58             if (batteryLow != null) {
59                 updateState(CHANNEL_BATTERY_LOW, batteryLow);
60             }
61
62             updateDeviceProperties(state);
63
64             logger.debug(
65                     "Updating thing for sensorId {} to state {batteryLevel: {}, batteryLow: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
66                     state.getDeviceId(), batteryLevel, batteryLow, state.getFirmwareVersion(), state.getModelId(),
67                     state.getVendor());
68         }
69     }
70
71     @Override
72     public void handleCommand(ChannelUID channelUID, Command command) {
73         if (active) {
74             if (command instanceof RefreshType) {
75                 TradfriCoapClient coapClient = this.coapClient;
76                 if (coapClient != null) {
77                     logger.debug("Refreshing channel {}", channelUID);
78                     coapClient.asyncGet(this);
79                 } else {
80                     logger.debug("coapClient is null!");
81                 }
82                 return;
83             }
84
85             logger.debug("The sensor is a read-only device and cannot handle commands.");
86         }
87     }
88 }