MythTV master
externrecscanner.cpp
Go to the documentation of this file.
1// -*- Mode: c++ -*-
2
3// Std C headers
4#include <algorithm>
5#include <cmath>
6#include <unistd.h>
7#include <utility>
8
9// Qt headers
10#include <QFile>
11#include <QRegularExpression>
12#include <QTextStream>
13
14// MythTV headers
16
17#include "cardutil.h"
18#include "channelutil.h"
19#include "externrecscanner.h"
21#include "scanmonitor.h"
22
23#define LOC QString("ExternRecChanFetch: ")
24
26 QString inputname,
27 uint sourceid,
28 ScanMonitor *monitor)
29 : m_scanMonitor(monitor)
30 , m_cardId(cardid)
31 , m_inputName(std::move(inputname))
32 , m_sourceId(sourceid)
33 , m_thread(new MThread("ExternRecChannelScanner", this))
34{
35 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Has ScanMonitor %1")
36 .arg(monitor ? "true" : "false"));
37}
38
40{
41 Stop();
42 delete m_thread;
43 m_thread = nullptr;
44}
45
50{
51 m_lock.lock();
52
53 while (m_threadRunning)
54 {
55 m_stopNow = true;
56 m_lock.unlock();
57 m_thread->wait(5ms);
58 m_lock.lock();
59 }
60
61 m_lock.unlock();
62
63 m_thread->wait();
64}
65
68{
69 Stop();
70 m_stopNow = false;
71 m_thread->start();
72}
73
75{
76 m_lock.lock();
77 m_threadRunning = true;
78 m_lock.unlock();
79
80
81 // Step 1/4 : Get info from DB
83
84 if (m_stopNow || cmd.isEmpty())
85 {
86 LOG(VB_CHANNEL, LOG_INFO, LOC + "Invalid external command");
87 QMutexLocker locker(&m_lock);
88 m_threadRunning = false;
89 m_stopNow = true;
90 return;
91 }
92
93 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("External Command: '%1'").arg(cmd));
94
95
96 // Step 2/4 : Download
97 if (m_scanMonitor)
98 {
100 m_scanMonitor->ScanAppendTextToLog(tr("Creating channel list"));
101 }
102
104
106 if (m_channelTotal < 1)
107 {
108 LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to load channels");
109 QMutexLocker locker(&m_lock);
110 m_threadRunning = false;
111 m_stopNow = true;
112 return;
113 }
114
115 std::vector<uint> existing = ChannelUtil::GetChanIDs(m_sourceId);
116 std::vector<uint>::iterator Iold;
117
118 // Step 3/4 : Process
119 if (m_scanMonitor)
120 m_scanMonitor->ScanAppendTextToLog(tr("Processing channels"));
121
122 QString channum;
123 QString name;
124 QString callsign;
125 QString xmltvid;
126 QString icon;
127 int atsc_major = 0;
128 int atsc_minor = 0;
129 int cnt = 0;
130
131 if (!fetch.FirstChannel(channum, name, callsign, xmltvid, icon))
132 {
133 LOG(VB_CHANNEL, LOG_WARNING, LOC + "No channels found.");
134 QMutexLocker locker(&m_lock);
135 m_threadRunning = false;
136 m_stopNow = true;
137 return;
138 }
139
140 if (m_scanMonitor)
141 m_scanMonitor->ScanAppendTextToLog(tr("Adding Channels"));
142
143 uint idx = 0;
144 for (;;)
145 {
146 static const QRegularExpression digitRE { "\\D+" };
147 QString msg = tr("Channel #%1 : %2").arg(channum, name);
148 QStringList digits = channum.split(digitRE);
149
150 if (digits.size() > 1)
151 {
152 atsc_major = digits.at(0).toInt();
153 atsc_minor = digits.at(1).toInt();
154 LOG(VB_CHANNEL, LOG_DEBUG, LOC +
155 QString("ATSC: %1.%2").arg(atsc_major).arg(atsc_minor));
156 }
157 else
158 {
159 atsc_major = atsc_minor = 0;
160 }
161
162 LOG(VB_CHANNEL, LOG_INFO, QString("Handling channel %1 %2")
163 .arg(channum, name));
164
165 int chanid = ChannelUtil::GetChanID(m_sourceId, channum);
166
167 if (m_scanMonitor)
169
170 if (chanid <= 0)
171 {
172 if (m_scanMonitor)
173 m_scanMonitor->ScanAppendTextToLog(tr("Adding %1").arg(msg));
174
175 chanid = ChannelUtil::CreateChanID(m_sourceId, channum);
176 ChannelUtil::CreateChannel(0, m_sourceId, chanid, callsign, name,
177 channum, 1, atsc_major, atsc_minor,
178 false, kChannelVisible, QString(),
179 icon, "Default", xmltvid);
180 }
181 else
182 {
183 if (m_scanMonitor)
184 m_scanMonitor->ScanAppendTextToLog(tr("Updating %1").arg(msg));
185
186 ChannelUtil::UpdateChannel(0, m_sourceId, chanid, callsign, name,
187 channum, 1, atsc_major, atsc_minor,
188 false, kChannelVisible, QString(),
189 icon, "Default", xmltvid);
190 }
191
193
194 Iold = std::ranges::find(existing, chanid);
195 if (Iold != existing.end())
196 {
197 existing.erase(Iold);
198 }
199
200 if (++idx < m_channelTotal)
201 fetch.NextChannel(channum, name, callsign, xmltvid, icon);
202 else
203 break;
204 }
205
206 // Remove any channels which are no longer valid
207 for (Iold = existing.begin(); Iold != existing.end(); ++Iold)
208 {
209 channum = ChannelUtil::GetChanNum(*Iold);
210
211 if (m_scanMonitor)
213 (tr("Removing unused Channel #%1").arg(channum));
214
216 }
217
218 // Step 4/4 : Finish up
219 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Found %1 channels").arg(cnt));
220 if (m_scanMonitor)
222 (tr("Found %1 channels.").arg(cnt));
223
224 if (m_scanMonitor)
225 {
230 }
231
232 QMutexLocker locker(&m_lock);
233 m_threadRunning = false;
234 m_stopNow = true;
235}
236
238{
239 uint minval = 35;
240 uint range = 70 - minval;
241 uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
242 if (m_scanMonitor)
244}
245
247{
248 uint minval = 70;
249 uint range = 100 - minval;
250 uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
251 if (m_scanMonitor)
253}
254
255void ExternRecChannelScanner::SetMessage(const QString &status)
256{
257 if (m_scanMonitor)
259}
260
261/* vim: set expandtab tabstop=4 shiftwidth=4: */
@ kChannelVisible
Definition: channelinfo.h:22
static QString GetVideoDevice(uint inputid)
Definition: cardutil.h:296
static std::vector< uint > GetChanIDs(int sourceid=-1, bool onlyVisible=false)
static int GetChanID(int db_mplexid, int service_transport_id, int major_channel, int minor_channel, int program_number)
static bool DeleteChannel(uint channel_id)
static bool UpdateChannel(uint db_mplexid, uint source_id, uint channel_id, const QString &callsign, const QString &service_name, const QString &chan_num, uint service_id, uint atsc_major_channel, uint atsc_minor_channel, bool use_on_air_guide, ChannelVisibleType visible, const QString &freqid=QString(), const QString &icon=QString(), QString format=QString(), const QString &xmltvid=QString(), const QString &default_authority=QString(), uint service_type=0, int recpriority=INT_MIN, int tmOffset=INT_MIN, int commMethod=INT_MIN)
static int CreateChanID(uint sourceid, const QString &chan_num)
Creates a unique channel ID for database use.
static QString GetChanNum(int chan_id)
Returns the channel-number string of the given channel.
static bool CreateChannel(uint db_mplexid, uint db_sourceid, uint new_channel_id, const QString &callsign, const QString &service_name, const QString &chan_num, uint service_id, uint atsc_major_channel, uint atsc_minor_channel, bool use_on_air_guide, ChannelVisibleType visible, const QString &freqid, const QString &icon=QString(), QString format="Default", const QString &xmltvid=QString(), const QString &default_authority=QString(), uint service_type=0, int recpriority=0, int tmOffset=0, int commMethod=-1)
ExternRecChannelScanner(uint cardid, QString inputname, uint sourceid, ScanMonitor *monitor=nullptr)
void Scan(void)
Scans the list.
void SetNumChannelsParsed(uint val)
void SetNumChannelsInserted(uint val)
void run(void) override
void Stop(void)
Stops the scanning thread running.
void SetMessage(const QString &status)
bool FirstChannel(QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
bool NextChannel(QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:281
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:298
void ScanPercentComplete(int pct)
void ScanComplete(void)
Definition: scanmonitor.cpp:98
void ScanAppendTextToLog(const QString &status)
unsigned int uint
Definition: compat.h:60
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)
#define LOC
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39