]> git.basschouten.com Git - openhab-addons.git/blob
487b9bc2e61074de1f833117189f4fc0382c0023
[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.draytonwiser.internal.handler;
14
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.BatteryLevel;
20 import org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.SignalStrength;
21 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApiException;
22 import org.openhab.binding.draytonwiser.internal.handler.RoomStatHandler.RoomStatData;
23 import org.openhab.binding.draytonwiser.internal.model.DeviceDTO;
24 import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
25 import org.openhab.binding.draytonwiser.internal.model.RoomStatDTO;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
36
37 /**
38  * The {@link RoomStatHandler} is responsible for handling commands, which are sent to one of the channels.
39  *
40  * @author Andrew Schofield - Initial contribution
41  * @author Hilbrand Bouwkamp - Simplified handler to handle null data
42  */
43 @NonNullByDefault
44 public class RoomStatHandler extends DraytonWiserThingHandler<RoomStatData> {
45
46     private String serialNumber = "";
47
48     public RoomStatHandler(final Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     public void initialize() {
54         super.initialize();
55         serialNumber = getConfig().get("serialNumber").toString();
56     }
57
58     @Override
59     protected void handleCommand(final String channelId, final Command command) throws DraytonWiserApiException {
60         if (command instanceof OnOffType && CHANNEL_DEVICE_LOCKED.equals(channelId)) {
61             setDeviceLocked(OnOffType.ON.equals(command));
62         }
63     }
64
65     @Override
66     protected void refresh() {
67         updateState(CHANNEL_CURRENT_TEMPERATURE, this::getTemperature);
68         updateState(CHANNEL_CURRENT_HUMIDITY, this::getHumidity);
69         updateState(CHANNEL_CURRENT_SETPOINT, this::getSetPoint);
70         updateState(CHANNEL_CURRENT_SIGNAL_RSSI, this::getSignalRSSI);
71         updateState(CHANNEL_CURRENT_SIGNAL_LQI, this::getSignalLQI);
72         updateState(CHANNEL_CURRENT_BATTERY_VOLTAGE, this::getBatteryVoltage);
73         updateState(CHANNEL_CURRENT_WISER_SIGNAL_STRENGTH, this::getWiserSignalStrength);
74         updateState(CHANNEL_CURRENT_SIGNAL_STRENGTH, this::getSignalStrength);
75         updateState(CHANNEL_CURRENT_WISER_BATTERY_LEVEL, this::getWiserBatteryLevel);
76         updateState(CHANNEL_CURRENT_BATTERY_LEVEL, this::getBatteryLevel);
77         updateState(CHANNEL_ZIGBEE_CONNECTED, this::getZigbeeConnected);
78         updateState(CHANNEL_DEVICE_LOCKED, this::getDeviceLocked);
79     }
80
81     @Override
82     protected @Nullable RoomStatData collectData(final DraytonWiserDTO domainDTOProxy) {
83         final RoomStatDTO roomStat = domainDTOProxy.getRoomStat(serialNumber);
84         final DeviceDTO device = roomStat == null ? null : domainDTOProxy.getExtendedDeviceProperties(roomStat.getId());
85
86         return roomStat == null || device == null ? null : new RoomStatData(roomStat, device);
87     }
88
89     private State getSetPoint() {
90         return new QuantityType<>(getData().roomStat.getSetPoint() / 10.0, SIUnits.CELSIUS);
91     }
92
93     private State getHumidity() {
94         final Integer humidity = getData().roomStat.getMeasuredHumidity();
95
96         return humidity == null ? UnDefType.UNDEF : new QuantityType<>(humidity, Units.PERCENT);
97     }
98
99     private State getTemperature() {
100         final int fullScaleTemp = getData().roomStat.getMeasuredTemperature();
101
102         return OFFLINE_TEMPERATURE == fullScaleTemp ? UnDefType.UNDEF
103                 : new QuantityType<>(fullScaleTemp / 10.0, SIUnits.CELSIUS);
104     }
105
106     private State getSignalRSSI() {
107         final Integer rssi = getData().device.getRssi();
108         return rssi == null ? UnDefType.UNDEF : new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS);
109     }
110
111     private State getSignalLQI() {
112         final Integer lqi = getData().device.getLqi();
113         return lqi == null ? UnDefType.UNDEF : new DecimalType(lqi);
114     }
115
116     private State getWiserSignalStrength() {
117         return new StringType(getData().device.getDisplayedSignalStrength());
118     }
119
120     private State getSignalStrength() {
121         return SignalStrength.toSignalStrength(getData().device.getDisplayedSignalStrength());
122     }
123
124     private State getBatteryVoltage() {
125         final Integer voltage = getData().device.getBatteryVoltage();
126         return voltage == null ? UnDefType.UNDEF : new QuantityType<>(voltage / 10.0, Units.VOLT);
127     }
128
129     private State getWiserBatteryLevel() {
130         return new StringType(getData().device.getBatteryLevel());
131     }
132
133     private State getBatteryLevel() {
134         return BatteryLevel.toBatteryLevel(getData().device.getBatteryLevel());
135     }
136
137     private State getZigbeeConnected() {
138         return OnOffType.from(OFFLINE_TEMPERATURE != getData().roomStat.getMeasuredTemperature());
139     }
140
141     private State getDeviceLocked() {
142         return getData().device.getDeviceLockEnabled() == null ? UnDefType.UNDEF
143                 : OnOffType.from(getData().device.getDeviceLockEnabled());
144     }
145
146     private void setDeviceLocked(final boolean state) throws DraytonWiserApiException {
147         getApi().setDeviceLocked(getData().device.getId(), state);
148     }
149
150     static class RoomStatData {
151         public final RoomStatDTO roomStat;
152         public final DeviceDTO device;
153
154         public RoomStatData(final RoomStatDTO roomStat, final DeviceDTO device) {
155             this.roomStat = roomStat;
156             this.device = device;
157         }
158     }
159 }