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