]> git.basschouten.com Git - openhab-addons.git/blob
9eee0775b9163547dea267fc911b6257da541fd4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.denonmarantz.internal.connector.telnet;
14
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;
22
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;
27
28 /**
29  * Manage telnet connection to the Denon/Marantz Receiver
30  *
31  * @author Jeroen Idserda - Initial contribution (1.x Binding)
32  * @author Jan-Willem Veldhuis - Refactored for 2.x
33  */
34 public class DenonMarantzTelnetClientThread extends Thread {
35
36     private Logger logger = LoggerFactory.getLogger(DenonMarantzTelnetClientThread.class);
37
38     private static final Integer RECONNECT_DELAY = 60000; // 1 minute
39
40     private static final Integer TIMEOUT = 60000; // 1 minute
41
42     private DenonMarantzConfiguration config;
43
44     private DenonMarantzTelnetListener listener;
45
46     private boolean connected = false;
47
48     private Socket socket;
49
50     private OutputStreamWriter out;
51
52     private BufferedReader in;
53
54     public DenonMarantzTelnetClientThread(DenonMarantzConfiguration config, DenonMarantzTelnetListener listener) {
55         logger.debug("Denon listener created");
56         this.config = config;
57         this.listener = listener;
58     }
59
60     @Override
61     public void run() {
62         while (!isInterrupted()) {
63             if (!connected) {
64                 connectTelnetSocket();
65             }
66
67             do {
68                 try {
69                     String line = in.readLine();
70                     if (line == null) {
71                         logger.debug("No more data read from client. Disconnecting..");
72                         listener.telnetClientConnected(false);
73                         disconnect();
74                         break;
75                     }
76                     logger.trace("Received from {}: {}", config.getHost(), line);
77                     if (!StringUtils.isBlank(line)) {
78                         listener.receivedLine(line);
79                     }
80                 } catch (SocketTimeoutException e) {
81                     logger.trace("Socket timeout");
82                     // Disconnects are not always detected unless you write to the socket.
83                     try {
84                         out.write('\r');
85                         out.flush();
86                     } catch (IOException e2) {
87                         logger.debug("Error writing to socket");
88                         connected = false;
89                     }
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);
94                     }
95                     connected = false;
96                     listener.telnetClientConnected(false);
97                 }
98             } while (!isInterrupted() && connected);
99         }
100         disconnect();
101         logger.debug("Stopped client thread");
102     }
103
104     public void sendCommand(String command) {
105         if (out != null) {
106             try {
107                 out.write(command + '\r');
108                 out.flush();
109             } catch (IOException e) {
110                 logger.debug("Error sending command", e);
111             }
112         } else {
113             logger.debug("Cannot send command, no telnet connection");
114         }
115     }
116
117     public void shutdown() {
118         disconnect();
119     }
120
121     private void connectTelnetSocket() {
122         disconnect();
123         int delay = 0;
124
125         while (!isInterrupted() && (socket == null || !socket.isConnected())) {
126             try {
127                 Thread.sleep(delay);
128                 logger.debug("Connecting to {}", config.getHost());
129
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
132                 // unresponsive.
133                 socket = new Socket();
134                 socket.connect(new InetSocketAddress(config.getHost(), config.getTelnetPort()), TIMEOUT);
135                 socket.setKeepAlive(true);
136                 socket.setSoTimeout(TIMEOUT);
137
138                 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
139                 out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
140
141                 connected = true;
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);
149             }
150             delay = RECONNECT_DELAY;
151         }
152     }
153
154     public boolean isConnected() {
155         return connected;
156     }
157
158     private void disconnect() {
159         if (socket != null) {
160             logger.debug("Disconnecting socket");
161             try {
162                 socket.close();
163             } catch (IOException e) {
164                 logger.debug("Error while disconnecting telnet client", e);
165             } finally {
166                 socket = null;
167                 out = null;
168                 in = null;
169                 listener.telnetClientConnected(false);
170             }
171         }
172     }
173 }