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