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