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