AirMapWeatherInfoManager.cc 3.03 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Gus Grubba's avatar
Gus Grubba committed
10
#include "AirMapWeatherInfoManager.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirMapManager.h"
12

13

14 15 16
#define WEATHER_UPDATE_DISTANCE 50000                   //-- 50km threshold for weather updates
#define WEATHER_UPDATE_TIME     30 * 60 * 60 * 1000     //-- 30 minutes threshold for weather updates

Gus Grubba's avatar
Gus Grubba committed
17 18
using namespace airmap;

Gus Grubba's avatar
Gus Grubba committed
19
AirMapWeatherInfoManager::AirMapWeatherInfoManager(AirMapSharedState& shared, QObject *parent)
Gus Grubba's avatar
Gus Grubba committed
20
    : AirspaceWeatherInfoProvider(parent)
21
    , _valid(false)
Gus Grubba's avatar
Gus Grubba committed
22
    , _shared(shared)
23 24 25 26
{
}

void
27
AirMapWeatherInfoManager::setROI(const QGCGeoBoundingCube& roi, bool reset)
28 29
{
    //-- If first time or we've moved more than WEATHER_UPDATE_DISTANCE, ask for weather updates.
30
    if(reset || (!_lastRoiCenter.isValid() || _lastRoiCenter.distanceTo(roi.center()) > WEATHER_UPDATE_DISTANCE)) {
31 32
        _lastRoiCenter = roi.center();
        _requestWeatherUpdate(_lastRoiCenter);
33 34 35 36
        _weatherTime.start();
    } else {
        //-- Check weather once every WEATHER_UPDATE_TIME
        if(_weatherTime.elapsed() > WEATHER_UPDATE_TIME) {
37
            _requestWeatherUpdate(roi.center());
38 39 40 41 42 43
            _weatherTime.start();
        }
    }
}

void
Gus Grubba's avatar
Gus Grubba committed
44
AirMapWeatherInfoManager::_requestWeatherUpdate(const QGeoCoordinate& coordinate)
45
{
46
    qCDebug(AirMapManagerLog) << "Weather Request (ROI Changed)";
47 48 49 50 51 52
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Weather information";
        _valid = false;
        emit weatherChanged();
        return;
    }
53 54 55 56
    Advisory::ReportWeather::Parameters params;
    params.longitude= static_cast<float>(coordinate.longitude());
    params.latitude = static_cast<float>(coordinate.latitude());
    _shared.client()->advisory().report_weather(params, [this, coordinate](const Advisory::ReportWeather::Result& result) {
57
        if (result) {
58
            _weather = result.value();
Gus Grubba's avatar
Gus Grubba committed
59
            _valid  = true;
60
            if(_weather.icon.empty()) {
Gus Grubba's avatar
Gus Grubba committed
61 62
                _icon = QStringLiteral("qrc:/airmapweather/unknown.svg");
            } else {
63
                _icon = QStringLiteral("qrc:/airmapweather/") + QString::fromStdString(_weather.icon).replace("-", "_") + QStringLiteral(".svg");
Gus Grubba's avatar
Gus Grubba committed
64
            }
65
            qCDebug(AirMapManagerLog) << "Weather Info: " << _valid << "Icon:" << QString::fromStdString(_weather.icon) << "Condition:" << QString::fromStdString(_weather.condition) << "Temp:" << _weather.temperature;
66
        } else {
Gus Grubba's avatar
Gus Grubba committed
67
            _valid  = false;
68 69
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
            qCDebug(AirMapManagerLog) << "Request Weather Failed" << QString::fromStdString(result.error().message()) << description;
70 71 72 73
        }
        emit weatherChanged();
    });
}