]> git.basschouten.com Git - openhab-addons.git/blob
b1405923491f981dfbda85a067411c1e02c4f6e7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 package org.openhab.binding.touchwand.internal;
14
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
16
17 import java.io.UnsupportedEncodingException;
18 import java.net.CookieManager;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.net.URLEncoder;
22 import java.nio.charset.StandardCharsets;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28
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;
40
41 /**
42  * The {@link TouchWandRestClient} is responsible for handling low level commands units TouchWand WonderFull hub
43  * REST API interface
44  *
45  * @author Roie Geron - Initial contribution
46  */
47 @NonNullByDefault
48 public class TouchWandRestClient {
49
50     private final Logger logger = LoggerFactory.getLogger(TouchWandRestClient.class);
51
52     static CookieManager cookieManager = new CookieManager();
53
54     private static final HttpMethod METHOD_GET = HttpMethod.GET;
55     private static final HttpMethod METHOD_POST = HttpMethod.POST;
56
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";
62
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}";
70
71     private static final String CONTENT_TYPE_APPLICATION_JSON = MimeTypes.Type.APPLICATION_JSON.asString();
72
73     private static final int REQUEST_TIMEOUT_SEC = 10;
74
75     private static final Map<String, String> COMMAND_MAP = new HashMap<String, String>();
76     static {
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?");
82     }
83
84     private String touchWandIpAddr = "";
85     private String touchWandPort = "";
86     private boolean isConnected = false;
87     private HttpClient httpClient;
88
89     public TouchWandRestClient(HttpClient httpClient) {
90         this.httpClient = httpClient;
91     }
92
93     public final boolean connect(String user, String pass, String ipAddr, String port) {
94         touchWandIpAddr = ipAddr;
95         touchWandPort = port;
96         isConnected = cmdLogin(user, pass, ipAddr);
97
98         return isConnected;
99     }
100
101     private final boolean cmdLogin(String user, String pass, String ipAddr) {
102         String encodedUser;
103         String encodedPass;
104         String response = "";
105
106         try {
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());
113         }
114
115         return !response.equals("Unauthorized");
116     }
117
118     public String cmdListUnits() {
119         String response = "";
120         if (isConnected) {
121             String command = buildUrl(CMD_LIST_UNITS);
122             response = sendCommand(command, METHOD_GET, "");
123         }
124         return response;
125     }
126
127     public String cmdGetUnitById(String id) {
128         String response = "";
129
130         if (isConnected) {
131             String command = buildUrl(CMD_GET_UNIT_BY_ID) + "id=" + id;
132             response = sendCommand(command, METHOD_GET, "");
133         }
134         return response;
135     }
136
137     public void cmdSwitchOnOff(String id, OnOffType onoff) {
138         String action;
139
140         if (OnOffType.OFF.equals(onoff)) {
141             action = String.format(ACTION_SWITCH_OFF, id);
142         } else {
143             action = String.format(ACTION_SWITCH_ON, id);
144         }
145         cmdUnitAction(action);
146     }
147
148     public void cmdShutterUp(String id) {
149         String action = String.format(ACTION_SHUTTER_UP, id);
150         cmdUnitAction(action);
151     }
152
153     public void cmdShutterDown(String id) {
154         String action = String.format(ACTION_SHUTTER_DOWN, id);
155         cmdUnitAction(action);
156     }
157
158     public void cmdShutterPosition(String id, String position) {
159         String action = String.format(ACTION_SHUTTER_POSITION, id, position);
160         cmdUnitAction(action);
161     }
162
163     public void cmdShutterStop(String id) {
164         String action = String.format(ACTION_SHUTTER_STOP, id);
165         cmdUnitAction(action);
166     }
167
168     public void cmdDimmerPosition(String id, String position) {
169         String action = String.format(ACTION_DIMMER_POSITION, id, position);
170         cmdUnitAction(action);
171     }
172
173     private String cmdUnitAction(String action) {
174         String response = "";
175         if (isConnected) {
176             String command = buildUrl(CMD_UNIT_ACTION);
177             response = sendCommand(command, METHOD_POST, action);
178         }
179         return response;
180     }
181
182     private String buildUrl(String command) {
183         String url = "http://" + touchWandIpAddr + ":" + touchWandPort + COMMAND_MAP.get(command);
184         return url;
185     }
186
187     private synchronized String sendCommand(String command, HttpMethod method, String content) {
188         ContentResponse response;
189         Request request;
190
191         URL url = null;
192         try {
193             url = new URL(command);
194         } catch (MalformedURLException e) {
195             logger.warn("Error building URL {} : {}", command, e.getMessage());
196             return "";
197         }
198
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);
204         }
205
206         try {
207             response = request.send();
208             return response.getContentAsString();
209         } catch (InterruptedException | TimeoutException | ExecutionException e) {
210             logger.warn("Error opening connecton to {} : {} ", touchWandIpAddr, e.getMessage());
211         }
212         return "";
213     }
214 }