File: openweathermapbackend.cpp

package info (click to toggle)
qt6-positioning 6.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,068 kB
  • sloc: cpp: 35,693; java: 514; xml: 58; python: 35; makefile: 23
file content (218 lines) | stat: -rw-r--r-- 8,068 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "openweathermapbackend.h"

#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qurlquery.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtPositioning/qgeocoordinate.h>

Q_DECLARE_LOGGING_CATEGORY(requestsLog)

static constexpr auto kZeroKelvin = 273.15;

static QString niceTemperatureString(double t)
{
    return QString::number(qRound(t - kZeroKelvin)) + QChar(0xB0);
}

/*
    Converts weather code to a string that will be used to show the icon.
    The possible strings are based on the icon names. The icon name is built up
    as follows:
        weather-[mystring].png
    where [mystring] is the value returned by this method.
    Check resources for the full list of available icons.
*/
static QString weatherCodeToString(const QString &code)
{
    if (code == u"01d" || code == u"01n")
        return "sunny";
    else if (code == u"02d" || code == u"02n")
        return "sunny-very-few-clouds";
    else if (code == u"03d" || code == u"03n")
        return "few-clouds";
    else if (code == u"04d" || code == u"04n")
        return "overcast";
    else if (code == u"09d" || code == u"09n" || code == u"10d" || code == u"10n")
        return "showers";
    else if (code == u"11d" || code == u"11n")
        return "thundershower";
    else if (code == u"13d" || code == u"13n")
        return "snow";
    else if (code == u"50d" || code == u"50n")
        return "fog";

    return "sunny"; // default choice
}

static void parseWeatherDescription(const QJsonObject &object, WeatherInfo &info)
{
    const QJsonArray weatherArray = object.value(u"weather").toArray();
    if (!weatherArray.isEmpty()) {
        const QJsonObject obj = weatherArray.first().toObject();
        info.m_weatherDescription = obj.value(u"description").toString();
        info.m_weatherIconId = weatherCodeToString(obj.value(u"icon").toString());
    } else {
        qCDebug(requestsLog, "An empty weather array is returned.");
    }
}

OpenWeatherMapBackend::OpenWeatherMapBackend(QObject *parent)
    : ProviderBackend(parent),
      m_networkManager(new QNetworkAccessManager(this)),
      m_appId(QStringLiteral("36496bad1955bf3365448965a42b9eac"))
{
}

void OpenWeatherMapBackend::requestWeatherInfo(const QString &city)
{
    QUrlQuery query;
    query.addQueryItem(QStringLiteral("q"), city);

    requestCurrentWeather(query, QGeoCoordinate());
}

void OpenWeatherMapBackend::requestWeatherInfo(const QGeoCoordinate &coordinate)
{
    QUrlQuery query;
    query.addQueryItem(QStringLiteral("lat"), QString::number(coordinate.latitude()));
    query.addQueryItem(QStringLiteral("lon"), QString::number(coordinate.longitude()));

    requestCurrentWeather(query, coordinate);
}

void OpenWeatherMapBackend::handleCurrentWeatherReply(QNetworkReply *reply,
                                                      const QGeoCoordinate &coordinate)
{
    if (!reply) {
        emit errorOccurred();
        return;
    }
    bool parsed = false;
    if (!reply->error()) {
        // extract info about current weather
        const QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
        const QJsonObject documentObject = document.object();

        LocationInfo currentLocation;
        currentLocation.m_name = documentObject.value(u"name").toString();
        if (coordinate.isValid())
            currentLocation.m_coordinate = coordinate;
        qCDebug(requestsLog) << "Got current weather for" << currentLocation.m_name;

        WeatherInfo currentWeather;

        parseWeatherDescription(documentObject, currentWeather);

        const QJsonObject mainObject = documentObject.value(u"main").toObject();
        const QJsonValue tempValue = mainObject.value(u"temp");
        if (tempValue.isDouble())
            currentWeather.m_temperature = niceTemperatureString(tempValue.toDouble());
        else
            qCDebug(requestsLog, "Failed to parse current temperature.");

        parsed = !currentLocation.m_name.isEmpty() && !currentWeather.m_temperature.isEmpty();

        if (parsed) {
            // request forecast
            requestWeatherForecast(currentLocation, currentWeather);
        }
    }
    if (!parsed) {
        emit errorOccurred();
        if (reply->error())
            qCDebug(requestsLog) << reply->errorString();
        else
            qCDebug(requestsLog, "Failed to parse current weather JSON.");
    }

    reply->deleteLater();
}

void OpenWeatherMapBackend::handleWeatherForecastReply(QNetworkReply *reply,
                                                       const LocationInfo &location,
                                                       const WeatherInfo &currentWeather)
{
    if (!reply) {
        emit errorOccurred();
        return;
    }
    if (!reply->error()) {
        QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
        const QJsonObject documentObject = document.object();

        QList<WeatherInfo> weatherDetails;
        // current weather will be the first in the list
        weatherDetails << currentWeather;

        QJsonArray daysList = documentObject.value(u"list").toArray();
        // include current day as well
        for (qsizetype i = 0; i < daysList.size(); ++i) {
            QJsonObject dayObject = daysList.at(i).toObject();
            WeatherInfo info;

            const QDateTime dt = QDateTime::fromSecsSinceEpoch(dayObject.value(u"dt").toInteger());
            info.m_dayOfWeek = dt.toString(u"ddd");

            const QJsonObject tempObject = dayObject.value(u"temp").toObject();

            const QJsonValue minTemp = tempObject.value(u"min");
            const QJsonValue maxTemp = tempObject.value(u"max");
            if (minTemp.isDouble() && maxTemp.isDouble()) {
                info.m_temperature = niceTemperatureString(minTemp.toDouble()) + QChar('/')
                        + niceTemperatureString(maxTemp.toDouble());
            } else {
                qCDebug(requestsLog, "Failed to parse min or max temperature.");
            }

            parseWeatherDescription(dayObject, info);

            if (!info.m_temperature.isEmpty() && !info.m_weatherIconId.isEmpty())
                weatherDetails.push_back(info);
        }

        emit weatherInformation(location, weatherDetails);
    } else {
        emit errorOccurred();
        qCDebug(requestsLog) << reply->errorString();
    }

    reply->deleteLater();
}

void OpenWeatherMapBackend::requestCurrentWeather(QUrlQuery &query,
                                                  const QGeoCoordinate &coordinate)
{
    QUrl url("http://api.openweathermap.org/data/2.5/weather");
    query.addQueryItem(QStringLiteral("mode"), QStringLiteral("json"));
    query.addQueryItem(QStringLiteral("APPID"), m_appId);
    url.setQuery(query);

    QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url));
    connect(reply, &QNetworkReply::finished, this,
            [this, reply, coordinate]() { handleCurrentWeatherReply(reply, coordinate); });
}

void OpenWeatherMapBackend::requestWeatherForecast(const LocationInfo &location,
                                                   const WeatherInfo &currentWeather)
{
    QUrl url("http://api.openweathermap.org/data/2.5/forecast/daily");
    QUrlQuery query;
    query.addQueryItem(QStringLiteral("q"), location.m_name);
    query.addQueryItem(QStringLiteral("mode"), QStringLiteral("json"));
    query.addQueryItem(QStringLiteral("cnt"), QStringLiteral("4"));
    query.addQueryItem(QStringLiteral("APPID"), m_appId);
    url.setQuery(query);

    QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url));
    connect(reply, &QNetworkReply::finished, this, [this, reply, location, currentWeather]() {
        handleWeatherForecastReply(reply, location, currentWeather);
    });
}