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