5 #include <QApplication>
9 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
12 #include <QStringConverter>
29 const QString& program)
31 QStringList arguments(
"-t");
32 const QString loc = QString(
"WeatherSource::ProbeTypes(%1 %2): ")
33 .arg(program, arguments.join(
" "));
43 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
47 QByteArray result = ms.
ReadAll();
48 QTextStream text(result);
52 QString
tmp = text.readLine();
54 while (
tmp.endsWith(
'\n') ||
tmp.endsWith(
'\r'))
62 LOG(VB_GENERAL, LOG_ERR, loc +
"Invalid output from -t option");
68 const QString& program,
69 std::chrono::seconds &updateTimeout,
70 std::chrono::seconds &scriptTimeout)
72 QStringList arguments(
"-T");
73 const QString loc = QString(
"WeatherSource::ProbeTimeouts(%1 %2): ")
74 .arg(program, arguments.join(
" "));
86 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
90 QByteArray result = ms.
ReadAll();
91 QTextStream text(result);
96 QString
tmp = text.readLine();
98 while (
tmp.endsWith(
'\n') ||
tmp.endsWith(
'\r'))
107 LOG(VB_GENERAL, LOG_ERR, loc +
"Invalid Script Output! No Lines");
111 QStringList temp = lines[0].split(
',');
112 if (temp.size() != 2)
114 LOG(VB_GENERAL, LOG_ERR, loc +
115 QString(
"Invalid Script Output! '%1'").arg(lines[0]));
119 std::array<bool,2> isOK {};
120 uint ut = temp[0].toUInt(isOK.data());
121 uint st = temp[1].toUInt(&isOK[1]);
122 if (!isOK[0] || !isOK[1])
124 LOG(VB_GENERAL, LOG_ERR, loc +
125 QString(
"Invalid Script Output! '%1'").arg(lines[0]));
130 scriptTimeout = std::chrono::seconds(st);
137 QStringList arguments(
"-v");
139 const QString loc = QString(
"WeatherSource::ProbeInfo(%1 %2): ")
140 .arg(
info.program, arguments.join(
" "));
149 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
153 QByteArray result = ms.
ReadAll();
154 QTextStream text(result);
157 while (!text.atEnd())
159 QString
tmp = text.readLine();
161 while (
tmp.endsWith(
'\n') ||
tmp.endsWith(
'\r'))
170 LOG(VB_GENERAL, LOG_ERR, loc +
"Invalid Script Output! No Lines");
174 QStringList temp = lines[0].split(
',');
175 if (temp.size() != 4)
177 LOG(VB_GENERAL, LOG_ERR, loc +
178 QString(
"Invalid Script Output! '%1'").arg(lines[0]));
183 info.version = temp[1];
184 info.author = temp[2];
185 info.email = temp[3];
202 if (!fi.isReadable() || !fi.isExecutable())
206 info.path = fi.absolutePath();
207 info.program = fi.absoluteFilePath();
214 "SELECT sourceid, source_name, update_timeout, retrieve_timeout, "
215 "path, author, version, email, types FROM weathersourcesettings "
216 "WHERE hostname = :HOST AND source_name = :NAME;";
223 LOG(VB_GENERAL, LOG_ERR,
"Invalid response from database");
231 info.updateTimeout = std::chrono::seconds(db.
value(2).toUInt());
232 info.scriptTimeout = std::chrono::seconds(db.
value(3).toUInt());
235 QString dbver = db.
value(6).toString();
236 if (dbver ==
info.version)
238 info.types = db.
value(8).toString().split(
",");
243 LOG(VB_GENERAL, LOG_INFO,
"New version of " +
info.name +
" found");
244 query =
"UPDATE weathersourcesettings SET source_name = :NAME, "
245 "path = :PATH, author = :AUTHOR, version = :VERSION, "
246 "email = :EMAIL, types = :TYPES WHERE sourceid = :ID";
271 query =
"INSERT INTO weathersourcesettings "
272 "(hostname, source_name, update_timeout, retrieve_timeout, "
273 "path, author, version, email, types) "
274 "VALUES (:HOST, :NAME, :UPDATETO, :RETTO, :PATH, :AUTHOR, "
275 ":VERSION, :EMAIL, :TYPES);";
287 db.
bindValue(
":UPDATETO", QString::number(
info.updateTimeout.count()));
288 db.
bindValue(
":RETTO", QString::number(
info.scriptTimeout.count()));
300 query =
"SELECT sourceid FROM weathersourcesettings "
301 "WHERE source_name = :NAME AND hostname = :HOST;";
314 LOG(VB_GENERAL, LOG_ERR,
"Error getting weather sourceid");
330 : m_ready(
info != nullptr),
331 m_inuse(
info != nullptr),
333 m_updateTimer(new QTimer(this))
336 if (!dir.exists(
"MythWeather"))
337 dir.mkdir(
"MythWeather");
338 dir.cd(
"MythWeather");
339 if (
info !=
nullptr) {
340 if (!dir.exists(
info->name))
341 dir.mkdir(
info->name);
344 m_dir = dir.absolutePath();
374 disconnect(
this,
nullptr, ws,
nullptr);
385 const QString loc = QString(
"WeatherSource::getLocationList(%1 %2): ")
386 .arg(program,
args.join(
" "));
396 LOG(VB_GENERAL, LOG_ERR, loc +
"Cannot run script");
401 QByteArray result = ms.
ReadAll();
402 QTextStream text(result);
403 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
404 text.setCodec(
"UTF-8");
406 text.setEncoding(QStringConverter::Utf8);
408 while (!text.atEnd())
410 QString
tmp = text.readLine().trimmed();
423 LOG(VB_GENERAL, LOG_INFO,
"Starting update of " +
m_info->
name);
427 LOG(VB_GENERAL, LOG_ERR, QString(
"%1 process exists, skipping.")
434 db.
prepare(
"SELECT updated FROM weathersourcesettings "
435 "WHERE sourceid = :ID AND "
436 "TIMESTAMPADD(SECOND,update_timeout-15,updated) > NOW()");
440 LOG(VB_GENERAL, LOG_NOTICE, QString(
"%1 recently updated, skipping.")
446 locale_file.replace(
"/",
"-");
450 if (cache.exists() && cache.open( QIODevice::ReadOnly ))
463 LOG(VB_GENERAL, LOG_NOTICE,
464 QString(
"No cachefile for %1, forcing update.")
470 QString program =
"nice";
476 if (!
m_dir.isEmpty())
516 LOG(VB_GENERAL, LOG_ERR, QString(
"script exit status %1").arg(status));
522 LOG(VB_GENERAL, LOG_ERR,
"Script returned no data");
529 locale_file.replace(
"/",
"-");
533 if (cache.open( QIODevice::WriteOnly ))
540 LOG(VB_GENERAL, LOG_ERR, QString(
"Unable to save data to cachefile: %1")
548 db.
prepare(
"UPDATE weathersourcesettings "
549 "SET updated = NOW() WHERE sourceid = :ID;");
571 QString unicode_buffer = QString::fromUtf8(
m_buffer);
572 QStringList data = unicode_buffer.split(
'\n', Qt::SkipEmptyParts);
576 for (
int i = 0; i < data.size(); ++i)
578 QStringList temp = data[i].split(
"::", Qt::SkipEmptyParts);
580 LOG(VB_GENERAL, LOG_ERR,
"Error parsing script file, ignoring");
583 LOG(VB_GENERAL, LOG_ERR,
584 QString(
"Unrecoverable error parsing script output %1")
586 LOG(VB_GENERAL, LOG_ERR, QString(
"data[%1]: '%2'")
587 .arg(i).arg(data[i]));
591 if (temp[1] !=
"---")
593 if (!
m_data[temp[0]].isEmpty())
595 m_data[temp[0]].append(
"\n" + temp[1]);
599 m_data[temp[0]] = temp[1];