5 #include <QApplication>
9 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
12 #include <QStringConverter>
28 const QString& program)
30 QStringList arguments(
"-t");
31 const QString loc = QString(
"WeatherSource::ProbeTypes(%1 %2): ")
32 .arg(program, arguments.join(
" "));
42 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
46 QByteArray result = ms.
ReadAll();
47 QTextStream text(result);
51 QString
tmp = text.readLine();
53 while (
tmp.endsWith(
'\n') ||
tmp.endsWith(
'\r'))
61 LOG(VB_GENERAL, LOG_ERR, loc +
"Invalid output from -t option");
67 const QString& program,
68 std::chrono::seconds &updateTimeout,
69 std::chrono::seconds &scriptTimeout)
71 QStringList arguments(
"-T");
72 const QString loc = QString(
"WeatherSource::ProbeTimeouts(%1 %2): ")
73 .arg(program, arguments.join(
" "));
85 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
89 QByteArray result = ms.
ReadAll();
90 QTextStream text(result);
95 QString
tmp = text.readLine();
97 while (
tmp.endsWith(
'\n') ||
tmp.endsWith(
'\r'))
106 LOG(VB_GENERAL, LOG_ERR, loc +
"Invalid Script Output! No Lines");
110 QStringList temp = lines[0].split(
',');
111 if (temp.size() != 2)
113 LOG(VB_GENERAL, LOG_ERR, loc +
114 QString(
"Invalid Script Output! '%1'").arg(lines[0]));
118 std::array<bool,2> isOK {};
119 uint ut = temp[0].toUInt(isOK.data());
120 uint st = temp[1].toUInt(&isOK[1]);
121 if (!isOK[0] || !isOK[1])
123 LOG(VB_GENERAL, LOG_ERR, loc +
124 QString(
"Invalid Script Output! '%1'").arg(lines[0]));
129 scriptTimeout = std::chrono::seconds(st);
136 QStringList arguments(
"-v");
138 const QString loc = QString(
"WeatherSource::ProbeInfo(%1 %2): ")
139 .arg(
info.program, arguments.join(
" "));
148 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
152 QByteArray result = ms.
ReadAll();
153 QTextStream text(result);
156 while (!text.atEnd())
158 QString
tmp = text.readLine();
160 while (
tmp.endsWith(
'\n') ||
tmp.endsWith(
'\r'))
169 LOG(VB_GENERAL, LOG_ERR, loc +
"Invalid Script Output! No Lines");
173 QStringList temp = lines[0].split(
',');
174 if (temp.size() != 4)
176 LOG(VB_GENERAL, LOG_ERR, loc +
177 QString(
"Invalid Script Output! '%1'").arg(lines[0]));
182 info.version = temp[1];
183 info.author = temp[2];
184 info.email = temp[3];
201 if (!fi.isReadable() || !fi.isExecutable())
205 info.path = fi.absolutePath();
206 info.program = fi.absoluteFilePath();
213 "SELECT sourceid, source_name, update_timeout, retrieve_timeout, "
214 "path, author, version, email, types FROM weathersourcesettings "
215 "WHERE hostname = :HOST AND source_name = :NAME;";
222 LOG(VB_GENERAL, LOG_ERR,
"Invalid response from database");
230 info.updateTimeout = std::chrono::seconds(db.
value(2).toUInt());
231 info.scriptTimeout = std::chrono::seconds(db.
value(3).toUInt());
234 QString dbver = db.
value(6).toString();
235 if (dbver ==
info.version)
237 info.types = db.
value(8).toString().split(
",");
242 LOG(VB_GENERAL, LOG_INFO,
"New version of " +
info.name +
" found");
243 query =
"UPDATE weathersourcesettings SET source_name = :NAME, "
244 "path = :PATH, author = :AUTHOR, version = :VERSION, "
245 "email = :EMAIL, types = :TYPES WHERE sourceid = :ID";
270 query =
"INSERT INTO weathersourcesettings "
271 "(hostname, source_name, update_timeout, retrieve_timeout, "
272 "path, author, version, email, types) "
273 "VALUES (:HOST, :NAME, :UPDATETO, :RETTO, :PATH, :AUTHOR, "
274 ":VERSION, :EMAIL, :TYPES);";
286 db.
bindValue(
":UPDATETO", QString::number(
info.updateTimeout.count()));
287 db.
bindValue(
":RETTO", QString::number(
info.scriptTimeout.count()));
299 query =
"SELECT sourceid FROM weathersourcesettings "
300 "WHERE source_name = :NAME AND hostname = :HOST;";
313 LOG(VB_GENERAL, LOG_ERR,
"Error getting weather sourceid");
329 : m_ready(
info != nullptr),
330 m_inuse(
info != nullptr),
332 m_updateTimer(new QTimer(this))
335 if (!dir.exists(
"MythWeather"))
336 dir.mkdir(
"MythWeather");
337 dir.cd(
"MythWeather");
338 if (
info !=
nullptr) {
339 if (!dir.exists(
info->name))
340 dir.mkdir(
info->name);
343 m_dir = dir.absolutePath();
373 disconnect(
this,
nullptr, ws,
nullptr);
384 const QString loc = QString(
"WeatherSource::getLocationList(%1 %2): ")
385 .arg(program,
args.join(
" "));
395 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
400 QByteArray result = ms.
ReadAll();
401 QTextStream text(result);
402 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
403 text.setCodec(
"UTF-8");
405 text.setEncoding(QStringConverter::Utf8);
407 while (!text.atEnd())
409 QString
tmp = text.readLine().trimmed();
422 LOG(VB_GENERAL, LOG_INFO,
"Starting update of " +
m_info->
name);
426 LOG(VB_GENERAL, LOG_ERR, QString(
"%1 process exists, skipping.")
433 db.
prepare(
"SELECT updated FROM weathersourcesettings "
434 "WHERE sourceid = :ID AND "
435 "TIMESTAMPADD(SECOND,update_timeout-15,updated) > NOW()");
439 LOG(VB_GENERAL, LOG_NOTICE, QString(
"%1 recently updated, skipping.")
445 locale_file.replace(
"/",
"-");
449 if (cache.exists() && cache.open( QIODevice::ReadOnly ))
462 LOG(VB_GENERAL, LOG_NOTICE,
463 QString(
"No cachefile for %1, forcing update.")
469 QString program =
"nice";
475 if (!
m_dir.isEmpty())
515 LOG(VB_GENERAL, LOG_ERR, QString(
"script exit status %1").arg(status));
521 LOG(VB_GENERAL, LOG_ERR,
"Script returned no data");
528 locale_file.replace(
"/",
"-");
532 if (cache.open( QIODevice::WriteOnly ))
539 LOG(VB_GENERAL, LOG_ERR, QString(
"Unable to save data to cachefile: %1")
547 db.
prepare(
"UPDATE weathersourcesettings "
548 "SET updated = NOW() WHERE sourceid = :ID;");
570 QString unicode_buffer = QString::fromUtf8(
m_buffer);
571 QStringList data = unicode_buffer.split(
'\n', Qt::SkipEmptyParts);
575 for (
int i = 0; i < data.size(); ++i)
577 QStringList temp = data[i].split(
"::", Qt::SkipEmptyParts);
579 LOG(VB_GENERAL, LOG_ERR,
"Error parsing script file, ignoring");
582 LOG(VB_GENERAL, LOG_ERR,
583 QString(
"Unrecoverable error parsing script output %1")
585 LOG(VB_GENERAL, LOG_ERR, QString(
"data[%1]: '%2'")
586 .arg(i).arg(data[i]));
590 if (temp[1] !=
"---")
592 if (!
m_data[temp[0]].isEmpty())
594 m_data[temp[0]].append(
"\n" + temp[1]);
598 m_data[temp[0]] = temp[1];