WeatherApp v0.1
Loading...
Searching...
No Matches
weather.cpp
Go to the documentation of this file.
1
7#include "weather.h"
8
10Weather::Weather(QString unit): _unitSystem(unit){}
11Weather::Weather(QString unit, QString lang): _unitSystem(unit), _language(lang){}
12
13//https://openweathermap.org/current#data
14void Weather::changeLanguage(QString lang){ _language = lang; }
15void Weather::changeUnit(QString unit){ _unitSystem = unit; }
17 if(_unitSystem == "metric"){
18 return "°C";
19 }
20 else if(_unitSystem == "imperial"){
21 return "°F";
22 }
23 return "K";
24}
25
26
27
28int Weather::getFromCity(QString city){
29 //city2geo(city);
30 QUrl url = QString("https://" + _api + "/data/2.5/weather"
31 + "?q=" + city
32 + "&units=" + _unitSystem
33 + "&lang=" + _language
34 + "&appid=" + _token);
35 qDebug() << "[URL] " << url;
36 return sendAndDecode(url);
37 return 0;
38}
39
40//https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}
41int Weather::getFromGeo(double lat, double lon){
42 QUrl url = QString("https://" + _api + "/data/2.5/weather"
43 + "?lat=" + QString::number(lat)
44 + "&lon=" + QString::number(lon)
45 + "&units=" + _unitSystem
46 + "&lang=" + _language
47 + "&appid=" + _token);
48 qDebug() << "[URL] " << url;
49 return sendAndDecode(url);
50}
51
53 QJsonObject jroot = QJsonDocument::fromJson(get(url)).object();
54
55 if(jroot.isEmpty())
56 return 0;
57
58 //qDebug() << "[JSON]" << jroot;
59
60 int cod = jroot["cod"].toInt();
61 switch(cod){
62 case 200: break;
63 case 400: return -1;
64 default: return -1;
65 }
66
67 QJsonObject jcoord = jroot["coord"].toObject();
68 data.coord.lat = jcoord["lat"].toDouble();
69 data.coord.lon = jcoord["lon"].toDouble();
70
71 QJsonObject jweather = jroot["weather"].toArray().at(0).toObject();
72 data.weather.id = jweather["id"].toInt();
73 data.weather.description = jweather["description"].toString();
74 data.weather.icon = jweather["icon"].toString();
75
76 QJsonObject jmain = jroot["main"].toObject();
77 data.temp = jmain["temp"].toDouble();
78 data.feels_like = jmain["feels_like"].toDouble();
79 data.temp_min = jmain["temp_min"].toDouble();
80 data.temp_max = jmain["temp_max"].toDouble();
81 data.pressure = jmain["pressure"].toInt();
82 data.humidity = jmain["humidity"].toInt();
83 data.sea_level = jmain["sea_level"].toInt();
84 data.grnd_level = jmain["grnd_level"].toInt();
85
86 QJsonObject jwind = jroot["wind"].toObject();
87 data.wind.speed = jwind["speed"].toDouble();
88 data.wind.deg = jwind["deg"].toDouble();
89 data.wind.gust = jwind["gust"].toDouble();
90
91 QJsonObject jclouds = jroot["clouds"].toObject();
92 data.clouds.all = jclouds["all"].toInt();
93
94 QJsonObject jrain = jroot["rain"].toObject();
95 data.rain.oneHour = jrain["1h"].toDouble();
96 data.rain.threeHours = jrain["3h"].toDouble();
97
98 QJsonObject jsnow = jroot["snow"].toObject();
99 data.snow.oneHour = jrain["1h"].toDouble();
100 data.snow.threeHours = jrain["3h"].toDouble();
101
102 QJsonObject jsys = jroot["sys"].toObject();
103 data.sys.type = jsys["type"].toInt();
104 data.sys.id = jsys["id"].toInt();
105 data.sys.message = jsys["message"].toString();
106 data.sys.country = jsys["country"].toString();
107 data.sys.sunrise = jsys["sunrise"].toInt();
108 data.sys.sunset = jsys["sunset"].toInt();
109
110
111 data.base = jroot["base"].toString();
112 data.visibility = jroot["visibility"].toInt();
113 data.dt = jroot["dt"].toDouble();
114 data.timezone = jroot["timezone"].toInt();
115 data.id = jroot["id"].toInt();
116 data.name = jroot["name"].toString();
117
118 return 1;
119}
120
121QByteArray Weather::get(QUrl url){
122 //config url
123 QNetworkRequest request(url);
124
125 //https
126 QSslConfiguration config = QSslConfiguration::defaultConfiguration();
127 config.setProtocol(QSsl::TlsV1_2);
128 request.setSslConfiguration(config);
129
130 //get
131 QNetworkReply *reply = _networkManager.get(request);
132 while (!reply->isFinished()){
133 qApp->processEvents();
134 }
135
136 //response
137 QByteArray read = reply->readAll();
138 reply->close();
139 reply->deleteLater();
140 return read;
141}
142
143//http://api.openweathermap.org/geo/1.0/direct?q={city name},{state code},{country code}&limit={limit}&appid={API key}
144void Weather::city2geo(QString city){
145 QUrl url = QString("https://" + _api + "/geo/1.0/direct?q=" + city + "&appid=" + _token);
146 qDebug() << "[URL] " << url;
147 QJsonObject jroot = QJsonDocument::fromJson(get(url)).object();
148 qDebug() << jroot;
149}
150
151QString Weather::geo2city(double lat, double lon){
152 return "";
153}
QString _api
API URL.
Definition: weather.h:90
int sendAndDecode(QUrl url)
send a request from URL and decode JSON response
Definition: weather.cpp:52
QString getTempUnit()
Return the current unit selected.
Definition: weather.cpp:16
QByteArray get(QUrl url)
https GET from URL
Definition: weather.cpp:121
void changeUnit(QString unit)
Change the current unit system.
Definition: weather.cpp:15
int getFromGeo(double lat, double lon)
Get data from coordinates.
Definition: weather.cpp:41
void changeLanguage(QString lang)
Change language of the API response.
Definition: weather.cpp:14
QNetworkAccessManager _networkManager
Network Manager.
Definition: weather.h:88
QString geo2city(double lat, double lon)
Definition: weather.cpp:151
Weather()
Default Constructor.
Definition: weather.cpp:9
int getFromCity(QString city)
Get data from a City Name.
Definition: weather.cpp:28
QString _token
API Token.
Definition: weather.h:92
void city2geo(QString city)
Definition: weather.cpp:144
struct Weather::@0 data
Struct to match the JSON format.
Weather Header.