MythTV  master
ExternalRecChannelFetcher.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Class ExternalFetcher
3  *
4  * Copyright (C) John Poet 2018
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 // C/C++ includes
22 #include <utility>
23 
24 // Qt includes
25 #include <QString>
26 
27 // MythTV includes
29 
31 
32 #include "ExternalStreamHandler.h"
33 
34 #define LOC QString("ExternalRec[%1](%2): ").arg(m_cardid).arg(m_command)
35 
37  QString cmd)
38  : m_cardid(cardid)
39  , m_command(std::move(cmd))
40  , m_streamHandler(ExternalStreamHandler::Get(m_command , m_cardid, m_cardid))
41 {
43  {
44  LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
45  Close();
46  }
47 }
48 
50 {
51  Close();
52 }
53 
55 {
56  if (m_streamHandler)
58 }
59 
61 {
62  if (!m_streamHandler)
63  {
64  LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to open external app.");
65  return false;
66  }
67 
68  if (!m_streamHandler->HasTuner())
69  {
70  LOG(VB_CHANNEL, LOG_ERR, LOC + "External app does not have a tuner.");
71  return false;
72  }
73 
74  return true;
75 }
76 
77 bool ExternalRecChannelFetcher::FetchChannel(const QString & cmd,
78  QString & channum,
79  QString & name,
80  QString & callsign,
81  QString & xmltvid,
82  QString & icon)
83 {
84  if (!Valid())
85  return false;
86 
87  QString result;
88 
89  if (!m_streamHandler->ProcessCommand(cmd, result))
90  {
91  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("%1 command failed.").arg(cmd));
92  return false;
93  }
94 
95  if (result.startsWith("ERR"))
96  {
97  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("%1: %2")
98  .arg(cmd, result));
99  return false;
100  }
101  if (result.startsWith("OK:DONE"))
102  {
103  LOG(VB_CHANNEL, LOG_INFO, LOC + result);
104  return false;
105  }
106 
107  // Expect csv: channum, name, callsign, xmltvid, icon
108  QStringList fields = result.mid(3).split(",");
109 
110  if (fields.size() != 4 && fields.size() != 5)
111  {
112  LOG(VB_CHANNEL, LOG_ERR, LOC +
113  QString("Expecting channum, name, callsign, xmltvid and "
114  "optionally icon; "
115  "Received '%1").arg(result));
116  return false;
117  }
118 
119  channum = fields[0];
120  name = fields[1];
121  callsign = fields[2];
122  xmltvid = fields[3];
123  if (fields.size() == 5)
124  icon = fields[4];
125 
126  return true;
127 }
128 
130 {
131  if (!Valid())
132  return 0;
133 
134  QString result;
135  int cnt = -1;
136 
137  if (!m_streamHandler->ProcessCommand("LoadChannels", result, 50s))
138  {
139  LOG(VB_CHANNEL, LOG_ERR, LOC + "LoadChannels command failed.");
140  return -1;
141  }
142 
143  if (result.startsWith("FOUND"))
144  cnt = result.mid(6).toInt();
145  else if (result.startsWith("OK"))
146  cnt = result.mid(3).toInt();
147  else
148  {
149  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("LoadChannels: %1").arg(result));
150  return -1;
151  }
152 
153  return cnt;
154 }
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:66
ExternalStreamHandler::HasTuner
bool HasTuner(void) const
Definition: ExternalStreamHandler.h:95
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
ExternalRecChannelFetcher::~ExternalRecChannelFetcher
~ExternalRecChannelFetcher(void)
Definition: ExternalRecChannelFetcher.cpp:49
ExternalStreamHandler.h
LOC
#define LOC
Definition: ExternalRecChannelFetcher.cpp:34
ExternalRecChannelFetcher::ExternalRecChannelFetcher
ExternalRecChannelFetcher(int cardid, QString cmd)
Definition: ExternalRecChannelFetcher.cpp:36
ExternalRecChannelFetcher::m_cardid
int m_cardid
Definition: ExternalRecChannelFetcher.h:66
ExternalStreamHandler
Definition: ExternalStreamHandler.h:69
mythlogging.h
ExternalRecChannelFetcher::LoadChannels
int LoadChannels(void)
Definition: ExternalRecChannelFetcher.cpp:129
ExternalRecChannelFetcher.h
ExternalRecChannelFetcher::Close
void Close(void)
Definition: ExternalRecChannelFetcher.cpp:54
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1242
ExternalRecChannelFetcher::Valid
bool Valid(void) const
Definition: ExternalRecChannelFetcher.cpp:60
ExternalRecChannelFetcher::FetchChannel
bool FetchChannel(const QString &cmd, QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
Definition: ExternalRecChannelFetcher.cpp:77
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:495
ExternalRecChannelFetcher::m_streamHandler
ExternalStreamHandler * m_streamHandler
Definition: ExternalRecChannelFetcher.h:69