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.solax.internal.connectivity;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.charset.StandardCharsets;
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;
27 * The {@link LocalHttpConnector} class uses HttpUtil to retrieve the raw JSON data from Inverter's Wi-Fi module.
29 * @author Konstantin Polihronov - Initial contribution
32 public class LocalHttpConnector implements SolaxConnector {
34 private static final int HTTP_REQUEST_TIME_OUT = 5000;
36 private static final String CONTENT_TYPE = "text/html; charset=utf-8";
38 private final Logger logger = LoggerFactory.getLogger(LocalHttpConnector.class);
40 private static final String OPT_TYPE = "optType";
41 private static final String READ_REALTIME_DATA = "ReadRealTimeData";
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;
48 public LocalHttpConnector(String passwordValue, String host) {
49 this.passwordValue = passwordValue;
50 this.uri = "http://" + host;
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);
64 private String createRequestBody() {
65 StringBuilder sb = new StringBuilder();
67 sb.append(OPT_TYPE).append("=").append(READ_REALTIME_DATA);
69 sb.append(PASSWORD).append("=").append(passwordValue);