]> git.basschouten.com Git - openhab-addons.git/blob
4a254634446a0d8181cbc111e1fce2875b41c78d
[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.bluetooth.daikinmadoka.internal;
14
15 import java.io.ByteArrayOutputStream;
16 import java.util.Comparator;
17 import java.util.concurrent.ConcurrentSkipListSet;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.commands.ResponseListener;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * As the protocol emutes an UART communication over BLE (characteristics write/notify), this class takes care of BLE
28  * transport.
29  *
30  * @author Benjamin Lafois - Initial contribution
31  */
32 @NonNullByDefault
33 public class BRC1HUartProcessor {
34
35     private final Logger logger = LoggerFactory.getLogger(BRC1HUartProcessor.class);
36
37     /**
38      * Maximum number of bytes per message chunk, including headers
39      */
40     public static final int MAX_CHUNK_SIZE = 20;
41
42     /**
43      * In the unlikely event of messages arrive in wrong order, this comparator will sort the queue
44      */
45     private Comparator<byte[]> chunkSorter = (byte[] m1, byte[] m2) -> m1[0] - m2[0];
46
47     private ConcurrentSkipListSet<byte[]> uartMessages = new ConcurrentSkipListSet<>(chunkSorter);
48
49     private ResponseListener responseListener;
50
51     private final Lock stateLock = new ReentrantLock();
52
53     public BRC1HUartProcessor(ResponseListener responseListener) {
54         this.responseListener = responseListener;
55     }
56
57     private boolean isMessageComplete() {
58         int messagesInQueue = this.uartMessages.size();
59
60         if (messagesInQueue <= 0) {
61             return false;
62         }
63
64         byte[] firstMessageInQueue = uartMessages.first();
65         if (firstMessageInQueue.length < 2) {
66             return false;
67         }
68
69         int expectedChunks = (int) Math.ceil(firstMessageInQueue[1] / (MAX_CHUNK_SIZE - 1.0));
70         if (expectedChunks != messagesInQueue) {
71             return false;
72         }
73
74         // Check that we have every single ID
75         int expected = 0;
76         for (byte[] m : this.uartMessages) {
77             if (m.length < 2) {
78                 return false;
79             }
80
81             if (m[0] != expected++) {
82                 return false;
83             }
84         }
85         return true;
86     }
87
88     public void chunkReceived(byte[] byteValue) {
89         byte[] fullReceivedMessage = null;
90         stateLock.lock();
91         try {
92             this.uartMessages.add(byteValue);
93             if (isMessageComplete()) {
94                 logger.debug("Complete message received!");
95
96                 // Beyond this point, full message received
97                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
98
99                 for (byte[] msg : uartMessages) {
100                     if (msg.length > 1) {
101                         bos.write(msg, 1, msg.length - 1);
102                     }
103                 }
104
105                 this.uartMessages.clear();
106                 fullReceivedMessage = bos.toByteArray();
107             }
108         } finally {
109             stateLock.unlock();
110         }
111
112         if (fullReceivedMessage != null) {
113             this.responseListener.receivedResponse(fullReceivedMessage);
114         }
115     }
116
117     public void abandon() {
118         this.uartMessages.clear();
119     }
120 }