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

10
#include "FactPanelController.h"
11
#include "MultiVehicleManager.h"
12
#include "UAS.h"
13
#include "QGCApplication.h"
14
#include "ParameterManager.h"
15

Don Gagne's avatar
Don Gagne committed
16 17
#include <QQmlEngine>

18 19 20
/// @file
///     @author Don Gagne <don@thegagnes.com>

Don Gagne's avatar
Don Gagne committed
21 22
QGC_LOGGING_CATEGORY(FactPanelControllerLog, "FactPanelControllerLog")

23
FactPanelController::FactPanelController()
24
{
25
    _vehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
26 27 28
    if (_vehicle) {
        _uas = _vehicle->uas();
        _autopilot = _vehicle->autopilotPlugin();
29 30
    } else {
        _vehicle = qgcApp()->toolbox()->multiVehicleManager()->offlineEditingVehicle();
31
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
32 33 34 35

    _missingParametersTimer.setInterval(500);
    _missingParametersTimer.setSingleShot(true);
    connect(&_missingParametersTimer, &QTimer::timeout, this, &FactPanelController::_checkForMissingParameters);
Don Gagne's avatar
Don Gagne committed
36 37 38
}

void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
39
{
40 41 42 43
    if (componentId == FactSystem::defaultComponentId) {
        componentId = _vehicle->defaultComponentId();
    }

Don Gagne's avatar
Don Gagne committed
44
    qgcApp()->reportMissingParameter(componentId, name);
45
    qCWarning(FactPanelControllerLog) << "Missing parameter:" << QString("%1:%2").arg(componentId).arg(name);
46 47
}

Don Gagne's avatar
Don Gagne committed
48
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
49 50
{
    bool noMissingFacts = true;
dogmaphobic's avatar
dogmaphobic committed
51

52
    foreach (const QString &name, names) {
53
        if (_vehicle && !_vehicle->parameterManager()->parameterExists(componentId, name)) {
Don Gagne's avatar
Don Gagne committed
54
            _reportMissingParameter(componentId, name);
55 56 57
            noMissingFacts = false;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
58

59 60 61
    return noMissingFacts;
}

Don Gagne's avatar
Don Gagne committed
62

dogmaphobic's avatar
dogmaphobic committed
63
Fact* FactPanelController::getParameterFact(int componentId, const QString& name, bool reportMissing)
Don Gagne's avatar
Don Gagne committed
64
{
65 66
    if (_vehicle && _vehicle->parameterManager()->parameterExists(componentId, name)) {
        Fact* fact = _vehicle->parameterManager()->getParameter(componentId, name);
Don Gagne's avatar
Don Gagne committed
67 68 69
        QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
        return fact;
    } else {
70
        if (reportMissing) {
dogmaphobic's avatar
dogmaphobic committed
71
            _reportMissingParameter(componentId, name);
72
        }
73
        return nullptr;
Don Gagne's avatar
Don Gagne committed
74 75 76 77 78
    }
}

bool FactPanelController::parameterExists(int componentId, const QString& name)
{
79
    return _vehicle ? _vehicle->parameterManager()->parameterExists(componentId, name) : false;
Don Gagne's avatar
Don Gagne committed
80 81
}

DonLakeFlyer's avatar
DonLakeFlyer committed
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
void FactPanelController::getMissingParameters(QStringList rgNames)
{
    for (const QString& name: rgNames) {
        _missingParameterWaitList.append(name);
        _vehicle->parameterManager()->refreshParameter(MAV_COMP_ID_AUTOPILOT1, name);
    }

    _missingParametersTimer.start();
}

void FactPanelController::_checkForMissingParameters(void)
{
    QStringList waitList = _missingParameterWaitList;
    for (const QString& name: waitList) {
        if (_vehicle->parameterManager()->parameterExists(MAV_COMP_ID_AUTOPILOT1, name)) {
            _missingParameterWaitList.removeOne(name);
        }
    }

    if (_missingParameterWaitList.isEmpty()) {
        emit missingParametersAvailable();
    } else {
        _missingParametersTimer.start();
    }
}