]> git.basschouten.com Git - openhab-addons.git/blob
b73d934bf2a5dc100a40f66445584a8937f15e4e
[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.oppo.internal.communication;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.oppo.internal.OppoException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * A class that reads messages from the Oppo player in a dedicated thread
24  *
25  * @author Laurent Garnier - Initial contribution
26  * @author Michael Lobstein - Adapted for the Oppo binding
27  */
28 @NonNullByDefault
29 public class OppoReaderThread 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
34     private final Logger logger = LoggerFactory.getLogger(OppoReaderThread.class);
35
36     private OppoConnector 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 OppoReaderThread(OppoConnector 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                         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         } catch (OppoException e) {
79             logger.debug("Reading failed: {}", e.getMessage(), e);
80         }
81
82         logger.debug("Data listener stopped");
83     }
84 }