2 * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
24 import org.openhab.binding.denonmarantz.internal.config.DenonMarantzConfiguration;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Manage telnet connection to the Denon/Marantz Receiver
31 * @author Jeroen Idserda - Initial contribution (1.x Binding)
32 * @author Jan-Willem Veldhuis - Refactored for 2.x
34 public class DenonMarantzTelnetClientThread extends Thread {
36 private Logger logger = LoggerFactory.getLogger(DenonMarantzTelnetClientThread.class);
38 private static final Integer RECONNECT_DELAY = 60000; // 1 minute
40 private static final Integer TIMEOUT = 60000; // 1 minute
42 private DenonMarantzConfiguration config;
44 private DenonMarantzTelnetListener listener;
46 private boolean connected = false;
48 private Socket socket;
50 private OutputStreamWriter out;
52 private BufferedReader in;
54 public DenonMarantzTelnetClientThread(DenonMarantzConfiguration config, DenonMarantzTelnetListener listener) {
55 logger.debug("Denon listener created");
57 this.listener = listener;
62 while (!isInterrupted()) {
64 connectTelnetSocket();
69 String line = in.readLine();
71 logger.debug("No more data read from client. Disconnecting..");
72 listener.telnetClientConnected(false);
76 logger.trace("Received from {}: {}", config.getHost(), line);
77 if (!StringUtils.isBlank(line)) {
78 listener.receivedLine(line);
80 } catch (SocketTimeoutException e) {
81 logger.trace("Socket timeout");
82 // Disconnects are not always detected unless you write to the socket.
86 } catch (IOException e2) {
87 logger.debug("Error writing to socket");
90 } catch (IOException e) {
91 if (!isInterrupted()) {
92 // only log if we don't stop this on purpose causing a SocketClosed
93 logger.debug("Error in telnet connection ", e);
96 listener.telnetClientConnected(false);
98 } while (!isInterrupted() && connected);
101 logger.debug("Stopped client thread");
104 public void sendCommand(String command) {
107 out.write(command + '\r');
109 } catch (IOException e) {
110 logger.debug("Error sending command", e);
113 logger.debug("Cannot send command, no telnet connection");
117 public void shutdown() {
121 private void connectTelnetSocket() {
125 while (!isInterrupted() && (socket == null || !socket.isConnected())) {
128 logger.debug("Connecting to {}", config.getHost());
130 // Use raw socket instead of TelnetClient here because TelnetClient sends an
131 // extra newline char after each write which causes the connection to become
133 socket = new Socket();
134 socket.connect(new InetSocketAddress(config.getHost(), config.getTelnetPort()), TIMEOUT);
135 socket.setKeepAlive(true);
136 socket.setSoTimeout(TIMEOUT);
138 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
139 out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
142 listener.telnetClientConnected(true);
143 logger.debug("Denon telnet client connected to {}", config.getHost());
144 } catch (IOException e) {
145 logger.debug("Cannot connect to {}", config.getHost(), e);
146 listener.telnetClientConnected(false);
147 } catch (InterruptedException e) {
148 logger.debug("Interrupted while connecting to {}", config.getHost(), e);
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);