/
weather.cpp
155 lines (119 loc) · 4.65 KB
/
weather.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "weather.h"
const QString Weather::_weatherSailfish = "/home/nemo/.local/share/sailfish-weather/weather.json";
const QString Weather::_weatherMeecast = "/home/nemo/.cache/harbour-meecast/current.xml";
Weather::Weather(QObject *parent) :
QObject(parent), watching(false)
{
_timestamp = 0;
_weatherSailfishNotifier = 0;
_weatherMeecastNotifier = 0;
_weatherSailfishFile.setFileName(_weatherSailfish);
_weatherMeecastFile.setFileName(_weatherMeecast);
}
Weather::~Weather()
{
stopWatching();
}
bool Weather::startWatching()
{
if (watching)
return true;
if (_weatherSailfishFile.exists()) {
_weatherSailfishNotifier = new QFileSystemWatcher(this);
_weatherSailfishNotifier->addPath(_weatherSailfish);
connect(_weatherSailfishNotifier, SIGNAL(fileChanged(QString)), this, SLOT(processSailfish(QString)));
processSailfish(_weatherSailfish);
printf("Started watching Sailfish weather changes.\n");
watching = true;
}
if (_weatherMeecastFile.exists()) {
_weatherMeecastNotifier = new QFileSystemWatcher(this);
_weatherMeecastNotifier->addPath(_weatherMeecast);
connect(_weatherMeecastNotifier, SIGNAL(fileChanged(QString)), this, SLOT(processMeecast(QString)));
processMeecast(_weatherMeecast);
printf("Started watching Meecast weather changes.\n");
watching = true;
}
return watching;
}
void Weather::stopWatching()
{
if (!watching)
return;
watching = false;
if (_weatherSailfishNotifier) {
_weatherSailfishNotifier->disconnect(SIGNAL(fileChanged(QString)));
delete _weatherSailfishNotifier;
_weatherSailfishNotifier = 0;
}
if (_weatherMeecastNotifier) {
_weatherMeecastNotifier->disconnect(SIGNAL(fileChanged(QString)));
delete _weatherMeecastNotifier;
_weatherMeecastNotifier = 0;
}
printf("Stopped watching weather changes.\n");
}
void Weather::triggerUpdate()
{
if (watching) {
processSailfish(_weatherSailfish);
processMeecast(_weatherMeecast);
}
}
void Weather::processSailfish(const QString &)
{
if (!_weatherSailfishFile.exists() || !_weatherSailfishFile.open(QFile::ReadOnly | QFile::Text))
{
return;
}
QTextStream in(&_weatherSailfishFile);
QString weatherString = in.readAll();
if (weatherString.isEmpty())
{
_weatherSailfishFile.close();
return;
}
QJsonDocument weatherJson = QJsonDocument::fromJson(weatherString.toUtf8());
QJsonObject weatherObject = weatherJson.object();
QJsonObject currentLocation = weatherObject["currentLocation"].toObject();
QJsonObject currentWeather = currentLocation["weather"].toObject();
QDateTime timestamp = QDateTime::fromString(currentWeather["timestamp"].toString(), Qt::ISODate);
timestamp.setTimeSpec(Qt::UTC);
if (timestamp.toTime_t() > _timestamp) {
_timestamp = timestamp.toTime_t();
printf("Temperature in %s is %d degrees (updated %s)\n", qPrintable(currentLocation["city"].toString()),
currentWeather["temperature"].toInt(),
qPrintable(timestamp.toLocalTime().toString(Qt::SystemLocaleLongDate)));
emit weatherUpdated(QString("%1d").arg(currentWeather["temperature"].toInt()));
}
_weatherSailfishFile.close();
}
void Weather::processMeecast(const QString &)
{
if (!_weatherMeecastFile.exists() || !_weatherMeecastFile.open(QFile::ReadOnly | QFile::Text))
{
return;
}
QXmlQuery query;
query.setFocus(&_weatherMeecastFile);
QString timestamp;
query.setQuery("/meecast/station/period[@itemnumber=\"1\"]/@start/string()");
query.evaluateTo(×tamp);
timestamp = timestamp.trimmed();
if (timestamp.toUInt() > _timestamp) {
_timestamp = timestamp.toUInt();
QString temperature;
query.setQuery("/meecast/station/period[@itemnumber=\"1\"]/temperature/string()");
query.evaluateTo(&temperature);
temperature = temperature.trimmed();
QString station;
query.setQuery("/meecast/station/@name/string()");
query.evaluateTo(&station);
station = station.trimmed();
printf("Temperature in %s is %d degrees (updated %s)\n", qPrintable(station),
temperature.toInt(),
qPrintable(QDateTime::fromTime_t(_timestamp).toString(Qt::SystemLocaleLongDate)));
emit weatherUpdated(QString("%1d").arg(temperature.toInt()));
}
_weatherMeecastFile.close();
}