2 * Copyright (c) 2010-2021 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.tivo.internal.service;
15 import static org.openhab.binding.tivo.internal.TiVoBindingConstants.CONFIG_SOCKET_TIMEOUT_MS;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.io.PrintStream;
22 import java.net.Socket;
23 import java.net.SocketTimeoutException;
24 import java.net.UnknownHostException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.tivo.internal.handler.TiVoHandler;
32 import org.openhab.binding.tivo.internal.service.TivoStatusData.ConnectionStatus;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * TivoStatusProvider class to maintain a connection out to the Tivo, monitor and process status messages returned..
39 * @author Jayson Kubilis - Initial contribution
40 * @author Andrew Black - Updates / compilation corrections
41 * @author Michael Lobstein - Updated for OH3
45 public class TivoStatusProvider {
46 private static final Pattern TIVO_STATUS_PATTERN = Pattern.compile("^CH_STATUS (\\d{4}) (?:(\\d{4}))?");
47 private static final int TIMEOUT_SEC = 3000;
49 private final Logger logger = LoggerFactory.getLogger(TivoStatusProvider.class);
50 private @Nullable Socket tivoSocket = null;
51 private @Nullable PrintStream streamWriter = null;
52 private @Nullable StreamReader streamReader = null;
53 private @Nullable TiVoHandler tivoHandler = null;
54 private TivoStatusData tivoStatusData = new TivoStatusData();
55 private TivoConfigData tivoConfigData = new TivoConfigData();
56 private final String thingUid;
59 * Instantiates a new TivoConfigStatusProvider.
61 * @param tivoConfigData {@link TivoConfigData} configuration data for the specific thing.
62 * @param tivoStatusData {@link TivoStatusData} status data for the specific thing.
63 * @param tivoHandler {@link TivoHandler} parent handler object for the TivoConfigStatusProvider.
67 public TivoStatusProvider(TivoConfigData tivoConfigData, TiVoHandler tivoHandler) {
68 this.tivoStatusData = new TivoStatusData(false, -1, -1, false, "INITIALISING", false, ConnectionStatus.UNKNOWN);
69 this.tivoConfigData = tivoConfigData;
70 this.tivoHandler = tivoHandler;
71 this.thingUid = tivoHandler.getThing().getUID().getAsString();
75 * {@link statusRefresh} initiates a connection to the TiVo. When a new connection is made and the TiVo is online,
76 * the current channel is always returned. The connection is then closed (allows the socket to be used by other
79 * @return {@link TivoStatusData} object
80 * @throws InterruptedException
82 public void statusRefresh() throws InterruptedException {
83 if (tivoStatusData.getConnectionStatus() != ConnectionStatus.INIT) {
84 logger.debug(" statusRefresh '{}' - EXISTING status data - '{}'", tivoConfigData.getCfgIdentifier(),
85 tivoStatusData.toString());
89 if (!tivoConfigData.isKeepConnActive()) {
95 * {@link cmdTivoSend} sends a command to the Tivo.
97 * @param tivoCommand the complete command string (KEYWORD + PARAMETERS e.g. SETCH 102) to send.
98 * @return {@link TivoStatusData} status data object, contains the result of the command.
99 * @throws InterruptedException
101 public @Nullable TivoStatusData cmdTivoSend(String tivoCommand) throws InterruptedException {
102 boolean connected = connTivoConnect();
103 PrintStream streamWriter = this.streamWriter;
105 if (!connected || streamWriter == null) {
106 return new TivoStatusData(false, -1, -1, false, "CONNECTION FAILED", false, ConnectionStatus.OFFLINE);
108 logger.debug("TiVo '{}' - sending command: '{}'", tivoConfigData.getCfgIdentifier(), tivoCommand);
110 // Handle special keyboard "repeat" commands
111 if (tivoCommand.contains("*")) {
112 repeatCount = Integer.parseInt(tivoCommand.substring(tivoCommand.indexOf("*") + 1));
113 tivoCommand = tivoCommand.substring(0, tivoCommand.indexOf("*"));
114 logger.debug("TiVo '{}' - repeating command: '{}' for '{}' times", tivoConfigData.getCfgIdentifier(),
115 tivoCommand, repeatCount);
117 for (int i = 1; i <= repeatCount; i++) {
119 streamWriter.println(tivoCommand.toString() + "\r");
120 if (streamWriter.checkError()) {
121 logger.debug("TiVo '{}' - called cmdTivoSend and encountered an IO error",
122 tivoConfigData.getCfgIdentifier());
123 tivoStatusData = new TivoStatusData(false, -1, -1, false, "CONNECTION FAILED", false,
124 ConnectionStatus.OFFLINE);
128 return tivoStatusData;
132 * {@link statusParse} processes the {@link TivoStatusData} status message returned from the TiVo.
134 * For channel status messages form 'CH_STATUS channel reason' or 'CH_STATUS channel sub-channel reason' calls
135 * {@link getParsedChannel} and returns the channel number (if a match is found in a valid formatted message).
137 * @param rawStatus string representing the message text returned by the TiVo
138 * @return TivoStatusData object conditionally populated based upon the raw status message
140 private TivoStatusData statusParse(String rawStatus) {
141 logger.debug(" statusParse '{}' - running on string '{}'", tivoConfigData.getCfgIdentifier(), rawStatus);
143 if (rawStatus.contentEquals("COMMAND_TIMEOUT")) {
144 // Ignore COMMAND_TIMEOUT, they occur a few seconds after each successful command, just return existing
146 return this.tivoStatusData;
150 return new TivoStatusData(false, -1, -1, false, "NO_STATUS_DATA_RETURNED", false,
151 tivoStatusData.getConnectionStatus());
153 return new TivoStatusData(true, -1, -1, false, "LIVETV_READY", true, ConnectionStatus.ONLINE);
154 case "CH_FAILED NO_LIVE":
155 return new TivoStatusData(false, -1, -1, false, "CH_FAILED NO_LIVE", true,
156 ConnectionStatus.STANDBY);
157 case "CH_FAILED RECORDING":
158 case "CH_FAILED MISSING_CHANNEL":
159 case "CH_FAILED MALFORMED_CHANNEL":
160 case "CH_FAILED INVALID_CHANNEL":
161 return new TivoStatusData(false, -1, -1, false, rawStatus, true, ConnectionStatus.ONLINE);
162 case "INVALID_COMMAND":
163 return new TivoStatusData(false, -1, -1, false, "INVALID_COMMAND", false, ConnectionStatus.ONLINE);
164 case "CONNECTION_RETRIES_EXHAUSTED":
165 return new TivoStatusData(false, -1, -1, false, "CONNECTION_RETRIES_EXHAUSTED", true,
166 ConnectionStatus.OFFLINE);
170 // Only other documented status is in the form 'CH_STATUS channel reason' or
171 // 'CH_STATUS channel sub-channel reason'
172 Matcher matcher = TIVO_STATUS_PATTERN.matcher(rawStatus);
173 int chNum = -1; // -1 used globally to indicate channel number error
175 boolean isRecording = false;
177 if (matcher.find()) {
178 logger.debug(" statusParse '{}' - groups '{}' with group count of '{}'", tivoConfigData.getCfgIdentifier(),
179 matcher.group(), matcher.groupCount());
180 if (matcher.groupCount() == 1 || matcher.groupCount() == 2) {
181 chNum = Integer.parseInt(matcher.group(1).trim());
182 logger.debug(" statusParse '{}' - parsed channel '{}'", tivoConfigData.getCfgIdentifier(), chNum);
184 if (matcher.groupCount() == 2) {
185 subChNum = Integer.parseInt(matcher.group(2).trim());
186 logger.debug(" statusParse '{}' - parsed sub-channel '{}'", tivoConfigData.getCfgIdentifier(),
190 if (rawStatus.contains("RECORDING")) {
194 rawStatus = rawStatus.replace(" REMOTE", "");
195 rawStatus = rawStatus.replace(" LOCAL", "");
196 return new TivoStatusData(true, chNum, subChNum, isRecording, rawStatus, true, ConnectionStatus.ONLINE);
198 logger.warn(" TiVo '{}' - Unhandled/unexpected status message: '{}'", tivoConfigData.getCfgIdentifier(),
200 return new TivoStatusData(false, -1, -1, false, rawStatus, false, tivoStatusData.getConnectionStatus());
204 * {@link connIsConnected} returns the connection state of the Socket, streamWriter and streamReader objects.
206 * @return true = connection exists and all objects look OK, false = connection does not exist or a problem has
210 private boolean connIsConnected() {
211 Socket tivoSocket = this.tivoSocket;
212 PrintStream streamWriter = this.streamWriter;
214 if (tivoSocket == null) {
215 logger.debug(" connIsConnected '{}' - FALSE: tivoSocket=null", tivoConfigData.getCfgIdentifier());
217 } else if (!tivoSocket.isConnected()) {
218 logger.debug(" connIsConnected '{}' - FALSE: tivoSocket.isConnected=false",
219 tivoConfigData.getCfgIdentifier());
221 } else if (tivoSocket.isClosed()) {
222 logger.debug(" connIsConnected '{}' - FALSE: tivoSocket.isClosed=true", tivoConfigData.getCfgIdentifier());
224 } else if (streamWriter == null) {
225 logger.debug(" connIsConnected '{}' - FALSE: tivoIOSendCommand=null", tivoConfigData.getCfgIdentifier());
227 } else if (streamWriter.checkError()) {
228 logger.debug(" connIsConnected '{}' - FALSE: tivoIOSendCommand.checkError()=true",
229 tivoConfigData.getCfgIdentifier());
231 } else if (streamReader == null) {
232 logger.debug(" connIsConnected '{}' - FALSE: streamReader=null", tivoConfigData.getCfgIdentifier());
239 * {@link connTivoConnect} manages the creation / retry process of the socket connection.
241 * @return true = connected, false = not connected
242 * @throws InterruptedException
244 public boolean connTivoConnect() throws InterruptedException {
245 for (int iL = 1; iL <= tivoConfigData.getNumRetry(); iL++) {
246 logger.debug(" connTivoConnect '{}' - starting connection process '{}' of '{}'.",
247 tivoConfigData.getCfgIdentifier(), iL, tivoConfigData.getNumRetry());
249 // Sort out the socket connection
250 if (connSocketConnect()) {
251 logger.debug(" connTivoConnect '{}' - Socket created / connection made.",
252 tivoConfigData.getCfgIdentifier());
253 StreamReader streamReader = this.streamReader;
254 if (streamReader != null && streamReader.isAlive()) {
258 logger.debug(" connTivoConnect '{}' - Socket creation failed.", tivoConfigData.getCfgIdentifier());
259 TiVoHandler tivoHandler = this.tivoHandler;
260 if (tivoHandler != null) {
261 tivoHandler.setStatusOffline();
271 * {@link connTivoReconnect} disconnect and reconnect the socket connection to the TiVo.
273 * @return boolean true = connection succeeded, false = connection failed
274 * @throws InterruptedException
276 public boolean connTivoReconnect() throws InterruptedException {
277 connTivoDisconnect();
279 return connTivoConnect();
283 * {@link connTivoDisconnect} cleanly closes the socket connection and dependent objects
286 public void connTivoDisconnect() throws InterruptedException {
287 TiVoHandler tivoHandler = this.tivoHandler;
288 StreamReader streamReader = this.streamReader;
289 PrintStream streamWriter = this.streamWriter;
290 Socket tivoSocket = this.tivoSocket;
292 logger.debug(" connTivoSocket '{}' - requested to disconnect/cleanup connection objects",
293 tivoConfigData.getCfgIdentifier());
295 // if isCfgKeepConnOpen = false, don't set status to OFFLINE since the socket is closed after each command
296 if (tivoHandler != null && tivoConfigData.isKeepConnActive()) {
297 tivoHandler.setStatusOffline();
300 if (streamWriter != null) {
301 streamWriter.close();
302 this.streamWriter = null;
306 if (tivoSocket != null) {
308 this.tivoSocket = null;
310 } catch (IOException e) {
311 logger.debug(" TiVo '{}' - I/O exception while disconnecting: '{}'. Connection closed.",
312 tivoConfigData.getCfgIdentifier(), e.getMessage());
315 if (streamReader != null) {
316 streamReader.interrupt();
317 streamReader.join(TIMEOUT_SEC);
318 this.streamReader = null;
323 * {@link connSocketConnect} opens a Socket connection to the TiVo. Creates a {@link StreamReader} (Input)
324 * thread to read the responses from the TiVo and a PrintStream (Output) {@link cmdTivoSend}
325 * to send commands to the device.
327 * @param pConnect true = make a new connection , false = close existing connection
328 * @return boolean true = connection succeeded, false = connection failed
329 * @throws InterruptedException
331 private synchronized boolean connSocketConnect() throws InterruptedException {
332 logger.debug(" connSocketConnect '{}' - attempting connection to host '{}', port '{}'",
333 tivoConfigData.getCfgIdentifier(), tivoConfigData.getHost(), tivoConfigData.getTcpPort());
335 if (connIsConnected()) {
336 logger.debug(" connSocketConnect '{}' - already connected to host '{}', port '{}'",
337 tivoConfigData.getCfgIdentifier(), tivoConfigData.getHost(), tivoConfigData.getTcpPort());
340 // something is wrong, so force a disconnect/clean up so we can try again
341 connTivoDisconnect();
345 Socket tivoSocket = new Socket(tivoConfigData.getHost(), tivoConfigData.getTcpPort());
346 tivoSocket.setKeepAlive(true);
347 tivoSocket.setSoTimeout(CONFIG_SOCKET_TIMEOUT_MS);
348 tivoSocket.setReuseAddress(true);
350 if (tivoSocket.isConnected() && !tivoSocket.isClosed()) {
351 if (streamWriter == null) {
352 streamWriter = new PrintStream(tivoSocket.getOutputStream(), false);
354 if (this.streamReader == null) {
355 StreamReader streamReader = new StreamReader(tivoSocket.getInputStream());
356 streamReader.start();
357 this.streamReader = streamReader;
359 this.tivoSocket = tivoSocket;
361 logger.debug(" connSocketConnect '{}' - socket creation failed to host '{}', port '{}'",
362 tivoConfigData.getCfgIdentifier(), tivoConfigData.getHost(), tivoConfigData.getTcpPort());
368 } catch (UnknownHostException e) {
369 logger.debug(" TiVo '{}' - while connecting, unexpected host error: '{}'",
370 tivoConfigData.getCfgIdentifier(), e.getMessage());
371 } catch (IOException e) {
372 if (tivoStatusData.getConnectionStatus() != ConnectionStatus.OFFLINE) {
373 logger.debug(" TiVo '{}' - I/O exception while connecting: '{}'", tivoConfigData.getCfgIdentifier(),
381 * {@link doNappTime} sleeps for the period specified by the getCmdWaitInterval parameter. Primarily used to allow
382 * the TiVo time to process responses after a command is issued.
384 * @throws InterruptedException
386 public void doNappTime() throws InterruptedException {
387 TimeUnit.MILLISECONDS.sleep(tivoConfigData.getCmdWaitInterval());
390 public TivoStatusData getServiceStatus() {
391 return tivoStatusData;
394 public void setServiceStatus(TivoStatusData tivoStatusData) {
395 this.tivoStatusData = tivoStatusData;
399 * {@link StreamReader} data stream reader that reads the status data returned from the TiVo.
402 public class StreamReader extends Thread {
403 private @Nullable BufferedReader bufferedReader = null;
406 * {@link StreamReader} construct a data stream reader that reads the status data returned from the TiVo via a
409 * @param inputStream socket input stream.
410 * @throws IOException
412 public StreamReader(InputStream inputStream) {
413 this.setName("OH-binding-" + thingUid + "-" + tivoConfigData.getHost() + ":" + tivoConfigData.getTcpPort());
414 this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
415 this.setDaemon(true);
421 logger.debug("streamReader {} is running. ", tivoConfigData.getCfgIdentifier());
422 while (!Thread.currentThread().isInterrupted()) {
423 String receivedData = null;
424 BufferedReader reader = bufferedReader;
425 if (reader == null) {
426 throw new IOException("streamReader failed: input stream is null");
430 receivedData = reader.readLine();
431 } catch (SocketTimeoutException e) {
432 // Do nothing. Just allow the thread to check if it has to stop.
435 if (receivedData != null) {
436 logger.debug("TiVo {} data received: {}", tivoConfigData.getCfgIdentifier(), receivedData);
437 TivoStatusData commandResult = statusParse(receivedData);
438 TiVoHandler handler = tivoHandler;
439 if (handler != null) {
440 handler.updateTivoStatus(tivoStatusData, commandResult);
442 tivoStatusData = commandResult;
446 } catch (IOException e) {
447 closeBufferedReader();
448 logger.debug("TiVo {} is disconnected. ", tivoConfigData.getCfgIdentifier(), e);
450 closeBufferedReader();
451 logger.debug("streamReader {} is stopped. ", tivoConfigData.getCfgIdentifier());
454 private void closeBufferedReader() {
455 BufferedReader reader = bufferedReader;
456 if (reader != null) {
459 this.bufferedReader = null;
460 } catch (IOException e) {
461 logger.debug("Error closing bufferedReader: {}", e.getMessage(), e);