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