]> git.basschouten.com Git - openhab-addons.git/blob
ab1525b7146176ba08e1bf0bf8cedc7cd7155464
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.solax.internal.connectivity;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.charset.StandardCharsets;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.http.HttpMethod;
22 import org.openhab.core.io.net.http.HttpUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link LocalHttpConnector} class uses HttpUtil to retrieve the raw JSON data from Inverter's Wi-Fi module.
28  *
29  * @author Konstantin Polihronov - Initial contribution
30  */
31 @NonNullByDefault
32 public class LocalHttpConnector implements SolaxConnector {
33
34     private static final int HTTP_REQUEST_TIME_OUT = 5000;
35
36     private static final String CONTENT_TYPE = "text/html; charset=utf-8";
37
38     private final Logger logger = LoggerFactory.getLogger(LocalHttpConnector.class);
39
40     private static final String OPT_TYPE = "optType";
41     private static final String READ_REALTIME_DATA = "ReadRealTimeData";
42
43     private static final String PASSWORD = "pwd";
44     // The serial number of the Wifi dongle is the password for the connection (default)
45     private String passwordValue;
46     private String uri;
47
48     public LocalHttpConnector(String passwordValue, String host) {
49         this.passwordValue = passwordValue;
50         this.uri = "http://" + host;
51     }
52
53     @Override
54     public @Nullable String retrieveData() throws IOException {
55         String requestBody = createRequestBody();
56         logger.trace("Uri: {}, Request body: {}", uri, requestBody);
57         String result = HttpUtil.executeUrl(HttpMethod.POST.name(), uri,
58                 new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)), CONTENT_TYPE,
59                 HTTP_REQUEST_TIME_OUT);
60         logger.trace("Retrieved content = {}", result);
61         return result;
62     }
63
64     private String createRequestBody() {
65         StringBuilder sb = new StringBuilder();
66
67         sb.append(OPT_TYPE).append("=").append(READ_REALTIME_DATA);
68         sb.append("&");
69         sb.append(PASSWORD).append("=").append(passwordValue);
70
71         return sb.toString();
72     }
73 }