]> git.basschouten.com Git - openhab-addons.git/blob
c348a6930f658401186b1d19fba3be52365478af
[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.plugwise.internal;
14
15 import static org.openhab.binding.plugwise.internal.PlugwiseCommunicationContext.*;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.nio.ByteBuffer;
20 import java.util.TooManyListenersException;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.plugwise.internal.protocol.AcknowledgementMessage;
27 import org.openhab.binding.plugwise.internal.protocol.Message;
28 import org.openhab.binding.plugwise.internal.protocol.MessageFactory;
29 import org.openhab.binding.plugwise.internal.protocol.field.MessageType;
30 import org.openhab.core.io.transport.serial.SerialPort;
31 import org.openhab.core.io.transport.serial.SerialPortEvent;
32 import org.openhab.core.io.transport.serial.SerialPortEventListener;
33 import org.openhab.core.util.StringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Processes messages received from the Plugwise Stick using a serial connection.
39  *
40  * @author Wouter Born, Karel Goderis - Initial contribution
41  */
42 @NonNullByDefault
43 public class PlugwiseMessageProcessor implements SerialPortEventListener {
44
45     private class MessageProcessorThread extends Thread {
46
47         public MessageProcessorThread() {
48             super("OH-binding-" + context.getBridgeUID() + "-message-processor");
49             setDaemon(true);
50         }
51
52         @Override
53         public void run() {
54             while (!interrupted()) {
55                 try {
56                     Message message = context.getReceivedQueue().take();
57                     if (message != null) {
58                         logger.debug("Took message from receivedQueue (length={})", context.getReceivedQueue().size());
59                         processMessage(message);
60                     } else {
61                         logger.debug("Skipping null message from receivedQueue (length={})",
62                                 context.getReceivedQueue().size());
63                     }
64                 } catch (InterruptedException e) {
65                     // That's our signal to stop
66                     break;
67                 } catch (Exception e) {
68                     logger.warn("Error while taking message from receivedQueue", e);
69                 }
70             }
71         }
72     }
73
74     /** Matches Plugwise responses into the following groups: protocolHeader command sequence payload CRC */
75     private static final Pattern RESPONSE_PATTERN = Pattern.compile("(.{4})(\\w{4})(\\w{4})(\\w*?)(\\w{4})");
76
77     private final Logger logger = LoggerFactory.getLogger(PlugwiseMessageProcessor.class);
78     private final PlugwiseCommunicationContext context;
79     private final MessageFactory messageFactory = new MessageFactory();
80
81     private final ByteBuffer readBuffer = ByteBuffer.allocate(PlugwiseCommunicationContext.MAX_BUFFER_SIZE);
82     private int previousByte = -1;
83
84     private @Nullable MessageProcessorThread thread;
85
86     public PlugwiseMessageProcessor(PlugwiseCommunicationContext context) {
87         this.context = context;
88     }
89
90     /**
91      * Parse a buffer into a Message and put it in the appropriate queue for further processing
92      *
93      * @param readBuffer - the string to parse
94      */
95     private void parseAndQueue(ByteBuffer readBuffer) {
96         String response = new String(readBuffer.array(), 0, readBuffer.limit());
97         response = StringUtils.chomp(response);
98
99         Matcher matcher = RESPONSE_PATTERN.matcher(response);
100
101         if (matcher.matches()) {
102             String protocolHeader = matcher.group(1);
103             String messageTypeHex = matcher.group(2);
104             String sequence = matcher.group(3);
105             String payload = matcher.group(4);
106             String crc = matcher.group(5);
107
108             if (protocolHeader.equals(PROTOCOL_HEADER)) {
109                 String calculatedCRC = Message.getCRC(messageTypeHex + sequence + payload);
110                 if (calculatedCRC.equals(crc)) {
111                     MessageType messageType = MessageType.forValue(Integer.parseInt(messageTypeHex, 16));
112                     int sequenceNumber = Integer.parseInt(sequence, 16);
113
114                     if (messageType == null) {
115                         logger.debug("Received unrecognized message: messageTypeHex=0x{}, sequence={}, payload={}",
116                                 messageTypeHex, sequenceNumber, payload);
117                         return;
118                     }
119
120                     logger.debug("Received message: messageType={}, sequenceNumber={}, payload={}", messageType,
121                             sequenceNumber, payload);
122
123                     try {
124                         Message message = messageFactory.createMessage(messageType, sequenceNumber, payload);
125
126                         if (message instanceof AcknowledgementMessage acknowledgementMessage
127                                 && !acknowledgementMessage.isExtended()) {
128                             logger.debug("Adding to acknowledgedQueue: {}", message);
129                             context.getAcknowledgedQueue().put(acknowledgementMessage);
130                         } else {
131                             logger.debug("Adding to receivedQueue: {}", message);
132                             context.getReceivedQueue().put(message);
133                         }
134                     } catch (IllegalArgumentException e) {
135                         logger.warn("Failed to create message", e);
136                     } catch (InterruptedException e) {
137                         Thread.interrupted();
138                     }
139                 } else {
140                     logger.warn("Plugwise protocol CRC error: {} does not match {} in message", calculatedCRC, crc);
141                 }
142             } else {
143                 logger.debug("Plugwise protocol header error: {} in message {}", protocolHeader, response);
144             }
145         } else if (!response.contains("APSRequestNodeInfo") && !response.contains("APSSetSleepBehaviour")
146                 && !response.startsWith("# ")) {
147             logger.warn("Plugwise protocol message error: {}", response);
148         }
149     }
150
151     private void processMessage(Message message) {
152         context.getFilteredListeners().notifyListeners(message);
153
154         // After processing the response to a message, we remove any reference to the original request
155         // stored in the sentQueue
156         // WARNING: We assume that each request sent out can only be followed bye EXACTLY ONE response - so
157         // far it seems that the Plugwise protocol is operating in that way
158
159         try {
160             context.getSentQueueLock().lock();
161
162             for (PlugwiseQueuedMessage queuedSentMessage : context.getSentQueue()) {
163                 if (queuedSentMessage != null
164                         && queuedSentMessage.getMessage().getSequenceNumber() == message.getSequenceNumber()) {
165                     logger.debug("Removing from sentQueue: {}", queuedSentMessage.getMessage());
166                     context.getSentQueue().remove(queuedSentMessage);
167                     break;
168                 }
169             }
170         } finally {
171             context.getSentQueueLock().unlock();
172         }
173     }
174
175     @SuppressWarnings("resource")
176     @Override
177     public void serialEvent(@Nullable SerialPortEvent event) {
178         if (event != null && event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
179             // We get here if data has been received
180             SerialPort serialPort = context.getSerialPort();
181             if (serialPort == null) {
182                 logger.debug("Failed to read available data from null serialPort");
183                 return;
184             }
185
186             try {
187                 InputStream inputStream = serialPort.getInputStream();
188                 if (inputStream == null) {
189                     logger.debug("Failed to read available data from null inputStream");
190                     return;
191                 }
192
193                 // Read data from serial device
194                 while (inputStream.available() > 0) {
195                     int currentByte = inputStream.read();
196                     // Plugwise sends ASCII data, but for some unknown reason we sometimes get data with unsigned
197                     // byte value >127 which in itself is very strange. We filter these out for the time being
198                     if (currentByte < 128) {
199                         readBuffer.put((byte) currentByte);
200                         if (previousByte == CR && currentByte == LF) {
201                             readBuffer.flip();
202                             parseAndQueue(readBuffer);
203                             readBuffer.clear();
204                             previousByte = -1;
205                         } else {
206                             previousByte = currentByte;
207                         }
208                     }
209                 }
210             } catch (IOException e) {
211                 logger.debug("Error receiving data on serial port {}: {}", context.getConfiguration().getSerialPort(),
212                         e.getMessage());
213             }
214         }
215     }
216
217     @SuppressWarnings("resource")
218     public void start() throws PlugwiseInitializationException {
219         SerialPort serialPort = context.getSerialPort();
220         if (serialPort == null) {
221             throw new PlugwiseInitializationException("Failed to add serial port listener because port is null");
222         }
223
224         try {
225             serialPort.addEventListener(this);
226         } catch (TooManyListenersException e) {
227             throw new PlugwiseInitializationException("Failed to add serial port listener", e);
228         }
229
230         thread = new MessageProcessorThread();
231         thread.start();
232     }
233
234     @SuppressWarnings("resource")
235     public void stop() {
236         PlugwiseUtils.stopBackgroundThread(thread);
237
238         SerialPort serialPort = context.getSerialPort();
239         if (serialPort != null) {
240             serialPort.removeEventListener();
241         }
242     }
243 }