]> git.basschouten.com Git - openhab-addons.git/blob
5d2c5765377b4af269d28d422e2162055aaad38c
[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.tplinksmarthome.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.tplinksmarthome.internal.model.GetRealtime;
18 import org.openhab.binding.tplinksmarthome.internal.model.GetSysinfo;
19 import org.openhab.binding.tplinksmarthome.internal.model.GsonUtil;
20 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
21 import org.openhab.binding.tplinksmarthome.internal.model.Realtime;
22 import org.openhab.binding.tplinksmarthome.internal.model.SetBrightness;
23 import org.openhab.binding.tplinksmarthome.internal.model.SetLedOff;
24 import org.openhab.binding.tplinksmarthome.internal.model.SetRelayState;
25 import org.openhab.binding.tplinksmarthome.internal.model.SetSwitchState;
26 import org.openhab.binding.tplinksmarthome.internal.model.Sysinfo;
27 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightState;
28 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightState.LightOnOff;
29 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightState.LightStateBrightness;
30 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightState.LightStateColor;
31 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightState.LightStateColorTemperature;
32 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightStateResponse;
33 import org.openhab.core.library.types.HSBType;
34 import org.openhab.core.library.types.OnOffType;
35
36 import com.google.gson.Gson;
37
38 /**
39  * Class to construct the tp-link json commands and convert retrieved results into data objects.
40  *
41  * @author Christian Fischer - Initial contribution
42  * @author Hilbrand Bouwkamp - Rewritten to use gson to parse json
43  */
44 @NonNullByDefault
45 public class Commands {
46
47     private static final String CONTEXT = "{\"context\":{\"child_ids\":[\"%s\"]},";
48     private static final String SYSTEM_GET_SYSINFO = "\"system\":{\"get_sysinfo\":{}}";
49     private static final String GET_SYSINFO = "{" + SYSTEM_GET_SYSINFO + "}";
50     private static final String REALTIME = "\"emeter\":{\"get_realtime\":{}}";
51     private static final String GET_REALTIME_AND_SYSINFO = "{" + SYSTEM_GET_SYSINFO + ", " + REALTIME + "}";
52     private static final String GET_REALTIME_BULB_AND_SYSINFO = "{" + SYSTEM_GET_SYSINFO
53             + ", \"smartlife.iot.common.emeter\":{\"get_realtime\":{}}}";
54
55     private final Gson gson = GsonUtil.createGson();
56     private final Gson gsonWithExpose = GsonUtil.createGsonWithExpose();
57
58     /**
59      * Returns the json to get the energy and sys info data from the device.
60      *
61      * @return The json string of the command to send to the device
62      */
63     public static String getRealtimeAndSysinfo() {
64         return GET_REALTIME_AND_SYSINFO;
65     }
66
67     /**
68      * Returns the json to get the energy and sys info data from the bulb.
69      *
70      * @return The json string of the command to send to the bulb
71      */
72     public static String getRealtimeBulbAndSysinfo() {
73         return GET_REALTIME_BULB_AND_SYSINFO;
74     }
75
76     /**
77      * Returns the json to get the energy and sys info data from an outlet device.
78      *
79      * @param id optional id of the device
80      * @return The json string of the command to send to the device
81      */
82     public static String getRealtimeWithContext(String id) {
83         return String.format(CONTEXT, id) + REALTIME + "}";
84     }
85
86     /**
87      * Returns the json response of the get_realtime command to the data object.
88      *
89      * @param realtimeResponse the json string
90      * @return The data object containing the energy data from the json string
91      */
92     @SuppressWarnings("null")
93     public Realtime getRealtimeResponse(String realtimeResponse) {
94         GetRealtime getRealtime = gson.fromJson(realtimeResponse, GetRealtime.class);
95         return getRealtime == null ? new Realtime() : getRealtime.getRealtime();
96     }
97
98     /**
99      * Returns the json to get the sys info data from the device.
100      *
101      * @return The json string of the command to send to the device
102      */
103     public static String getSysinfo() {
104         return GET_SYSINFO;
105     }
106
107     /**
108      * Returns the json response of the get_sysinfo command to the data object.
109      *
110      * @param getSysinfoReponse the json string
111      * @return The data object containing the state data from the json string
112      */
113     @SuppressWarnings("null")
114     public Sysinfo getSysinfoReponse(String getSysinfoReponse) {
115         GetSysinfo getSysinfo = gson.fromJson(getSysinfoReponse, GetSysinfo.class);
116         return getSysinfo == null ? new Sysinfo() : getSysinfo.getSysinfo();
117     }
118
119     /**
120      * Returns the json for the set_relay_state command to switch on or off.
121      *
122      * @param onOff the switch state to set
123      * @param childId optional child id if multiple children are supported by a single device
124      * @return The json string of the command to send to the device
125      */
126     public String setRelayState(OnOffType onOff, @Nullable String childId) {
127         SetRelayState relayState = new SetRelayState();
128         relayState.setRelayState(onOff);
129         if (childId != null) {
130             relayState.setChildId(childId);
131         }
132         return gsonWithExpose.toJson(relayState);
133     }
134
135     /**
136      * Returns the json response of the set_relay_state command to the data object.
137      *
138      * @param relayStateResponse the json string
139      * @return The data object containing the state data from the json string
140      */
141     public @Nullable SetRelayState setRelayStateResponse(String relayStateResponse) {
142         return gsonWithExpose.fromJson(relayStateResponse, SetRelayState.class);
143     }
144
145     /**
146      * Returns the json for the set_switch_state command to switch a dimmer on or off.
147      *
148      * @param onOff the switch state to set
149      * @return The json string of the command to send to the device
150      */
151     public String setSwitchState(OnOffType onOff) {
152         SetSwitchState switchState = new SetSwitchState();
153         switchState.setSwitchState(onOff);
154         return gsonWithExpose.toJson(switchState);
155     }
156
157     /**
158      * Returns the json response of the set_switch_state command to the data object.
159      *
160      * @param switchStateResponse the json string
161      * @return The data object containing the state data from the json string
162      */
163     public @Nullable SetSwitchState setSwitchStateResponse(String switchStateResponse) {
164         return gsonWithExpose.fromJson(switchStateResponse, SetSwitchState.class);
165     }
166
167     /**
168      * Returns the json for the set_brightness command to set the brightness value.
169      *
170      * @param brightness the brightness value to set
171      * @return The json string of the command to send to the device
172      */
173     public String setDimmerBrightness(int brightness) {
174         SetBrightness setBrightness = new SetBrightness();
175         setBrightness.setBrightness(brightness);
176         return gsonWithExpose.toJson(setBrightness);
177     }
178
179     /**
180      * Returns the json response of the set_brightness command to the data object.
181      *
182      * @param dimmerBrightnessResponse the json string
183      * @return The data object containing the state data from the json string
184      */
185     public @Nullable HasErrorResponse setDimmerBrightnessResponse(String dimmerBrightnessResponse) {
186         return gsonWithExpose.fromJson(dimmerBrightnessResponse, SetBrightness.class);
187     }
188
189     /**
190      * Returns the json for the set_light_state command to switch a bulb on or off.
191      *
192      * @param onOff the switch state to set
193      * @param transitionPeriod the transition period for the action to take place
194      * @return The json string of the command to send to the device
195      */
196     public String setLightState(OnOffType onOff, int transitionPeriod) {
197         TransitionLightState transitionLightState = new TransitionLightState();
198         LightOnOff lightState = new LightOnOff();
199         lightState.setOnOff(onOff);
200         lightState.setTransitionPeriod(transitionPeriod);
201         transitionLightState.setLightState(lightState);
202         return gson.toJson(transitionLightState);
203     }
204
205     /**
206      * Returns the json for the set_led_off command to switch the led of the device on or off.
207      *
208      * @param onOff the led state to set
209      * @param childId optional child id if multiple children are supported by a single device
210      * @return The json string of the command to send to the device
211      */
212     public String setLedOn(OnOffType onOff, @Nullable String childId) {
213         SetLedOff sLOff = new SetLedOff();
214         sLOff.setLed(onOff);
215         if (childId != null) {
216             sLOff.setChildId(childId);
217         }
218         return gsonWithExpose.toJson(sLOff);
219     }
220
221     /**
222      * Returns the json response for the set_led_off command to the data object.
223      *
224      * @param setLedOnResponse the json string
225      * @return The data object containing the data from the json string
226      */
227     public @Nullable SetLedOff setLedOnResponse(String setLedOnResponse) {
228         return gsonWithExpose.fromJson(setLedOnResponse, SetLedOff.class);
229     }
230
231     /**
232      * Returns the json for the set_light_State command to set the brightness.
233      *
234      * @param brightness the brightness value
235      * @param transitionPeriod the transition period for the action to take place
236      * @return The json string of the command to send to the device
237      */
238     public String setBrightness(int brightness, int transitionPeriod) {
239         TransitionLightState transitionLightState = new TransitionLightState();
240         LightStateBrightness lightState = new LightStateBrightness();
241         lightState.setOnOff(brightness == 0 ? OnOffType.OFF : OnOffType.ON);
242         lightState.setBrightness(brightness);
243         lightState.setTransitionPeriod(transitionPeriod);
244         transitionLightState.setLightState(lightState);
245         return gson.toJson(transitionLightState);
246     }
247
248     /**
249      * Returns the json for the set_light_State command to set the color.
250      *
251      * @param hsb the color to set
252      * @param transitionPeriod the transition period for the action to take place
253      * @return The json string of the command to send to the device
254      */
255     public String setColor(HSBType hsb, int transitionPeriod) {
256         TransitionLightState transitionLightState = new TransitionLightState();
257         LightStateColor lightState = new LightStateColor();
258         int brightness = hsb.getBrightness().intValue();
259         lightState.setOnOff(brightness == 0 ? OnOffType.OFF : OnOffType.ON);
260         lightState.setBrightness(brightness);
261         lightState.setHue(hsb.getHue().intValue());
262         lightState.setSaturation(hsb.getSaturation().intValue());
263         lightState.setTransitionPeriod(transitionPeriod);
264         transitionLightState.setLightState(lightState);
265         return gson.toJson(transitionLightState);
266     }
267
268     /**
269      * Returns the json for the set_light_State command to set the color temperature.
270      *
271      * @param colorTemperature the color temperature to set
272      * @param transitionPeriod the transition period for the action to take place
273      * @return The json string of the command to send to the device
274      */
275     public String setColorTemperature(int colorTemperature, int transitionPeriod) {
276         TransitionLightState transitionLightState = new TransitionLightState();
277         LightStateColorTemperature lightState = new LightStateColorTemperature();
278         lightState.setOnOff(OnOffType.ON);
279         lightState.setColorTemperature(colorTemperature);
280         lightState.setTransitionPeriod(transitionPeriod);
281         transitionLightState.setLightState(lightState);
282         return gson.toJson(transitionLightState);
283     }
284
285     /**
286      * Returns the json response for the set_light_state command.
287      *
288      * @param response the json string
289      * @return The data object containing the state data from the json string
290      */
291     public @Nullable TransitionLightStateResponse setTransitionLightStateResponse(String response) {
292         return gson.fromJson(response, TransitionLightStateResponse.class);
293     }
294 }