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.touchwand.internal;
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
17 import java.net.CookieManager;
18 import java.net.MalformedURLException;
20 import java.net.URLEncoder;
21 import java.nio.charset.StandardCharsets;
22 import java.util.HashMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jetty.client.HttpClient;
30 import org.eclipse.jetty.client.api.ContentProvider;
31 import org.eclipse.jetty.client.api.ContentResponse;
32 import org.eclipse.jetty.client.api.Request;
33 import org.eclipse.jetty.client.util.StringContentProvider;
34 import org.eclipse.jetty.http.HttpMethod;
35 import org.eclipse.jetty.http.MimeTypes;
36 import org.openhab.core.library.types.OnOffType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link TouchWandRestClient} is responsible for handling low level commands units TouchWand WonderFull hub
44 * @author Roie Geron - Initial contribution
47 public class TouchWandRestClient {
49 private final Logger logger = LoggerFactory.getLogger(TouchWandRestClient.class);
51 static CookieManager cookieManager = new CookieManager();
53 private static final HttpMethod METHOD_GET = HttpMethod.GET;
54 private static final HttpMethod METHOD_POST = HttpMethod.POST;
56 private static final String CMD_LOGIN = "login";
57 private static final String CMD_LIST_UNITS = "listunits";
58 private static final String CMD_LIST_SCENARIOS = "listsencarios";
59 private static final String CMD_UNIT_ACTION = "action";
60 private static final String CMD_GET_UNIT_BY_ID = "getunitbyid";
62 private static final String ACTION_SWITCH_OFF = "{\"id\":%s,\"value\":" + SWITCH_STATUS_OFF + "}";
63 private static final String ACTION_SWITCH_ON = "{\"id\":%s,\"value\":" + SWITCH_STATUS_ON + "}";
64 private static final String ACTION_SHUTTER_DOWN = "{\"id\":%s,\"value\":0,\"type\":\"height\"}";
65 private static final String ACTION_SHUTTER_UP = "{\"id\":%s,\"value\":255,\"type\":\"height\"}";
66 private static final String ACTION_SHUTTER_STOP = "{\"id\":%s,\"value\":0,\"type\":\"stop\"}";
67 private static final String ACTION_SHUTTER_POSITION = "{\"id\":%s,\"value\":%s}";
68 private static final String ACTION_DIMMER_POSITION = "{\"id\":%s,\"value\":%s}";
69 private static final String ACTION_THERMOSTAT_ON = "{\"id\":%s,\"value\":" + THERMOSTAT_STATE_ON + "}";
70 private static final String ACTION_THERMOSTAT_OFF = "{\"id\":%s,\"value\":" + THERMOSTAT_STATE_OFF + "}";
71 private static final String ACTION_THERMOSTAT_MODE = "{\"id\":%s,\"ac-all\":\"mode\",\"fan\":\"%s\"}";
72 private static final String ACTION_THERMOSTAT_FAN_LEVEL = "{\"id\":%s,\"ac-all\":\"fan\",\"fan\":\"%s\"}";
73 private static final String ACTION_THERMOSTAT_TARGET_TEMPERATURE = "{\"id\":%s,\"ac-all\":\"temp\",\"temp_val\":%s}";
75 private static final String CONTENT_TYPE_APPLICATION_JSON = MimeTypes.Type.APPLICATION_JSON.asString();
77 private static final int REQUEST_TIMEOUT_SEC = 10;
79 private static final Map<String, String> COMMAND_MAP = new HashMap<String, String>();
81 COMMAND_MAP.put(CMD_LOGIN, "/auth/login?");
82 COMMAND_MAP.put(CMD_LIST_UNITS, "/units/listUnits");
83 COMMAND_MAP.put(CMD_LIST_SCENARIOS, "/scenarios/listScenarios");
84 COMMAND_MAP.put(CMD_UNIT_ACTION, "/units/action");
85 COMMAND_MAP.put(CMD_GET_UNIT_BY_ID, "/units/getUnitByID?");
88 private String touchWandIpAddr = "";
89 private String touchWandPort = "";
90 private boolean isConnected = false;
91 private HttpClient httpClient;
93 public TouchWandRestClient(HttpClient httpClient) {
94 this.httpClient = httpClient;
97 public final boolean connect(String user, String pass, String ipAddr, String port) {
98 touchWandIpAddr = ipAddr;
100 isConnected = cmdLogin(user, pass, ipAddr);
105 private final boolean cmdLogin(String user, String pass, String ipAddr) {
106 String encodedUser = URLEncoder.encode(user, StandardCharsets.UTF_8);
107 String encodedPass = URLEncoder.encode(pass, StandardCharsets.UTF_8);
108 String response = "";
109 String command = buildUrl(CMD_LOGIN) + "user=" + encodedUser + "&" + "psw=" + encodedPass;
110 response = sendCommand(command, METHOD_GET, "");
112 return !"Unauthorized".equals(response);
115 public String cmdListUnits() {
116 String response = "";
118 String command = buildUrl(CMD_LIST_UNITS);
119 response = sendCommand(command, METHOD_GET, "");
124 public String cmdGetUnitById(String id) {
125 String response = "";
128 String command = buildUrl(CMD_GET_UNIT_BY_ID) + "id=" + id;
129 response = sendCommand(command, METHOD_GET, "");
134 public void cmdSwitchOnOff(String id, OnOffType onoff) {
137 if (OnOffType.OFF.equals(onoff)) {
138 action = String.format(ACTION_SWITCH_OFF, id);
140 action = String.format(ACTION_SWITCH_ON, id);
142 cmdUnitAction(action);
145 public void cmdShutterUp(String id) {
146 String action = String.format(ACTION_SHUTTER_UP, id);
147 cmdUnitAction(action);
150 public void cmdShutterDown(String id) {
151 String action = String.format(ACTION_SHUTTER_DOWN, id);
152 cmdUnitAction(action);
155 public void cmdShutterPosition(String id, String position) {
156 String action = String.format(ACTION_SHUTTER_POSITION, id, position);
157 cmdUnitAction(action);
160 public void cmdShutterStop(String id) {
161 String action = String.format(ACTION_SHUTTER_STOP, id);
162 cmdUnitAction(action);
165 public void cmdDimmerPosition(String id, String position) {
166 String action = String.format(ACTION_DIMMER_POSITION, id, position);
167 cmdUnitAction(action);
170 public void cmdThermostatOnOff(String id, OnOffType onoff) {
173 if (OnOffType.OFF.equals(onoff)) {
174 action = String.format(ACTION_THERMOSTAT_OFF, id);
176 action = String.format(ACTION_THERMOSTAT_ON, id);
178 cmdUnitAction(action);
181 public void cmdThermostatMode(String id, String mode) {
182 String action = String.format(ACTION_THERMOSTAT_MODE, id, mode);
183 cmdUnitAction(action);
186 public void cmdThermostatFanLevel(String id, String fanLevel) {
187 String action = String.format(ACTION_THERMOSTAT_FAN_LEVEL, id, fanLevel);
188 cmdUnitAction(action);
191 public void cmdThermostatTargetTemperature(String id, String targetTemperature) {
192 String action = String.format(ACTION_THERMOSTAT_TARGET_TEMPERATURE, id, targetTemperature);
193 cmdUnitAction(action);
196 private String cmdUnitAction(String action) {
197 String response = "";
199 String command = buildUrl(CMD_UNIT_ACTION);
200 response = sendCommand(command, METHOD_POST, action);
205 private String buildUrl(String command) {
206 String url = "http://" + touchWandIpAddr + ":" + touchWandPort + COMMAND_MAP.get(command);
210 private synchronized String sendCommand(String command, HttpMethod method, String content) {
211 ContentResponse response;
216 url = new URL(command);
217 } catch (MalformedURLException e) {
218 logger.warn("Error building URL {} : {}", command, e.getMessage());
222 request = httpClient.newRequest(url.toString()).timeout(REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS).method(method);
223 if (method.equals(METHOD_POST) && (!content.isEmpty())) {
224 ContentProvider contentProvider = new StringContentProvider(CONTENT_TYPE_APPLICATION_JSON, content,
225 StandardCharsets.UTF_8);
226 request = request.content(contentProvider);
230 response = request.send();
231 return response.getContentAsString();
232 } catch (InterruptedException | TimeoutException | ExecutionException e) {
233 logger.warn("Error opening connection to {} : {} ", touchWandIpAddr, e.getMessage());