MythTV  master
scaninfo.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <cstdint>
3 #include <utility>
4 
5 // Qt headers
6 #include <QString>
7 
8 // MythTV headers
9 #include "mythdate.h"
10 #include "mythdb.h"
11 #include "scaninfo.h"
12 #include "mythdbcon.h"
13 #include "mythlogging.h"
14 
15 ScanInfo::ScanInfo(uint scanid, uint cardid, uint sourceid,
16  bool processed, QDateTime scandate) :
17  m_scanid(scanid), m_cardid(cardid), m_sourceid(sourceid),
18  m_processed(processed), m_scandate(std::move(scandate))
19 {
20 }
21 
23 {
24  LOG(VB_CHANSCAN, LOG_INFO, QString("SaveScan() scan.size(): %1")
25  .arg(scan.size()));
26 
27  uint scanid = 0;
28  if (scan.empty() || scan[0].m_channels.empty())
29  return scanid;
30 
31  uint sourceid = scan[0].m_channels[0].m_sourceId;
32  uint cardid = scan[0].m_cardid;
33 
34  // Delete saved scans when there are too many or when they are too old
35  const std::vector<ScanInfo> list = LoadScanList(sourceid);
36  for (uint i = 0; i < list.size(); i++)
37  {
38  if (((i + 10) < (list.size())) ||
39  (list[i].m_scandate < MythDate::current().addMonths(-6)))
40  {
41  LOG(VB_CHANSCAN, LOG_INFO, "SaveScan() " +
42  QString("Delete saved scan id:%1 date:%2")
43  .arg(list[i].m_scanid).arg(list[i].m_scandate.toString()));
44  ScanInfo::DeleteScan(list[i].m_scanid);
45  }
46  }
47 
49  query.prepare(
50  "INSERT INTO channelscan ( cardid, sourceid, scandate) "
51  "VALUES (:CARDID, :SOURCEID, :SCANDATE) ");
52  query.bindValue(":CARDID", cardid);
53  query.bindValue(":SOURCEID", sourceid);
54  query.bindValue(":SCANDATE", MythDate::current());
55 
56  if (!query.exec())
57  {
58  MythDB::DBError("SaveScan 1", query);
59  return scanid;
60  }
61 
62  query.prepare("SELECT MAX(scanid) FROM channelscan");
63  if (!query.exec())
64  MythDB::DBError("SaveScan 2", query);
65  else if (query.next())
66  scanid = query.value(0).toUInt();
67 
68  if (!scanid)
69  return scanid;
70 
71  for (const auto & si : scan)
72  si.SaveScan(scanid);
73 
74  return scanid;
75 }
76 
78 {
81  MSqlQuery query2(MSqlQuery::InitCon());
82  query.prepare(
83  "SELECT frequency, inversion, symbolrate, " // 0, 1, 2
84  " fec, polarity, " // 3, 4
85  " hp_code_rate, lp_code_rate, modulation, " // 5, 6, 7
86  " transmission_mode, guard_interval, hierarchy, " // 8, 9, 10
87  " modulation, bandwidth, sistandard, " // 11, 12, 13
88  " tuner_type, transportid, mod_sys, " // 14, 15, 16
89  " rolloff " // 17
90  "FROM channelscan_dtv_multiplex "
91  "WHERE scanid = :SCANID");
92  query.bindValue(":SCANID", scanid);
93  if (!query.exec())
94  {
95  MythDB::DBError("LoadScan 1", query);
96  return list;
97  }
98 
99  while (query.next())
100  {
101  ScanDTVTransport mux;
102  mux.ParseTuningParams(
103  (DTVTunerType) query.value(14).toUInt(),
104  query.value(0).toString(), query.value(1).toString(), // frequency inversion
105  query.value(2).toString(), query.value(3).toString(), // symbolrate fec
106  query.value(4).toString(), query.value(5).toString(), // polarity hp_code_rate
107  query.value(6).toString(), query.value(7).toString(), // lp_code_rate modulation
108  query.value(8).toString(), query.value(9).toString(), // transmission_mode guard_interval
109  query.value(10).toString(), query.value(11).toString(), // hierarchy modulation
110  query.value(12).toString(), query.value(16).toString(), // bandwidth mod_sys
111  query.value(17).toString()); // roloff
112 
113  mux.m_sistandard = query.value(13).toString(); // sistandard
114 
115  query2.prepare(
116  "SELECT "
117  " mplex_id, source_id, channel_id, " // 0, 1, 2
118  " callsign, service_name, chan_num, " // 3, 4, 5
119  " service_id, atsc_major_channel, atsc_minor_channel, " // 6, 7, 8
120  " use_on_air_guide, hidden, hidden_in_guide, " // 9, 10, 11
121  " freqid, icon, tvformat, " // 12, 13, 14
122  " xmltvid, pat_tsid, vct_tsid, " // 15, 16, 17
123  " vct_chan_tsid, sdt_tsid, orig_netid, " // 18, 19, 20
124  " netid, si_standard, in_channels_conf, " // 21, 22, 23
125  " in_pat, in_pmt, in_vct, " // 24, 25, 26
126  " in_nit, in_sdt, is_encrypted, " // 27, 28, 29
127  " is_data_service, is_audio_service, is_opencable, " // 30, 31, 32
128  " could_be_opencable, decryption_status, default_authority, " // 33, 34, 35
129  " service_type " // 36
130  "FROM channelscan_channel "
131  "WHERE transportid = :TRANSPORTID");
132  query2.bindValue(":TRANSPORTID", query.value(15).toUInt());
133 
134  if (!query2.exec())
135  {
136  MythDB::DBError("LoadScan 2", query2);
137  continue;
138  }
139 
140  while (query2.next())
141  {
142  QString si_standard = query2.value(22).toString();
143  si_standard = (si_standard.isEmpty()) ?
144  query.value(13).toString() : si_standard;
145 
146  ChannelInsertInfo chan(
147  query2.value(0).toUInt(), // mplex_id
148  query2.value(1).toUInt(), // source_id
149  query2.value(2).toUInt(), // channel_id
150  query2.value(3).toString(), // callsign
151  query2.value(4).toString(), // service_name
152  query2.value(5).toString(), // chan_num
153  query2.value(6).toUInt(), // service_id
154 
155  query2.value(7).toUInt(), // atsc_major_channel
156  query2.value(8).toUInt(), // atsc_minor_channel
157  query2.value(9).toBool(), // use_on_air_guide
158  query2.value(10).toBool(), // hidden
159  query2.value(11).toBool(), // hidden_in_guide
160 
161  query2.value(12).toString(), // freqid
162  query2.value(13).toString(), // icon
163  query2.value(14).toString(), // tvformat
164  query2.value(15).toString(), // xmltvid
165 
166  query2.value(16).toUInt(), // pat_tsid
167  query2.value(17).toUInt(), // vct_tsid
168  query2.value(18).toUInt(), // vct_chan_tsid
169  query2.value(19).toUInt(), // sdt_tsid
170 
171  query2.value(20).toUInt(), // orig_netid
172  query2.value(21).toUInt(), // netid
173 
174  si_standard,
175 
176  query2.value(23).toBool(), // in_channels_conf
177  query2.value(24).toBool(), // in_pat
178  query2.value(25).toBool(), // in_pmt
179  query2.value(26).toBool(), // in_vct
180  query2.value(27).toBool(), // in_nit
181  query2.value(28).toBool(), // in_sdt
182 
183  query2.value(29).toBool(), // is_encrypted
184  query2.value(30).toBool(), // is_data_service
185  query2.value(31).toBool(), // is_audio_service
186  query2.value(32).toBool(), // is_opencable
187  query2.value(33).toBool(), // could_be_opencable
188  query2.value(34).toInt(), // decryption_status
189  query2.value(35).toString(), // default_authority
190  query2.value(36).toUInt()); // service_type
191  mux.m_channels.push_back(chan);
192  }
193 
194  list.push_back(mux);
195  }
196 
197  return list;
198 }
199 
201 {
203  query.prepare(
204  "UPDATE channelscan "
205  "SET processed = 1 "
206  "WHERE scanid = :SCANID");
207  query.bindValue(":SCANID", scanid);
208 
209  if (!query.exec())
210  {
211  MythDB::DBError("MarkProcessed", query);
212  return false;
213  }
214 
215  return true;
216 }
217 
219 {
221  query.prepare(
222  "DELETE FROM channelscan_channel "
223  "WHERE scanid = :SCANID");
224  query.bindValue(":SCANID", scanid);
225 
226  if (!query.exec())
227  {
228  MythDB::DBError("DeleteScan", query);
229  return false;
230  }
231 
232  query.prepare(
233  "DELETE FROM channelscan_dtv_multiplex "
234  "WHERE scanid = :SCANID");
235  query.bindValue(":SCANID", scanid);
236 
237  if (!query.exec())
238  {
239  MythDB::DBError("DeleteScan", query);
240  return false;
241  }
242 
243  query.prepare(
244  "DELETE FROM channelscan "
245  "WHERE scanid = :SCANID");
246  query.bindValue(":SCANID", scanid);
247 
248  if (!query.exec())
249  {
250  MythDB::DBError("DeleteScan", query);
251  return false;
252  }
253 
254  return true;
255 }
256 
258 {
259  std::vector<ScanInfo> scans = LoadScanList(sourceid);
260  for (auto &scan : scans)
261  {
262  DeleteScan(scan.m_scanid);
263  }
264 }
265 
266 std::vector<ScanInfo> LoadScanList(void)
267 {
268  std::vector<ScanInfo> list;
269 
271  query.prepare(
272  "SELECT scanid, cardid, sourceid, processed, scandate "
273  "FROM channelscan "
274  "ORDER BY scanid, sourceid, cardid, scandate");
275 
276  if (!query.exec())
277  {
278  MythDB::DBError("LoadScanList", query);
279  return list;
280  }
281 
282  while (query.next())
283  {
284  list.emplace_back(query.value(0).toUInt(),
285  query.value(1).toUInt(),
286  query.value(2).toUInt(),
287  (bool) query.value(3).toUInt(),
288  MythDate::as_utc(query.value(4).toDateTime()));
289  }
290 
291  return list;
292 }
293 
294 std::vector<ScanInfo> LoadScanList(uint sourceid)
295 {
296  std::vector<ScanInfo> list;
297 
299  query.prepare(
300  "SELECT scanid, cardid, sourceid, processed, scandate "
301  "FROM channelscan "
302  "WHERE sourceid = :SOURCEID "
303  "ORDER BY scanid, sourceid, cardid, scandate");
304  query.bindValue(":SOURCEID", sourceid);
305 
306  if (!query.exec())
307  {
308  MythDB::DBError("LoadScanList", query);
309  return list;
310  }
311 
312  while (query.next())
313  {
314  list.emplace_back(query.value(0).toUInt(),
315  query.value(1).toUInt(),
316  query.value(2).toUInt(),
317  (bool) query.value(3).toUInt(),
318  MythDate::as_utc(query.value(4).toDateTime()));
319  }
320 
321  return list;
322 }
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
SaveScan
uint SaveScan(const ScanDTVTransportList &scan)
Definition: scaninfo.cpp:22
LoadScanList
std::vector< ScanInfo > LoadScanList(void)
Definition: scaninfo.cpp:266
mythdb.h
MythDate::as_utc
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:23
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
hardwareprofile.scan.scan
def scan(profile, smoonURL, gate)
Definition: scan.py:57
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
ChannelInsertInfo
Definition: channelinfo.h:134
DTVTunerType
Definition: dtvconfparserhelpers.h:77
mythdate.h
mythlogging.h
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:178
scaninfo.h
ScanInfo::MarkProcessed
static bool MarkProcessed(uint scanid)
Definition: scaninfo.cpp:200
ScanInfo::ScanInfo
ScanInfo()=default
uint
unsigned int uint
Definition: compat.h:141
ScanDTVTransportList
std::vector< ScanDTVTransport > ScanDTVTransportList
Definition: dtvmultiplex.h:143
ScanDTVTransport::ParseTuningParams
bool ParseTuningParams(DTVTunerType type, const QString &frequency, const QString &inversion, const QString &symbolrate, const QString &fec, const QString &polarity, const QString &hp_code_rate, const QString &lp_code_rate, const QString &ofdm_modulation, const QString &trans_mode, const QString &guard_interval, const QString &hierarchy, const QString &modulation, const QString &bandwidth, const QString &mod_sys, const QString &rolloff)
Definition: dtvmultiplex.cpp:681
ScanInfo::DeleteScansFromSource
static void DeleteScansFromSource(uint sourceid)
Definition: scaninfo.cpp:257
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
DTVMultiplex::m_sistandard
QString m_sistandard
Definition: dtvmultiplex.h:111
LoadScan
ScanDTVTransportList LoadScan(uint scanid)
Definition: scaninfo.cpp:77
query
MSqlQuery query(MSqlQuery::InitCon())
ScanDTVTransport
Definition: dtvmultiplex.h:116
ScanDTVTransport::m_channels
ChannelInsertInfoList m_channels
Definition: dtvmultiplex.h:138
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
ScanInfo::DeleteScan
static bool DeleteScan(uint scanid)
Definition: scaninfo.cpp:218