-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenWeatherAPI.cpp
150 lines (104 loc) · 4.11 KB
/
OpenWeatherAPI.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
/*
weatherfxlite
A lite replacement for the venerable Bushnell WeatherFX station. RIP.
Copyright (C) 2022 iAchieved.it LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "OpenWeatherAPI.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
#define OPENWEATHERMAP_CURRENT_COND "https://api.openweathermap.org/data/2.5/weather"
#define OPENWEATHERMAP_FORECAST "https://api.openweathermap.org/data/2.5/forecast"
#include "config.h"
OpenWeatherAPI::OpenWeatherAPI() {
}
OpenWeatherAPI::~OpenWeatherAPI() {
}
void OpenWeatherAPI::updateCurrentConditions(void) {
QString url = OPENWEATHERMAP_CURRENT_COND;
url += "?appId=";
url += OPENWEATHERMAP_APPID;
url += OPENWEATHERMAP_LATLNG;
url += "&units=imperial";
QUrl current(url);
fDownloader = new FileDownloader(current, this);
connect(fDownloader, SIGNAL(downloaded()), this, SLOT(parseCurrentConditions()));
}
void OpenWeatherAPI::updateCurrentForecast(void) {
QString url(OPENWEATHERMAP_FORECAST);
url += "?appId=";
url += OPENWEATHERMAP_APPID;
url += OPENWEATHERMAP_LATLNG;
url += "&units=imperial";
QUrl forecast(url);
fcastDownloader = new FileDownloader(forecast, this);
connect(fcastDownloader, SIGNAL(downloaded()), this, SLOT(parseCurrentForecast()));
}
void OpenWeatherAPI::parseCurrentConditions(void) {
QString current(fDownloader->downloadedData());
//cout << current.toStdString() << endl;
QJsonDocument doc = QJsonDocument::fromJson(current.toUtf8());
QJsonObject json = doc.object();
// "main":{"temp":90.61,"feels_like":97.25,"temp_min":87.03,"temp_max":93.06,"pressure":1014,"humidity":53}
if (json.contains("main")) {
QJsonObject m = json["main"].toObject();
QJsonValue t = m["temp"];
currentConditions.temperature = floor(t.toDouble());
}
// "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}]
if (json.contains("weather")) {
QJsonArray w = json["weather"].toArray();
QJsonValue a = w[0];
QJsonValue c = a["main"];
currentConditions.condition = c.toString();
}
emit currentConditionsUpdate();
}
void OpenWeatherAPI::parseCurrentForecast(void) {
QDateTime now = QDateTime().currentDateTime();
QString forecast(fcastDownloader->downloadedData());
cout << forecast.toStdString() << endl;
QJsonDocument doc = QJsonDocument::fromJson(forecast.toUtf8());
QJsonObject json = doc.object();
if (json.contains("list")) {
QJsonArray forecasts = json["list"].toArray(); // forecasts are in 3 hour chunks
for (auto i = forecasts.begin(); i != forecasts.end(); i++) {
QJsonObject f = QJsonValue(*i).toObject();
if (f.contains("dt_txt")) {
QJsonValue dt = f["dt_text"];
QDateTime qdt = QDateTime::fromString(dt.toString(), "yyyy-MM-dd HH:mm:ss");
// Is the forecast for today?
if (qdt.date() == now.date()) {
if (f.contains("main")) {
QJsonObject m = f["main"].toObject();
double hi = floor(m["temp_max"].toDouble());
double lo = floor(m["temp_min"].toDouble());
if (lo < currentConditions.low) currentConditions.low = lo;
if (hi > currentConditions.high) currentConditions.high = hi;
}
}
}
}
}
emit currentForecastUpdate();
}
CurrentConditions OpenWeatherAPI::getCurrentConditions(void) {
return currentConditions;
}
CurrentConditions OpenWeatherAPI::getCurrentForecast(void) {
return currentConditions;
}