2 * Copyright (c) 2010-2022 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.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;
36 import com.google.gson.Gson;
39 * Class to construct the tp-link json commands and convert retrieved results into data objects.
41 * @author Christian Fischer - Initial contribution
42 * @author Hilbrand Bouwkamp - Rewritten to use gson to parse json
45 public class Commands {
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\":{}}}";
55 private final Gson gson = GsonUtil.createGson();
56 private final Gson gsonWithExpose = GsonUtil.createGsonWithExpose();
59 * Returns the json to get the energy and sys info data from the device.
61 * @return The json string of the command to send to the device
63 public static String getRealtimeAndSysinfo() {
64 return GET_REALTIME_AND_SYSINFO;
68 * Returns the json to get the energy and sys info data from the bulb.
70 * @return The json string of the command to send to the bulb
72 public static String getRealtimeBulbAndSysinfo() {
73 return GET_REALTIME_BULB_AND_SYSINFO;
77 * Returns the json to get the energy and sys info data from an outlet device.
79 * @param id optional id of the device
80 * @return The json string of the command to send to the device
82 public static String getRealtimeWithContext(String id) {
83 return String.format(CONTEXT, id) + REALTIME + "}";
87 * Returns the json response of the get_realtime command to the data object.
89 * @param realtimeResponse the json string
90 * @return The data object containing the energy data from the json string
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();
99 * Returns the json to get the sys info data from the device.
101 * @return The json string of the command to send to the device
103 public static String getSysinfo() {
108 * Returns the json response of the get_sysinfo command to the data object.
110 * @param getSysinfoReponse the json string
111 * @return The data object containing the state data from the json string
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();
120 * Returns the json for the set_relay_state command to switch on or off.
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
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);
132 return gsonWithExpose.toJson(relayState);
136 * Returns the json response of the set_relay_state command to the data object.
138 * @param relayStateResponse the json string
139 * @return The data object containing the state data from the json string
141 public @Nullable SetRelayState setRelayStateResponse(String relayStateResponse) {
142 return gsonWithExpose.fromJson(relayStateResponse, SetRelayState.class);
146 * Returns the json for the set_switch_state command to switch a dimmer on or off.
148 * @param onOff the switch state to set
149 * @return The json string of the command to send to the device
151 public String setSwitchState(OnOffType onOff) {
152 SetSwitchState switchState = new SetSwitchState();
153 switchState.setSwitchState(onOff);
154 return gsonWithExpose.toJson(switchState);
158 * Returns the json response of the set_switch_state command to the data object.
160 * @param switchStateResponse the json string
161 * @return The data object containing the state data from the json string
163 public @Nullable SetSwitchState setSwitchStateResponse(String switchStateResponse) {
164 return gsonWithExpose.fromJson(switchStateResponse, SetSwitchState.class);
168 * Returns the json for the set_brightness command to set the brightness value.
170 * @param brightness the brightness value to set
171 * @return The json string of the command to send to the device
173 public String setDimmerBrightness(int brightness) {
174 SetBrightness setBrightness = new SetBrightness();
175 setBrightness.setBrightness(brightness);
176 return gsonWithExpose.toJson(setBrightness);
180 * Returns the json response of the set_brightness command to the data object.
182 * @param dimmerBrightnessResponse the json string
183 * @return The data object containing the state data from the json string
185 public @Nullable HasErrorResponse setDimmerBrightnessResponse(String dimmerBrightnessResponse) {
186 return gsonWithExpose.fromJson(dimmerBrightnessResponse, SetBrightness.class);
190 * Returns the json for the set_light_state command to switch a bulb on or off.
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
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);
206 * Returns the json for the set_led_off command to switch the led of the device on or off.
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
212 public String setLedOn(OnOffType onOff, @Nullable String childId) {
213 SetLedOff sLOff = new SetLedOff();
215 if (childId != null) {
216 sLOff.setChildId(childId);
218 return gsonWithExpose.toJson(sLOff);
222 * Returns the json response for the set_led_off command to the data object.
224 * @param setLedOnResponse the json string
225 * @return The data object containing the data from the json string
227 public @Nullable SetLedOff setLedOnResponse(String setLedOnResponse) {
228 return gsonWithExpose.fromJson(setLedOnResponse, SetLedOff.class);
232 * Returns the json for the set_light_State command to set the brightness.
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
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);
249 * Returns the json for the set_light_State command to set the color.
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
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);
269 * Returns the json for the set_light_State command to set the color temperature.
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
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);
286 * Returns the json response for the set_light_state command.
288 * @param response the json string
289 * @return The data object containing the state data from the json string
291 public @Nullable TransitionLightStateResponse setTransitionLightStateResponse(String response) {
292 return gson.fromJson(response, TransitionLightStateResponse.class);