2 * Copyright (c) 2010-2021 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.io.UnsupportedEncodingException;
18 import java.net.CookieManager;
19 import java.net.MalformedURLException;
21 import java.net.URLEncoder;
22 import java.nio.charset.StandardCharsets;
23 import java.util.HashMap;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jetty.client.HttpClient;
31 import org.eclipse.jetty.client.api.ContentProvider;
32 import org.eclipse.jetty.client.api.ContentResponse;
33 import org.eclipse.jetty.client.api.Request;
34 import org.eclipse.jetty.client.util.StringContentProvider;
35 import org.eclipse.jetty.http.HttpMethod;
36 import org.eclipse.jetty.http.MimeTypes;
37 import org.openhab.core.library.types.OnOffType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link TouchWandRestClient} is responsible for handling low level commands units TouchWand WonderFull hub
45 * @author Roie Geron - Initial contribution
48 public class TouchWandRestClient {
50 private final Logger logger = LoggerFactory.getLogger(TouchWandRestClient.class);
52 static CookieManager cookieManager = new CookieManager();
54 private static final HttpMethod METHOD_GET = HttpMethod.GET;
55 private static final HttpMethod METHOD_POST = HttpMethod.POST;
57 private static final String CMD_LOGIN = "login";
58 private static final String CMD_LIST_UNITS = "listunits";
59 private static final String CMD_LIST_SCENARIOS = "listsencarios";
60 private static final String CMD_UNIT_ACTION = "action";
61 private static final String CMD_GET_UNIT_BY_ID = "getunitbyid";
63 private static final String ACTION_SWITCH_OFF = "{\"id\":%s,\"value\":" + SWITCH_STATUS_OFF + "}";
64 private static final String ACTION_SWITCH_ON = "{\"id\":%s,\"value\":" + SWITCH_STATUS_ON + "}";
65 private static final String ACTION_SHUTTER_DOWN = "{\"id\":%s,\"value\":0,\"type\":\"height\"}";
66 private static final String ACTION_SHUTTER_UP = "{\"id\":%s,\"value\":255,\"type\":\"height\"}";
67 private static final String ACTION_SHUTTER_STOP = "{\"id\":%s,\"value\":0,\"type\":\"stop\"}";
68 private static final String ACTION_SHUTTER_POSITION = "{\"id\":%s,\"value\":%s}";
69 private static final String ACTION_DIMMER_POSITION = "{\"id\":%s,\"value\":%s}";
71 private static final String CONTENT_TYPE_APPLICATION_JSON = MimeTypes.Type.APPLICATION_JSON.asString();
73 private static final int REQUEST_TIMEOUT_SEC = 10;
75 private static final Map<String, String> COMMAND_MAP = new HashMap<String, String>();
77 COMMAND_MAP.put(CMD_LOGIN, "/auth/login?");
78 COMMAND_MAP.put(CMD_LIST_UNITS, "/units/listUnits");
79 COMMAND_MAP.put(CMD_LIST_SCENARIOS, "/scenarios/listScenarios");
80 COMMAND_MAP.put(CMD_UNIT_ACTION, "/units/action");
81 COMMAND_MAP.put(CMD_GET_UNIT_BY_ID, "/units/getUnitByID?");
84 private String touchWandIpAddr = "";
85 private String touchWandPort = "";
86 private boolean isConnected = false;
87 private HttpClient httpClient;
89 public TouchWandRestClient(HttpClient httpClient) {
90 this.httpClient = httpClient;
93 public final boolean connect(String user, String pass, String ipAddr, String port) {
94 touchWandIpAddr = ipAddr;
96 isConnected = cmdLogin(user, pass, ipAddr);
101 private final boolean cmdLogin(String user, String pass, String ipAddr) {
104 String response = "";
107 encodedUser = URLEncoder.encode(user, StandardCharsets.UTF_8.toString());
108 encodedPass = URLEncoder.encode(pass, StandardCharsets.UTF_8.toString());
109 String command = buildUrl(CMD_LOGIN) + "user=" + encodedUser + "&" + "psw=" + encodedPass;
110 response = sendCommand(command, METHOD_GET, "");
111 } catch (UnsupportedEncodingException e) {
112 logger.warn("Error url encoding username or password : {}", e.getMessage());
115 return !response.equals("Unauthorized");
118 public String cmdListUnits() {
119 String response = "";
121 String command = buildUrl(CMD_LIST_UNITS);
122 response = sendCommand(command, METHOD_GET, "");
127 public String cmdGetUnitById(String id) {
128 String response = "";
131 String command = buildUrl(CMD_GET_UNIT_BY_ID) + "id=" + id;
132 response = sendCommand(command, METHOD_GET, "");
137 public void cmdSwitchOnOff(String id, OnOffType onoff) {
140 if (OnOffType.OFF.equals(onoff)) {
141 action = String.format(ACTION_SWITCH_OFF, id);
143 action = String.format(ACTION_SWITCH_ON, id);
145 cmdUnitAction(action);
148 public void cmdShutterUp(String id) {
149 String action = String.format(ACTION_SHUTTER_UP, id);
150 cmdUnitAction(action);
153 public void cmdShutterDown(String id) {
154 String action = String.format(ACTION_SHUTTER_DOWN, id);
155 cmdUnitAction(action);
158 public void cmdShutterPosition(String id, String position) {
159 String action = String.format(ACTION_SHUTTER_POSITION, id, position);
160 cmdUnitAction(action);
163 public void cmdShutterStop(String id) {
164 String action = String.format(ACTION_SHUTTER_STOP, id);
165 cmdUnitAction(action);
168 public void cmdDimmerPosition(String id, String position) {
169 String action = String.format(ACTION_DIMMER_POSITION, id, position);
170 cmdUnitAction(action);
173 private String cmdUnitAction(String action) {
174 String response = "";
176 String command = buildUrl(CMD_UNIT_ACTION);
177 response = sendCommand(command, METHOD_POST, action);
182 private String buildUrl(String command) {
183 String url = "http://" + touchWandIpAddr + ":" + touchWandPort + COMMAND_MAP.get(command);
187 private synchronized String sendCommand(String command, HttpMethod method, String content) {
188 ContentResponse response;
193 url = new URL(command);
194 } catch (MalformedURLException e) {
195 logger.warn("Error building URL {} : {}", command, e.getMessage());
199 request = httpClient.newRequest(url.toString()).timeout(REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS).method(method);
200 if (method.equals(METHOD_POST) && (!content.isEmpty())) {
201 ContentProvider contentProvider = new StringContentProvider(CONTENT_TYPE_APPLICATION_JSON, content,
202 StandardCharsets.UTF_8);
203 request = request.content(contentProvider);
207 response = request.send();
208 return response.getContentAsString();
209 } catch (InterruptedException | TimeoutException | ExecutionException e) {
210 logger.warn("Error opening connecton to {} : {} ", touchWandIpAddr, e.getMessage());