3#include <QtCore/QFileInfo>
4#include <QtCore/QJsonDocument>
29 , _managerVehicle (_controllerVehicle)
30 , _missionController (this)
31 , _geoFenceController (this)
32 , _rallyPointController (this)
37#ifdef QGC_UNITTEST_BUILD
41 , _controllerVehicle (new
Vehicle(firmwareType, vehicleType))
42 , _managerVehicle (_controllerVehicle)
43 , _missionController (this)
44 , _geoFenceController (this)
45 , _rallyPointController (this)
51void PlanMasterController::_commonInit(
void)
79 _missionController.start (_flyView);
80 _geoFenceController.start (_flyView);
81 _rallyPointController.start (_flyView);
83 _activeVehicleChanged(_multiVehicleMgr->activeVehicle());
86 _updatePlanCreatorsList();
92 _deleteWhenSendCompleted = deleteWhenSendCompleted;
93 _missionController.start(_flyView);
94 _geoFenceController.start(_flyView);
95 _rallyPointController.start(_flyView);
96 _activeVehicleChanged(vehicle);
99void PlanMasterController::_activeVehicleChanged(
Vehicle* activeVehicle)
101 if (_managerVehicle == activeVehicle) {
106 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged" << activeVehicle;
108 if (_managerVehicle) {
111 disconnect(_managerVehicle->missionManager(),
nullptr,
this,
nullptr);
112 disconnect(_managerVehicle->geoFenceManager(),
nullptr,
this,
nullptr);
113 disconnect(_managerVehicle->rallyPointManager(),
nullptr,
this,
nullptr);
116 _loadSequence = SyncSequence::Idle;
117 _sendSequence = SyncSequence::Idle;
120 bool newOffline =
false;
121 if (activeVehicle ==
nullptr) {
123 _managerVehicle = _controllerVehicle;
127 _managerVehicle = activeVehicle;
144 _offline = newOffline;
152 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Fly View - No active vehicle, clearing stale plan";
156 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Fly View - New active vehicle, loading new plan from manager vehicle";
157 _showPlanFromManagerVehicle();
163 _setDirtyForUpload(
true);
168 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Plan View - Previous dirty plan exists, no new active vehicle, sending promptForPlanUsageOnVehicleChange signal";
174 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Plan View - Previous clean plan exists, no new active vehicle, clear stale plan";
178 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Plan View - Previous clean plan exists, new active vehicle, loading from new manager vehicle";
179 _showPlanFromManagerVehicle();
184 _setDirtyStates(
false,
false);
187 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Plan View - No previous plan, no longer connected to vehicle, nothing to do";
190 qCDebug(PlanMasterControllerLog) <<
"_activeVehicleChanged: Plan View - No previous plan, new active vehicle, loading from new manager vehicle";
191 _showPlanFromManagerVehicle();
202 _updatePlanCreatorsList();
209 if (sharedLink->linkConfiguration()->isHighLatency()) {
219 qCCritical(PlanMasterControllerLog) <<
"PlanMasterController::loadFromVehicle called while offline";
220 }
else if (_flyView) {
221 qCCritical(PlanMasterControllerLog) <<
"PlanMasterController::loadFromVehicle called from Fly view";
223 qCCritical(PlanMasterControllerLog) <<
"PlanMasterController::loadFromVehicle called while syncInProgress";
225 _loadSequence = SyncSequence::Mission;
226 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::loadFromVehicle calling _missionController.loadFromVehicle";
227 _missionController.loadFromVehicle();
232void PlanMasterController::_loadMissionComplete(
void)
234 if (_loadSequence != SyncSequence::Mission) {
237 _loadSequence = SyncSequence::GeoFence;
238 if (_geoFenceController.supported()) {
239 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::_loadMissionComplete calling _geoFenceController.loadFromVehicle";
240 _geoFenceController.loadFromVehicle();
242 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::_loadMissionComplete GeoFence not supported skipping";
243 _geoFenceController.removeAll();
244 _loadGeoFenceComplete();
248void PlanMasterController::_loadGeoFenceComplete(
void)
250 if (_loadSequence != SyncSequence::GeoFence) {
253 _loadSequence = SyncSequence::RallyPoints;
254 if (_rallyPointController.supported()) {
255 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::_loadGeoFenceComplete calling _rallyPointController.loadFromVehicle";
256 _rallyPointController.loadFromVehicle();
258 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::_loadGeoFenceComplete Rally Points not supported skipping";
259 _rallyPointController.removeAll();
260 _loadRallyPointsComplete();
264void PlanMasterController::_loadRallyPointsComplete(
void)
266 if (_loadSequence != SyncSequence::RallyPoints) {
269 _loadSequence = SyncSequence::Idle;
270 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::_loadRallyPointsComplete";
274 _clearCurrentPlanFile();
275 _setDirtyStates(
false ,
false );
278void PlanMasterController::_sendMissionComplete(
void)
280 if (_sendSequence != SyncSequence::Mission) {
283 _sendSequence = SyncSequence::GeoFence;
284 if (_geoFenceController.supported()) {
285 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle start GeoFence sendToVehicle";
286 _geoFenceController.sendToVehicle();
288 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle GeoFence not supported skipping";
289 _sendGeoFenceComplete();
293void PlanMasterController::_sendGeoFenceComplete(
void)
295 if (_sendSequence != SyncSequence::GeoFence) {
298 _sendSequence = SyncSequence::RallyPoints;
299 if (_rallyPointController.supported()) {
300 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle start rally sendToVehicle";
301 _rallyPointController.sendToVehicle();
303 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle Rally Points not support skipping";
304 _sendRallyPointsComplete();
308void PlanMasterController::_sendRallyPointsComplete(
void)
310 if (_sendSequence != SyncSequence::RallyPoints) {
313 _sendSequence = SyncSequence::Idle;
314 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle Rally Point send complete";
315 _setDirtyForUpload(
false);
316 if (_deleteWhenSendCompleted) {
325 if (sharedLink->linkConfiguration()->isHighLatency()) {
335 qCCritical(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle called while offline";
337 qCCritical(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle called while syncInProgress";
339 qCDebug(PlanMasterControllerLog) <<
"PlanMasterController::sendToVehicle start mission sendToVehicle";
340 _sendSequence = SyncSequence::Mission;
341 _missionController.sendToVehicle();
348 QString errorMessage = tr(
"Error loading Plan file (%1). %2").arg(filename).arg(
"%1");
350 if (filename.isEmpty()) {
354 QFileInfo fileInfo(filename);
355 QFile file(filename);
357 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
358 errorString = file.errorString() + QStringLiteral(
" ") + filename;
363 bool success =
false;
365 success = _missionController.loadTextFile(file, errorString);
367 success = _loadPlanJson(file.readAll(), errorString);
374 _currentPlanFile = QString::asprintf(
"%s/%s.%s", fileInfo.path().toLocal8Bit().data(), fileInfo.completeBaseName().toLocal8Bit().data(),
AppSettings::planFileExtension);
376 _setDirtyStates(
false ,
true );
378 _clearCurrentPlanFile();
382bool PlanMasterController::_loadPlanJson(
const QByteArray& bytes, QString& errorString)
384 QJsonDocument jsonDoc;
389 QJsonObject json = jsonDoc.object();
398 const QList<JsonParsing::KeyValidateInfo> rgKeyInfo = {
420 QJsonObject planJson;
422 QJsonObject missionJson;
423 QJsonObject fenceJson;
424 QJsonObject rallyJson;
428 _missionController.save(missionJson);
431 _geoFenceController.save(fenceJson);
432 _rallyPointController.save(rallyJson);
437 return QJsonDocument(planJson);
443 if (!_currentPlanFile.isEmpty()) {
444 const bool saveSuccess =
saveToFile(_currentPlanFile);
453 if (filename.isEmpty()) {
457 QString planFilename = filename;
458 if (!QFileInfo(filename).fileName().contains(
".")) {
462 QFile file(planFilename);
464 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
468 const QByteArray saveBytes =
saveToJson().toJson();
469 const qint64 bytesWritten = file.write(saveBytes);
470 if (bytesWritten != saveBytes.size()) {
474 if(_currentPlanFile != planFilename) {
475 _currentPlanFile = planFilename;
478 _setDirtyForSave(
false);
486 if (filename.isEmpty()) {
490 QString kmlFilename = filename;
491 if (!QFileInfo(filename).fileName().contains(
".")) {
495 QFile file(kmlFilename);
497 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
501 _missionController.addMissionToKML(planKML);
502 QTextStream stream(&file);
503 stream << planKML.toString();
510 _suppressOverallDirtyUpdate =
true;
511 _missionController.removeAll();
512 _geoFenceController.removeAll();
513 _rallyPointController.removeAll();
514 _suppressOverallDirtyUpdate =
false;
516 _setDirtyStates(
false,
false);
518 _clearCurrentPlanFile();
526 _missionController.removeAllFromVehicle();
527 if (_geoFenceController.supported()) {
528 _geoFenceController.removeAllFromVehicle();
530 if (_rallyPointController.supported()) {
531 _rallyPointController.removeAllFromVehicle();
533 _setDirtyForUpload(
false);
534 _clearCurrentPlanFile();
536 qCCritical(PlanMasterControllerLog) <<
"PlanMasterController::removeAllFromVehicle called while offline";
543 return _missionController.containsItems() || _geoFenceController.containsItems() || _rallyPointController.containsItems();
546void PlanMasterController::_updateShowCreateFromTemplate(
void)
551 _userSelectedManualCreation =
false;
555 if (show != _showCreateFromTemplate) {
556 _showCreateFromTemplate = show;
568 return _currentPlanFile.isEmpty() ? QString() : QFileInfo(_currentPlanFile).completeBaseName();
571void PlanMasterController::_clearCurrentPlanFile()
573 if (!_currentPlanFile.isEmpty()) {
574 _currentPlanFile.clear();
598 filters << tr(
"Plan Files (*.%1)").arg(
fileExtension()) << tr(
"All Files (*)");
611void PlanMasterController::_showPlanFromManagerVehicle(
void)
615 _loadSequence = SyncSequence::InitialPlanLoad;
620 if (!_missionController.showPlanFromManagerVehicle()) {
621 if (!_geoFenceController.showPlanFromManagerVehicle()) {
622 _rallyPointController.showPlanFromManagerVehicle();
628 _clearCurrentPlanFile();
629 _setDirtyStates(
false,
false);
632void PlanMasterController::_initialPlanRequestCompleteChanged(
bool initialPlanRequestComplete)
634 if (!initialPlanRequestComplete || _loadSequence != SyncSequence::InitialPlanLoad) {
637 _loadSequence = SyncSequence::Idle;
638 qCDebug(PlanMasterControllerLog) <<
"_initialPlanRequestCompleteChanged: initial download complete";
641 _showPlanFromManagerVehicle();
646 return _missionController.syncInProgress() ||
647 _geoFenceController.syncInProgress() ||
648 _rallyPointController.syncInProgress();
653 return _missionController.isEmpty() &&
654 _geoFenceController.isEmpty() &&
655 _rallyPointController.isEmpty();
658void PlanMasterController::_updateOverallDirty(
void)
664 const bool saveDirty = _missionController.dirty() || _geoFenceController.dirty() || _rallyPointController.dirty();
666 _setDirtyForSave(
true);
670void PlanMasterController::_setDirtyForSave(
bool dirtyForSave)
673 _clearElementDirtyStates();
680 _setDirtyForUpload(
true);
685void PlanMasterController::_setDirtyForUpload(
bool dirtyForUpload)
693void PlanMasterController::_setDirtyStates(
bool dirtyForSave,
bool dirtyForUpload)
696 _clearElementDirtyStates();
699 const bool saveChanged = (_dirtyForSave !=
dirtyForSave);
714void PlanMasterController::_clearElementDirtyStates()
716 _suppressOverallDirtyUpdate =
true;
717 _missionController.setDirty(
false);
718 _geoFenceController.setDirty(
false);
719 _rallyPointController.setDirty(
false);
720 _suppressOverallDirtyUpdate =
false;
723void PlanMasterController::_updatePlanCreatorsList(
void)
729 const auto vehicleClass = _managerVehicle->vehicleClass();
732 if (_planCreators && _planCreatorsVehicleClass == vehicleClass) {
736 if (!_planCreators) {
737 _planCreators =
new QmlObjectListModel(
this);
739 _planCreators->clearAndDeleteContents();
742 _planCreatorsVehicleClass = vehicleClass;
748 for (PlanCreator* creator : creators) {
749 if (creator->supportsVehicleClass(vehicleClass)) {
750 _planCreators->append(creator);
761 const int missionState = _missionController.readyForSaveState();
766 if (_rallyPointController.terrainQueryPending()) {
777 qCDebug(PlanMasterControllerLog) <<
"showPlanFromManagerVehicle: Plan View - No new vehicle, clear any previous plan";
781 qCDebug(PlanMasterControllerLog) <<
"showPlanFromManagerVehicle: Plan View - New vehicle available, show plan from new manager vehicle";
782 _showPlanFromManagerVehicle();
794 if (show != _showCreateFromTemplate) {
795 _showCreateFromTemplate = show;
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static constexpr const char * planFileExtension
static constexpr const char * kmlFileExtension
static constexpr const char * waypointsFileExtension
void sendComplete(bool error)
Used to convert a Plan to a KML document.
void activeVehicleChanged(Vehicle *activeVehicle)
void syncInProgressChanged(bool syncInProgress)
void containsItemsChanged()
void dirtyChanged(bool dirty)
void sendComplete(bool error)
void newMissionItemsAvailable(bool removeAllRequested)
Q_INVOKABLE void removeAll(void)
Removes all from controller only, sync required to remove from vehicle.
PlanMasterController(QObject *parent=nullptr)
Q_INVOKABLE bool saveToCurrent()
void containsItemsChanged()
QStringList saveNameFilters(void) const
void promptForPlanUsageOnVehicleChange(void)
void showCreateFromTemplateChanged()
Q_INVOKABLE void saveToKml(const QString &filename)
void setUserSelectedManualCreation(bool userSelectedManualCreation)
Q_INVOKABLE void showPlanFromManagerVehicle(void)
Replaces any current plan with the plan from the manager vehicle even if offline.
static constexpr int kPlanFileVersion
Q_INVOKABLE void start(void)
Should be called immediately upon Component.onCompleted.
QStringList loadNameFilters(void) const
void planCreatorsChanged(QmlObjectListModel *planCreators)
Q_INVOKABLE void loadFromVehicle(void)
void managerVehicleChanged(Vehicle *managerVehicle)
QString kmlFileExtension(void) const
void syncInProgressChanged(void)
Q_INVOKABLE void loadFromFile(const QString &filename)
Q_INVOKABLE void startStaticActiveVehicle(Vehicle *vehicle, bool deleteWhenSendCompleted=false)
bool dirtyForUpload(void) const
static constexpr const char * kJsonGeoFenceObjectKey
void dirtyForSaveChanged(bool dirtyForSave)
QString currentPlanFileName(void) const
void userSelectedManualCreationChanged()
bool syncInProgress(void) const
void currentPlanFileChanged(void)
bool showCreateFromTemplate(void) const
bool dirtyForSave(void) const
QString fileExtension(void) const
~PlanMasterController()
true: User is not using a template to create the plan
Q_INVOKABLE bool saveToFile(const QString &filename)
void dirtyForUploadChanged(bool dirtyForUpload)
static constexpr const char * kPlanFileType
static constexpr const char * kJsonMissionObjectKey
Q_INVOKABLE void removeAllFromVehicle(void)
Removes all from vehicle and controller.
static void sendPlanToVehicle(Vehicle *vehicle, const QString &filename)
QJsonDocument saveToJson()
Q_INVOKABLE int readyForSaveState(void) const
void offlineChanged(bool offlineEditing)
Q_INVOKABLE void sendToVehicle(void)
bool userSelectedManualCreation(void) const
static constexpr const char * kJsonRallyPointsObjectKey
bool containsItems(void) const
virtual void postSaveToMissionJson(PlanMasterController *pController, QJsonObject &missionJson)
Allows custom builds to add custom items to the mission section of the plan file after the item is cr...
virtual void preLoadFromJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to load custom items from the plan file before the document is parsed.
virtual void preSaveToMissionJson(PlanMasterController *pController, QJsonObject &missionJson)
Allows custom builds to add custom items to the mission section of the plan file before the item is c...
virtual void preSaveToJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to add custom items to the plan file before the document is created.
virtual void postSaveToJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to add custom items to the plan file after the document is created.
virtual QList< PlanCreator * > planCreators(PlanMasterController *planMasterController)
static QGCCorePlugin * instance()
virtual void postLoadFromJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to load custom items from the plan file after the document is parsed.
static VehicleClass_t vehicleClass(MAV_TYPE mavType)
static FirmwareClass_t firmwareClass(MAV_AUTOPILOT autopilot)
void sendComplete(bool error)
static SettingsManager * instance()
AppSettings * appSettings() const
void initialPlanRequestCompleteChanged(bool initialPlanRequestComplete)
bool initialPlanRequestComplete() const
void vehicleTypeChanged()
bool validateExternalQGCJsonFile(const QJsonObject &jsonObject, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
bool isJsonFile(const QByteArray &bytes, QJsonDocument &jsonDoc, QString &errorString)
Determines whether an in-memory byte buffer contains parseable JSON content.
void saveQGCJsonFileHeader(QJsonObject &jsonObject, const QString &fileType, int version)
Saves the standard QGC file header (groundStation, fileType, version) into the json object.
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.