2 * Copyright (c) 2010-2023 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.tplinksmarthome.internal;
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;
37 import com.google.gson.Gson;
40 * Class to construct the tp-link json commands and convert retrieved results into data objects.
42 * @author Christian Fischer - Initial contribution
43 * @author Hilbrand Bouwkamp - Rewritten to use gson to parse json
46 public class Commands {
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\":{}}}";
56 private final Gson gson = GsonUtil.createGson();
57 private final Gson gsonWithExpose = GsonUtil.createGsonWithExpose();
60 * Returns the json to get the energy and sys info data from the device.
62 * @return The json string of the command to send to the device
64 public static String getRealtimeAndSysinfo() {
65 return GET_REALTIME_AND_SYSINFO;
69 * Returns the json to get the energy and sys info data from the bulb.
71 * @return The json string of the command to send to the bulb
73 public static String getRealtimeBulbAndSysinfo() {
74 return GET_REALTIME_BULB_AND_SYSINFO;
78 * Returns the json to get the energy and sys info data from an outlet device.
80 * @param id optional id of the device
81 * @return The json string of the command to send to the device
83 public static String getRealtimeWithContext(final String id) {
84 return String.format(CONTEXT, id) + REALTIME + "}";
88 * Returns the json response of the get_realtime command to the data object.
90 * @param realtimeResponse the json string
91 * @return The data object containing the energy data from the json string
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();
100 * Returns the json to get the sys info data from the device.
102 * @return The json string of the command to send to the device
104 public static String getSysinfo() {
109 * Returns the json response of the get_sysinfo command to the data object.
111 * @param getSysinfoReponse the json string
112 * @return The data object containing the state data from the json string
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();
121 * Returns the json for the set_relay_state command to switch on or off.
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
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);
133 return gsonWithExpose.toJson(relayState);
137 * Returns the json response of the set_relay_state command to the data object.
139 * @param relayStateResponse the json string
140 * @return The data object containing the state data from the json string
142 public @Nullable SetRelayState setRelayStateResponse(final String relayStateResponse) {
143 return gsonWithExpose.fromJson(relayStateResponse, SetRelayState.class);
147 * Returns the json for the set_switch_state command to switch a dimmer on or off.
149 * @param onOff the switch state to set
150 * @return The json string of the command to send to the device
152 public String setSwitchState(final OnOffType onOff) {
153 final SetSwitchState switchState = new SetSwitchState();
154 switchState.setSwitchState(onOff);
155 return gsonWithExpose.toJson(switchState);
159 * Returns the json response of the set_switch_state command to the data object.
161 * @param switchStateResponse the json string
162 * @return The data object containing the state data from the json string
164 public @Nullable SetSwitchState setSwitchStateResponse(final String switchStateResponse) {
165 return gsonWithExpose.fromJson(switchStateResponse, SetSwitchState.class);
169 * Returns the json for the set_brightness command to set the brightness value.
171 * @param brightness the brightness value to set
172 * @return The json string of the command to send to the device
174 public String setDimmerBrightness(final int brightness) {
175 final SetBrightness setBrightness = new SetBrightness();
176 setBrightness.setBrightness(brightness);
177 return gsonWithExpose.toJson(setBrightness);
181 * Returns the json response of the set_brightness command to the data object.
183 * @param dimmerBrightnessResponse the json string
184 * @return The data object containing the state data from the json string
186 public @Nullable HasErrorResponse setDimmerBrightnessResponse(final String dimmerBrightnessResponse) {
187 return gsonWithExpose.fromJson(dimmerBrightnessResponse, SetBrightness.class);
191 * Returns the json for the set_led_off command to switch the led of the device on or off.
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
197 public String setLedOn(final OnOffType onOff, @Nullable final String childId) {
198 final SetLedOff sLOff = new SetLedOff();
200 if (childId != null) {
201 sLOff.setChildId(childId);
203 return gsonWithExpose.toJson(sLOff);
207 * Returns the json response for the set_led_off command to the data object.
209 * @param setLedOnResponse the json string
210 * @return The data object containing the data from the json string
212 public @Nullable SetLedOff setLedOnResponse(final String setLedOnResponse) {
213 return gsonWithExpose.fromJson(setLedOnResponse, SetLedOff.class);
217 * Returns the json for the transition_light_state command to switch a bulb on or off.
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
223 public String setTransitionLightState(final OnOffType onOff, final int transitionPeriod) {
224 return setTransitionLightState(new LightOnOff(), onOff, transitionPeriod);
228 * Returns the json for the set_light_State command to set the brightness.
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
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);
241 * Returns the json for the set_light_State command to set the color.
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
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);
257 * Returns the json for the set_light_State command to set the color temperature.
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
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);
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);
279 * Returns the json response for the set_light_state command.
281 * @param response the json string
282 * @return The data object containing the state data from the json string
284 public @Nullable TransitionLightStateResponse setTransitionLightStateResponse(final String response) {
285 return gson.fromJson(response, TransitionLightStateResponse.class);
288 // ---------------------------------------------------------------
291 * Returns the json for the set_light_state command to switch a light strip on or off.
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
297 public String setLightStripState(final OnOffType onOff, final int transition) {
298 return setLightStripState(new SetLightState.LightOnOff(), onOff, transition);
302 * Returns the json for the set_light_State command to set the brightness.
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
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);
315 * Returns the json for the set_light_State command to set the color.
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
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);
331 * Returns the json for the set_light_State command to set the color temperature.
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
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);
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);
354 * Returns the json response for the set_light_state command.
356 * @param response the json string
357 * @return The data object containing the state data from the json string
359 public @Nullable SetLightState setLightStripStateResponse(final String response) {
360 return gsonWithExpose.fromJson(response, SetLightState.class);