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.denonmarantz.internal.connector.telnet;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStreamWriter;
19 import java.net.InetSocketAddress;
20 import java.net.Socket;
21 import java.net.SocketTimeoutException;
23 import org.openhab.binding.denonmarantz.internal.config.DenonMarantzConfiguration;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Manage telnet connection to the Denon/Marantz Receiver
30 * @author Jeroen Idserda - Initial contribution (1.x Binding)
31 * @author Jan-Willem Veldhuis - Refactored for 2.x
33 public class DenonMarantzTelnetClientThread extends Thread {
35 private Logger logger = LoggerFactory.getLogger(DenonMarantzTelnetClientThread.class);
37 private static final Integer RECONNECT_DELAY = 60000; // 1 minute
39 private static final Integer TIMEOUT = 60000; // 1 minute
41 private DenonMarantzConfiguration config;
43 private DenonMarantzTelnetListener listener;
45 private boolean connected = false;
47 private Socket socket;
49 private OutputStreamWriter out;
51 private BufferedReader in;
53 public DenonMarantzTelnetClientThread(DenonMarantzConfiguration config, DenonMarantzTelnetListener listener) {
54 logger.debug("Denon listener created");
56 this.listener = listener;
61 while (!isInterrupted()) {
63 connectTelnetSocket();
68 String line = in.readLine();
70 logger.debug("No more data read from client. Disconnecting..");
71 listener.telnetClientConnected(false);
75 logger.trace("Received from {}: {}", config.getHost(), line);
76 if (!line.isBlank()) {
77 listener.receivedLine(line);
79 } catch (SocketTimeoutException e) {
80 logger.trace("Socket timeout");
81 // Disconnects are not always detected unless you write to the socket.
85 } catch (IOException e2) {
86 logger.debug("Error writing to socket");
89 } catch (IOException e) {
90 if (!isInterrupted()) {
91 // only log if we don't stop this on purpose causing a SocketClosed
92 logger.debug("Error in telnet connection ", e);
95 listener.telnetClientConnected(false);
97 } while (!isInterrupted() && connected);
100 logger.debug("Stopped client thread");
103 public void sendCommand(String command) {
106 out.write(command + '\r');
108 } catch (IOException e) {
109 logger.debug("Error sending command", e);
112 logger.debug("Cannot send command, no telnet connection");
116 public void shutdown() {
120 private void connectTelnetSocket() {
124 while (!isInterrupted() && (socket == null || !socket.isConnected())) {
127 logger.debug("Connecting to {}", config.getHost());
129 // Use raw socket instead of TelnetClient here because TelnetClient sends an
130 // extra newline char after each write which causes the connection to become
132 socket = new Socket();
133 socket.connect(new InetSocketAddress(config.getHost(), config.getTelnetPort()), TIMEOUT);
134 socket.setKeepAlive(true);
135 socket.setSoTimeout(TIMEOUT);
137 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
138 out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
141 listener.telnetClientConnected(true);
142 logger.debug("Denon telnet client connected to {}", config.getHost());
143 } catch (IOException e) {
144 logger.debug("Cannot connect to {}", config.getHost(), e);
145 listener.telnetClientConnected(false);
146 } catch (InterruptedException e) {
147 logger.debug("Interrupted while connecting to {}", config.getHost(), e);
148 Thread.currentThread().interrupt();
150 delay = RECONNECT_DELAY;
154 public boolean isConnected() {
158 private void disconnect() {
159 if (socket != null) {
160 logger.debug("Disconnecting socket");
163 } catch (IOException e) {
164 logger.debug("Error while disconnecting telnet client", e);
169 listener.telnetClientConnected(false);