]> git.basschouten.com Git - openhab-addons.git/blob
88506094bb640e1d7696495196b2155239c49a80
[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.fineoffsetweatherstation.internal.handler;
14
15 import java.math.BigDecimal;
16
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.thing.Channel;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.UnDefType;
33
34 import tech.units.indriya.unit.Units;
35
36 /**
37  * The {@link FineOffsetSensorHandler} keeps track of the signal and battery of the sensor attached to the gateway.
38  *
39  * @author Andreas Berger - Initial contribution
40  */
41 @NonNullByDefault
42 public class FineOffsetSensorHandler extends BaseThingHandler {
43     private boolean disposed;
44
45     public FineOffsetSensorHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void handleCommand(ChannelUID channelUID, Command command) {
51     }
52
53     @Override
54     public void initialize() {
55         updateStatus(ThingStatus.ONLINE);
56         disposed = false;
57     }
58
59     @Override
60     public void dispose() {
61         disposed = true;
62     }
63
64     public void updateSensorState(@Nullable SensorDevice sensorDevice) {
65         if (disposed) {
66             return;
67         }
68         if (sensorDevice == null) {
69             // this only happens, if sensor data was read out correctly from the gateway, but the things' device
70             // (sensor) is no longer part of the paired sensors
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE);
72             getThing().getChannels().forEach(c -> updateState(c.getUID(), UnDefType.UNDEF));
73             return;
74         }
75         if (sensorDevice.getSignal() == 0) {
76             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
77         } else {
78             updateStatus(ThingStatus.ONLINE);
79         }
80         updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_SIGNAL,
81                 new DecimalType(sensorDevice.getSignal()));
82         BatteryStatus batteryStatus = sensorDevice.getBatteryStatus();
83
84         updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_LOW_BATTERY,
85                 batteryStatus.isLow() ? OnOffType.ON : OnOffType.OFF);
86         Integer percentage = batteryStatus.getPercentage();
87         if (percentage != null) {
88             updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL,
89                     new DecimalType(new BigDecimal(percentage)));
90         } else {
91             @Nullable
92             Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL);
93             if (channel != null) {
94                 updateThing(editThing().withoutChannels(channel).build());
95             }
96         }
97         Double voltage = batteryStatus.getVoltage();
98         if (voltage != null) {
99             updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_VOLTAGE,
100                     new QuantityType<>(voltage, Units.VOLT));
101         } else {
102             @Nullable
103             Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_VOLTAGE);
104             if (channel != null) {
105                 updateThing(editThing().withoutChannels(channel).build());
106             }
107         }
108     }
109 }