]> git.basschouten.com Git - openhab-addons.git/blob
b81894314bec5f4331a0f11b96cc0bed5f1c2f44
[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
31 /**
32  * The {@link FineOffsetSensorHandler} keeps track of the signal and battery of the sensor attached to the gateway.
33  *
34  * @author Andreas Berger - Initial contribution
35  */
36 @NonNullByDefault
37 public class FineOffsetSensorHandler extends BaseThingHandler {
38     private boolean disposed;
39
40     public FineOffsetSensorHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     public void handleCommand(ChannelUID channelUID, Command command) {
46     }
47
48     @Override
49     public void initialize() {
50         updateStatus(ThingStatus.ONLINE);
51         disposed = false;
52     }
53
54     @Override
55     public void dispose() {
56         disposed = true;
57     }
58
59     public void updateSensorState(@Nullable SensorDevice sensorDevice) {
60         if (disposed) {
61             return;
62         }
63         if (sensorDevice == null) {
64             updateStatus(ThingStatus.OFFLINE);
65             return;
66         }
67         if (sensorDevice.getSignal() == 0) {
68             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
69         } else {
70             updateStatus(ThingStatus.ONLINE);
71         }
72         updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_SIGNAL,
73                 new DecimalType(sensorDevice.getSignal()));
74         updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_LOW_BATTERY,
75                 sensorDevice.getBatteryStatus().isLow() ? OnOffType.ON : OnOffType.OFF);
76         Integer percentage = sensorDevice.getBatteryStatus().getPercentage();
77         if (percentage != null) {
78             updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL,
79                     new DecimalType(new BigDecimal(percentage)));
80         } else {
81             @Nullable
82             Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL);
83             if (channel != null) {
84                 updateThing(editThing().withoutChannels(channel).build());
85             }
86         }
87     }
88 }