2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wlanthermo.internal.api.esp32;
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.*;
16 import static org.openhab.binding.wlanthermo.internal.WlanThermoUtil.requireNonNull;
18 import java.awt.Color;
19 import java.math.BigInteger;
20 import java.util.List;
22 import javax.measure.Unit;
23 import javax.measure.quantity.Temperature;
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;
44 * The {@link WlanThermoEsp32CommandHandler} is responsible for mapping the Commands to the respective data fields
47 * @author Christian Schlipp - Initial contribution
50 public class WlanThermoEsp32CommandHandler {
52 public static State getState(ChannelUID channelUID, Data data, Settings settings)
53 throws WlanThermoUnknownChannelException, WlanThermoInputException {
55 String groupId = requireNonNull(channelUID.getGroupId());
56 System system = data.getSystem();
57 Unit<Temperature> unit = "F".equals(system.getUnit()) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
59 List<Channel> channelList = data.getChannel();
60 if (SYSTEM.equals(groupId)) {
61 switch (channelUID.getIdWithoutGroup()) {
63 if (system.getSoc() != null) {
64 return new DecimalType(system.getSoc());
66 return UnDefType.UNDEF;
69 if (system.getCharge() != null) {
70 return OnOffType.from(system.getCharge());
72 return UnDefType.UNDEF;
74 case SYSTEM_RSSI_SIGNALSTRENGTH:
75 int dbm = system.getRssi();
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;
83 return SIGNAL_STRENGTH_1;
86 return new QuantityType<>(system.getRssi(), Units.DECIBEL_MILLIWATTS);
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()) {
94 return new StringType(channel.getName());
96 return new StringType(settings.getSensors().get(channel.getTyp()).getName());
98 return channel.getTemp() == 999.0 ? UnDefType.UNDEF
99 : new QuantityType<>(channel.getTemp(), unit);
101 return new QuantityType<>(channel.getMin(), unit);
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()) {
112 return OnOffType.OFF;
114 case CHANNEL_ALARM_OPENHAB_LOW:
115 if (channel.getTemp() != 999 && channel.getTemp() < channel.getMin()) {
118 return OnOffType.OFF;
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());
126 return UnDefType.UNDEF;
128 case CHANNEL_COLOR_NAME:
129 String colorHex = channel.getColor();
130 if (colorHex != null && !colorHex.isEmpty()) {
131 return new StringType(WlanThermoEsp32Util.toColorName(colorHex));
133 return UnDefType.UNDEF;
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());
155 return UnDefType.UNDEF;
158 throw new WlanThermoUnknownChannelException(channelUID);
161 public static boolean setState(ChannelUID channelUID, Command command, Data data, Settings settings) {
164 groupId = requireNonNull(channelUID.getGroupId());
165 } catch (WlanThermoInputException ignore) {
169 List<Channel> channelList = data.getChannel();
170 System system = data.getSystem();
171 Unit<Temperature> unit = "F".equals(system.getUnit()) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
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()) {
179 if (command instanceof StringType) {
180 channel.setName(command.toFullString());
185 if (command instanceof QuantityType) {
187 channel.setMin(requireNonNull(((QuantityType<?>) command).toUnit(unit)).doubleValue());
189 } catch (WlanThermoInputException ignore) {
195 if (command instanceof QuantityType) {
197 channel.setMax(requireNonNull(((QuantityType<?>) command).toUnit(unit)).doubleValue());
199 } catch (WlanThermoInputException ignore) {
204 case CHANNEL_ALARM_DEVICE:
205 if (command instanceof OnOffType) {
207 if (command == OnOffType.ON) {
208 value = BigInteger.valueOf(channel.getAlarm()).setBit(1);
210 value = BigInteger.valueOf(channel.getAlarm()).clearBit(1);
212 channel.setAlarm(value.intValue());
216 case CHANNEL_ALARM_PUSH:
217 if (command instanceof OnOffType) {
219 if (command == OnOffType.ON) {
220 value = BigInteger.valueOf(channel.getAlarm()).setBit(0);
222 value = BigInteger.valueOf(channel.getAlarm()).clearBit(0);
224 channel.setAlarm(value.intValue());
228 case CHANNEL_COLOR_NAME:
229 if (command instanceof StringType) {
230 channel.setColor(WlanThermoEsp32Util.toHex(((StringType) command).toString()));
235 if (command instanceof HSBType) {
236 channel.setColor(WlanThermoUtil.toHex((HSBType) command));
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());
251 case CHANNEL_PITMASTER_PIDPROFILE:
252 pm.setPid(((DecimalType) command).intValue());
254 case CHANNEL_PITMASTER_SETPOINT:
256 pm.setSet(requireNonNull(((QuantityType<?>) command).toUnit(unit)).doubleValue());
258 } catch (WlanThermoInputException ignore) {
261 case CHANNEL_PITMASTER_STATE:
262 String state = ((StringType) command).toString();
263 if (state.equalsIgnoreCase("off") || state.equalsIgnoreCase("manual")
264 || state.equalsIgnoreCase("auto")) {
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();
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;
297 throw new WlanThermoUnknownChannelException(channelUID);