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:port
63 * @param host hostname or ip address of the SolarMax device to be contacted
64 * @param port port the SolarMax is listening on (default is 12345)
65 * @param commandList a list of commands to be sent to the SolarMax device
67 * @throws UnknownHostException if the host is unknown
68 * @throws SolarMaxException if some other exception occurs
70 public static SolarMaxData getAllValuesFromSolarMax(final String host, int port) 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, port, commandList);
85 boolean allCommandsAnswered = true;
86 for (SolarMaxCommandKey solarMaxCommandKey : commandList) {
87 if (!valuesFromSolarMax.containsKey(solarMaxCommandKey)) {
88 allCommandsAnswered = false;
92 solarMaxData.setDataDateTime(ZonedDateTime.now());
93 solarMaxData.setCommunicationSuccessful(allCommandsAnswered);
94 solarMaxData.setData(valuesFromSolarMax);
100 * gets values from the SolarMax device addressable at host:port
102 * @param host hostname or ip address of the SolarMax device to be contacted
103 * @param port port the SolarMax is listening on (default is 12345)
104 * @param commandList a list of commands to be sent to the SolarMax device
106 * @throws UnknownHostException if the host is unknown
107 * @throws SolarMaxException if some other exception occurs
109 private static Map<SolarMaxCommandKey, @Nullable String> getValuesFromSolarMax(final String host, int port,
110 final List<SolarMaxCommandKey> commandList) throws SolarMaxException {
113 Map<SolarMaxCommandKey, @Nullable String> returnMap = new HashMap<>();
115 // SolarMax can't answer correclty if too many commands are send in a single request, so limit it to 16 at a
117 int maxConcurrentCommands = 16;
118 int requestsRequired = (commandList.size() / maxConcurrentCommands);
119 if (commandList.size() % maxConcurrentCommands != 0) {
120 requestsRequired = requestsRequired + 1;
122 for (int requestNumber = 0; requestNumber < requestsRequired; requestNumber++) {
123 LOGGER.debug(" Requesting data from {}:{} with timeout of {}ms", host, port, responseTimeout);
125 int firstCommandNumber = requestNumber * maxConcurrentCommands;
126 int lastCommandNumber = (requestNumber + 1) * maxConcurrentCommands;
127 if (lastCommandNumber > commandList.size()) {
128 lastCommandNumber = commandList.size();
130 List<SolarMaxCommandKey> commandsToSend = commandList.subList(firstCommandNumber, lastCommandNumber);
133 socket = getSocketConnection(host, port);
134 } catch (UnknownHostException e) {
135 throw new SolarMaxConnectionException(e);
137 returnMap.putAll(getValuesFromSolarMax(socket, commandsToSend));
139 // SolarMax can't deal with requests too close to one another, so just wait a moment
142 } catch (InterruptedException e) {
149 static String getCommandString(List<SolarMaxCommandKey> commandList) {
150 String commandString = "";
151 for (SolarMaxCommandKey command : commandList) {
152 if (!commandString.isEmpty()) {
153 commandString = commandString + ";";
155 commandString = commandString + command.getCommandKey();
157 return commandString;
160 private static Map<SolarMaxCommandKey, @Nullable String> getValuesFromSolarMax(final Socket socket,
161 final List<SolarMaxCommandKey> commandList) throws SolarMaxException {
162 OutputStream outputStream = null;
163 InputStream inputStream = null;
165 outputStream = socket.getOutputStream();
166 inputStream = socket.getInputStream();
168 return getValuesFromSolarMax(outputStream, inputStream, commandList);
169 } catch (final SolarMaxException | IOException e) {
170 throw new SolarMaxException("Error getting input/output streams from socket", e);
174 if (outputStream != null) {
175 outputStream.close();
177 if (inputStream != null) {
180 } catch (final IOException e) {
181 // ignore the error, we're dying anyway...
186 private static Map<SolarMaxCommandKey, @Nullable String> getValuesFromSolarMax(final OutputStream outputStream,
187 final InputStream inputStream, final List<SolarMaxCommandKey> commandList) throws SolarMaxException {
188 Map<SolarMaxCommandKey, @Nullable String> returnedValues;
189 String commandString = getCommandString(commandList);
190 String request = contructRequest(commandString);
192 LOGGER.trace(" ==>: {}", request);
194 outputStream.write(request.getBytes());
196 String response = "";
197 byte[] responseByte = new byte[1];
199 // get everything from the stream
201 // read one byte from the stream
202 int bytesRead = inputStream.read(responseByte);
204 // if there was nothing left, break
209 // add the received byte to the response
210 final String responseString = new String(responseByte);
211 response = response + responseString;
213 // if it was the final expected character "}", break
214 if ("}".equals(responseString)) {
219 LOGGER.trace(" <==: {}", response);
221 if (!validateResponse(response)) {
222 throw new SolarMaxException("Invalid response received: " + response);
225 returnedValues = extractValuesFromResponse(response);
227 return returnedValues;
228 } catch (IOException e) {
229 LOGGER.debug("Error communicating via input/output streams: {} ", e.getMessage());
230 throw new SolarMaxException(e);
235 * @param response e.g.
236 * "{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}"
237 * @return a map of keys and values
239 static Map<SolarMaxCommandKey, @Nullable String> extractValuesFromResponse(String response) {
240 final Map<SolarMaxCommandKey, @Nullable String> responseMap = new HashMap<>();
242 // in case there is no response
243 if (response.indexOf("|") == -1) {
244 LOGGER.warn("Response doesn't contain data. Response: {}", response);
248 // extract the body first
249 // start by getting the part of the response between the two pipes
250 String body = response.substring(response.indexOf("|") + 1, response.lastIndexOf("|"));
252 // the name/value pairs now lie after the ":"
253 body = body.substring(body.indexOf(":") + 1);
255 // split into an array of name=value pairs
256 String[] entries = body.split(";");
257 for (String entry : entries) {
259 if (entry.length() != 0) {
260 // could be split on "=" instead of fixed length or made to respect length of command, but they're all 3
261 // characters long (then plus "=")
262 String str = entry.substring(0, 3);
264 String responseValue = (entry.length() >= 5) ? entry.substring(4) : null;
266 SolarMaxCommandKey key = SolarMaxCommandKey.getKeyFromString(str);
267 if (key != SolarMaxCommandKey.UNKNOWN) {
268 responseMap.put(key, responseValue);
276 private static Socket getSocketConnection(final String host, int port)
277 throws SolarMaxConnectionException, UnknownHostException {
278 port = (port == 0) ? DEFAULT_PORT : port;
283 socket = new Socket();
284 socket.connect(new InetSocketAddress(host, port), CONNECTION_TIMEOUT);
285 socket.setSoTimeout(responseTimeout);
286 } catch (final UnknownHostException e) {
288 } catch (final IOException e) {
289 throw new SolarMaxConnectionException("Error connecting to port '" + port + "' on host '" + host + "'", e);
295 public static boolean connectionTest(final String host, int port) throws UnknownHostException {
296 Socket socket = null;
299 socket = getSocketConnection(host, port);
300 } catch (SolarMaxConnectionException e) {
303 if (socket != null) {
306 } catch (IOException e) {
307 // ignore any error while trying to close the socket
316 * @return timeout for responses in milliseconds
318 public static int getResponseTimeout() {
319 return responseTimeout;
323 * @param responseTimeout timeout for responses in milliseconds
325 public static void setResponseTimeout(int responseTimeout) {
326 SolarMaxConnector.responseTimeout = responseTimeout;
330 * @param destinationDevice device number - used if devices are daisy-chained. Normally it will be "1"
331 * @param questions appears to be able to handle multiple commands. For now, one at a time is good fishing
332 * @return the request to be sent to the SolarMax device
334 static String contructRequest(final String questions) {
336 String dstHex = String.format("%02X", 1); // destinationDevice defaults to 1 and is ignored with TCP/IP
339 String msg = "64:" + questions;
340 int lenInt = ("{" + src + ";" + dstHex + ";" + len + "|" + msg + "|" + cs + "}").length();
342 // given the following, I'd expect problems if the request is longer than 255 characters. Since I'm not sure
343 // though, I won't fixe what isn't (yet) broken
344 String lenHex = String.format("%02X", lenInt);
346 String checksum = calculateChecksum16(src + ";" + dstHex + ";" + lenHex + "|" + msg + "|");
348 return "{" + src + ";" + dstHex + ";" + lenHex + "|" + msg + "|" + checksum + "}";
352 * calculates the "checksum16" of the given string argument
354 static String calculateChecksum16(String str) {
355 byte[] bytes = str.getBytes();
358 // loop through each of the bytes and add them together
359 for (byte aByte : bytes) {
363 // calculate the "checksum16"
364 sum = sum % (int) Math.pow(2, 16);
366 // return Integer.toHexString(sum);
367 return String.format("%04X", sum);
370 static boolean validateResponse(final String header) {
371 // probably should implement a patter matcher with a patternString like "/\\{([0-9A-F]{2});FB;([0-9A-F]{2})/",