]> git.basschouten.com Git - openhab-addons.git/blob
8f05c3e41baf63731bebafd6b09e9a817489b028
[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.solarmax.internal.connector;
14
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;
25 import java.util.Map;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * 
34  * The {@link SolarMaxConnector} class is used to communicated with the SolarMax device (on a binary level)
35  *
36  * With a little help from https://github.com/sushiguru/solar-pv/blob/master/solmax/pv.php
37  * 
38  * @author Jamie Townsend - Initial contribution
39  */
40 @NonNullByDefault
41 public class SolarMaxConnector {
42
43     /**
44      * default port number of SolarMax devices is...
45      */
46     private static final int DEFAULT_PORT = 12345;
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(SolarMaxConnector.class);
49
50     /**
51      * default timeout for socket connections is 1 second
52      */
53     private static final int CONNECTION_TIMEOUT = 1000;
54
55     /**
56      * default timeout for socket responses is 10 seconds
57      */
58     private static int responseTimeout = 10000;
59
60     /**
61      * gets all known values from the SolarMax device addressable at host:portNumber
62      * 
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
66      * @return
67      * @throws SolarMaxException if some other exception occurs
68      */
69     public static SolarMaxData getAllValuesFromSolarMax(final String host, final int portNumber,
70             final int deviceAddress) throws SolarMaxException {
71         List<SolarMaxCommandKey> commandList = new ArrayList<>();
72
73         for (SolarMaxCommandKey solarMaxCommandKey : SolarMaxCommandKey.values()) {
74             if (solarMaxCommandKey != SolarMaxCommandKey.UNKNOWN) {
75                 commandList.add(solarMaxCommandKey);
76             }
77         }
78
79         SolarMaxData solarMaxData = new SolarMaxData();
80
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
83
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;
90                 break;
91             }
92         }
93         solarMaxData.setDataDateTime(ZonedDateTime.now());
94         solarMaxData.setCommunicationSuccessful(allCommandsAnswered);
95         solarMaxData.setData(valuesFromSolarMax);
96
97         return solarMaxData;
98     }
99
100     /**
101      * gets values from the SolarMax device addressable at host:portNumber
102      * 
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
106      * @return
107      * @throws UnknownHostException if the host is unknown
108      * @throws SolarMaxException if some other exception occurs
109      */
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 {
113         Socket socket;
114
115         Map<SolarMaxCommandKey, @Nullable String> returnMap = new HashMap<>();
116
117         // SolarMax can't answer correclty if too many commands are send in a single request, so limit it to 16 at a
118         // time
119         int maxConcurrentCommands = 16;
120         int requestsRequired = (commandList.size() / maxConcurrentCommands);
121         if (commandList.size() % maxConcurrentCommands != 0) {
122             requestsRequired = requestsRequired + 1;
123         }
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);
127
128             int firstCommandNumber = requestNumber * maxConcurrentCommands;
129             int lastCommandNumber = (requestNumber + 1) * maxConcurrentCommands;
130             if (lastCommandNumber > commandList.size()) {
131                 lastCommandNumber = commandList.size();
132             }
133             List<SolarMaxCommandKey> commandsToSend = commandList.subList(firstCommandNumber, lastCommandNumber);
134
135             try {
136                 socket = getSocketConnection(host, portNumber);
137             } catch (UnknownHostException e) {
138                 throw new SolarMaxConnectionException(e);
139             }
140             returnMap.putAll(getValuesFromSolarMax(socket, deviceAddress, commandsToSend));
141
142             // SolarMax can't deal with requests too close to one another, so just wait a moment
143             try {
144                 Thread.sleep(10);
145             } catch (InterruptedException e) {
146                 // do nothing
147             }
148         }
149         return returnMap;
150     }
151
152     static String getCommandString(List<SolarMaxCommandKey> commandList) {
153         String commandString = "";
154         for (SolarMaxCommandKey command : commandList) {
155             if (!commandString.isEmpty()) {
156                 commandString = commandString + ";";
157             }
158             commandString = commandString + command.getCommandKey();
159         }
160         return commandString;
161     }
162
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;
167         try {
168             outputStream = socket.getOutputStream();
169             inputStream = socket.getInputStream();
170
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);
174         } finally {
175             try {
176                 socket.close();
177                 if (outputStream != null) {
178                     outputStream.close();
179                 }
180                 if (inputStream != null) {
181                     inputStream.close();
182                 }
183             } catch (final IOException e) {
184                 // ignore the error, we're dying anyway...
185             }
186         }
187     }
188
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);
195         try {
196             LOGGER.trace("    ==>: {}", request);
197
198             outputStream.write(request.getBytes());
199
200             String response = "";
201             byte[] responseByte = new byte[1];
202
203             // get everything from the stream
204             while (true) {
205                 // read one byte from the stream
206                 int bytesRead = inputStream.read(responseByte);
207
208                 // if there was nothing left, break
209                 if (bytesRead < 1) {
210                     break;
211                 }
212
213                 // add the received byte to the response
214                 final String responseString = new String(responseByte);
215                 response = response + responseString;
216
217                 // if it was the final expected character "}", break
218                 if ("}".equals(responseString)) {
219                     break;
220                 }
221             }
222
223             LOGGER.trace("    <==: {}", response);
224
225             if (!validateResponse(response)) {
226                 throw new SolarMaxException("Invalid response received: " + response);
227             }
228
229             returnedValues = extractValuesFromResponse(response);
230
231             return returnedValues;
232         } catch (IOException e) {
233             LOGGER.debug("Error communicating via input/output streams: {} ", e.getMessage());
234             throw new SolarMaxException(e);
235         }
236     }
237
238     /**
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
242      */
243     static Map<SolarMaxCommandKey, @Nullable String> extractValuesFromResponse(String response) {
244         final Map<SolarMaxCommandKey, @Nullable String> responseMap = new HashMap<>();
245
246         // in case there is no response
247         if (response.indexOf("|") == -1) {
248             LOGGER.warn("Response doesn't contain data. Response: {}", response);
249             return responseMap;
250         }
251
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("|"));
255
256         // the name/value pairs now lie after the ":"
257         body = body.substring(body.indexOf(":") + 1);
258
259         // split into an array of name=value pairs
260         String[] entries = body.split(";");
261         for (String entry : entries) {
262
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);
267
268                 String responseValue = (entry.length() >= 5) ? entry.substring(4) : null;
269
270                 SolarMaxCommandKey key = SolarMaxCommandKey.getKeyFromString(str);
271                 if (key != SolarMaxCommandKey.UNKNOWN) {
272                     responseMap.put(key, responseValue);
273                 }
274             }
275         }
276
277         return responseMap;
278     }
279
280     private static Socket getSocketConnection(final String host, int portNumber)
281             throws SolarMaxConnectionException, UnknownHostException {
282         portNumber = (portNumber == 0) ? DEFAULT_PORT : portNumber;
283
284         Socket socket;
285
286         try {
287             socket = new Socket();
288             socket.connect(new InetSocketAddress(host, portNumber), CONNECTION_TIMEOUT);
289             socket.setSoTimeout(responseTimeout);
290         } catch (final UnknownHostException e) {
291             throw e;
292         } catch (final IOException e) {
293             throw new SolarMaxConnectionException(
294                     "Error connecting to portNumber '" + portNumber + "' on host '" + host + "'", e);
295         }
296
297         return socket;
298     }
299
300     public static boolean connectionTest(final String host, final int portNumber) throws UnknownHostException {
301         Socket socket = null;
302
303         try {
304             socket = getSocketConnection(host, portNumber);
305         } catch (SolarMaxConnectionException e) {
306             return false;
307         } finally {
308             if (socket != null) {
309                 try {
310                     socket.close();
311                 } catch (IOException e) {
312                     // ignore any error while trying to close the socket
313                 }
314             }
315         }
316
317         return true;
318     }
319
320     /**
321      * @return timeout for responses in milliseconds
322      */
323     public static int getResponseTimeout() {
324         return responseTimeout;
325     }
326
327     /**
328      * @param responseTimeout timeout for responses in milliseconds
329      */
330     public static void setResponseTimeout(int responseTimeout) {
331         SolarMaxConnector.responseTimeout = responseTimeout;
332     }
333
334     /**
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
338      */
339     static String contructRequest(final int deviceAddress, final String questions) {
340         String src = "FB";
341         String dstHex = String.format("%02X", deviceAddress); // destinationDevice defaults to 1
342         String len = "00";
343         String cs = "0000";
344         String msg = "64:" + questions;
345         int lenInt = ("{" + src + ";" + dstHex + ";" + len + "|" + msg + "|" + cs + "}").length();
346
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);
350
351         String checksum = calculateChecksum16(src + ";" + dstHex + ";" + lenHex + "|" + msg + "|");
352
353         return "{" + src + ";" + dstHex + ";" + lenHex + "|" + msg + "|" + checksum + "}";
354     }
355
356     /**
357      * calculates the "checksum16" of the given string argument
358      */
359     static String calculateChecksum16(String str) {
360         byte[] bytes = str.getBytes();
361         int sum = 0;
362
363         // loop through each of the bytes and add them together
364         for (byte aByte : bytes) {
365             sum = sum + aByte;
366         }
367
368         // calculate the "checksum16"
369         sum = sum % (int) Math.pow(2, 16);
370
371         // return Integer.toHexString(sum);
372         return String.format("%04X", sum);
373     }
374
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})/",
377         // but for now...
378         return true;
379     }
380 }