]> git.basschouten.com Git - openhab-addons.git/blob
ccdb0446262a5414d80dea28d36df4f6db05d557
[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.mini;
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.*;
19
20 import javax.measure.Unit;
21 import javax.measure.quantity.Temperature;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants;
25 import org.openhab.binding.wlanthermo.internal.WlanThermoInputException;
26 import org.openhab.binding.wlanthermo.internal.WlanThermoUnknownChannelException;
27 import org.openhab.binding.wlanthermo.internal.api.mini.dto.builtin.*;
28 import org.openhab.core.library.types.*;
29 import org.openhab.core.library.unit.ImperialUnits;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
34
35 /**
36  * The {@link WlanThermoMiniCommandHandler} is responsible for mapping the Commands to the respective data fields
37  * of the API.
38  *
39  * @author Christian Schlipp - Initial contribution
40  */
41 @NonNullByDefault
42 public class WlanThermoMiniCommandHandler {
43
44     public static final String ERROR = "er";
45
46     public static State getState(ChannelUID channelUID, App app)
47             throws WlanThermoUnknownChannelException, WlanThermoInputException {
48         String groupId = requireNonNull(channelUID.getGroupId());
49         Unit<Temperature> unit = "fahrenheit".equals(app.getTempUnit()) ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS;
50
51         if (SYSTEM.equals(groupId)) {
52             switch (channelUID.getIdWithoutGroup()) {
53                 case WlanThermoBindingConstants.SYSTEM_CPU_TEMP:
54                     if (app.getCpuTemp() == null) {
55                         return UnDefType.UNDEF;
56                     } else {
57                         return new DecimalType(app.getCpuTemp());
58                     }
59                 case WlanThermoBindingConstants.SYSTEM_CPU_LOAD:
60                     if (app.getCpuLoad() == null) {
61                         return UnDefType.UNDEF;
62                     } else {
63                         return new DecimalType(app.getCpuLoad());
64                     }
65             }
66         } else if (channelUID.getId().startsWith(CHANNEL_PREFIX)) {
67             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PREFIX.length()));
68             if (channelId >= 0 && channelId <= 9) {
69                 Channel channel = app.getChannel();
70                 if (channel == null) {
71                     return UnDefType.UNDEF;
72                 }
73                 Data data = channel.getData(channelId);
74                 switch (channelUID.getIdWithoutGroup()) {
75                     case WlanThermoBindingConstants.CHANNEL_NAME:
76                         return new StringType(data.getName());
77                     case WlanThermoBindingConstants.CHANNEL_TEMP:
78                         if (data.getState().equals(ERROR)) {
79                             return UnDefType.UNDEF;
80                         } else {
81                             return new QuantityType<>(data.getTemp(), unit);
82                         }
83                     case WlanThermoBindingConstants.CHANNEL_MIN:
84                         return new QuantityType<>(data.getTempMin(), unit);
85                     case WlanThermoBindingConstants.CHANNEL_MAX:
86                         return new QuantityType<>(data.getTempMax(), unit);
87                     case WlanThermoBindingConstants.CHANNEL_ALARM_DEVICE:
88                         return OnOffType.from(data.getAlert());
89                     case WlanThermoBindingConstants.CHANNEL_ALARM_OPENHAB_HIGH:
90                         if (!data.getState().equals(ERROR) && data.getTemp() > data.getTempMax()) {
91                             return OnOffType.ON;
92                         } else {
93                             return OnOffType.OFF;
94                         }
95                     case WlanThermoBindingConstants.CHANNEL_ALARM_OPENHAB_LOW:
96                         if (!data.getState().equals(ERROR) && data.getTemp() < data.getTempMin()) {
97                             return OnOffType.ON;
98                         } else {
99                             return OnOffType.OFF;
100                         }
101                     case WlanThermoBindingConstants.CHANNEL_COLOR:
102                         Color c = Color.decode(WlanThermoMiniUtil.toHex(data.getColor()));
103                         return HSBType.fromRGB(c.getRed(), c.getGreen(), c.getBlue());
104                     case WlanThermoBindingConstants.CHANNEL_COLOR_NAME:
105                         return new StringType(data.getColor());
106                 }
107             }
108         } else if (channelUID.getId().startsWith(CHANNEL_PITMASTER_PREFIX)) {
109             Pit pit;
110             if (groupId.equals(CHANNEL_PITMASTER_1)) {
111                 pit = app.getPit();
112             } else if (groupId.equals(CHANNEL_PITMASTER_2)) {
113                 pit = app.getPit2();
114             } else {
115                 return UnDefType.UNDEF;
116             }
117             if (pit == null || !pit.getEnabled()) {
118                 return UnDefType.UNDEF;
119             }
120             switch (channelUID.getIdWithoutGroup()) {
121                 case WlanThermoBindingConstants.CHANNEL_PITMASTER_ENABLED:
122                     return OnOffType.from(pit.getEnabled());
123                 case WlanThermoBindingConstants.CHANNEL_PITMASTER_CURRENT:
124                     return new DecimalType(pit.getCurrent());
125                 case WlanThermoBindingConstants.CHANNEL_PITMASTER_SETPOINT:
126                     return new QuantityType<>(pit.getSetpoint(), unit);
127                 case WlanThermoBindingConstants.CHANNEL_PITMASTER_DUTY_CYCLE:
128                     return new DecimalType(pit.getControlOut());
129                 case WlanThermoBindingConstants.CHANNEL_PITMASTER_LID_OPEN:
130                     return OnOffType.from(pit.getOpenLid().equals("True"));
131                 case WlanThermoBindingConstants.CHANNEL_PITMASTER_CHANNEL_ID:
132                     return new DecimalType(pit.getCh());
133             }
134         }
135         throw new WlanThermoUnknownChannelException(channelUID);
136     }
137
138     public static String getTrigger(ChannelUID channelUID, App app)
139             throws WlanThermoUnknownChannelException, WlanThermoInputException {
140         String groupId = requireNonNull(channelUID.getGroupId());
141
142         if (channelUID.getId().startsWith(CHANNEL_PREFIX)) {
143             int channelId = Integer.parseInt(groupId.substring(CHANNEL_PREFIX.length())) - 1;
144             if (channelId >= 0 && channelId <= 9) {
145                 Channel channel = app.getChannel();
146                 if (channel == null) {
147                     throw new WlanThermoInputException();
148                 }
149                 Data data = channel.getData(channelId);
150                 if (CHANNEL_ALARM_OPENHAB.equals(channelUID.getIdWithoutGroup())) {
151                     if (!data.getState().equals(ERROR)) {
152                         if (data.getTemp() > data.getTempMax()) {
153                             return TRIGGER_ALARM_MAX;
154                         } else if (data.getTemp() < data.getTempMin()) {
155                             return TRIGGER_ALARM_MIN;
156                         } else {
157                             return TRIGGER_NONE;
158                         }
159                     }
160                 }
161             }
162         }
163         throw new WlanThermoUnknownChannelException(channelUID);
164     }
165 }