]> git.basschouten.com Git - openhab-addons.git/blob
844ecc9c31dda6a74a42c11e47583b62a4e9f1d9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.TRVHandler.SmartValveData;
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.SmartValveDTO;
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 TRVHandler} is responsible for handling commands, which are
39  * sent to one of the channels.
40  *
41  * @author Andrew Schofield - Initial contribution
42  * @author Hilbrand Bouwkamp - Simplified handler to handle null data
43  */
44 @NonNullByDefault
45 public class TRVHandler extends DraytonWiserThingHandler<SmartValveData> {
46
47     private String serialNumber = "";
48
49     public TRVHandler(final Thing thing) {
50         super(thing);
51     }
52
53     @Override
54     public void initialize() {
55         super.initialize();
56         serialNumber = getConfig().get("serialNumber").toString();
57     }
58
59     @Override
60     protected void handleCommand(final String channelId, final Command command) throws DraytonWiserApiException {
61         if (command instanceof OnOffType && CHANNEL_DEVICE_LOCKED.equals(channelId)) {
62             setDeviceLocked(OnOffType.ON.equals(command));
63         }
64     }
65
66     @Override
67     protected void refresh() {
68         updateState(CHANNEL_CURRENT_TEMPERATURE, this::getTemperature);
69         updateState(CHANNEL_CURRENT_DEMAND, this::getDemand);
70         updateState(CHANNEL_CURRENT_SETPOINT, this::getSetPoint);
71         updateState(CHANNEL_CURRENT_SIGNAL_RSSI, this::getSignalRSSI);
72         updateState(CHANNEL_CURRENT_SIGNAL_LQI, this::getSignalLQI);
73         updateState(CHANNEL_CURRENT_BATTERY_VOLTAGE, this::getBatteryVoltage);
74         updateState(CHANNEL_CURRENT_WISER_SIGNAL_STRENGTH, this::getWiserSignalStrength);
75         updateState(CHANNEL_CURRENT_SIGNAL_STRENGTH, this::getSignalStrength);
76         updateState(CHANNEL_CURRENT_WISER_BATTERY_LEVEL, this::getWiserBatteryLevel);
77         updateState(CHANNEL_CURRENT_BATTERY_LEVEL, this::getBatteryLevel);
78         updateState(CHANNEL_ZIGBEE_CONNECTED, this::getZigbeeConnected);
79         updateState(CHANNEL_DEVICE_LOCKED, this::getDeviceLocked);
80     }
81
82     @Override
83     protected @Nullable SmartValveData collectData(final DraytonWiserDTO domainDTOProxy) {
84         final SmartValveDTO smartValve = domainDTOProxy.getSmartValve(serialNumber);
85         final DeviceDTO device = smartValve == null ? null
86                 : domainDTOProxy.getExtendedDeviceProperties(smartValve.getId());
87
88         return smartValve == null || device == null ? null : new SmartValveData(smartValve, device);
89     }
90
91     private State getSetPoint() {
92         return new QuantityType<>(getData().smartValve.getSetPoint() / 10.0, SIUnits.CELSIUS);
93     }
94
95     private State getDemand() {
96         return new QuantityType<>(getData().smartValve.getPercentageDemand(), Units.PERCENT);
97     }
98
99     private State getTemperature() {
100         final int fullScaleTemp = getData().smartValve.getMeasuredTemperature();
101
102         return OFFLINE_TEMPERATURE == fullScaleTemp ? UnDefType.UNDEF
103                 : new QuantityType<>(fullScaleTemp / 10.0, SIUnits.CELSIUS);
104     }
105
106     private State getSignalRSSI() {
107         return new DecimalType(getData().device.getRssi());
108     }
109
110     private State getSignalLQI() {
111         return new DecimalType(getData().device.getLqi());
112     }
113
114     private State getWiserSignalStrength() {
115         return new StringType(getData().device.getDisplayedSignalStrength());
116     }
117
118     private State getSignalStrength() {
119         return SignalStrength.toSignalStrength(getData().device.getDisplayedSignalStrength());
120     }
121
122     private State getBatteryVoltage() {
123         return new QuantityType<>(getData().device.getBatteryVoltage() / 10.0, Units.VOLT);
124     }
125
126     private State getWiserBatteryLevel() {
127         return new StringType(getData().device.getBatteryLevel());
128     }
129
130     private State getBatteryLevel() {
131         return BatteryLevel.toBatteryLevel(getData().device.getBatteryLevel());
132     }
133
134     private State getZigbeeConnected() {
135         return OnOffType.from(getData().smartValve.getMeasuredTemperature() != OFFLINE_TEMPERATURE);
136     }
137
138     private State getDeviceLocked() {
139         final Boolean locked = getData().device.getDeviceLockEnabled();
140
141         return locked == null ? UnDefType.UNDEF : OnOffType.from(locked);
142     }
143
144     private void setDeviceLocked(final Boolean state) throws DraytonWiserApiException {
145         getApi().setDeviceLocked(getData().device.getId(), state);
146     }
147
148     static class SmartValveData {
149         public final SmartValveDTO smartValve;
150         public final DeviceDTO device;
151
152         public SmartValveData(final SmartValveDTO smartValve, final DeviceDTO device) {
153             this.smartValve = smartValve;
154             this.device = device;
155         }
156     }
157 }