]> git.basschouten.com Git - openhab-addons.git/blob
064b2a110e827c325c573d8eeff52a1a7e47a643
[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.kaleidescape.internal.communication;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.kaleidescape.internal.KaleidescapeException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * A class that reads messages from the Kaleidescape component in a dedicated thread
24  *
25  * @author Laurent Garnier - Initial contribution
26  * @author Michael Lobstein - Adapted for the Kaleidescape binding
27  */
28 @NonNullByDefault
29 public class KaleidescapeReaderThread extends Thread {
30     private static final int READ_BUFFER_SIZE = 16;
31     private static final int SIZE = 512;
32     private static final char TERM_CHAR = '\r';
33
34     private final Logger logger = LoggerFactory.getLogger(KaleidescapeReaderThread.class);
35
36     private KaleidescapeConnector connector;
37
38     /**
39      * Constructor
40      *
41      * @param connector the object that should handle the received message
42      * @param uid the thing uid string
43      * @param connectionId a string that uniquely identifies the particular connection
44      */
45     public KaleidescapeReaderThread(KaleidescapeConnector connector, String uid, String connectionId) {
46         super("OH-binding-" + uid + "-" + connectionId);
47         this.connector = connector;
48         setDaemon(true);
49     }
50
51     @Override
52     public void run() {
53         logger.debug("Data listener started");
54
55         byte[] readDataBuffer = new byte[READ_BUFFER_SIZE];
56         byte[] dataBuffer = new byte[SIZE];
57         int index = 0;
58
59         try {
60             while (!Thread.interrupted()) {
61                 int len = connector.readInput(readDataBuffer);
62                 if (len > 0) {
63                     for (int i = 0; i < len; i++) {
64
65                         if (index < SIZE) {
66                             dataBuffer[index++] = readDataBuffer[i];
67                         }
68                         if (readDataBuffer[i] == TERM_CHAR) {
69                             if (index >= SIZE) {
70                                 dataBuffer[index - 1] = (byte) TERM_CHAR;
71                             }
72                             byte[] msg = Arrays.copyOf(dataBuffer, index);
73                             connector.handleIncomingMessage(msg);
74                             index = 0;
75                         }
76
77                     }
78                 }
79             }
80         } catch (KaleidescapeException e) {
81             logger.debug("Reading failed: {}", e.getMessage(), e);
82         }
83
84         logger.debug("Data listener stopped");
85     }
86 }