]> git.basschouten.com Git - openhab-addons.git/blob
6d43fd0d09c9676aff1d8501310b7472b78f1d82
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
14 package org.openhab.binding.touchwand.internal;
15
16 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.CookieManager;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.net.URLEncoder;
23 import java.nio.charset.StandardCharsets;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.TimeoutException;
29
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jetty.client.HttpClient;
32 import org.eclipse.jetty.client.api.ContentProvider;
33 import org.eclipse.jetty.client.api.ContentResponse;
34 import org.eclipse.jetty.client.api.Request;
35 import org.eclipse.jetty.client.util.StringContentProvider;
36 import org.eclipse.jetty.http.HttpMethod;
37 import org.eclipse.jetty.http.MimeTypes;
38 import org.openhab.core.library.types.OnOffType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * The {@link TouchWandRestClient} is responsible for handling low level commands units TouchWand WonderFull hub
44  * REST API interface
45  *
46  * @author Roie Geron - Initial contribution
47  */
48 @NonNullByDefault
49 public class TouchWandRestClient {
50
51     private final Logger logger = LoggerFactory.getLogger(TouchWandRestClient.class);
52
53     static CookieManager cookieManager = new CookieManager();
54
55     private static final HttpMethod METHOD_GET = HttpMethod.GET;
56     private static final HttpMethod METHOD_POST = HttpMethod.POST;
57
58     private static final String CMD_LOGIN = "login";
59     private static final String CMD_LIST_UNITS = "listunits";
60     private static final String CMD_LIST_SCENARIOS = "listsencarios";
61     private static final String CMD_UNIT_ACTION = "action";
62     private static final String CMD_GET_UNIT_BY_ID = "getunitbyid";
63
64     private static final String ACTION_SWITCH_OFF = "{\"id\":%s,\"value\":" + SWITCH_STATUS_OFF + "}";
65     private static final String ACTION_SWITCH_ON = "{\"id\":%s,\"value\":" + SWITCH_STATUS_ON + "}";
66     private static final String ACTION_SHUTTER_DOWN = "{\"id\":%s,\"value\":0,\"type\":\"height\"}";
67     private static final String ACTION_SHUTTER_UP = "{\"id\":%s,\"value\":255,\"type\":\"height\"}";
68     private static final String ACTION_SHUTTER_STOP = "{\"id\":%s,\"value\":0,\"type\":\"stop\"}";
69     private static final String ACTION_SHUTTER_POSITION = "{\"id\":%s,\"value\":%s}";
70     private static final String ACTION_DIMMER_POSITION = "{\"id\":%s,\"value\":%s}";
71
72     private static final String CONTENT_TYPE_APPLICATION_JSON = MimeTypes.Type.APPLICATION_JSON.asString();
73
74     private static final int REQUEST_TIMEOUT_SEC = 10;
75
76     private static final Map<String, String> COMMAND_MAP = new HashMap<String, String>();
77     static {
78         COMMAND_MAP.put(CMD_LOGIN, "/auth/login?");
79         COMMAND_MAP.put(CMD_LIST_UNITS, "/units/listUnits");
80         COMMAND_MAP.put(CMD_LIST_SCENARIOS, "/scenarios/listScenarios");
81         COMMAND_MAP.put(CMD_UNIT_ACTION, "/units/action");
82         COMMAND_MAP.put(CMD_GET_UNIT_BY_ID, "/units/getUnitByID?");
83     }
84
85     private String touchWandIpAddr = "";
86     private String touchWandPort = "";
87     private boolean isConnected = false;
88     private HttpClient httpClient;
89
90     public TouchWandRestClient(HttpClient httpClient) {
91         this.httpClient = httpClient;
92     }
93
94     public final boolean connect(String user, String pass, String ipAddr, String port) {
95         touchWandIpAddr = ipAddr;
96         touchWandPort = port;
97         isConnected = cmdLogin(user, pass, ipAddr);
98
99         return isConnected;
100     }
101
102     private final boolean cmdLogin(String user, String pass, String ipAddr) {
103         String encodedUser;
104         String encodedPass;
105         String response = "";
106
107         try {
108             encodedUser = URLEncoder.encode(user, StandardCharsets.UTF_8.toString());
109             encodedPass = URLEncoder.encode(pass, StandardCharsets.UTF_8.toString());
110             String command = buildUrl(CMD_LOGIN) + "user=" + encodedUser + "&" + "psw=" + encodedPass;
111             response = sendCommand(command, METHOD_GET, "");
112         } catch (UnsupportedEncodingException e) {
113             logger.warn("Error url encoding username or password : {}", e.getMessage());
114         }
115
116         return !response.equals("Unauthorized");
117     }
118
119     public String cmdListUnits() {
120         String command = buildUrl(CMD_LIST_UNITS);
121         String response = sendCommand(command, METHOD_GET, "");
122
123         return response;
124     }
125
126     public String cmdGetUnitById(String id) {
127         String command = buildUrl(CMD_GET_UNIT_BY_ID) + "id=" + id;
128         String response = sendCommand(command, METHOD_GET, "");
129
130         return response;
131     }
132
133     public void cmdSwitchOnOff(String id, OnOffType onoff) {
134         String action;
135
136         if (OnOffType.OFF.equals(onoff)) {
137             action = String.format(ACTION_SWITCH_OFF, id);
138         } else {
139             action = String.format(ACTION_SWITCH_ON, id);
140         }
141         cmdUnitAction(action);
142     }
143
144     public void cmdShutterUp(String id) {
145         String action = String.format(ACTION_SHUTTER_UP, id);
146         cmdUnitAction(action);
147     }
148
149     public void cmdShutterDown(String id) {
150         String action = String.format(ACTION_SHUTTER_DOWN, id);
151         cmdUnitAction(action);
152     }
153
154     public void cmdShutterPosition(String id, String position) {
155         String action = String.format(ACTION_SHUTTER_POSITION, id, position);
156         cmdUnitAction(action);
157     }
158
159     public void cmdShutterStop(String id) {
160         String action = String.format(ACTION_SHUTTER_STOP, id);
161         cmdUnitAction(action);
162     }
163
164     public void cmdDimmerPosition(String id, String position) {
165         String action = String.format(ACTION_DIMMER_POSITION, id, position);
166         cmdUnitAction(action);
167     }
168
169     private String cmdUnitAction(String action) {
170         String command = buildUrl(CMD_UNIT_ACTION);
171         String response = sendCommand(command, METHOD_POST, action);
172
173         return response;
174     }
175
176     private String buildUrl(String command) {
177         String url = "http://" + touchWandIpAddr + ":" + touchWandPort + COMMAND_MAP.get(command);
178         return url;
179     }
180
181     private synchronized String sendCommand(String command, HttpMethod method, String content) {
182         ContentResponse response;
183         Request request;
184
185         URL url = null;
186         try {
187             url = new URL(command);
188         } catch (MalformedURLException e) {
189             logger.warn("Error building URL {} : {}", command, e.getMessage());
190             return "";
191         }
192
193         request = httpClient.newRequest(url.toString()).timeout(REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS).method(method);
194         if (method.equals(METHOD_POST) && (!content.isEmpty())) {
195             ContentProvider contentProvider = new StringContentProvider(CONTENT_TYPE_APPLICATION_JSON, content,
196                     StandardCharsets.UTF_8);
197             request = request.content(contentProvider);
198         }
199
200         try {
201             response = request.send();
202             return response.getContentAsString();
203         } catch (InterruptedException | TimeoutException | ExecutionException e) {
204             logger.warn("Error opening connecton to {} : {} ", touchWandIpAddr, e.getMessage());
205         }
206         return "";
207     }
208 }