2 * Copyright (c) 2010-2023 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.rotel.internal.communication;
15 import java.io.InterruptedIOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.rotel.internal.RotelException;
19 import org.openhab.binding.rotel.internal.protocol.RotelAbstractProtocolHandler;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * A class that reads messages from the Rotel device in a dedicated thread
26 * @author Laurent Garnier - Initial contribution
29 public class RotelReaderThread extends Thread {
31 private static final int READ_BUFFER_SIZE = 16;
33 private final Logger logger = LoggerFactory.getLogger(RotelReaderThread.class);
35 private final RotelConnector connector;
36 private final RotelAbstractProtocolHandler protocolHandler;
41 * @param connector the connector to read input data
42 * @param protocolHandler the protocol handler
43 * @param threadName the name of the thread
45 public RotelReaderThread(RotelConnector connector, RotelAbstractProtocolHandler protocolHandler,
48 this.connector = connector;
49 this.protocolHandler = protocolHandler;
54 logger.debug("Data listener started");
56 byte[] readDataBuffer = new byte[READ_BUFFER_SIZE];
59 while (!Thread.interrupted()) {
60 int len = connector.readInput(readDataBuffer);
62 protocolHandler.handleIncomingData(readDataBuffer, len);
65 } catch (InterruptedIOException e) {
66 Thread.currentThread().interrupt();
67 logger.debug("Interrupted via InterruptedIOException");
68 } catch (RotelException e) {
69 logger.debug("Reading failed: {}", e.getMessage(), e);
70 protocolHandler.handleInIncomingError();
73 logger.debug("Data listener stopped");