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.fineoffsetweatherstation.internal.handler;
15 import java.math.BigDecimal;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants;
20 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.BatteryStatus;
21 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SensorDevice;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.UnDefType;
36 * The {@link FineOffsetSensorHandler} keeps track of the signal and battery of the sensor attached to the gateway.
38 * @author Andreas Berger - Initial contribution
41 public class FineOffsetSensorHandler extends BaseThingHandler {
42 private boolean disposed;
44 public FineOffsetSensorHandler(Thing thing) {
49 public void handleCommand(ChannelUID channelUID, Command command) {
53 public void initialize() {
54 updateStatus(ThingStatus.ONLINE);
59 public void dispose() {
63 public void updateSensorState(@Nullable SensorDevice sensorDevice) {
67 if (sensorDevice == null) {
68 // this only happens, if sensor data was read out correctly from the gateway, but the things' device
69 // (sensor) is no longer part of the paired sensors
70 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE);
71 getThing().getChannels().forEach(c -> updateState(c.getUID(), UnDefType.UNDEF));
74 if (sensorDevice.getSignal() == 0) {
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
77 updateStatus(ThingStatus.ONLINE);
79 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_SIGNAL,
80 new DecimalType(sensorDevice.getSignal()));
81 BatteryStatus batteryStatus = sensorDevice.getBatteryStatus();
83 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_LOW_BATTERY,
84 batteryStatus.isLow() ? OnOffType.ON : OnOffType.OFF);
85 Integer percentage = batteryStatus.getPercentage();
86 if (percentage != null) {
87 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL,
88 new DecimalType(new BigDecimal(percentage)));
91 Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL);
92 if (channel != null) {
93 updateThing(editThing().withoutChannels(channel).build());
96 Double voltage = batteryStatus.getVoltage();
97 if (voltage != null) {
98 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_VOLTAGE,
99 new QuantityType<>(voltage, Units.VOLT));
102 Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_VOLTAGE);
103 if (channel != null) {
104 updateThing(editThing().withoutChannels(channel).build());