Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions src/FactSystem/FactControls/FactPanelController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
****************************************************************************/

#include "FactPanelController.h"
#include "AutoPilotPlugin.h"
#include "MultiVehicleManager.h"
#include "QGCApplication.h"
#include "ParameterManager.h"
#include "AutoPilotPlugin.h"
#include "Vehicle.h"
#include "QGCApplication.h"
#include "QGCLoggingCategory.h"
#include "Vehicle.h"

#include <QtQml/QQmlEngine>

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

QGC_LOGGING_CATEGORY(FactPanelControllerLog, "FactPanelControllerLog")
QGC_LOGGING_CATEGORY(FactPanelControllerLog, "qgc.factsystem.factcontrols.factpanelcontroller")

FactPanelController::FactPanelController(QObject *parent)
: QObject(parent)
, _vehicle(MultiVehicleManager::instance()->activeVehicle())
{
_vehicle = MultiVehicleManager::instance()->activeVehicle();
// qCDebug(FactPanelControllerLog) << Q_FUNC_INFO << this;

if (_vehicle) {
_autopilot = _vehicle->autopilotPlugin();
} else {
Expand All @@ -34,24 +33,29 @@ FactPanelController::FactPanelController(QObject *parent)

_missingParametersTimer.setInterval(500);
_missingParametersTimer.setSingleShot(true);
connect(&_missingParametersTimer, &QTimer::timeout, this, &FactPanelController::_checkForMissingParameters);
(void) connect(&_missingParametersTimer, &QTimer::timeout, this, &FactPanelController::_checkForMissingParameters);
}

FactPanelController::~FactPanelController()
{
// qCDebug(FactPanelControllerLog) << Q_FUNC_INFO << this;
}

void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
void FactPanelController::_reportMissingParameter(int componentId, const QString &name) const
{
if (componentId == ParameterManager::defaultComponentId) {
componentId = _vehicle->defaultComponentId();
}

qgcApp()->reportMissingParameter(componentId, name);
qCWarning(FactPanelControllerLog) << "Missing parameter:" << QString("%1:%2").arg(componentId).arg(name);
qCWarning(FactPanelControllerLog) << "Missing parameter:" << QStringLiteral("%1:%2").arg(componentId).arg(name);
}

bool FactPanelController::_allParametersExists(int componentId, QStringList names)
bool FactPanelController::_allParametersExists(int componentId, const QStringList &names) const
{
bool noMissingFacts = true;

foreach (const QString &name, names) {
for (const QString &name : names) {
if (_vehicle && !_vehicle->parameterManager()->parameterExists(componentId, name)) {
_reportMissingParameter(componentId, name);
noMissingFacts = false;
Expand All @@ -61,42 +65,42 @@ bool FactPanelController::_allParametersExists(int componentId, QStringList name
return noMissingFacts;
}


Fact* FactPanelController::getParameterFact(int componentId, const QString& name, bool reportMissing)
Fact *FactPanelController::getParameterFact(int componentId, const QString &name, bool reportMissing) const
{
if (_vehicle && _vehicle->parameterManager()->parameterExists(componentId, name)) {
Fact* fact = _vehicle->parameterManager()->getParameter(componentId, name);
Fact *const fact = _vehicle->parameterManager()->getParameter(componentId, name);
QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
return fact;
} else {
if (reportMissing) {
_reportMissingParameter(componentId, name);
}
return nullptr;
}

if (reportMissing) {
_reportMissingParameter(componentId, name);
}

return nullptr;
}

bool FactPanelController::parameterExists(int componentId, const QString& name)
bool FactPanelController::parameterExists(int componentId, const QString &name) const
{
return _vehicle ? _vehicle->parameterManager()->parameterExists(componentId, name) : false;
return (_vehicle ? _vehicle->parameterManager()->parameterExists(componentId, name) : false);
}

void FactPanelController::getMissingParameters(QStringList rgNames)
void FactPanelController::getMissingParameters(const QStringList &rgNames)
{
for (const QString& name: rgNames) {
for (const QString &name: rgNames) {
_missingParameterWaitList.append(name);
_vehicle->parameterManager()->refreshParameter(MAV_COMP_ID_AUTOPILOT1, name);
}

_missingParametersTimer.start();
}

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

Expand Down
35 changes: 16 additions & 19 deletions src/FactSystem/FactControls/FactPanelController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,56 @@
*
****************************************************************************/


#pragma once

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

#include <QtCore/QLoggingCategory>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QTimer>
#include <QtCore/QLoggingCategory>

Q_DECLARE_LOGGING_CATEGORY(FactPanelControllerLog)

class AutoPilotPlugin;
class Vehicle;
class Fact;

/// FactPanelController is used for handling missing Facts from C++ code.
/// Used for handling missing Facts from C++ code.
class FactPanelController : public QObject
{
Q_OBJECT
Q_MOC_INCLUDE("Vehicle.h")
Q_MOC_INCLUDE("Fact.h")
Q_PROPERTY(Vehicle *vehicle MEMBER _vehicle CONSTANT)

public:
FactPanelController(QObject *parent = nullptr);
~FactPanelController();

Q_PROPERTY(Vehicle* vehicle MEMBER _vehicle CONSTANT)

Q_INVOKABLE Fact* getParameterFact (int componentId, const QString& name, bool reportMissing = true);
Q_INVOKABLE bool parameterExists (int componentId, const QString& name);
Q_INVOKABLE Fact *getParameterFact(int componentId, const QString &name, bool reportMissing = true) const;
Q_INVOKABLE bool parameterExists(int componentId, const QString &name) const;

/// Queries the vehicle for parameters which were not available on initial download but should be available now.
/// Signals missingParametersAvailable when done. Only works for MAV_COMP_ID_AUTOPILOT1 parameters.
Q_INVOKABLE void getMissingParameters(QStringList rgNames);
Q_INVOKABLE void getMissingParameters(const QStringList &rgNames);

signals:
void missingParametersAvailable(void);
void missingParametersAvailable();

protected:
/// Checks for existence of the specified parameters
/// @return true: all parameters exists, false: parameters missing and reported
bool _allParametersExists(int componentId, QStringList names);
/// @return true: all parameters exists, false: parameters missing and reported
bool _allParametersExists(int componentId, const QStringList &names) const;

/// Report a missing parameter
void _reportMissingParameter(int componentId, const QString& name);
void _reportMissingParameter(int componentId, const QString &name) const;

Vehicle* _vehicle = nullptr;
AutoPilotPlugin* _autopilot = nullptr;
Vehicle *_vehicle = nullptr;
AutoPilotPlugin *_autopilot = nullptr;

private slots:
void _checkForMissingParameters(void);
void _checkForMissingParameters();

private:
QStringList _missingParameterWaitList;
QTimer _missingParametersTimer;
QTimer _missingParametersTimer;
};
Loading