AirMapWeatherInfoManager.cc 2.97 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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
#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
16 17
using namespace airmap;

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

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

void
Gus Grubba's avatar
Gus Grubba committed
43
AirMapWeatherInfoManager::_requestWeatherUpdate(const QGeoCoordinate& coordinate)
44
{
45
    qCDebug(AirMapManagerLog) << "Request Weather";
46 47 48 49 50 51 52
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Weather information";
        _valid = false;
        emit weatherChanged();
        return;
    }
    Status::GetStatus::Parameters params;
Gus Grubba's avatar
Gus Grubba committed
53 54 55
    params.longitude= coordinate.longitude();
    params.latitude = coordinate.latitude();
    params.weather  = true;
56 57
    _shared.client()->status().get_status_by_point(params, [this, coordinate](const Status::GetStatus::Result& result) {
        if (result) {
58
            _weather = result.value().weather;
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) + 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();
    });
}