]> git.basschouten.com Git - openhab-addons.git/blob
6968a102dd97ecb82e8125d6a4076fee0cabd09e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.openthermgateway.internal;
14
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.PrintWriter;
19 import java.net.InetSocketAddress;
20 import java.net.Socket;
21 import java.util.AbstractMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.types.State;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link OpenThermGatewaySocketConnector} is responsible for handling the socket connection
36  *
37  * @author Arjen Korevaar - Initial contribution
38  * @author Arjan Mels - Improved robustness by re-sending commands, handling all message types (not only Boiler)
39  */
40 @NonNullByDefault
41 public class OpenThermGatewaySocketConnector implements OpenThermGatewayConnector {
42     private static final int COMMAND_RESPONSE_TIME_MILLISECONDS = 100;
43     private static final int COMMAND_TIMEOUT_MILLISECONDS = 5000;
44
45     private final Logger logger = LoggerFactory.getLogger(OpenThermGatewaySocketConnector.class);
46
47     private final OpenThermGatewayCallback callback;
48     private final String ipaddress;
49     private final int port;
50
51     private @Nullable PrintWriter writer;
52
53     private volatile boolean stopping;
54     private boolean connected;
55
56     private Map<String, Entry<Long, GatewayCommand>> pendingCommands = new ConcurrentHashMap<>();
57
58     public OpenThermGatewaySocketConnector(OpenThermGatewayCallback callback, String ipaddress, int port) {
59         this.callback = callback;
60         this.ipaddress = ipaddress;
61         this.port = port;
62     }
63
64     @Override
65     public void run() {
66         stopping = false;
67         connected = false;
68
69         logger.debug("Connecting OpenThermGatewaySocketConnector to {}:{}", this.ipaddress, this.port);
70
71         callback.connecting();
72
73         try (Socket socket = new Socket()) {
74             socket.connect(new InetSocketAddress(this.ipaddress, this.port), COMMAND_TIMEOUT_MILLISECONDS);
75             socket.setSoTimeout(COMMAND_TIMEOUT_MILLISECONDS);
76
77             connected = true;
78
79             callback.connected();
80
81             logger.debug("OpenThermGatewaySocketConnector connected");
82
83             try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
84                     PrintWriter wrt = new PrintWriter(socket.getOutputStream(), true)) {
85                 // Make writer accessible on class level
86                 writer = wrt;
87
88                 sendCommand(GatewayCommand.parse(GatewayCommandCode.PrintReport, "A"));
89                 // Set the OTGW to report every message it receives and transmits
90                 sendCommand(GatewayCommand.parse(GatewayCommandCode.PrintSummary, "0"));
91
92                 while (!stopping && !Thread.currentThread().isInterrupted()) {
93                     @Nullable
94                     String message = reader.readLine();
95
96                     if (message != null) {
97                         handleMessage(message);
98                     } else {
99                         logger.debug("Connection closed by OpenTherm Gateway");
100                         break;
101                     }
102                 }
103
104                 logger.debug("Stopping OpenThermGatewaySocketConnector");
105             } finally {
106                 connected = false;
107
108                 logger.debug("OpenThermGatewaySocketConnector disconnected");
109                 callback.disconnected();
110             }
111         } catch (IOException ex) {
112             logger.warn("Unable to connect to the OpenTherm Gateway.", ex);
113             callback.disconnected();
114         }
115     }
116
117     @Override
118     public void stop() {
119         logger.debug("Stopping OpenThermGatewaySocketConnector");
120         stopping = true;
121     }
122
123     @Override
124     public boolean isConnected() {
125         return connected;
126     }
127
128     @Override
129     public void sendCommand(GatewayCommand command) {
130         @Nullable
131         PrintWriter wrtr = writer;
132
133         String msg = command.toFullString();
134
135         pendingCommands.put(command.getCode(),
136                 new AbstractMap.SimpleImmutableEntry<>(System.currentTimeMillis(), command));
137
138         if (connected) {
139             logger.debug("Sending message: {}", msg);
140             if (wrtr != null) {
141                 wrtr.print(msg + "\r\n");
142                 wrtr.flush();
143             }
144         } else {
145             logger.debug("Unable to send message: {}. OpenThermGatewaySocketConnector is not connected.", msg);
146         }
147     }
148
149     private void handleMessage(String message) {
150         if (message.length() > 2 && message.charAt(2) == ':') {
151             String code = message.substring(0, 2);
152             String value = message.substring(3);
153
154             logger.debug("Received command confirmation: {}: {}", code, value);
155             pendingCommands.remove(code);
156             return;
157         }
158
159         long currentTime = System.currentTimeMillis();
160
161         for (Entry<Long, GatewayCommand> timeAndCommand : pendingCommands.values()) {
162             long responseTime = timeAndCommand.getKey() + COMMAND_RESPONSE_TIME_MILLISECONDS;
163             long timeoutTime = timeAndCommand.getKey() + COMMAND_TIMEOUT_MILLISECONDS;
164
165             if (currentTime > responseTime && currentTime <= timeoutTime) {
166                 logger.debug("Resending command: {}", timeAndCommand.getValue());
167                 sendCommand(timeAndCommand.getValue());
168             } else if (currentTime > timeoutTime) {
169                 pendingCommands.remove(timeAndCommand.getValue().getCode());
170             }
171         }
172
173         Message msg = Message.parse(message);
174
175         if (msg == null) {
176             logger.trace("Received message: {}, (unknown)", message);
177             return;
178         } else {
179             logger.trace("Received message: {}, {} {} {}", message, msg.getID(), msg.getCode(), msg.getMessageType());
180         }
181
182         if (DataItemGroup.dataItemGroups.containsKey(msg.getID())) {
183             DataItem[] dataItems = DataItemGroup.dataItemGroups.get(msg.getID());
184
185             for (DataItem dataItem : dataItems) {
186                 State state = null;
187
188                 switch (dataItem.getDataType()) {
189                     case FLAGS:
190                         state = OnOffType.from(msg.getBit(dataItem.getByteType(), dataItem.getBitPos()));
191                         break;
192                     case UINT8:
193                     case UINT16:
194                         state = new DecimalType(msg.getUInt(dataItem.getByteType()));
195                         break;
196                     case INT8:
197                     case INT16:
198                         state = new DecimalType(msg.getInt(dataItem.getByteType()));
199                         break;
200                     case FLOAT:
201                         state = new DecimalType(msg.getFloat());
202                         break;
203                     case DOWTOD:
204                         break;
205                 }
206
207                 logger.trace("  Data: {} {} {} {}", dataItem.getID(), dataItem.getSubject(), dataItem.getDataType(),
208                         state == null ? "" : state);
209             }
210         }
211
212         if (msg.getMessageType() == MessageType.READACK || msg.getMessageType() == MessageType.WRITEDATA
213                 || msg.getID() == 0 || msg.getID() == 1) {
214             receiveMessage(msg);
215         }
216     }
217
218     private void receiveMessage(Message message) {
219         callback.receiveMessage(message);
220     }
221 }