MythTV  master
weatherSource.cpp
Go to the documentation of this file.
1 #include <unistd.h>
2 
3 // QT headers
4 #include <QDir>
5 #include <QFile>
6 #include <QTextStream>
7 #include <QTextCodec>
8 #include <QApplication>
9 
10 // MythTV headers
11 #include <mythcontext.h>
12 #include <mythdb.h>
13 #include <compat.h>
14 #include <mythdirs.h>
15 #include <mythsystemlegacy.h>
16 #include <exitcodes.h>
17 
18 // MythWeather headers
19 #include "weatherScreen.h"
20 #include "weatherSource.h"
21 
22 QStringList WeatherSource::ProbeTypes(const QString& workingDirectory,
23  const QString& program)
24 {
25  QStringList arguments("-t");
26  const QString loc = QString("WeatherSource::ProbeTypes(%1 %2): ")
27  .arg(program).arg(arguments.join(" "));
28  QStringList types;
29 
30  uint flags = kMSRunShell | kMSStdOut |
32  MythSystemLegacy ms(program, arguments, flags);
33  ms.SetDirectory(workingDirectory);
34  ms.Run();
35  if (ms.Wait() != GENERIC_EXIT_OK)
36  {
37  LOG(VB_GENERAL, LOG_ERR, loc + "Cannot run script");
38  return types;
39  }
40 
41  QByteArray result = ms.ReadAll();
42  QTextStream text(result);
43 
44  while (!text.atEnd())
45  {
46  QString tmp = text.readLine();
47 
48  while (tmp.endsWith('\n') || tmp.endsWith('\r'))
49  tmp.chop(1);
50 
51  if (!tmp.isEmpty())
52  types += tmp;
53  }
54 
55  if (types.empty())
56  LOG(VB_GENERAL, LOG_ERR, loc + "Invalid output from -t option");
57 
58  return types;
59 }
60 
61 bool WeatherSource::ProbeTimeouts(const QString& workingDirectory,
62  const QString& program,
63  uint &updateTimeout,
64  uint &scriptTimeout)
65 {
66  QStringList arguments("-T");
67  const QString loc = QString("WeatherSource::ProbeTimeouts(%1 %2): ")
68  .arg(program).arg(arguments.join(" "));
69 
71  scriptTimeout = DEFAULT_SCRIPT_TIMEOUT;
72 
73  uint flags = kMSRunShell | kMSStdOut |
75  MythSystemLegacy ms(program, arguments, flags);
76  ms.SetDirectory(workingDirectory);
77  ms.Run();
78  if (ms.Wait() != GENERIC_EXIT_OK)
79  {
80  LOG(VB_GENERAL, LOG_ERR, loc + "Cannot run script");
81  return false;
82  }
83 
84  QByteArray result = ms.ReadAll();
85  QTextStream text(result);
86 
87  QStringList lines;
88  while (!text.atEnd())
89  {
90  QString tmp = text.readLine();
91 
92  while (tmp.endsWith('\n') || tmp.endsWith('\r'))
93  tmp.chop(1);
94 
95  if (!tmp.isEmpty())
96  lines << tmp;
97  }
98 
99  if (lines.empty())
100  {
101  LOG(VB_GENERAL, LOG_ERR, loc + "Invalid Script Output! No Lines");
102  return false;
103  }
104 
105  QStringList temp = lines[0].split(',');
106  if (temp.size() != 2)
107  {
108  LOG(VB_GENERAL, LOG_ERR, loc +
109  QString("Invalid Script Output! '%1'").arg(lines[0]));
110  return false;
111  }
112 
113  bool isOK[2];
114  uint ut = temp[0].toUInt(&isOK[0]);
115  uint st = temp[1].toUInt(&isOK[1]);
116  if (!isOK[0] || !isOK[1])
117  {
118  LOG(VB_GENERAL, LOG_ERR, loc +
119  QString("Invalid Script Output! '%1'").arg(lines[0]));
120  return false;
121  }
122 
123  updateTimeout = ut * 1000;
124  scriptTimeout = st;
125 
126  return true;
127 }
128 
130 {
131  QStringList arguments("-v");
132 
133  const QString loc = QString("WeatherSource::ProbeInfo(%1 %2): ")
134  .arg(info.program).arg(arguments.join(" "));
135 
136  uint flags = kMSRunShell | kMSStdOut |
138  MythSystemLegacy ms(info.program, arguments, flags);
139  ms.SetDirectory(info.path);
140  ms.Run();
141  if (ms.Wait() != GENERIC_EXIT_OK)
142  {
143  LOG(VB_GENERAL, LOG_ERR, loc + "Cannot run script");
144  return false;
145  }
146 
147  QByteArray result = ms.ReadAll();
148  QTextStream text(result);
149 
150  QStringList lines;
151  while (!text.atEnd())
152  {
153  QString tmp = text.readLine();
154 
155  while (tmp.endsWith('\n') || tmp.endsWith('\r'))
156  tmp.chop(1);
157 
158  if (!tmp.isEmpty())
159  lines << tmp;
160  }
161 
162  if (lines.empty())
163  {
164  LOG(VB_GENERAL, LOG_ERR, loc + "Invalid Script Output! No Lines");
165  return false;
166  }
167 
168  QStringList temp = lines[0].split(',');
169  if (temp.size() != 4)
170  {
171  LOG(VB_GENERAL, LOG_ERR, loc +
172  QString("Invalid Script Output! '%1'").arg(lines[0]));
173  return false;
174  }
175 
176  info.name = temp[0];
177  info.version = temp[1];
178  info.author = temp[2];
179  info.email = temp[3];
180 
181  return true;
182 }
183 
184 /* Basic logic of this behemouth...
185  * run script with -v flag, this returns among other things, the version number
186  * Search the database using the name (also returned from -v).
187  * if it exists, compare versions from -v and db
188  * if the same, populate the info struct from db, and we're done
189  * if they differ, get the rest of the needed information from the script and
190  * update the database, note, it does not overwrite the existing timeout values.
191  * if the script is not in the database, we probe it for types and default
192  * timeout values, and add it to the database
193  */
195 {
196  QStringList temp;
197 
198  if (!fi.isReadable() || !fi.isExecutable())
199  return nullptr;
200 
201  ScriptInfo info;
202  info.path = fi.absolutePath();
203  info.program = fi.absoluteFilePath();
204 
205  if (!WeatherSource::ProbeInfo(info))
206  return nullptr;
207 
209  QString query =
210  "SELECT sourceid, source_name, update_timeout, retrieve_timeout, "
211  "path, author, version, email, types FROM weathersourcesettings "
212  "WHERE hostname = :HOST AND source_name = :NAME;";
213  db.prepare(query);
214  db.bindValue(":HOST", gCoreContext->GetHostName());
215  db.bindValue(":NAME", info.name);
216 
217  if (!db.exec())
218  {
219  LOG(VB_GENERAL, LOG_ERR, "Invalid response from database");
220  return nullptr;
221  }
222 
223  // the script exists in the db
224  if (db.next())
225  {
226  info.id = db.value(0).toInt();
227  info.updateTimeout = db.value(2).toUInt() * 1000;
228  info.scriptTimeout = db.value(3).toUInt();
229 
230  // compare versions, if equal... be happy
231  QString dbver = db.value(6).toString();
232  if (dbver == info.version)
233  {
234  info.types = db.value(8).toString().split(",");
235  }
236  else
237  {
238  // versions differ, change db to match script output
239  LOG(VB_GENERAL, LOG_INFO, "New version of " + info.name + " found");
240  query = "UPDATE weathersourcesettings SET source_name = :NAME, "
241  "path = :PATH, author = :AUTHOR, version = :VERSION, "
242  "email = :EMAIL, types = :TYPES WHERE sourceid = :ID";
243  db.prepare(query);
244  // these info values were populated when getting the version number
245  // we leave the timeout values in
246  db.bindValue(":NAME", info.name);
247  db.bindValue(":PATH", info.program);
248  db.bindValue(":AUTHOR", info.author);
249  db.bindValue(":VERSION", info.version);
250 
251  // run the script to get supported data types
252  info.types = WeatherSource::ProbeTypes(info.path, info.program);
253 
254  db.bindValue(":TYPES", info.types.join(","));
255  db.bindValue(":ID", info.id);
256  db.bindValue(":EMAIL", info.email);
257  if (!db.exec())
258  {
259  MythDB::DBError("Updating weather source settings.", db);
260  return nullptr;
261  }
262  }
263  }
264  else
265  {
266  // Script is not in db, probe it and insert it into db
267  query = "INSERT INTO weathersourcesettings "
268  "(hostname, source_name, update_timeout, retrieve_timeout, "
269  "path, author, version, email, types) "
270  "VALUES (:HOST, :NAME, :UPDATETO, :RETTO, :PATH, :AUTHOR, "
271  ":VERSION, :EMAIL, :TYPES);";
272 
274  info.program,
275  info.updateTimeout,
276  info.scriptTimeout))
277  {
278  return nullptr;
279  }
280  db.prepare(query);
281  db.bindValue(":NAME", info.name);
282  db.bindValue(":HOST", gCoreContext->GetHostName());
283  db.bindValue(":UPDATETO", QString::number(info.updateTimeout/1000));
284  db.bindValue(":RETTO", QString::number(info.scriptTimeout));
285  db.bindValue(":PATH", info.program);
286  db.bindValue(":AUTHOR", info.author);
287  db.bindValue(":VERSION", info.version);
288  db.bindValue(":EMAIL", info.email);
289  info.types = ProbeTypes(info.path, info.program);
290  db.bindValue(":TYPES", info.types.join(","));
291  if (!db.exec())
292  {
293  MythDB::DBError("Inserting weather source", db);
294  return nullptr;
295  }
296  query = "SELECT sourceid FROM weathersourcesettings "
297  "WHERE source_name = :NAME AND hostname = :HOST;";
298  // a little annoying, but look at what we just inserted to get the id
299  // number, not sure if we really need it, but better safe than sorry.
300  db.prepare(query);
301  db.bindValue(":HOST", gCoreContext->GetHostName());
302  db.bindValue(":NAME", info.name);
303  if (!db.exec())
304  {
305  MythDB::DBError("Getting weather sourceid", db);
306  return nullptr;
307  }
308  if (!db.next())
309  {
310  LOG(VB_GENERAL, LOG_ERR, "Error getting weather sourceid");
311  return nullptr;
312  }
313  info.id = db.value(0).toInt();
314  }
315 
316  return new ScriptInfo(info);
317 }
318 
326  : m_ready(info != nullptr),
327  m_inuse(info != nullptr),
328  m_info(info),
329  m_updateTimer(new QTimer(this))
330 {
331  QDir dir(GetConfDir());
332  if (!dir.exists("MythWeather"))
333  dir.mkdir("MythWeather");
334  dir.cd("MythWeather");
335  if (info != nullptr) {
336  if (!dir.exists(info->name))
337  dir.mkdir(info->name);
338  dir.cd(info->name);
339  }
340  m_dir = dir.absolutePath();
341 
342  connect( m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTimeout()));
343 }
344 
346 {
347  if (m_ms)
348  {
350  m_ms->Wait(5);
351  delete m_ms;
352  }
353  delete m_updateTimer;
354 }
355 
357 {
358  connect(this, SIGNAL(newData(QString, units_t, DataMap)),
359  ws, SLOT(newData(QString, units_t, DataMap)));
360  ++m_connectCnt;
361 
362  if (!m_data.empty())
363  {
364  emit newData(m_locale, m_units, m_data);
365  }
366 }
367 
369 {
370  disconnect(this, nullptr, ws, nullptr);
371  --m_connectCnt;
372 }
373 
374 QStringList WeatherSource::getLocationList(const QString &str)
375 {
376  QString program = m_info->program;
377  QStringList args;
378  args << "-l";
379  args << str;
380 
381  const QString loc = QString("WeatherSource::getLocationList(%1 %2): ")
382  .arg(program).arg(args.join(" "));
383 
384  uint flags = kMSRunShell | kMSStdOut |
386  MythSystemLegacy ms(program, args, flags);
387  ms.SetDirectory(m_info->path);
388  ms.Run();
389 
390  if (ms.Wait() != GENERIC_EXIT_OK)
391  {
392  LOG(VB_GENERAL, LOG_ERR, loc + "Cannot run script");
393  return QStringList();
394  }
395 
396  QStringList locs;
397  QByteArray result = ms.ReadAll();
398  QTextStream text(result);
399 
400  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
401  while (!text.atEnd())
402  {
403  QString tmp = text.readLine();
404 
405  while (tmp.endsWith('\n') || tmp.endsWith('\r'))
406  tmp.chop(1);
407 
408  if (!tmp.isEmpty())
409  {
410  QString loc_string = codec->toUnicode(tmp.toUtf8());
411  locs << loc_string;
412  }
413  }
414 
415  return locs;
416 }
417 
418 void WeatherSource::startUpdate(bool forceUpdate)
419 {
420  m_buffer.clear();
421 
423  LOG(VB_GENERAL, LOG_INFO, "Starting update of " + m_info->name);
424 
425  if (m_ms)
426  {
427  LOG(VB_GENERAL, LOG_ERR, QString("%1 process exists, skipping.")
428  .arg(m_info->name));
429  return;
430  }
431 
432  if (!forceUpdate)
433  {
434  db.prepare("SELECT updated FROM weathersourcesettings "
435  "WHERE sourceid = :ID AND "
436  "TIMESTAMPADD(SECOND,update_timeout-15,updated) > NOW()");
437  db.bindValue(":ID", getId());
438  if (db.exec() && db.size() > 0)
439  {
440  LOG(VB_GENERAL, LOG_NOTICE, QString("%1 recently updated, skipping.")
441  .arg(m_info->name));
442 
443  if (m_cachefile.isEmpty())
444  {
445  QString locale_file(m_locale);
446  locale_file.replace("/", "-");
447  m_cachefile = QString("%1/cache_%2").arg(m_dir).arg(locale_file);
448  }
449  QFile cache(m_cachefile);
450  if (cache.exists() && cache.open( QIODevice::ReadOnly ))
451  {
452  m_buffer = cache.readAll();
453  cache.close();
454 
455  processData();
456 
457  if (m_connectCnt)
458  {
459  emit newData(m_locale, m_units, m_data);
460  }
461  return;
462  }
463  LOG(VB_GENERAL, LOG_NOTICE,
464  QString("No cachefile for %1, forcing update.")
465  .arg(m_info->name));
466  }
467  }
468 
469  m_data.clear();
470  QString program = "nice";
471  QStringList args;
472  args << m_info->program;
473  args << "-u";
474  args << (m_units == SI_UNITS ? "SI" : "ENG");
475 
476  if (!m_dir.isEmpty())
477  {
478  args << "-d";
479  args << m_dir;
480  }
481  args << m_locale;
482 
485  m_ms = new MythSystemLegacy(program, args, flags);
487 
488  connect(m_ms, SIGNAL(finished()), this, SLOT(processExit()));
489  connect(m_ms, SIGNAL(error(uint)), this, SLOT(processExit(uint)));
490 
492 }
493 
495 {
496  startUpdate();
498 }
499 
501 {
502  m_ms->disconnect(); // disconnects all signals
503 
504  if (status == GENERIC_EXIT_OK)
505  {
506  m_buffer = m_ms->ReadAll();
507  }
508 
509  delete m_ms;
510  m_ms = nullptr;
511 
512  if (status != GENERIC_EXIT_OK)
513  {
514  LOG(VB_GENERAL, LOG_ERR, QString("script exit status %1").arg(status));
515  return;
516  }
517 
518  if (m_buffer.isEmpty())
519  {
520  LOG(VB_GENERAL, LOG_ERR, "Script returned no data");
521  return;
522  }
523 
524  if (m_cachefile.isEmpty())
525  {
526  QString locale_file(m_locale);
527  locale_file.replace("/", "-");
528  m_cachefile = QString("%1/cache_%2").arg(m_dir).arg(locale_file);
529  }
530  QFile cache(m_cachefile);
531  if (cache.open( QIODevice::WriteOnly ))
532  {
533  cache.write(m_buffer);
534  cache.close();
535  }
536  else
537  {
538  LOG(VB_GENERAL, LOG_ERR, QString("Unable to save data to cachefile: %1")
539  .arg(m_cachefile));
540  }
541 
542  processData();
543 
545 
546  db.prepare("UPDATE weathersourcesettings "
547  "SET updated = NOW() WHERE sourceid = :ID;");
548 
549  db.bindValue(":ID", getId());
550  if (!db.exec())
551  {
552  MythDB::DBError("Updating weather source's last update time", db);
553  return;
554  }
555 
556  if (m_connectCnt)
557  {
558  emit newData(m_locale, m_units, m_data);
559  }
560 }
561 
563 {
564  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
565  QString unicode_buffer = codec->toUnicode(m_buffer);
566  QStringList data = unicode_buffer.split('\n', QString::SkipEmptyParts);
567 
568  m_data.clear();
569 
570  for (int i = 0; i < data.size(); ++i)
571  {
572  QStringList temp = data[i].split("::", QString::SkipEmptyParts);
573  if (temp.size() > 2)
574  LOG(VB_GENERAL, LOG_ERR, "Error parsing script file, ignoring");
575  if (temp.size() < 2)
576  {
577  LOG(VB_GENERAL, LOG_ERR,
578  QString("Unrecoverable error parsing script output %1")
579  .arg(temp.size()));
580  LOG(VB_GENERAL, LOG_ERR, QString("data[%1]: '%2'")
581  .arg(i).arg(data[i]));
582  return; // we don't emit signal
583  }
584 
585  if (temp[1] != "---")
586  {
587  if (!m_data[temp[0]].isEmpty())
588  {
589  m_data[temp[0]].append("\n" + temp[1]);
590  }
591  else
592  m_data[temp[0]] = temp[1];
593  }
594  }
595 }
596 
#define DEFAULT_SCRIPT_TIMEOUT
Definition: weatherUtils.h:20
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
void Run(time_t timeout=0)
Runs a command inside the /bin/sh shell. Returns immediately.
void connectScreen(WeatherScreen *ws)
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
static QStringList ProbeTypes(const QString &workingDirectory, const QString &program)
static ScriptInfo * ProbeScript(const QFileInfo &fi)
static void error(const char *str,...)
Definition: vbi.cpp:42
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
QString name
Definition: weatherSource.h:21
QString m_locale
Definition: weatherSource.h:95
MythSystemLegacy * m_ms
Definition: weatherSource.h:93
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
void disconnectScreen(WeatherScreen *ws)
int size(void) const
Definition: mythdbcon.h:203
units_t m_units
Definition: weatherSource.h:98
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QStringList types
Definition: weatherSource.h:25
static guint32 * tmp
Definition: goom_core.cpp:30
QString path
Definition: weatherSource.h:27
QTimer * m_updateTimer
Definition: weatherSource.h:99
QString version
Definition: weatherSource.h:22
static bool ProbeInfo(ScriptInfo &scriptInfo)
QString GetConfDir(void)
Definition: mythdirs.cpp:224
QVariant value(int i) const
Definition: mythdbcon.h:198
static bool ProbeTimeouts(const QString &workingDirectory, const QString &program, uint &updateTimeout, uint &scriptTimeout)
void newData(QString, units_t, DataMap)
#define DEFAULT_UPDATE_TIMEOUT
Definition: weatherUtils.h:19
MSqlQuery query(MSqlQuery::InitCon())
void processExit(uint status=0)
QByteArray & ReadAll()
QStringList getLocationList(const QString &str)
~WeatherSource() override
unsigned int uint
Definition: compat.h:140
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
ScriptInfo * m_info
Definition: weatherSource.h:92
QByteArray m_buffer
Definition: weatherSource.h:97
void Signal(MythSignal sig)
uint Wait(time_t timeout=0)
run process through shell
Definition: mythsystem.h:41
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
avoid disabling UI drawing
Definition: mythsystem.h:35
unsigned char units_t
Definition: weatherUtils.h:24
QMap< QString, QString > DataMap
Definition: weatherUtils.h:25
void startUpdate(bool forceUpdate=false)
run child in the background
Definition: mythsystem.h:36
QString author
Definition: weatherSource.h:23
WeatherSource(ScriptInfo *info)
Watch out, we store the parameter as a member variable, don't go deleting it, that wouldn't be good.
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
Weather screen.
Definition: weatherScreen.h:26
void startUpdateTimer()
Definition: weatherSource.h:69
#define SI_UNITS
Definition: weatherUtils.h:17
QString GetHostName(void)
void SetDirectory(const QString &directory)
QString program
Definition: weatherSource.h:26
unsigned int scriptTimeout
Definition: weatherSource.h:28
avoid blocking LIRC & Joystick Menu
Definition: mythsystem.h:34
QString m_cachefile
Definition: weatherSource.h:96
QString email
Definition: weatherSource.h:24
unsigned int updateTimeout
Definition: weatherSource.h:29
allow access to stdout
Definition: mythsystem.h:39
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23