WimaPlaner.cc 32.9 KB
Newer Older
1
#include "WimaPlaner.h"
2

3 4 5 6
#include "MissionController.h"
#include "MissionSettingsItem.h"
#include "PlanMasterController.h"
#include "QGCApplication.h"
7
#include "QGCLoggingCategory.h"
8 9
#include "QGCMapPolygon.h"
#include "SimpleMissionItem.h"
10

11 12 13
#include "Geometry/GeoUtilities.h"
#include "Geometry/PlanimetryCalculus.h"
#include "OptimisationTools.h"
14

15 16 17 18
#include "CircularSurvey.h"
#include "Geometry/WimaArea.h"
#include "Geometry/WimaAreaData.h"
#include "WimaBridge.h"
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "StateMachine.h"
using namespace wima_planer_detail;

#include <functional>

QGC_LOGGING_CATEGORY(WimaPlanerLog, "WimaPlanerLog")

class CommandRAII {
  std::function<void(void)> f;

public:
  CommandRAII(const std::function<void(void)> &fun) : f(fun) {}
  ~CommandRAII() { f(); }
};

35 36 37
const char *WimaPlaner::wimaFileExtension = "wima";
const char *WimaPlaner::areaItemsName = "AreaItems";
const char *WimaPlaner::missionItemsName = "MissionItems";
38 39

WimaPlaner::WimaPlaner(QObject *parent)
40
    : QObject(parent), _masterController(nullptr), _missionController(nullptr),
41 42 43 44
      _currentAreaIndex(-1), _copyMAreaToSurvey(true), _copySAreaToSurvey(true),
      _corridorChanged(true), _joinedArea(this), _arrivalPathLength(0),
      _returnPathLength(0), _survey(nullptr), _surveyChanged(true),
      _synchronized(false), _nemoInterface(this),
45
      _stateMachine(new StateMachine), _areasMonitored(false),
46
      _missionControllerMonitored(false), _progressLocked(false) {
47 48

  connect(this, &WimaPlaner::currentPolygonIndexChanged, this,
49
          &WimaPlaner::updatePolygonInteractivity);
50

51 52 53
  // Monitoring.
  enableAreaMonitoring();
  // Mission controller not set at this point. Not enabling monitoring.
54

Valentin Platzgummer's avatar
Valentin Platzgummer committed
55
#ifndef NDEBUG
56 57 58 59 60
  // for debugging and testing purpose, remove if not needed anymore
  connect(&_autoLoadTimer, &QTimer::timeout, this,
          &WimaPlaner::autoLoadMission);
  _autoLoadTimer.setSingleShot(true);
  _autoLoadTimer.start(300);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
61
#endif
62 63

  // NemoInterface
64 65
  connect(&this->_nemoInterface, &NemoInterface::progressChanged, this,
          &WimaPlaner::nemoInterfaceProgressChangedHandler);
66 67 68 69

  // StateMachine
  connect(this->_stateMachine.get(), &StateMachine::upToDateChanged, this,
          &WimaPlaner::needsUpdateChanged);
70 71
  connect(this->_stateMachine.get(), &StateMachine::surveyReadyChanged, this,
          &WimaPlaner::readyForSynchronizationChanged);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
72 73
  connect(this->_stateMachine.get(), &StateMachine::surveyReadyChanged, this,
          &WimaPlaner::surveyReadyChanged);
74
}
75

76 77
WimaPlaner::~WimaPlaner() {}

78 79 80 81 82 83
PlanMasterController *WimaPlaner::masterController() {
  return _masterController;
}

MissionController *WimaPlaner::missionController() {
  return _missionController;
84 85
}

86
QmlObjectListModel *WimaPlaner::visualItems() { return &_visualItems; }
87

88 89 90 91
int WimaPlaner::currentPolygonIndex() const { return _currentAreaIndex; }

QString WimaPlaner::currentFile() const { return _currentFile; }

92 93
QStringList WimaPlaner::loadNameFilters() const {
  QStringList filters;
94

95 96 97 98 99
  filters << tr("Supported types (*.%1 *.%2)")
                 .arg(wimaFileExtension)
                 .arg(AppSettings::planFileExtension)
          << tr("All Files (*.*)");
  return filters;
100 101
}

102 103
QStringList WimaPlaner::saveNameFilters() const {
  QStringList filters;
104

105 106 107 108
  filters << tr("Supported types (*.%1 *.%2)")
                 .arg(wimaFileExtension)
                 .arg(AppSettings::planFileExtension);
  return filters;
109 110
}

111 112
QString WimaPlaner::fileExtension() const { return wimaFileExtension; }

113 114
QGeoCoordinate WimaPlaner::joinedAreaCenter() const {
  return _joinedArea.center();
115 116
}

117 118
NemoInterface *WimaPlaner::nemoInterface() { return &_nemoInterface; }

119
void WimaPlaner::setMasterController(PlanMasterController *masterC) {
120 121 122 123
  if (_masterController != masterC) {
    _masterController = masterC;
    emit masterControllerChanged();
  }
124 125
}

126
void WimaPlaner::setMissionController(MissionController *missionC) {
127 128 129 130 131 132
  if (_missionController != missionC) {
    disableMissionControllerMonitoring();
    _missionController = missionC;
    enableMissionControllerMonitoring();
    emit missionControllerChanged();
  }
133 134
}

135 136 137 138
void WimaPlaner::setCurrentPolygonIndex(int index) {
  if (index >= 0 && index < _visualItems.count() &&
      index != _currentAreaIndex) {
    _currentAreaIndex = index;
139

140 141
    emit currentPolygonIndexChanged(index);
  }
142 143
}

144 145 146 147 148
void WimaPlaner::setProgressLocked(bool l) {
  if (this->_progressLocked != l) {
    this->_progressLocked = l;
    emit progressLockedChanged();
    if (!this->_progressLocked) {
149 150
      if (this->_measurementArea.setProgress(this->_nemoInterface.progress()))
        this->_update();
151
    }
152
  }
153 154
}

155
bool WimaPlaner::synchronized() { return _synchronized; }
156

157
bool WimaPlaner::needsUpdate() { return !this->_stateMachine->upToDate(); }
158

159 160 161 162
bool WimaPlaner::readyForSynchronization() {
  return this->_stateMachine->surveyReady();
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
163 164
bool WimaPlaner::surveyReady() { return this->_stateMachine->surveyReady(); }

165 166
bool WimaPlaner::progressLocked() { return this->_progressLocked; }

167 168 169
void WimaPlaner::removeArea(int index) {
  if (index >= 0 && index < _visualItems.count()) {
    WimaArea *area = qobject_cast<WimaArea *>(_visualItems.removeAt(index));
170

171
    if (area == nullptr) {
172 173
      qCWarning(WimaPlanerLog)
          << "removeArea(): nullptr catched, internal error.";
174
      return;
175
    }
176 177
    area->clear();
    area->borderPolygon()->clear();
178

179
    emit visualItemsChanged();
180

181 182 183 184 185 186 187
    if (_visualItems.count() == 0) {
      // this branch is reached if all items are removed
      // to guarentee proper behavior, _currentAreaIndex must be set to a
      // invalid value, as on constructor init.
      resetAllInteractive();
      _currentAreaIndex = -1;
      return;
188 189
    }

190 191
    if (_currentAreaIndex >= _visualItems.count()) {
      setCurrentPolygonIndex(_visualItems.count() - 1);
192
    } else {
193
      updatePolygonInteractivity(_currentAreaIndex);
194
    }
195
  } else {
196
    qCWarning(WimaPlanerLog) << "removeArea(): Index out of bounds!";
197
  }
198 199
}

200 201 202
bool WimaPlaner::addMeasurementArea() {
  if (!_visualItems.contains(&_measurementArea)) {
    _visualItems.append(&_measurementArea);
203

204 205
    int newIndex = _visualItems.count() - 1;
    setCurrentPolygonIndex(newIndex);
206

207 208 209 210 211
    emit visualItemsChanged();
    return true;
  } else {
    return false;
  }
212 213
}

214 215 216
bool WimaPlaner::addServiceArea() {
  if (!_visualItems.contains(&_serviceArea)) {
    _visualItems.append(&_serviceArea);
217

218 219
    int newIndex = _visualItems.count() - 1;
    setCurrentPolygonIndex(newIndex);
220

221 222 223 224 225 226
    emit visualItemsChanged();
    return true;
  } else {
    return false;
  }
}
227

228 229 230
bool WimaPlaner::addCorridor() {
  if (!_visualItems.contains(&_corridor)) {
    _visualItems.append(&_corridor);
231

232 233
    int newIndex = _visualItems.count() - 1;
    setCurrentPolygonIndex(newIndex);
234

235 236 237 238 239
    emit visualItemsChanged();
    return true;
  } else {
    return false;
  }
240 241
}

242 243
void WimaPlaner::removeAll() {
  bool changesApplied = false;
244
  // Delete Pointers.
245 246 247 248
  while (_visualItems.count() > 0) {
    removeArea(0);
    changesApplied = true;
  }
249 250 251 252 253
  // Reset Items.
  _measurementArea = WimaMeasurementArea();
  _joinedArea = WimaJoinedArea();
  _serviceArea = WimaServiceArea();
  _corridor = WimaCorridor();
254

255
  // Remove missions items.
256 257
  _missionController->removeAll();
  _currentFile = "";
258
  _survey = nullptr;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
259

260 261 262 263
  emit currentFileChanged();
  if (changesApplied)
    emit visualItemsChanged();
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
264

265
void WimaPlaner::update() { this->_update(); }
266

267 268
void WimaPlaner::_update() {
  setSynchronized(false);
269

270 271
  switch (this->_stateMachine->state()) {
  case STATE::NEEDS_INIT: {
272 273 274 275 276 277 278 279 280
    if (this->_measurementArea.ready()) {
      this->_stateMachine->updateState(EVENT::INIT_DONE);
      this->_update();
    } else {
      this->_stateMachine->updateState(EVENT::M_AREA_NOT_READY);
    }
  } break;

  case STATE::WAITING_FOR_TILE_UPDATE: {
281
  } break;
282

283 284 285 286 287
  case STATE::NEEDS_J_AREA_UPDATE: {
    // check if at least service area and measurement area are available
    if (_visualItems.indexOf(&_serviceArea) == -1 ||
        _visualItems.indexOf(&_measurementArea) == -1)
      return;
288

289 290 291 292 293
    // Check if polygons have at least three vertices
    if (_serviceArea.count() < 3) {
      qgcApp()->showMessage(tr("Service area has less than three vertices."));
      return;
    }
294

295 296 297 298 299
    if (_measurementArea.count() < 3) {
      qgcApp()->showMessage(
          tr("Measurement area has less than three vertices."));
      return;
    }
300

301 302 303 304 305 306
    // Check for simple polygons
    if (!_serviceArea.isSimplePolygon()) {
      qgcApp()->showMessage(tr("Service area is not a simple polygon. "
                               "Only simple polygons allowed.\n"));
      return;
    }
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    if (!_corridor.isSimplePolygon() && _corridor.count() > 0) {
      qgcApp()->showMessage(tr("Corridor is not a simple polygon. Only "
                               "simple polygons allowed.\n"));
      return;
    }

    if (!_measurementArea.isSimplePolygon()) {
      qgcApp()->showMessage(tr("Measurement area is not a simple "
                               "polygon. Only simple polygons allowed.\n"));
      return;
    }

    if (!_serviceArea.containsCoordinate(_serviceArea.depot())) {
      qgcApp()->showMessage(tr("Depot not inside service area."));
      return;
    }
324

325
    // Join areas.
326 327 328 329 330 331 332 333 334
    _joinedArea.setPath(_serviceArea.path());
    if (_corridor.count() >= 3) {
      _joinedArea.join(_corridor);
    }
    if (!_joinedArea.join(_measurementArea)) {
      qgcApp()->showMessage(
          tr("Not able to join areas. Service and measurement area"
             " must be overlapping, or connected through a "
             "corridor."));
335
      return;
336
    }
337 338 339
    this->_stateMachine->updateState(EVENT::J_AREA_UPDATED);
    this->_update();
  } break; // STATE::NEEDS_J_AREA_UPDATE
340

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  case STATE::NEEDS_SURVEY_UPDATE: {
    // Need to insert Survey?
    QmlObjectListModel *missionItems = _missionController->visualItems();
    int surveyIndex = missionItems->indexOf(_survey);
    // Create survey item if not yet present.
    if (surveyIndex < 0) {
      _missionController->insertComplexMissionItem(
          _missionController->circularSurveyComplexItemName(),
          _measurementArea.center(), missionItems->count());
      _survey = qobject_cast<CircularSurvey *>(
          missionItems->get(missionItems->count() - 1));

      if (_survey == nullptr) {
        qCWarning(WimaPlanerLog) << "_survey == nullptr";
        return;
      }
357

358 359 360 361 362 363 364 365 366 367
      // establish connections
      _survey->setRefPoint(_measurementArea.center());
      _survey->setHidePolygon(true);
      connect(_survey, &CircularSurvey::calculatingChanged, this,
              &WimaPlaner::CSCalculatingChangedHandler);
      connect(_survey, &CircularSurvey::missionItemReady, this,
              &WimaPlaner::CSMissionItemReadyHandler);
      connect(_survey, &CircularSurvey::destroyed, this,
              &WimaPlaner::CSDestroyedHandler);
    }
368 369

    // update survey area
370 371 372 373
    disconnect(_survey, &CircularSurvey::calculatingChanged, this,
               &WimaPlaner::CSCalculatingChangedHandler);
    _survey->setMeasurementArea(this->_measurementArea);
    _survey->setJoinedArea(this->_joinedArea);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
374
    _survey->setDepot(this->_serviceArea.depot());
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    connect(_survey, &CircularSurvey::calculatingChanged, this,
            &WimaPlaner::CSCalculatingChangedHandler);

    // Folloing statement just for completeness.
    this->_stateMachine->updateState(EVENT::SURVEY_UPDATE_TRIGGERED);
  } break; // STATE::NEEDS_SURVEY_UPDATE

  case STATE::WAITING_FOR_SURVEY_UPDATE: {
  } break;

  case STATE::NEEDS_PATH_UPDATE: {
    // Check if survey is present.
    QmlObjectListModel *missionItems = _missionController->visualItems();
    int surveyIndex = missionItems->indexOf(_survey);
    if (surveyIndex < 0) {
      this->_stateMachine->updateState(EVENT::SURVEY_DESTROYED);
      this->_update();
    } else {
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407
      // Remove old arrival and return path.
      int size = missionItems->count();
      for (int i = surveyIndex + 1; i < size; i++)
        _missionController->removeMissionItem(surveyIndex + 1);
      for (int i = surveyIndex - 1; i > 1; i--)
        _missionController->removeMissionItem(i);

      // set home position to serArea center
      MissionSettingsItem *settingsItem =
          qobject_cast<MissionSettingsItem *>(missionItems->get(0));
      if (settingsItem == nullptr) {
        qCWarning(WimaPlanerLog) << "update(): settingsItem == nullptr";
        return;
      }
408

409 410 411 412
      // set altitudes
      auto depot = _serviceArea.depot();
      depot.setAltitude(0);
      settingsItem->setCoordinate(depot);
413

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
      // set takeoff position
      bool setCommandNeeded = false;
      if (missionItems->count() < 3) {
        setCommandNeeded = true;
        _missionController->insertSimpleMissionItem(depot, 1);
      }
      SimpleMissionItem *takeOffItem =
          qobject_cast<SimpleMissionItem *>(missionItems->get(1));
      if (takeOffItem == nullptr) {
        qCWarning(WimaPlanerLog) << "update(): takeOffItem == nullptr";
        return;
      }
      if (setCommandNeeded)
        _missionController->setTakeoffCommand(*takeOffItem);
      takeOffItem->setCoordinate(depot);
429

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      if (_survey->visualTransectPoints().size() == 0) {
        qCWarning(WimaPlanerLog) << "update(): survey no points";
        return;
      }

      // calculate path from take off to survey
      QGeoCoordinate start = depot;
      QGeoCoordinate end = _survey->coordinate();
      QVector<QGeoCoordinate> path;
      if (!shortestPath(start, end, path)) {
        qgcApp()->showMessage(
            QString(tr("Not able to calculate path from "
                       "takeoff position to measurement area."))
                .toLocal8Bit()
                .data());
        return;
      }
      _arrivalPathLength = path.size() - 1;
      for (int i = 1; i < path.count() - 1; i++) {
449
        (void)_missionController->insertSimpleMissionItem(
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
            path[i], missionItems->count() - 1);
      }

      // calculate return path
      start = _survey->exitCoordinate();
      end = depot;
      path.clear();
      if (!shortestPath(start, end, path)) {
        qgcApp()->showMessage(
            QString(tr("Not able to calculate the path from "
                       "measurement area to landing position."))
                .toLocal8Bit()
                .data());
        return;
      }
      _returnPathLength =
          path.size() - 1; // -1: fist item is last measurement point
      for (int i = 1; i < path.count() - 1; i++) {
468
        (void)_missionController->insertSimpleMissionItem(
469 470 471
            path[i], missionItems->count());
      }

472 473 474
      // Add waypoint (rover ignores land command).
      (void)_missionController->insertSimpleMissionItem(depot,
                                                        missionItems->count());
475
      // create land position item
476 477
      (void)_missionController->insertSimpleMissionItem(depot,
                                                        missionItems->count());
478 479 480 481 482 483
      SimpleMissionItem *landItem = qobject_cast<SimpleMissionItem *>(
          missionItems->get(missionItems->count() - 1));
      if (landItem == nullptr) {
        qCWarning(WimaPlanerLog) << "update(): landItem == nullptr";
        return;
      }
484
      if (!_missionController->setLandCommand(*landItem))
485 486 487
        return;

      this->_stateMachine->updateState(EVENT::PATH_UPDATED);
488
    }
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  } break; // STATE::NEEDS_PATH_UPDATE

  case STATE::UP_TO_DATE: {
  } break; // STATE::UP_TO_DATE

  } // switch
}

void WimaPlaner::CSDestroyedHandler() {
  this->_stateMachine->updateState(EVENT::SURVEY_DESTROYED);
}

void WimaPlaner::CSMissionItemReadyHandler() {
  this->_stateMachine->updateState(EVENT::SURVEY_UPDATED);
  this->_update();
}

void WimaPlaner::CSCalculatingChangedHandler() {
  if (this->_survey->calculating()) {
    this->_stateMachine->updateState(EVENT::SURVEY_UPDATE_TRIGGERED);
509
  }
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
}

void WimaPlaner::mAreaPathChangedHandler() {
  this->_stateMachine->updateState(EVENT::M_AREA_PATH_CHANGED);
}

void WimaPlaner::mAreaTilesChangedHandler() {
  this->_nemoInterface.setTileData(this->_measurementArea.tileData());
  this->_stateMachine->updateState(EVENT::M_AREA_TILES_CHANGED);
}

void WimaPlaner::mAreaProgressChangedHandler() {
  this->_stateMachine->updateState(EVENT::M_AREA_PROGRESS_CHANGED);
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
525 526
void WimaPlaner::mAreaProgressAcceptedHandler() { this->_update(); }

527 528 529 530 531 532 533 534
void WimaPlaner::mAreaReadyChangedHandler() {
  if (this->_measurementArea.ready()) {
    this->_stateMachine->updateState(EVENT::M_AREA_READY);
  } else {
    this->_stateMachine->updateState(EVENT::M_AREA_NOT_READY);
  }
}

535 536 537 538 539 540 541
void WimaPlaner::sAreaPathChangedHandler() {
  this->_stateMachine->updateState(EVENT::S_AREA_PATH_CHANGED);
}

void WimaPlaner::corridorPathChangedHandler() {
  this->_stateMachine->updateState(EVENT::CORRIDOR_PATH_CHANGED);
}
542

543 544
void WimaPlaner::depotChangedHandler() {
  this->_stateMachine->updateState(EVENT::DEPOT_CHANGED);
545
}
546

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
void WimaPlaner::missionControllerVisualItemsChangedHandler() {
  // Search for survey.
  auto surveyIndex = _missionController->visualItems()->indexOf(_survey);
  if (surveyIndex < 0) {
    // survey not found.
    this->_stateMachine->updateState(EVENT::SURVEY_DESTROYED);
  } else {
    this->_stateMachine->updateState(EVENT::PATH_CHANGED);
  }
}

void WimaPlaner::missionControllerWaypointPathChangedHandler() {
  missionControllerVisualItemsChangedHandler();
}

void WimaPlaner::missionControllerNewItemsFromVehicleHandler() {
  this->_stateMachine->updateState(EVENT::MISSION_ITEMS_DESTROYED);
}

void WimaPlaner::missionControllerMissionItemCountChangedHandler() {
  missionControllerVisualItemsChangedHandler();
}

570
void WimaPlaner::nemoInterfaceProgressChangedHandler() {
571 572 573 574 575 576 577
  auto p = this->_nemoInterface.progress();
  WimaBridge::instance()->setProgress(p);

  if (!progressLocked()) {
    this->_measurementArea.setProgress(p);
    this->_update();
  }
578 579
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
void WimaPlaner::saveToCurrent() { saveToFile(_currentFile); }

void WimaPlaner::saveToFile(const QString &filename) {
  if (filename.isEmpty()) {
    return;
  }

  QString planFilename = filename;
  if (!QFileInfo(filename).fileName().contains(".")) {
    planFilename += QString(".%1").arg(wimaFileExtension);
  }

  QFile file(planFilename);
  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
    qgcApp()->showMessage(
        tr("Plan save error %1 : %2").arg(filename).arg(file.errorString()));
    _currentFile.clear();
    emit currentFileChanged();
  } else {
    FileType fileType = FileType::WimaFile;
    if (planFilename.contains(QString(".%1").arg(wimaFileExtension))) {
      fileType = FileType::WimaFile;
    } else if (planFilename.contains(
                   QString(".%1").arg(AppSettings::planFileExtension))) {
      fileType = FileType::PlanFile;
605
    } else {
606 607 608 609 610 611 612
      if (planFilename.contains(".")) {
        qgcApp()->showMessage(tr("File format not supported"));
      } else {
        qgcApp()->showMessage(tr("File without file extension not accepted."));
        return;
      }
    }
613

614 615 616 617 618
    QJsonDocument saveDoc = saveToJson(fileType);
    file.write(saveDoc.toJson());
    if (_currentFile != planFilename) {
      _currentFile = planFilename;
      emit currentFileChanged();
619
    }
620
  }
621 622
}

623
bool WimaPlaner::loadFromCurrent() { return loadFromFile(_currentFile); }
624

625
bool WimaPlaner::loadFromFile(const QString &filename) {
626
  // Remove obsolete connections.
627 628 629 630 631 632 633
  disableAreaMonitoring();
  disableMissionControllerMonitoring();
  CommandRAII onExit([this] {
    this->enableAreaMonitoring();
    this->enableMissionControllerMonitoring();
  });

634 635 636 637 638 639 640 641 642 643 644 645 646
  // disconnect old survey
  if (_survey != nullptr) {
    disconnect(_survey, &CircularSurvey::calculatingChanged, this,
               &WimaPlaner::CSCalculatingChangedHandler);
    disconnect(_survey, &CircularSurvey::missionItemReady, this,
               &WimaPlaner::CSMissionItemReadyHandler);
    disconnect(_survey, &CircularSurvey::destroyed, this,
               &WimaPlaner::CSDestroyedHandler);
  }

  setSynchronized(false);

  // Precondition.
647 648 649
  QString errorString;
  QString errorMessage =
      tr("Error loading Plan file (%1). %2").arg(filename).arg("%1");
650

651 652 653
  if (filename.isEmpty()) {
    return false;
  }
654

655 656
  QFileInfo fileInfo(filename);
  QFile file(filename);
657

658 659 660 661 662
  if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    errorString = file.errorString() + QStringLiteral(" ") + filename;
    qgcApp()->showMessage(errorMessage.arg(errorString));
    return false;
  }
663

664 665 666
  if (fileInfo.suffix() == wimaFileExtension) {
    QJsonDocument jsonDoc;
    QByteArray bytes = file.readAll();
667

668 669 670 671
    if (!JsonHelper::isJsonFile(bytes, jsonDoc, errorString)) {
      qgcApp()->showMessage(errorMessage.arg(errorString));
      return false;
    }
672

673 674 675 676
    QJsonObject json = jsonDoc.object();
    // AreaItems
    QJsonArray areaArray = json[areaItemsName].toArray();
    _visualItems.clear();
677

678 679 680
    int validAreaCounter = 0;
    for (int i = 0; i < areaArray.size() && validAreaCounter < 3; i++) {
      QJsonObject jsonArea = areaArray[i].toObject();
681

682 683 684 685 686
      if (jsonArea.contains(WimaArea::areaTypeName) &&
          jsonArea[WimaArea::areaTypeName].isString()) {
        if (jsonArea[WimaArea::areaTypeName] ==
            WimaMeasurementArea::WimaMeasurementAreaName) {
          bool success = _measurementArea.loadFromJson(jsonArea, errorString);
687

688 689
          if (!success) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
690
            return false;
691 692 693 694 695 696 697 698 699 700 701
          }

          validAreaCounter++;
          _visualItems.append(&_measurementArea);
          emit visualItemsChanged();
        } else if (jsonArea[WimaArea::areaTypeName] ==
                   WimaServiceArea::wimaServiceAreaName) {
          bool success = _serviceArea.loadFromJson(jsonArea, errorString);

          if (!success) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
702
            return false;
703 704 705 706 707 708 709 710 711 712 713 714 715
          }

          validAreaCounter++;
          _visualItems.append(&_serviceArea);
          emit visualItemsChanged();
        } else if (jsonArea[WimaArea::areaTypeName] ==
                   WimaCorridor::WimaCorridorName) {
          bool success = _corridor.loadFromJson(jsonArea, errorString);

          if (!success) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
            return false;
          }
716

717 718 719 720 721 722 723 724
          validAreaCounter++;
          _visualItems.append(&_corridor);
          emit visualItemsChanged();
        } else {
          errorString +=
              QString(tr("%s not supported.\n").arg(WimaArea::areaTypeName));
          qgcApp()->showMessage(errorMessage.arg(errorString));
          return false;
725
        }
726 727 728
      } else {
        errorString += QString(tr("Invalid or non existing entry for %s.\n")
                                   .arg(WimaArea::areaTypeName));
729
        return false;
730
      }
731 732
    }

733 734 735
    _currentFile.sprintf("%s/%s.%s", fileInfo.path().toLocal8Bit().data(),
                         fileInfo.completeBaseName().toLocal8Bit().data(),
                         wimaFileExtension);
736

737
    emit currentFileChanged();
738

739
    QJsonObject missionObject = json[missionItemsName].toObject();
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    QJsonDocument missionJsonDoc = QJsonDocument(missionObject);
    // create temporary file with missionItems
    QFile temporaryFile;
    QString cropedFileName = filename.section("/", 0, -2);
    QString temporaryFileName;
    for (int i = 0;; i++) {
      temporaryFileName =
          cropedFileName +
          QString("/temp%1.%2").arg(i).arg(AppSettings::planFileExtension);

      if (!QFile::exists(temporaryFileName)) {
        temporaryFile.setFileName(temporaryFileName);
        if (temporaryFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
          break;
        }
      }
757

758
      if (i > 1000) {
759 760
        qCWarning(WimaPlanerLog)
            << "loadFromFile(): not able to create temporary file.";
761
        return false;
762
      }
763 764
    }

765 766
    temporaryFile.write(missionJsonDoc.toJson());
    temporaryFile.close();
767

768 769 770
    // load from temporary file
    _masterController->loadFromFile(temporaryFileName);
    QmlObjectListModel *missionItems = _missionController->visualItems();
771
    _survey = nullptr;
772
    for (int i = 0; i < missionItems->count(); i++) {
773 774 775 776 777 778 779 780 781
      _survey = missionItems->value<CircularSurvey *>(i);
      if (_survey != nullptr) {
        _survey->setHidePolygon(true);
        connect(_survey, &CircularSurvey::calculatingChanged, this,
                &WimaPlaner::CSCalculatingChangedHandler);
        connect(_survey, &CircularSurvey::missionItemReady, this,
                &WimaPlaner::CSMissionItemReadyHandler);
        connect(_survey, &CircularSurvey::destroyed, this,
                &WimaPlaner::CSDestroyedHandler);
782 783
        break;
      }
784 785
    }

786 787
    // remove temporary file
    if (!temporaryFile.remove()) {
788 789 790
      qCWarning(WimaPlanerLog)
          << "WimaPlaner::loadFromFile(): not able to remove "
             "temporary file.";
791 792
    }
    return true;
793 794 795 796 797 798 799
  } else {
    errorString += QString(tr("File extension not supported.\n"));
    qgcApp()->showMessage(errorMessage.arg(errorString));
    return false;
  }
}

800
void WimaPlaner::updatePolygonInteractivity(int index) {
801 802 803 804 805 806 807 808 809
  if (index >= 0 && index < _visualItems.count()) {
    resetAllInteractive();
    WimaArea *interactivePoly =
        qobject_cast<WimaArea *>(_visualItems.get(index));
    if (interactivePoly != nullptr)
      interactivePoly->setWimaAreaInteractive(true);
  }
}

810
void WimaPlaner::synchronize() {
811 812 813 814 815 816 817
  if (readyForSynchronization()) {
    WimaPlanData planData;
    if (toPlanData(planData)) {
      WimaBridge::instance()->setPlanData(planData);
      setSynchronized(true);
    } else {
      qCWarning(WimaPlanerLog) << "error creating plan data.";
818
    }
819
  }
820 821
}

822 823 824
bool WimaPlaner::shortestPath(const QGeoCoordinate &start,
                              const QGeoCoordinate &destination,
                              QVector<QGeoCoordinate> &path) {
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  using namespace GeoUtilities;
  using namespace PolygonCalculus;
  QPolygonF polygon2D;
  toCartesianList(_joinedArea.coordinateList(), /*origin*/ start, polygon2D);
  QPointF start2D(0, 0);
  QPointF end2D;
  QVector<QPointF> path2D;
  toCartesian(destination, start, end2D);
  bool retVal =
      PolygonCalculus::shortestPath(polygon2D, start2D, end2D, path2D);
  toGeoList(path2D, /*origin*/ start, path);

  return retVal;
}

840 841 842 843
void WimaPlaner::setSynchronized(bool s) {
  if (this->_synchronized != s) {
    this->_synchronized = s;
    emit this->synchronizedChanged();
844 845 846
  }
}

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
void WimaPlaner::enableAreaMonitoring() {
  if (!areasMonitored()) {
    connect(&this->_measurementArea, &WimaArea::pathChanged, this,
            &WimaPlaner::mAreaPathChangedHandler);
    connect(&this->_measurementArea, &WimaMeasurementArea::tilesChanged, this,
            &WimaPlaner::mAreaTilesChangedHandler);
    connect(&this->_measurementArea, &WimaMeasurementArea::progressChanged,
            this, &WimaPlaner::mAreaProgressChangedHandler);
    connect(&this->_measurementArea, &WimaMeasurementArea::progressAccepted,
            this, &WimaPlaner::mAreaProgressAcceptedHandler);
    connect(&this->_measurementArea, &WimaMeasurementArea::readyChanged, this,
            &WimaPlaner::mAreaReadyChangedHandler);
    connect(&this->_serviceArea, &WimaArea::pathChanged, this,
            &WimaPlaner::sAreaPathChangedHandler);
    connect(&this->_serviceArea, &WimaServiceArea::depotChanged, this,
            &WimaPlaner::depotChangedHandler);
    connect(&this->_corridor, &WimaArea::pathChanged, this,
            &WimaPlaner::corridorPathChangedHandler);
    this->_areasMonitored = true;
  }
}

void WimaPlaner::disableAreaMonitoring() {
  if (areasMonitored()) {
    disconnect(&this->_measurementArea, &WimaArea::pathChanged, this,
               &WimaPlaner::mAreaPathChangedHandler);
    disconnect(&this->_measurementArea, &WimaMeasurementArea::tilesChanged,
               this, &WimaPlaner::mAreaTilesChangedHandler);
    disconnect(&this->_measurementArea, &WimaMeasurementArea::progressChanged,
               this, &WimaPlaner::mAreaProgressChangedHandler);
    disconnect(&this->_measurementArea, &WimaMeasurementArea::progressAccepted,
               this, &WimaPlaner::mAreaProgressAcceptedHandler);
    disconnect(&this->_measurementArea, &WimaMeasurementArea::readyChanged,
               this, &WimaPlaner::mAreaReadyChangedHandler);
    disconnect(&this->_serviceArea, &WimaArea::pathChanged, this,
               &WimaPlaner::sAreaPathChangedHandler);
    disconnect(&this->_serviceArea, &WimaServiceArea::depotChanged, this,
               &WimaPlaner::depotChangedHandler);
    disconnect(&this->_corridor, &WimaArea::pathChanged, this,
               &WimaPlaner::corridorPathChangedHandler);
    this->_areasMonitored = false;
  }
}

void WimaPlaner::enableMissionControllerMonitoring() {
  if (!missionControllerMonitored() && this->missionController() != nullptr) {
    connect(this->missionController(), &MissionController::visualItemsChanged,
            this, &WimaPlaner::missionControllerVisualItemsChangedHandler);
    connect(this->missionController(), &MissionController::waypointPathChanged,
            this, &WimaPlaner::missionControllerWaypointPathChangedHandler);
    connect(this->missionController(), &MissionController::newItemsFromVehicle,
            this, &WimaPlaner::missionControllerNewItemsFromVehicleHandler);
    connect(this->missionController(),
            &MissionController::missionItemCountChanged, this,
            &WimaPlaner::missionControllerMissionItemCountChangedHandler);
    this->_missionControllerMonitored = true;
  }
}

void WimaPlaner::disableMissionControllerMonitoring() {
  if (missionControllerMonitored() && this->missionController() != nullptr) {
    disconnect(this->missionController(),
               &MissionController::visualItemsChanged, this,
               &WimaPlaner::missionControllerVisualItemsChangedHandler);
    disconnect(this->missionController(),
               &MissionController::waypointPathChanged, this,
               &WimaPlaner::missionControllerWaypointPathChangedHandler);
    disconnect(this->missionController(),
               &MissionController::newItemsFromVehicle, this,
               &WimaPlaner::missionControllerNewItemsFromVehicleHandler);
    disconnect(this->missionController(),
               &MissionController::missionItemCountChanged, this,
               &WimaPlaner::missionControllerMissionItemCountChangedHandler);
    this->_missionControllerMonitored = false;
  }
}

bool WimaPlaner::areasMonitored() { return this->_areasMonitored; }

bool WimaPlaner::missionControllerMonitored() {
  return this->_missionControllerMonitored;
928 929
}

930 931 932 933 934 935 936
void WimaPlaner::resetAllInteractive() {
  // Marks all areas as inactive (area.interactive == false)
  int itemCount = _visualItems.count();
  if (itemCount > 0) {
    for (int i = 0; i < itemCount; i++) {
      WimaArea *iteratorPoly = qobject_cast<WimaArea *>(_visualItems.get(i));
      iteratorPoly->setWimaAreaInteractive(false);
937
    }
938
  }
939 940
}

941
void WimaPlaner::setInteractive() {
942
  updatePolygonInteractivity(_currentAreaIndex);
943 944
}

945 946 947
/*!
 * \fn WimaPlanData WimaPlaner::toPlanData()
 *
948
 * Returns a \c WimaPlanData object containing information about the current
949 950
 * mission. The \c WimaPlanData object holds only the data which is relevant
 * for the \c WimaController class. Should only be called if update() was
951
 * successful.
952 953 954
 *
 * \sa WimaController, WimaPlanData
 */
955
bool WimaPlaner::toPlanData(WimaPlanData &planData) {
956 957

  // store areas
958 959 960 961
  planData.set(WimaMeasurementAreaData(_measurementArea));
  planData.set(WimaServiceAreaData(_serviceArea));
  planData.set(WimaCorridorData(_corridor));
  planData.set(WimaJoinedAreaData(_joinedArea));
962
  return true;
963 964 965 966 967 968
}

#ifndef NDEBUG
void WimaPlaner::autoLoadMission() {
  loadFromFile("/home/valentin/Desktop/drones/qgroundcontrol/Paths/"
               "KlingenbachTest.wima");
969
  synchronize();
970
}
971
#endif
972

973 974 975 976 977 978
QJsonDocument WimaPlaner::saveToJson(FileType fileType) {
  /// This function save all areas (of WimaPlaner) and all mission items (of
  /// MissionController) to a QJsonDocument
  /// @param fileType is either WimaFile or PlanFile (enum), if fileType ==
  /// PlanFile only mission items are stored
  QJsonObject json;
979

980 981
  if (fileType == FileType::WimaFile) {
    QJsonArray jsonArray;
982

983 984
    for (int i = 0; i < _visualItems.count(); i++) {
      QJsonObject json;
985

986
      WimaArea *area = qobject_cast<WimaArea *>(_visualItems.get(i));
987

988
      if (area == nullptr) {
989
        qCWarning(WimaPlanerLog) << "saveing, area == nullptr!";
990 991
        return QJsonDocument();
      }
992

993 994 995 996 997 998 999 1000
      // check the type of area, create and append the JsonObject to the
      // JsonArray once determined
      WimaMeasurementArea *opArea = qobject_cast<WimaMeasurementArea *>(area);
      if (opArea != nullptr) {
        opArea->saveToJson(json);
        jsonArray.append(json);
        continue;
      }
1001

1002 1003 1004 1005 1006 1007
      WimaServiceArea *serArea = qobject_cast<WimaServiceArea *>(area);
      if (serArea != nullptr) {
        serArea->saveToJson(json);
        jsonArray.append(json);
        continue;
      }
1008

1009 1010 1011 1012 1013 1014
      WimaCorridor *corridor = qobject_cast<WimaCorridor *>(area);
      if (corridor != nullptr) {
        corridor->saveToJson(json);
        jsonArray.append(json);
        continue;
      }
1015

1016 1017 1018
      // if non of the obove branches was trigger, type must be WimaArea
      area->saveToJson(json);
      jsonArray.append(json);
1019 1020
    }

1021 1022
    json[areaItemsName] = jsonArray;
    json[missionItemsName] = _masterController->saveToJson().object();
1023

1024 1025 1026 1027
    return QJsonDocument(json);
  } else if (fileType == FileType::PlanFile) {
    return _masterController->saveToJson();
  }
1028

1029 1030
  return QJsonDocument(json);
}