]> git.basschouten.com Git - openhab-addons.git/blob
cf72a89ebaf935386b18eab079b113d8fc8f602f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.wlanthermo.internal.api.esp32;
14
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.*;
16 import static org.openhab.binding.wlanthermo.internal.WlanThermoUtil.requireNonNull;
17
18 import java.awt.Color;
19 import java.math.BigInteger;
20 import java.util.List;
21
22 import javax.measure.Unit;
23 import javax.measure.quantity.Temperature;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.wlanthermo.internal.WlanThermoInputException;
27 import org.openhab.binding.wlanthermo.internal.WlanThermoUnknownChannelException;
28 import org.openhab.binding.wlanthermo.internal.WlanThermoUtil;
29 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Channel;
30 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Data;
31 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Pm;
32 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.System;
33 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.settings.Settings;
34 import org.openhab.core.library.types.*;
35 import org.openhab.core.library.unit.ImperialUnits;
36 import org.openhab.core.library.unit.SIUnits;
37 import org.openhab.core.library.unit.Units;
38 import org.openhab.core.thing.ChannelUID;
39 import org.openhab.core.types.Command;
40 import org.openhab.core.types.State;
41 import org.openhab.core.types.UnDefType;
42
43 /**
44  * The {@link WlanThermoEsp32CommandHandler} is responsible for mapping the Commands to the respective data fields
45  * of the API.
46  *
47  * @author Christian Schlipp - Initial contribution
48  */
49 @NonNullByDefault
50 public class WlanThermoEsp32CommandHandler {
51
52     public static State getState(ChannelUID channelUID, Data data, Settings settings)
53             throws WlanThermoUnknownChannelException, WlanThermoInputException {
54
55         String groupId = requireNonNull(channelUID.getGroupId());
56         System system = data.getSystem();
57         Unit<Temperature> unit = "F".equals(system.getUnit()) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
58
59         List<Channel> channelList = data.getChannel();
60         if (SYSTEM.equals(groupId)) {
61             switch (channelUID.getIdWithoutGroup()) {
62                 case SYSTEM_SOC:
63                     if (system.getSoc() != null) {
64                         return new DecimalType(system.getSoc());
65                     } else {
66                         return UnDefType.UNDEF;
67                     }
68                 case SYSTEM_CHARGE:
69                     if (system.getCharge() != null) {
70                         return OnOffType.from(system.getCharge());
71                     } else {
72                         return UnDefType.UNDEF;
73                     }
74                 case SYSTEM_RSSI_SIGNALSTRENGTH:
75                     int dbm = system.getRssi();
76                     if (dbm >= -80) {
77                         return SIGNAL_STRENGTH_4;
78                     } else if (dbm >= -95) {
79                         return SIGNAL_STRENGTH_3;
80                     } else if (dbm >= -105) {
81                         return SIGNAL_STRENGTH_2;
82                     } else {
83                         return SIGNAL_STRENGTH_1;
84                     }
85                 case SYSTEM_RSSI:
86                     return new QuantityType<>(system.getRssi(), Units.DECIBEL_MILLIWATTS);
87             }
88         } else if (channelUID.getId().startsWith(CHANNEL_PREFIX)) {
89             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PREFIX.length())) - 1;
90             if (channelList != null && channelList.size() > 0 && channelId < channelList.size()) {
91                 Channel channel = channelList.get(channelId);
92                 switch (channelUID.getIdWithoutGroup()) {
93                     case CHANNEL_NAME:
94                         return new StringType(channel.getName());
95                     case CHANNEL_TYP:
96                         return new StringType(settings.getSensors().get(channel.getTyp()).getName());
97                     case CHANNEL_TEMP:
98                         return channel.getTemp() == 999.0 ? UnDefType.UNDEF
99                                 : new QuantityType<>(channel.getTemp(), unit);
100                     case CHANNEL_MIN:
101                         return new QuantityType<>(channel.getMin(), unit);
102                     case CHANNEL_MAX:
103                         return new QuantityType<>(channel.getMax(), unit);
104                     case CHANNEL_ALARM_DEVICE:
105                         return OnOffType.from(BigInteger.valueOf(channel.getAlarm()).testBit(1));
106                     case CHANNEL_ALARM_PUSH:
107                         return OnOffType.from(BigInteger.valueOf(channel.getAlarm()).testBit(0));
108                     case CHANNEL_ALARM_OPENHAB_HIGH:
109                         if (channel.getTemp() != 999 && channel.getTemp() > channel.getMax()) {
110                             return OnOffType.ON;
111                         } else {
112                             return OnOffType.OFF;
113                         }
114                     case CHANNEL_ALARM_OPENHAB_LOW:
115                         if (channel.getTemp() != 999 && channel.getTemp() < channel.getMin()) {
116                             return OnOffType.ON;
117                         } else {
118                             return OnOffType.OFF;
119                         }
120                     case CHANNEL_COLOR:
121                         String color = channel.getColor();
122                         if (color != null && !color.isEmpty()) {
123                             Color c = Color.decode(color);
124                             return HSBType.fromRGB(c.getRed(), c.getGreen(), c.getBlue());
125                         } else {
126                             return UnDefType.UNDEF;
127                         }
128                     case CHANNEL_COLOR_NAME:
129                         String colorHex = channel.getColor();
130                         if (colorHex != null && !colorHex.isEmpty()) {
131                             return new StringType(WlanThermoEsp32Util.toColorName(colorHex));
132                         } else {
133                             return UnDefType.UNDEF;
134                         }
135                 }
136             }
137         } else if (channelUID.getId().startsWith(CHANNEL_PITMASTER_PREFIX)) {
138             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PITMASTER_PREFIX.length())) - 1;
139             if (settings.getFeatures().getPitmaster() && data.getPitmaster() != null
140                     && data.getPitmaster().getPm() != null && data.getPitmaster().getPm().size() > channelId) {
141                 Pm pm = data.getPitmaster().getPm().get(channelId);
142                 switch (channelUID.getIdWithoutGroup()) {
143                     case CHANNEL_PITMASTER_CHANNEL_ID:
144                         return new DecimalType(pm.getChannel());
145                     case CHANNEL_PITMASTER_PIDPROFILE:
146                         return new DecimalType(pm.getPid());
147                     case CHANNEL_PITMASTER_DUTY_CYCLE:
148                         return new DecimalType(pm.getValue());
149                     case CHANNEL_PITMASTER_SETPOINT:
150                         return new QuantityType<>(pm.getSet(), unit);
151                     case CHANNEL_PITMASTER_STATE:
152                         return new StringType(pm.getTyp());
153                 }
154             } else {
155                 return UnDefType.UNDEF;
156             }
157         }
158         throw new WlanThermoUnknownChannelException(channelUID);
159     }
160
161     public static boolean setState(ChannelUID channelUID, Command command, Data data, Settings settings) {
162         String groupId;
163         try {
164             groupId = requireNonNull(channelUID.getGroupId());
165         } catch (WlanThermoInputException ignore) {
166             return false;
167         }
168
169         List<Channel> channelList = data.getChannel();
170         System system = data.getSystem();
171         Unit<Temperature> unit = "F".equals(system.getUnit()) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
172
173         if (channelUID.getId().startsWith(CHANNEL_PREFIX)) {
174             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PREFIX.length())) - 1;
175             if (channelList.size() > 0 && channelId < channelList.size()) {
176                 Channel channel = channelList.get(channelId);
177                 switch (channelUID.getIdWithoutGroup()) {
178                     case CHANNEL_NAME:
179                         if (command instanceof StringType) {
180                             channel.setName(command.toFullString());
181                             return true;
182                         }
183                         return false;
184                     case CHANNEL_MIN:
185                         if (command instanceof QuantityType) {
186                             try {
187                                 channel.setMin(requireNonNull(((QuantityType<?>) command).toUnit(unit)).doubleValue());
188                                 return true;
189                             } catch (WlanThermoInputException ignore) {
190                                 return false;
191                             }
192                         }
193                         return false;
194                     case CHANNEL_MAX:
195                         if (command instanceof QuantityType) {
196                             try {
197                                 channel.setMax(requireNonNull(((QuantityType<?>) command).toUnit(unit)).doubleValue());
198                                 return true;
199                             } catch (WlanThermoInputException ignore) {
200                                 return false;
201                             }
202                         }
203                         return false;
204                     case CHANNEL_ALARM_DEVICE:
205                         if (command instanceof OnOffType) {
206                             BigInteger value;
207                             if (command == OnOffType.ON) {
208                                 value = BigInteger.valueOf(channel.getAlarm()).setBit(1);
209                             } else {
210                                 value = BigInteger.valueOf(channel.getAlarm()).clearBit(1);
211                             }
212                             channel.setAlarm(value.intValue());
213                             return true;
214                         }
215                         return false;
216                     case CHANNEL_ALARM_PUSH:
217                         if (command instanceof OnOffType) {
218                             BigInteger value;
219                             if (command == OnOffType.ON) {
220                                 value = BigInteger.valueOf(channel.getAlarm()).setBit(0);
221                             } else {
222                                 value = BigInteger.valueOf(channel.getAlarm()).clearBit(0);
223                             }
224                             channel.setAlarm(value.intValue());
225                             return true;
226                         }
227                         return false;
228                     case CHANNEL_COLOR_NAME:
229                         if (command instanceof StringType) {
230                             channel.setColor(WlanThermoEsp32Util.toHex(((StringType) command).toString()));
231                             return true;
232                         }
233                         return false;
234                     case CHANNEL_COLOR:
235                         if (command instanceof HSBType) {
236                             channel.setColor(WlanThermoUtil.toHex((HSBType) command));
237                             return true;
238                         }
239                         return false;
240                 }
241             }
242         } else if (channelUID.getId().startsWith(CHANNEL_PITMASTER_PREFIX)) {
243             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PITMASTER_PREFIX.length())) - 1;
244             if (settings.getFeatures().getPitmaster() && data.getPitmaster() != null
245                     && data.getPitmaster().getPm() != null && data.getPitmaster().getPm().size() > channelId) {
246                 Pm pm = data.getPitmaster().getPm().get(channelId);
247                 switch (channelUID.getIdWithoutGroup()) {
248                     case CHANNEL_PITMASTER_CHANNEL_ID:
249                         pm.setChannel(((DecimalType) command).intValue());
250                         return true;
251                     case CHANNEL_PITMASTER_PIDPROFILE:
252                         pm.setPid(((DecimalType) command).intValue());
253                         return true;
254                     case CHANNEL_PITMASTER_SETPOINT:
255                         try {
256                             pm.setSet(requireNonNull(((QuantityType<?>) command).toUnit(unit)).doubleValue());
257                             return true;
258                         } catch (WlanThermoInputException ignore) {
259                             return false;
260                         }
261                     case CHANNEL_PITMASTER_STATE:
262                         String state = ((StringType) command).toString();
263                         if (state.equalsIgnoreCase("off") || state.equalsIgnoreCase("manual")
264                                 || state.equalsIgnoreCase("auto")) {
265                             pm.setTyp(state);
266                             return true;
267                         }
268                         return false;
269                 }
270             }
271         }
272         return false;
273     }
274
275     public static String getTrigger(ChannelUID channelUID, Data data)
276             throws WlanThermoUnknownChannelException, WlanThermoInputException {
277         String groupId = requireNonNull(channelUID.getGroupId());
278         List<Channel> channelList = data.getChannel();
279
280         if (channelUID.getId().startsWith(CHANNEL_PREFIX)) {
281             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PREFIX.length())) - 1;
282             if (channelList.size() > 0 && channelId < channelList.size()) {
283                 Channel channel = channelList.get(channelId);
284                 if (CHANNEL_ALARM_OPENHAB.equals(channelUID.getIdWithoutGroup())) {
285                     if (channel.getTemp() != 999) {
286                         if (channel.getTemp() > channel.getMax()) {
287                             return TRIGGER_ALARM_MAX;
288                         } else if (channel.getTemp() < channel.getMin()) {
289                             return TRIGGER_ALARM_MIN;
290                         } else {
291                             return TRIGGER_NONE;
292                         }
293                     }
294                 }
295             }
296         }
297         throw new WlanThermoUnknownChannelException(channelUID);
298     }
299 }