]> git.basschouten.com Git - openhab-addons.git/blob
ce63a97c261cbcd6737d66f9a9e9032eca363df9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
34
35 /**
36  * The {@link FineOffsetSensorHandler} keeps track of the signal and battery of the sensor attached to the gateway.
37  *
38  * @author Andreas Berger - Initial contribution
39  */
40 @NonNullByDefault
41 public class FineOffsetSensorHandler extends BaseThingHandler {
42     private boolean disposed;
43
44     public FineOffsetSensorHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void handleCommand(ChannelUID channelUID, Command command) {
50     }
51
52     @Override
53     public void initialize() {
54         updateStatus(ThingStatus.ONLINE);
55         disposed = false;
56     }
57
58     @Override
59     public void dispose() {
60         disposed = true;
61     }
62
63     public void updateSensorState(@Nullable SensorDevice sensorDevice) {
64         if (disposed) {
65             return;
66         }
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));
72             return;
73         }
74         if (sensorDevice.getSignal() == 0) {
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
76         } else {
77             updateStatus(ThingStatus.ONLINE);
78         }
79         updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_SIGNAL,
80                 new DecimalType(sensorDevice.getSignal()));
81         BatteryStatus batteryStatus = sensorDevice.getBatteryStatus();
82
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)));
89         } else {
90             @Nullable
91             Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL);
92             if (channel != null) {
93                 updateThing(editThing().withoutChannels(channel).build());
94             }
95         }
96         Double voltage = batteryStatus.getVoltage();
97         if (voltage != null) {
98             updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_VOLTAGE,
99                     new QuantityType<>(voltage, Units.VOLT));
100         } else {
101             @Nullable
102             Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_VOLTAGE);
103             if (channel != null) {
104                 updateThing(editThing().withoutChannels(channel).build());
105             }
106         }
107     }
108 }