2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.oppo.internal.communication;
15 import java.util.Arrays;
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;
23 * A class that reads messages from the Oppo player in a dedicated thread
25 * @author Laurent Garnier - Initial contribution
26 * @author Michael Lobstein - Adapted for the Oppo binding
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';
34 private final Logger logger = LoggerFactory.getLogger(OppoReaderThread.class);
36 private OppoConnector connector;
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
45 public OppoReaderThread(OppoConnector connector, String uid, String connectionId) {
46 super("OH-binding-" + uid + "-" + connectionId);
47 this.connector = connector;
53 logger.debug("Data listener started");
55 byte[] readDataBuffer = new byte[READ_BUFFER_SIZE];
56 byte[] dataBuffer = new byte[SIZE];
60 while (!Thread.interrupted()) {
61 int len = connector.readInput(readDataBuffer);
63 for (int i = 0; i < len; i++) {
65 dataBuffer[index++] = readDataBuffer[i];
67 if (readDataBuffer[i] == TERM_CHAR) {
69 dataBuffer[index - 1] = (byte) TERM_CHAR;
71 byte[] msg = Arrays.copyOf(dataBuffer, index);
72 connector.handleIncomingMessage(msg);
78 } catch (OppoException e) {
79 logger.debug("Reading failed: {}", e.getMessage(), e);
82 logger.debug("Data listener stopped");