17 if(_unitSystem ==
"metric"){
20 else if(_unitSystem ==
"imperial"){
30 QUrl url = QString(
"https://" +
_api +
"/data/2.5/weather"
32 +
"&units=" + _unitSystem
33 +
"&lang=" + _language
35 qDebug() <<
"[URL] " << url;
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
48 qDebug() <<
"[URL] " << url;
53 QJsonObject jroot = QJsonDocument::fromJson(
get(url)).object();
60 int cod = jroot[
"cod"].toInt();
67 QJsonObject jcoord = jroot[
"coord"].toObject();
68 data.coord.lat = jcoord[
"lat"].toDouble();
69 data.coord.lon = jcoord[
"lon"].toDouble();
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();
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();
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();
91 QJsonObject jclouds = jroot[
"clouds"].toObject();
92 data.clouds.all = jclouds[
"all"].toInt();
94 QJsonObject jrain = jroot[
"rain"].toObject();
95 data.rain.oneHour = jrain[
"1h"].toDouble();
96 data.rain.threeHours = jrain[
"3h"].toDouble();
98 QJsonObject jsnow = jroot[
"snow"].toObject();
99 data.snow.oneHour = jrain[
"1h"].toDouble();
100 data.snow.threeHours = jrain[
"3h"].toDouble();
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();
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();
123 QNetworkRequest request(url);
126 QSslConfiguration config = QSslConfiguration::defaultConfiguration();
127 config.setProtocol(QSsl::TlsV1_2);
128 request.setSslConfiguration(config);
132 while (!reply->isFinished()){
133 qApp->processEvents();
137 QByteArray read = reply->readAll();
139 reply->deleteLater();
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();
int sendAndDecode(QUrl url)
send a request from URL and decode JSON response
QString getTempUnit()
Return the current unit selected.
QByteArray get(QUrl url)
https GET from URL
void changeUnit(QString unit)
Change the current unit system.
int getFromGeo(double lat, double lon)
Get data from coordinates.
void changeLanguage(QString lang)
Change language of the API response.
QNetworkAccessManager _networkManager
Network Manager.
QString geo2city(double lat, double lon)
Weather()
Default Constructor.
int getFromCity(QString city)
Get data from a City Name.
void city2geo(QString city)
struct Weather::@0 data
Struct to match the JSON format.