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.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.SocketException;
24 import java.net.SocketTimeoutException;
25 import java.net.UnknownHostException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.tivo.internal.handler.TiVoHandler;
33 import org.openhab.binding.tivo.internal.service.TivoStatusData.ConnectionStatus;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * TivoStatusProvider class to maintain a connection out to the Tivo, monitor and process status messages returned..
40 * @author Jayson Kubilis - Initial contribution
41 * @author Andrew Black - Updates / compilation corrections
42 * @author Michael Lobstein - Updated for OH3
46 public class TivoStatusProvider {
47 private static final Pattern TIVO_STATUS_PATTERN = Pattern.compile("^CH_STATUS (\\d{4}) (?:(\\d{4}))?");
48 private static final int TIMEOUT_SEC = 3000;
50 private final Logger logger = LoggerFactory.getLogger(TivoStatusProvider.class);
51 private @Nullable Socket tivoSocket = null;
52 private @Nullable PrintStream streamWriter = null;
53 private @Nullable StreamReader streamReader = null;
54 private @Nullable TiVoHandler tivoHandler = null;
55 private TivoStatusData tivoStatusData = new TivoStatusData();
56 private TivoConfigData tivoConfigData = new TivoConfigData();
57 private final String thingUid;
60 * Instantiates a new TivoConfigStatusProvider.
62 * @param tivoConfigData {@link TivoConfigData} configuration 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
76 * online, the current channel is always returned. The connection is then closed (allows the socket to be used
79 * @throws InterruptedException
81 public void statusRefresh() throws InterruptedException {
82 if (tivoStatusData.getConnectionStatus() != ConnectionStatus.INIT) {
83 logger.debug(" statusRefresh '{}' - EXISTING status data - '{}'", tivoConfigData.getCfgIdentifier(),
84 tivoStatusData.toString());
87 // this will close the connection and re-open every 12 hours
88 if (tivoConfigData.isKeepConnActive()) {
95 if (!tivoConfigData.isKeepConnActive()) {
101 * {@link cmdTivoSend} sends a command to the Tivo.
103 * @param tivoCommand the complete command string (KEYWORD + PARAMETERS e.g. SETCH 102) to send.
104 * @return {@link TivoStatusData} status data object, contains the result of the command.
105 * @throws InterruptedException
107 public @Nullable TivoStatusData cmdTivoSend(String tivoCommand) throws InterruptedException {
108 boolean connected = connTivoConnect();
109 PrintStream streamWriter = this.streamWriter;
111 if (!connected || streamWriter == null) {
112 return new TivoStatusData(false, -1, -1, false, "CONNECTION FAILED", false, ConnectionStatus.OFFLINE);
114 logger.debug("TiVo '{}' - sending command: '{}'", tivoConfigData.getCfgIdentifier(), tivoCommand);
116 // Handle special keyboard "repeat" commands
117 if (tivoCommand.contains("*")) {
118 repeatCount = Integer.parseInt(tivoCommand.substring(tivoCommand.indexOf("*") + 1));
119 tivoCommand = tivoCommand.substring(0, tivoCommand.indexOf("*"));
120 logger.debug("TiVo '{}' - repeating command: '{}' for '{}' times", tivoConfigData.getCfgIdentifier(),
121 tivoCommand, repeatCount);
123 for (int i = 1; i <= repeatCount; i++) {
125 streamWriter.println(tivoCommand + "\r");
126 if (streamWriter.checkError()) {
127 logger.debug("TiVo '{}' - called cmdTivoSend and encountered an IO error",
128 tivoConfigData.getCfgIdentifier());
129 tivoStatusData = new TivoStatusData(false, -1, -1, false, "CONNECTION FAILED", false,
130 ConnectionStatus.OFFLINE);
134 return tivoStatusData;
138 * {@link statusParse} processes the {@link TivoStatusData} status message returned from the TiVo.
140 * For channel status messages form 'CH_STATUS channel reason' or 'CH_STATUS channel sub-channel reason' calls
141 * {@link getParsedChannel} and returns the channel number (if a match is found in a valid formatted message).
143 * @param rawStatus string representing the message text returned by the TiVo
144 * @return TivoStatusData object conditionally populated based upon the raw status message
146 private TivoStatusData statusParse(String rawStatus) {
147 logger.debug(" statusParse '{}' - running on string '{}'", tivoConfigData.getCfgIdentifier(), rawStatus);
149 if (rawStatus.contentEquals("COMMAND_TIMEOUT")) {
150 // If connection status was UNKNOWN, COMMAND_TIMEOUT indicates the Tivo is alive, so update the status
151 if (this.tivoStatusData.getConnectionStatus() == ConnectionStatus.UNKNOWN) {
152 return new TivoStatusData(false, -1, -1, false, "COMMAND_TIMEOUT", false, ConnectionStatus.ONLINE);
154 // Otherwise ignore COMMAND_TIMEOUT, they occur a few seconds after each successful command, just return
155 // existing status again
156 return this.tivoStatusData;
160 return new TivoStatusData(false, -1, -1, false, "NO_STATUS_DATA_RETURNED", false,
161 tivoStatusData.getConnectionStatus());
163 return new TivoStatusData(true, -1, -1, false, "LIVETV_READY", true, ConnectionStatus.ONLINE);
164 case "CH_FAILED NO_LIVE":
165 return new TivoStatusData(false, -1, -1, false, "CH_FAILED NO_LIVE", true,
166 ConnectionStatus.STANDBY);
167 case "CH_FAILED RECORDING":
168 case "CH_FAILED MISSING_CHANNEL":
169 case "CH_FAILED MALFORMED_CHANNEL":
170 case "CH_FAILED INVALID_CHANNEL":
171 return new TivoStatusData(false, -1, -1, false, rawStatus, true, ConnectionStatus.ONLINE);
172 case "INVALID_COMMAND":
173 return new TivoStatusData(false, -1, -1, false, "INVALID_COMMAND", false, ConnectionStatus.ONLINE);
174 case "CONNECTION_RETRIES_EXHAUSTED":
175 return new TivoStatusData(false, -1, -1, false, "CONNECTION_RETRIES_EXHAUSTED", true,
176 ConnectionStatus.OFFLINE);
180 // Only other documented status is in the form 'CH_STATUS channel reason' or
181 // 'CH_STATUS channel sub-channel reason'
182 Matcher matcher = TIVO_STATUS_PATTERN.matcher(rawStatus);
183 int chNum = -1; // -1 used globally to indicate channel number error
185 boolean isRecording = false;
187 if (matcher.find()) {
188 logger.debug(" statusParse '{}' - groups '{}' with group count of '{}'", tivoConfigData.getCfgIdentifier(),
189 matcher.group(), matcher.groupCount());
190 if (matcher.groupCount() == 1 || matcher.groupCount() == 2) {
191 chNum = Integer.parseInt(matcher.group(1).trim());
192 logger.debug(" statusParse '{}' - parsed channel '{}'", tivoConfigData.getCfgIdentifier(), chNum);
194 if (matcher.groupCount() == 2 && matcher.group(2) != null) {
195 subChNum = Integer.parseInt(matcher.group(2).trim());
196 logger.debug(" statusParse '{}' - parsed sub-channel '{}'", tivoConfigData.getCfgIdentifier(),
200 if (rawStatus.contains("RECORDING")) {
204 rawStatus = rawStatus.replace(" REMOTE", "");
205 rawStatus = rawStatus.replace(" LOCAL", "");
206 return new TivoStatusData(true, chNum, subChNum, isRecording, rawStatus, true, ConnectionStatus.ONLINE);
208 logger.warn(" TiVo '{}' - Unhandled/unexpected status message: '{}'", tivoConfigData.getCfgIdentifier(),
210 return new TivoStatusData(false, -1, -1, false, rawStatus, false, tivoStatusData.getConnectionStatus());
214 * {@link connIsConnected} returns the connection state of the Socket, streamWriter and streamReader objects.
216 * @return true = connection exists and all objects look OK, false = connection does not exist or a problem has
220 private boolean connIsConnected() {
221 Socket tivoSocket = this.tivoSocket;
222 PrintStream streamWriter = this.streamWriter;
224 if (tivoSocket == null) {
225 logger.debug(" connIsConnected '{}' - FALSE: tivoSocket=null", tivoConfigData.getCfgIdentifier());
227 } else if (!tivoSocket.isConnected()) {
228 logger.debug(" connIsConnected '{}' - FALSE: tivoSocket.isConnected=false",
229 tivoConfigData.getCfgIdentifier());
231 } else if (tivoSocket.isClosed()) {
232 logger.debug(" connIsConnected '{}' - FALSE: tivoSocket.isClosed=true", tivoConfigData.getCfgIdentifier());
234 } else if (streamWriter == null) {
235 logger.debug(" connIsConnected '{}' - FALSE: tivoIOSendCommand=null", tivoConfigData.getCfgIdentifier());
237 } else if (streamWriter.checkError()) {
238 logger.debug(" connIsConnected '{}' - FALSE: tivoIOSendCommand.checkError()=true",
239 tivoConfigData.getCfgIdentifier());
241 } else if (streamReader == null) {
242 logger.debug(" connIsConnected '{}' - FALSE: streamReader=null", tivoConfigData.getCfgIdentifier());
249 * {@link connTivoConnect} manages the creation / retry process of the socket connection.
251 * @return true = connected, false = not connected
252 * @throws InterruptedException
254 public boolean connTivoConnect() throws InterruptedException {
255 for (int iL = 1; iL <= tivoConfigData.getNumRetry(); iL++) {
256 logger.debug(" connTivoConnect '{}' - starting connection process '{}' of '{}'.",
257 tivoConfigData.getCfgIdentifier(), iL, tivoConfigData.getNumRetry());
259 // Sort out the socket connection
260 if (connSocketConnect()) {
261 logger.debug(" connTivoConnect '{}' - Socket created / connection made.",
262 tivoConfigData.getCfgIdentifier());
263 StreamReader streamReader = this.streamReader;
264 if (streamReader != null && streamReader.isAlive()) {
265 // Send a newline to poke the Tivo to verify it responds with INVALID_COMMAND/COMMAND_TIMEOUT
266 streamWriter.println("\r");
270 logger.debug(" connTivoConnect '{}' - Socket creation failed.", tivoConfigData.getCfgIdentifier());
271 TiVoHandler tivoHandler = this.tivoHandler;
272 if (tivoHandler != null) {
273 tivoHandler.setStatusOffline();
283 * {@link connTivoReconnect} disconnect and reconnect the socket connection to the TiVo.
285 * @return boolean true = connection succeeded, false = connection failed
286 * @throws InterruptedException
288 public boolean connTivoReconnect() throws InterruptedException {
289 connTivoDisconnect();
291 return connTivoConnect();
295 * {@link connTivoDisconnect} cleanly closes the socket connection and dependent objects
298 public void connTivoDisconnect() throws InterruptedException {
299 TiVoHandler tivoHandler = this.tivoHandler;
300 StreamReader streamReader = this.streamReader;
301 PrintStream streamWriter = this.streamWriter;
302 Socket tivoSocket = this.tivoSocket;
304 logger.debug(" connTivoSocket '{}' - requested to disconnect/cleanup connection objects",
305 tivoConfigData.getCfgIdentifier());
307 // if isCfgKeepConnOpen = false, don't set status to OFFLINE since the socket is closed after each command
308 if (tivoHandler != null && tivoConfigData.isKeepConnActive()) {
309 tivoHandler.setStatusOffline();
312 if (streamWriter != null) {
313 streamWriter.close();
314 this.streamWriter = null;
318 if (tivoSocket != null) {
320 this.tivoSocket = null;
322 } catch (IOException e) {
323 logger.debug(" TiVo '{}' - I/O exception while disconnecting: '{}'. Connection closed.",
324 tivoConfigData.getCfgIdentifier(), e.getMessage());
327 if (streamReader != null) {
328 streamReader.interrupt();
329 streamReader.join(TIMEOUT_SEC);
330 this.streamReader = null;
335 * {@link connSocketConnect} opens a Socket connection to the TiVo. Creates a {@link StreamReader} (Input)
336 * thread to read the responses from the TiVo and a PrintStream (Output) {@link cmdTivoSend}
337 * to send commands to the device.
339 * @param pConnect true = make a new connection , false = close existing connection
340 * @return boolean true = connection succeeded, false = connection failed
341 * @throws InterruptedException
343 private synchronized boolean connSocketConnect() throws InterruptedException {
344 logger.debug(" connSocketConnect '{}' - attempting connection to host '{}', port '{}'",
345 tivoConfigData.getCfgIdentifier(), tivoConfigData.getHost(), tivoConfigData.getTcpPort());
347 if (connIsConnected()) {
348 logger.debug(" connSocketConnect '{}' - already connected to host '{}', port '{}'",
349 tivoConfigData.getCfgIdentifier(), tivoConfigData.getHost(), tivoConfigData.getTcpPort());
352 // something is wrong, so force a disconnect/clean up so we can try again
353 connTivoDisconnect();
357 Socket tivoSocket = new Socket(tivoConfigData.getHost(), tivoConfigData.getTcpPort());
358 tivoSocket.setKeepAlive(true);
359 tivoSocket.setSoTimeout(CONFIG_SOCKET_TIMEOUT_MS);
360 tivoSocket.setReuseAddress(true);
362 if (tivoSocket.isConnected() && !tivoSocket.isClosed()) {
363 if (streamWriter == null) {
364 streamWriter = new PrintStream(tivoSocket.getOutputStream(), false);
366 if (this.streamReader == null) {
367 StreamReader streamReader = new StreamReader(tivoSocket.getInputStream());
368 streamReader.start();
369 this.streamReader = streamReader;
371 this.tivoSocket = tivoSocket;
373 logger.debug(" connSocketConnect '{}' - socket creation failed to host '{}', port '{}'",
374 tivoConfigData.getCfgIdentifier(), tivoConfigData.getHost(), tivoConfigData.getTcpPort());
380 } catch (UnknownHostException e) {
381 logger.debug(" TiVo '{}' - while connecting, unexpected host error: '{}'",
382 tivoConfigData.getCfgIdentifier(), e.getMessage());
383 } catch (IOException e) {
384 if (tivoStatusData.getConnectionStatus() != ConnectionStatus.OFFLINE) {
385 logger.debug(" TiVo '{}' - I/O exception while connecting: '{}'", tivoConfigData.getCfgIdentifier(),
393 * {@link doNappTime} sleeps for the period specified by the getCmdWaitInterval parameter. Primarily used to allow
394 * the TiVo time to process responses after a command is issued.
396 * @throws InterruptedException
398 public void doNappTime() throws InterruptedException {
399 TimeUnit.MILLISECONDS.sleep(tivoConfigData.getCmdWaitInterval());
402 public TivoStatusData getServiceStatus() {
403 return tivoStatusData;
406 public void setServiceStatus(TivoStatusData tivoStatusData) {
407 this.tivoStatusData = tivoStatusData;
411 * {@link StreamReader} data stream reader that reads the status data returned from the TiVo.
414 public class StreamReader extends Thread {
415 private @Nullable BufferedReader bufferedReader = null;
418 * {@link StreamReader} construct a data stream reader that reads the status data returned from the TiVo via a
421 * @param inputStream socket input stream.
422 * @throws IOException
424 public StreamReader(InputStream inputStream) {
425 this.setName("OH-binding-" + thingUid + "-" + tivoConfigData.getHost() + ":" + tivoConfigData.getTcpPort());
426 this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
427 this.setDaemon(true);
433 logger.debug("streamReader {} is running. ", tivoConfigData.getCfgIdentifier());
434 while (!Thread.currentThread().isInterrupted()) {
435 String receivedData = null;
436 BufferedReader reader = bufferedReader;
437 if (reader == null) {
438 throw new IOException("streamReader failed: input stream is null");
442 receivedData = reader.readLine();
443 } catch (SocketTimeoutException | SocketException e) {
444 // Do nothing. Just allow the thread to check if it has to stop.
447 if (receivedData != null) {
448 logger.debug("TiVo {} data received: {}", tivoConfigData.getCfgIdentifier(), receivedData);
449 TivoStatusData commandResult = statusParse(receivedData);
450 TiVoHandler handler = tivoHandler;
451 if (handler != null) {
452 handler.updateTivoStatus(tivoStatusData, commandResult);
454 tivoStatusData = commandResult;
458 } catch (IOException e) {
459 closeBufferedReader();
460 logger.debug("TiVo {} is disconnected. ", tivoConfigData.getCfgIdentifier(), e);
462 closeBufferedReader();
463 logger.debug("streamReader {} is stopped. ", tivoConfigData.getCfgIdentifier());
466 private void closeBufferedReader() {
467 BufferedReader reader = bufferedReader;
468 if (reader != null) {
471 this.bufferedReader = null;
472 } catch (IOException e) {
473 logger.debug("Error closing bufferedReader: {}", e.getMessage(), e);