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.solarmax.internal.connector;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.net.InetSocketAddress;
19 import java.net.Socket;
20 import java.net.UnknownHostException;
21 import java.time.ZonedDateTime;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
34 * The {@link SolarMaxConnector} class is used to communicated with the SolarMax device (on a binary level)
36 * With a little help from https://github.com/sushiguru/solar-pv/blob/master/solmax/pv.php
38 * @author Jamie Townsend - Initial contribution
41 public class SolarMaxConnector {
44 * default port number of SolarMax devices is...
46 private static final int DEFAULT_PORT = 12345;
48 private static final Logger LOGGER = LoggerFactory.getLogger(SolarMaxConnector.class);
51 * default timeout for socket connections is 1 second
53 private static final int CONNECTION_TIMEOUT = 1000;
56 * default timeout for socket responses is 10 seconds
58 private static int responseTimeout = 10000;
61 * gets all known values from the SolarMax device addressable at host:portNumber
63 * @param host hostname or ip address of the SolarMax device to be contacted
64 * @param portNumber portNumber the SolarMax is listening on (default is 12345)
65 * @param deviceAddress
67 * @throws SolarMaxException if some other exception occurs
69 public static SolarMaxData getAllValuesFromSolarMax(final String host, final int portNumber,
70 final int deviceAddress) throws SolarMaxException {
71 List<SolarMaxCommandKey> commandList = new ArrayList<>();
73 for (SolarMaxCommandKey solarMaxCommandKey : SolarMaxCommandKey.values()) {
74 if (solarMaxCommandKey != SolarMaxCommandKey.UNKNOWN) {
75 commandList.add(solarMaxCommandKey);
79 SolarMaxData solarMaxData = new SolarMaxData();
81 // get the data from the SolarMax device. If we didn't get as many values back as we asked for, there were
82 // communications problems, so set communicationSuccessful appropriately
84 Map<SolarMaxCommandKey, @Nullable String> valuesFromSolarMax = getValuesFromSolarMax(host, portNumber,
85 deviceAddress, commandList);
86 boolean allCommandsAnswered = true;
87 for (SolarMaxCommandKey solarMaxCommandKey : commandList) {
88 if (!valuesFromSolarMax.containsKey(solarMaxCommandKey)) {
89 allCommandsAnswered = false;
93 solarMaxData.setDataDateTime(ZonedDateTime.now());
94 solarMaxData.setCommunicationSuccessful(allCommandsAnswered);
95 solarMaxData.setData(valuesFromSolarMax);
101 * gets values from the SolarMax device addressable at host:portNumber
103 * @param host hostname or ip address of the SolarMax device to be contacted
104 * @param portNumber portNumber the SolarMax is listening on (default is 12345)
105 * @param commandList a list of commands to be sent to the SolarMax device
107 * @throws UnknownHostException if the host is unknown
108 * @throws SolarMaxException if some other exception occurs
110 private static Map<SolarMaxCommandKey, @Nullable String> getValuesFromSolarMax(final String host,
111 final int portNumber, final int deviceAddress, final List<SolarMaxCommandKey> commandList)
112 throws SolarMaxException {
115 Map<SolarMaxCommandKey, @Nullable String> returnMap = new HashMap<>();
117 // SolarMax can't answer correclty if too many commands are send in a single request, so limit it to 16 at a
119 int maxConcurrentCommands = 16;
120 int requestsRequired = (commandList.size() / maxConcurrentCommands);
121 if (commandList.size() % maxConcurrentCommands != 0) {
122 requestsRequired = requestsRequired + 1;
124 for (int requestNumber = 0; requestNumber < requestsRequired; requestNumber++) {
125 LOGGER.debug(" Requesting data from {}:{} (Device Address {}) with timeout of {}ms", host, portNumber,
126 deviceAddress, responseTimeout);
128 int firstCommandNumber = requestNumber * maxConcurrentCommands;
129 int lastCommandNumber = (requestNumber + 1) * maxConcurrentCommands;
130 if (lastCommandNumber > commandList.size()) {
131 lastCommandNumber = commandList.size();
133 List<SolarMaxCommandKey> commandsToSend = commandList.subList(firstCommandNumber, lastCommandNumber);
136 socket = getSocketConnection(host, portNumber);
137 } catch (UnknownHostException e) {
138 throw new SolarMaxConnectionException(e);
140 returnMap.putAll(getValuesFromSolarMax(socket, deviceAddress, commandsToSend));
142 // SolarMax can't deal with requests too close to one another, so just wait a moment
145 } catch (InterruptedException e) {
152 static String getCommandString(List<SolarMaxCommandKey> commandList) {
153 String commandString = "";
154 for (SolarMaxCommandKey command : commandList) {
155 if (!commandString.isEmpty()) {
156 commandString = commandString + ";";
158 commandString = commandString + command.getCommandKey();
160 return commandString;
163 private static Map<SolarMaxCommandKey, @Nullable String> getValuesFromSolarMax(final Socket socket,
164 final int deviceAddress, final List<SolarMaxCommandKey> commandList) throws SolarMaxException {
165 OutputStream outputStream = null;
166 InputStream inputStream = null;
168 outputStream = socket.getOutputStream();
169 inputStream = socket.getInputStream();
171 return getValuesFromSolarMax(outputStream, inputStream, deviceAddress, commandList);
172 } catch (final SolarMaxException | IOException e) {
173 throw new SolarMaxException("Error getting input/output streams from socket", e);
177 if (outputStream != null) {
178 outputStream.close();
180 if (inputStream != null) {
183 } catch (final IOException e) {
184 // ignore the error, we're dying anyway...
189 private static Map<SolarMaxCommandKey, @Nullable String> getValuesFromSolarMax(final OutputStream outputStream,
190 final InputStream inputStream, final int deviceAddress, final List<SolarMaxCommandKey> commandList)
191 throws SolarMaxException {
192 Map<SolarMaxCommandKey, @Nullable String> returnedValues;
193 String commandString = getCommandString(commandList);
194 String request = contructRequest(deviceAddress, commandString);
196 LOGGER.trace(" ==>: {}", request);
198 outputStream.write(request.getBytes());
200 String response = "";
201 byte[] responseByte = new byte[1];
203 // get everything from the stream
205 // read one byte from the stream
206 int bytesRead = inputStream.read(responseByte);
208 // if there was nothing left, break
213 // add the received byte to the response
214 final String responseString = new String(responseByte);
215 response = response + responseString;
217 // if it was the final expected character "}", break
218 if ("}".equals(responseString)) {
223 LOGGER.trace(" <==: {}", response);
225 if (!validateResponse(response)) {
226 throw new SolarMaxException("Invalid response received: " + response);
229 returnedValues = extractValuesFromResponse(response);
231 return returnedValues;
232 } catch (IOException e) {
233 LOGGER.debug("Error communicating via input/output streams: {} ", e.getMessage());
234 throw new SolarMaxException(e);
239 * @param response e.g.
240 * "{01;FB;6D|64:KDY=82;KMT=8F;KYR=23F7;KT0=72F1;TNF=1386;TKK=28;PAC=1F70;PRL=28;IL1=236;UL1=8F9;SYS=4E28,0|19E5}"
241 * @return a map of keys and values
243 static Map<SolarMaxCommandKey, @Nullable String> extractValuesFromResponse(String response) {
244 final Map<SolarMaxCommandKey, @Nullable String> responseMap = new HashMap<>();
246 // in case there is no response
247 if (response.indexOf("|") == -1) {
248 LOGGER.warn("Response doesn't contain data. Response: {}", response);
252 // extract the body first
253 // start by getting the part of the response between the two pipes
254 String body = response.substring(response.indexOf("|") + 1, response.lastIndexOf("|"));
256 // the name/value pairs now lie after the ":"
257 body = body.substring(body.indexOf(":") + 1);
259 // split into an array of name=value pairs
260 String[] entries = body.split(";");
261 for (String entry : entries) {
263 if (entry.length() != 0) {
264 // could be split on "=" instead of fixed length or made to respect length of command, but they're all 3
265 // characters long (then plus "=")
266 String str = entry.substring(0, 3);
268 String responseValue = (entry.length() >= 5) ? entry.substring(4) : null;
270 SolarMaxCommandKey key = SolarMaxCommandKey.getKeyFromString(str);
271 if (key != SolarMaxCommandKey.UNKNOWN) {
272 responseMap.put(key, responseValue);
280 private static Socket getSocketConnection(final String host, int portNumber)
281 throws SolarMaxConnectionException, UnknownHostException {
282 portNumber = (portNumber == 0) ? DEFAULT_PORT : portNumber;
287 socket = new Socket();
288 socket.connect(new InetSocketAddress(host, portNumber), CONNECTION_TIMEOUT);
289 socket.setSoTimeout(responseTimeout);
290 } catch (final UnknownHostException e) {
292 } catch (final IOException e) {
293 throw new SolarMaxConnectionException(
294 "Error connecting to portNumber '" + portNumber + "' on host '" + host + "'", e);
300 public static boolean connectionTest(final String host, final int portNumber) throws UnknownHostException {
301 Socket socket = null;
304 socket = getSocketConnection(host, portNumber);
305 } catch (SolarMaxConnectionException e) {
308 if (socket != null) {
311 } catch (IOException e) {
312 // ignore any error while trying to close the socket
321 * @return timeout for responses in milliseconds
323 public static int getResponseTimeout() {
324 return responseTimeout;
328 * @param responseTimeout timeout for responses in milliseconds
330 public static void setResponseTimeout(int responseTimeout) {
331 SolarMaxConnector.responseTimeout = responseTimeout;
335 * @param destinationDevice device number - used if devices are daisy-chained. Normally it will be "1"
336 * @param questions appears to be able to handle multiple commands. For now, one at a time is good fishing
337 * @return the request to be sent to the SolarMax device
339 static String contructRequest(final int deviceAddress, final String questions) {
341 String dstHex = String.format("%02X", deviceAddress); // destinationDevice defaults to 1
344 String msg = "64:" + questions;
345 int lenInt = ("{" + src + ";" + dstHex + ";" + len + "|" + msg + "|" + cs + "}").length();
347 // given the following, I'd expect problems if the request is longer than 255 characters. Since I'm not sure
348 // though, I won't fixe what isn't (yet) broken
349 String lenHex = String.format("%02X", lenInt);
351 String checksum = calculateChecksum16(src + ";" + dstHex + ";" + lenHex + "|" + msg + "|");
353 return "{" + src + ";" + dstHex + ";" + lenHex + "|" + msg + "|" + checksum + "}";
357 * calculates the "checksum16" of the given string argument
359 static String calculateChecksum16(String str) {
360 byte[] bytes = str.getBytes();
363 // loop through each of the bytes and add them together
364 for (byte aByte : bytes) {
368 // calculate the "checksum16"
369 sum = sum % (int) Math.pow(2, 16);
371 // return Integer.toHexString(sum);
372 return String.format("%04X", sum);
375 static boolean validateResponse(final String header) {
376 // probably should implement a patter matcher with a patternString like "/\\{([0-9A-F]{2});FB;([0-9A-F]{2})/",