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