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