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.plugwise.internal;
15 import static org.openhab.binding.plugwise.internal.PlugwiseCommunicationContext.*;
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;
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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Processes messages received from the Plugwise Stick using a serial connection.
39 * @author Wouter Born, Karel Goderis - Initial contribution
42 public class PlugwiseMessageProcessor implements SerialPortEventListener {
44 private class MessageProcessorThread extends Thread {
46 public MessageProcessorThread() {
47 super("OH-binding-" + context.getBridgeUID() + "-message-processor");
53 while (!interrupted()) {
55 Message message = context.getReceivedQueue().take();
56 if (message != null) {
57 logger.debug("Took message from receivedQueue (length={})", context.getReceivedQueue().size());
58 processMessage(message);
60 logger.debug("Skipping null message from receivedQueue (length={})",
61 context.getReceivedQueue().size());
63 } catch (InterruptedException e) {
64 // That's our signal to stop
66 } catch (Exception e) {
67 logger.warn("Error while taking message from receivedQueue", e);
73 /** Matches Plugwise responses into the following groups: protocolHeader command sequence payload CRC */
74 private static final Pattern RESPONSE_PATTERN = Pattern.compile("(.{4})(\\w{4})(\\w{4})(\\w*?)(\\w{4})");
76 private final Logger logger = LoggerFactory.getLogger(PlugwiseMessageProcessor.class);
77 private final PlugwiseCommunicationContext context;
78 private final MessageFactory messageFactory = new MessageFactory();
80 private final ByteBuffer readBuffer = ByteBuffer.allocate(PlugwiseCommunicationContext.MAX_BUFFER_SIZE);
81 private int previousByte = -1;
83 private @Nullable MessageProcessorThread thread;
85 public PlugwiseMessageProcessor(PlugwiseCommunicationContext context) {
86 this.context = context;
90 * Parse a buffer into a Message and put it in the appropriate queue for further processing
92 * @param readBuffer - the string to parse
94 private void parseAndQueue(ByteBuffer readBuffer) {
95 String response = new String(readBuffer.array(), 0, readBuffer.limit());
96 response = response.replace("\r", "").replace("\n", "");
98 Matcher matcher = RESPONSE_PATTERN.matcher(response);
100 if (matcher.matches()) {
101 String protocolHeader = matcher.group(1);
102 String messageTypeHex = matcher.group(2);
103 String sequence = matcher.group(3);
104 String payload = matcher.group(4);
105 String crc = matcher.group(5);
107 if (protocolHeader.equals(PROTOCOL_HEADER)) {
108 String calculatedCRC = Message.getCRC(messageTypeHex + sequence + payload);
109 if (calculatedCRC.equals(crc)) {
110 MessageType messageType = MessageType.forValue(Integer.parseInt(messageTypeHex, 16));
111 int sequenceNumber = Integer.parseInt(sequence, 16);
113 if (messageType == null) {
114 logger.debug("Received unrecognized message: messageTypeHex=0x{}, sequence={}, payload={}",
115 messageTypeHex, sequenceNumber, payload);
119 logger.debug("Received message: messageType={}, sequenceNumber={}, payload={}", messageType,
120 sequenceNumber, payload);
123 Message message = messageFactory.createMessage(messageType, sequenceNumber, payload);
125 if (message instanceof AcknowledgementMessage acknowledgementMessage
126 && !acknowledgementMessage.isExtended()) {
127 logger.debug("Adding to acknowledgedQueue: {}", message);
128 context.getAcknowledgedQueue().put(acknowledgementMessage);
130 logger.debug("Adding to receivedQueue: {}", message);
131 context.getReceivedQueue().put(message);
133 } catch (IllegalArgumentException e) {
134 logger.warn("Failed to create message", e);
135 } catch (InterruptedException e) {
136 Thread.interrupted();
139 logger.warn("Plugwise protocol CRC error: {} does not match {} in message", calculatedCRC, crc);
142 logger.debug("Plugwise protocol header error: {} in message {}", protocolHeader, response);
144 } else if (!response.contains("APSRequestNodeInfo") && !response.contains("APSSetSleepBehaviour")
145 && !response.startsWith("# ")) {
146 logger.warn("Plugwise protocol message error: {}", response);
150 private void processMessage(Message message) {
151 context.getFilteredListeners().notifyListeners(message);
153 // After processing the response to a message, we remove any reference to the original request
154 // stored in the sentQueue
155 // WARNING: We assume that each request sent out can only be followed bye EXACTLY ONE response - so
156 // far it seems that the Plugwise protocol is operating in that way
159 context.getSentQueueLock().lock();
161 for (PlugwiseQueuedMessage queuedSentMessage : context.getSentQueue()) {
162 if (queuedSentMessage != null
163 && queuedSentMessage.getMessage().getSequenceNumber() == message.getSequenceNumber()) {
164 logger.debug("Removing from sentQueue: {}", queuedSentMessage.getMessage());
165 context.getSentQueue().remove(queuedSentMessage);
170 context.getSentQueueLock().unlock();
174 @SuppressWarnings("resource")
176 public void serialEvent(@Nullable SerialPortEvent event) {
177 if (event != null && event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
178 // We get here if data has been received
179 SerialPort serialPort = context.getSerialPort();
180 if (serialPort == null) {
181 logger.debug("Failed to read available data from null serialPort");
186 InputStream inputStream = serialPort.getInputStream();
187 if (inputStream == null) {
188 logger.debug("Failed to read available data from null inputStream");
192 // Read data from serial device
193 while (inputStream.available() > 0) {
194 int currentByte = inputStream.read();
195 // Plugwise sends ASCII data, but for some unknown reason we sometimes get data with unsigned
196 // byte value >127 which in itself is very strange. We filter these out for the time being
197 if (currentByte < 128) {
198 readBuffer.put((byte) currentByte);
199 if (previousByte == CR && currentByte == LF) {
201 parseAndQueue(readBuffer);
205 previousByte = currentByte;
209 } catch (IOException e) {
210 logger.debug("Error receiving data on serial port {}: {}", context.getConfiguration().getSerialPort(),
216 @SuppressWarnings("resource")
217 public void start() throws PlugwiseInitializationException {
218 SerialPort serialPort = context.getSerialPort();
219 if (serialPort == null) {
220 throw new PlugwiseInitializationException("Failed to add serial port listener because port is null");
224 serialPort.addEventListener(this);
225 } catch (TooManyListenersException e) {
226 throw new PlugwiseInitializationException("Failed to add serial port listener", e);
229 thread = new MessageProcessorThread();
233 @SuppressWarnings("resource")
235 PlugwiseUtils.stopBackgroundThread(thread);
237 SerialPort serialPort = context.getSerialPort();
238 if (serialPort != null) {
239 serialPort.removeEventListener();