summaryrefslogtreecommitdiff
path: root/saltoqd/weathermanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'saltoqd/weathermanager.cpp')
-rw-r--r--saltoqd/weathermanager.cpp93
1 files changed, 84 insertions, 9 deletions
diff --git a/saltoqd/weathermanager.cpp b/saltoqd/weathermanager.cpp
index ce36f8c..82126d7 100644
--- a/saltoqd/weathermanager.cpp
+++ b/saltoqd/weathermanager.cpp
@@ -1,18 +1,93 @@
+#include <QtCore/QFile>
+#include <QtCore/QStandardPaths>
+#include <QtCore/QJsonArray>
+#include <QtGui/QPainter>
+
#include "fmsmanager.h"
+#include "msolimageiohandler.h"
#include "weathermanager.h"
+static QString getBackgroundImage(int temp)
+{
+ if (temp > 35) {
+ return "temp40.png";
+ } else if (temp > 25) {
+ return "temp30.png";
+ } else if (temp > 15) {
+ return "temp20.png";
+ } else if (temp > 5) {
+ return "temp10.png";
+ } else if (temp > -5) {
+ return "temp0.png";
+ } else if (temp > -15) {
+ return "tempminus10.png";
+ } else {
+ return "tempminus20.png";
+ }
+}
+
WeatherManager::WeatherManager(FmsManager *fms, ToqManager *toq) :
- QObject(toq), _toq(toq), _fms(fms)
+ QObject(toq), _toq(toq), _fms(fms),
+ _file(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sailfish-weather/weather.json"),
+ _watcher(new QFileSystemWatcher(QStringList(_file), this)),
+ _refreshTimer(new QTimer(this))
{
- connect(_toq, &ToqManager::connected,
- this, &WeatherManager::handleToqConnected);
+ connect(_watcher, &QFileSystemWatcher::fileChanged,
+ _refreshTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
+ connect(_refreshTimer, &QTimer::timeout,
+ this, &WeatherManager::doRefresh);
+
+ qDebug() << "Monitoring file" << _file;
+
+ _refreshTimer->setSingleShot(true);
+ _refreshTimer->setInterval(2000);
+ _refreshTimer->start();
}
-void WeatherManager::handleToqConnected()
+QImage WeatherManager::constructImage(const QJsonObject &obj)
{
-#if 0
- qDebug() << "Putting file";
- QByteArray data(img_data, sizeof(img_data));
- _fms->updateFile("/apps/weather/99.img", data);
-#endif
+ QJsonObject weather = obj["weather"].toObject();
+ QString city = obj["city"].toString();
+ int temp = weather["temperatureFeel"].toInt();
+ QImage img(RES_PATH "/card" + getBackgroundImage(temp));
+
+ if (img.isNull()) {
+ qWarning() << "Invalid background image" << QString(RES_PATH "/card" + getBackgroundImage(temp));
+ return img;
+ }
+
+ QPainter p(&img);
+ p.setPen(QColor(230, 230, 230));
+ p.setFont(QFont("Qualcomm", 22));
+ p.drawText(10, 36, city);
+
+ p.setFont(QFont("Qualcomm", 48));
+ p.drawText(14, 110, QString::fromUtf8("%1 °C").arg(temp));
+
+ return img;
+}
+
+void WeatherManager::doRefresh()
+{
+ QFile file(_file);
+
+ if (!file.open(QIODevice::ReadOnly)) {
+ qWarning() << "Failed to open" << _file;
+ return;
+ }
+
+ QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
+ QJsonObject root = doc.object();
+ QJsonObject currentLoc = root["currentLocation"].toObject();
+ QJsonArray locs = root["savedLocations"].toArray();
+
+ int index = 1;
+ QImage img = constructImage(currentLoc);
+ _fms->updateFile(QString::fromLatin1("/apps/weather/%1.img").arg(index), convertImageToMsol(img));
+
+ for (const QJsonValue &value : locs) {
+ index++;
+ img = constructImage(value.toObject());
+ _fms->updateFile(QString::fromLatin1("/apps/weather/%1.img").arg(index), convertImageToMsol(img));
+ }
}