NemoInterface.cpp 47.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "NemoInterface.h"

#include "QGCApplication.h"
#include "QGCLoggingCategory.h"
#include "QGCToolbox.h"
#include "SettingsFact.h"
#include "SettingsManager.h"
#include "WimaSettings.h"

#include <shared_mutex>

#include <QTimer>
13
#include <QUrl>
14

15
#include "GenericSingelton.h"
16
#include "geometry/MeasurementArea.h"
17
#include "geometry/geometry.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
18
#include "nemo_interface/FutureWatcher.h"
19
#include "nemo_interface/MeasurementTile.h"
20
#include "nemo_interface/QNemoHeartbeat.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21 22
#include "nemo_interface/TaskDispatcher.h"

23 24
#include "ros_bridge/include/messages/geographic_msgs/geopoint.h"
#include "ros_bridge/include/messages/nemo_msgs/heartbeat.h"
25
#include "ros_bridge/include/messages/nemo_msgs/progress_array.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
26
#include "ros_bridge/include/messages/nemo_msgs/tile.h"
27 28 29
#include "ros_bridge/rapidjson/include/rapidjson/document.h"
#include "ros_bridge/rapidjson/include/rapidjson/ostreamwrapper.h"
#include "ros_bridge/rapidjson/include/rapidjson/writer.h"
30
#include "rosbridge/rosbridge.h"
31 32 33

QGC_LOGGING_CATEGORY(NemoInterfaceLog, "NemoInterfaceLog")

34 35
#define SYNC_INTERVAL 1000        // ms
#define NO_HEARTBEAT_TIMEOUT 5000 // ms
Valentin Platzgummer's avatar
Valentin Platzgummer committed
36
static constexpr auto maxResponseTime = std::chrono::milliseconds(10000);
37

38 39 40
static const char *progressTopic = "/nemo/progress";
static const char *heartbeatTopic = "/nemo/heartbeat";

Valentin Platzgummer's avatar
Valentin Platzgummer committed
41
using hrc = std::chrono::high_resolution_clock;
42
using ROSBridgePtr = std::shared_ptr<Rosbridge>;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
43 44 45 46 47 48 49 50 51 52

typedef ros_bridge::messages::nemo_msgs::tile::GenericTile<QGeoCoordinate,
                                                           QList>
    Tile;
typedef std::map<std::int64_t, std::shared_ptr<Tile>> TileMap;
typedef std::map<std::int64_t, std::shared_ptr<const Tile>> TileMapConst;
typedef ros_bridge::messages::nemo_msgs::heartbeat::Heartbeat Heartbeat;
typedef nemo_interface::TaskDispatcher Dispatcher;
typedef nemo_interface::FutureWatcher<QVariant, std::shared_future>
    FutureWatcher;
53 54

class NemoInterface::Impl {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
55 56
  enum class STATE {
    STOPPED,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
57
    START_BRIDGE,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
58 59
    WEBSOCKET_DETECTED,
    TRY_TOPIC_SERVICE_SETUP,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
60 61
    USER_SYNC,
    SYS_SYNC,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
62
    READY,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
63 64
    WEBSOCKET_TIMEOUT,
    HEARTBEAT_TIMEOUT
Valentin Platzgummer's avatar
Valentin Platzgummer committed
65
  };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
66

Valentin Platzgummer's avatar
Valentin Platzgummer committed
67
public:
68
  Impl(NemoInterface *p);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
69
  ~Impl();
70 71 72 73

  void start();
  void stop();

Valentin Platzgummer's avatar
Valentin Platzgummer committed
74 75
  // Tile editing.
  // 	Functions that require communication to device.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
76 77 78
  std::shared_future<QVariant> addTiles(const TilePtrArray &tileArray);
  std::shared_future<QVariant> removeTiles(const IDArray &idArray);
  std::shared_future<QVariant> clearTiles();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
79 80

  // 	Functions that don't require communication to device.
81 82 83 84 85
  TileArray getTiles(const IDArray &idArray) const;
  TileArray getAllTiles() const;
  LogicalArray containsTiles(const IDArray &idArray) const;
  std::size_t size() const;
  bool empty() const;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
86 87

  // Progress.
88 89
  ProgressArray getProgress() const;
  ProgressArray getProgress(const IDArray &idArray) const;
90

91 92 93
  NemoInterface::STATUS status() const;
  bool running() const; // thread safe
  bool ready() const;   // thread safe
Valentin Platzgummer's avatar
Valentin Platzgummer committed
94

95 96
  const QString &infoString() const;
  const QString &warningString() const;
97 98

private:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
99
  void _doTopicServiceSetup();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
100 101
  void _doAction();
  void _trySynchronize();
102 103 104
  bool _isSynchronized() const;
  bool _userSync() const;          // thread safe
  bool _sysSync() const;           // thread safe
Valentin Platzgummer's avatar
Valentin Platzgummer committed
105
  void _onFutureWatcherFinished(); // thread safe
106
  void _onHeartbeatTimeout();      // thread safe
107
  void _onRosbridgeStateChanged();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
108 109 110 111 112 113 114 115 116 117

  // called from dispatcher thread!
  QVariant _callAddTiles(
      std::shared_ptr<QVector<std::shared_ptr<const Tile>>> pTileArray);
  // called from dispatcher thread!
  QVariant _callRemoveTiles(std::shared_ptr<IDArray> pIdArray);
  // called from dispatcher thread!
  QVariant _callClearTiles();
  // called from dispatcher thread!
  QVariant _callGetProgress(std::shared_ptr<IDArray> pIdArray);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
118 119 120 121 122 123 124 125
  QVariant _callGetAllProgress();
  enum class CALL_NAME {
    ADD_TILES,
    REMOVE_TILES,
    CLEAR_TILES,
    GET_PROGRESS,
    GET_ALL_PROGRESS
  };
126
  QString _toString(CALL_NAME name);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
127 128

  void _addTilesRemote(
129 130
      std::shared_ptr<QVector<std::shared_ptr<const Tile>>> pTileArray,
      std::promise<bool> promise);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
131
  void
132 133 134 135 136 137 138 139 140
  _addTilesRemote2(std::shared_ptr<QVector<std::shared_ptr<Tile>>> pTileArray,
                   std::promise<bool> promise);
  void _removeTilesRemote(std::shared_ptr<IDArray> idArray,
                          std::promise<bool> promise);
  void _clearTilesRemote(std::promise<bool> promise);
  void _updateProgress(std::shared_ptr<ProgressArray> pArray,
                       std::promise<bool> promise);
  void _onHeartbeatReceived(const QNemoHeartbeat &hb,
                            std::promise<bool> promise);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
141 142
  void _setInfoString(const QString &info);
  void _setWarningString(const QString &warning);
143

Valentin Platzgummer's avatar
Valentin Platzgummer committed
144
  bool _setState(STATE newState); // not thread safe
Valentin Platzgummer's avatar
Valentin Platzgummer committed
145
  static bool _ready(STATE s);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
146 147
  static bool _userSync(STATE s);
  static bool _sysSync(STATE s);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
148
  static bool _running(STATE s);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
149 150 151
  static NemoInterface::STATUS _status(STATE state);
  static QString _toString(STATE s);
  static QString _toString(NemoInterface::STATUS s);
152

Valentin Platzgummer's avatar
Valentin Platzgummer committed
153
  std::atomic<STATE> _state;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
154
  std::atomic<CALL_NAME> _lastCall;
155
  ROSBridgePtr _pRosbridge;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
156 157 158
  TileMap _remoteTiles;
  TileMapConst _localTiles;
  NemoInterface *const _parent;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
159 160 161 162 163
  Dispatcher _dispatcher;
  QString _infoString;
  QString _warningString;
  QTimer _timeoutTimer;
  QNemoHeartbeat _lastHeartbeat;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
164
  FutureWatcher _futureWatcher;
165 166 167 168 169 170
};

using StatusMap = std::map<NemoInterface::STATUS, QString>;
static StatusMap statusMap{
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::NOT_CONNECTED, "Not Connected"),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
171 172
    std::make_pair<NemoInterface::STATUS, QString>(NemoInterface::STATUS::SYNC,
                                                   "Synchronizing"),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
173 174
    std::make_pair<NemoInterface::STATUS, QString>(NemoInterface::STATUS::READY,
                                                   "Ready"),
175 176 177 178 179 180
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::TIMEOUT, "Timeout"),
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::WEBSOCKET_DETECTED, "Websocket Detected")};

NemoInterface::Impl::Impl(NemoInterface *p)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
181
    : _state(STATE::STOPPED), _parent(p) {
182 183 184 185 186 187 188

  // ROS Bridge.
  WimaSettings *wimaSettings =
      qgcApp()->toolbox()->settingsManager()->wimaSettings();
  auto connectionStringFact = wimaSettings->rosbridgeConnectionString();
  auto setConnectionString = [connectionStringFact, this] {
    auto connectionString = connectionStringFact->rawValue().toString();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
189

Valentin Platzgummer's avatar
Valentin Platzgummer committed
190 191
    bool wasRunning = this->running();
    this->stop();
192 193
    this->_pRosbridge = std::make_shared<Rosbridge>(
        QUrl(QString("ws://") + connectionString.toLocal8Bit().data()));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
194 195 196
    if (wasRunning) {
      this->start();
    }
197 198 199 200 201
  };
  connect(connectionStringFact, &SettingsFact::rawValueChanged,
          setConnectionString);
  setConnectionString();

Valentin Platzgummer's avatar
Valentin Platzgummer committed
202
  // Heartbeat timeout.
203 204
  connect(&this->_timeoutTimer, &QTimer::timeout,
          std::bind(&Impl::_onHeartbeatTimeout, this));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
205 206

  // Connection timer (temporary workaround)
207 208
  connect(this->_pRosbridge.get(), &Rosbridge::stateChanged,
          [this] { this->_onRosbridgeStateChanged(); });
Valentin Platzgummer's avatar
Valentin Platzgummer committed
209 210 211

  connect(&this->_futureWatcher, &FutureWatcher::finished,
          [this] { this->_onFutureWatcherFinished(); });
212 213
}

214
NemoInterface::Impl::~Impl() { this->_pRosbridge->stop(); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
215

216
void NemoInterface::Impl::start() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
217 218 219 220
  if (!running()) {
    this->_setState(STATE::START_BRIDGE);
    this->_doAction();
  }
221 222 223
}

void NemoInterface::Impl::stop() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
224 225 226 227
  if (running()) {
    this->_setState(STATE::STOPPED);
    this->_doAction();
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
228 229
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
230 231
std::shared_future<QVariant>
NemoInterface::Impl::addTiles(const TilePtrArray &tileArray) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
232 233
  using namespace nemo_interface;

234
  // qDebug() << "addTiles called";
235

Valentin Platzgummer's avatar
Valentin Platzgummer committed
236 237 238 239
  if (tileArray.size() > 0) {

    // copy unknown tiles
    auto pTileArray = std::make_shared<QVector<std::shared_ptr<const Tile>>>();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
240
    auto pIdArray = std::make_shared<IDArray>();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
241 242 243 244 245 246 247 248 249
    for (const auto *pTile : tileArray) {
      auto id = pTile->id();
      const auto it = this->_localTiles.find(id);
      Q_ASSERT(it == _localTiles.end());
      if (Q_LIKELY(it == _localTiles.end())) {
        auto pTileCopy =
            std::make_shared<const Tile>(pTile->coordinateList(), 0.0, id);
        _localTiles.insert(std::make_pair(id, pTileCopy));
        pTileArray->push_back(pTileCopy);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
250
        pIdArray->push_back(id);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
251 252 253 254 255
      } else {
        qCDebug(NemoInterfaceLog)
            << "addTiles(): tile with id: " << pTile->id() << "already added.";
      }
    }
256 257 258
    if (pTileArray->size() > 0) {
      this->_parent->tilesChanged();
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
259

Valentin Platzgummer's avatar
Valentin Platzgummer committed
260 261 262
    // ready for send?
    if (pTileArray->size() > 0 && (this->ready() || this->_userSync())) {

Valentin Platzgummer's avatar
Valentin Platzgummer committed
263
      this->_setState(STATE::USER_SYNC);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
264
      this->_doAction();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
265

Valentin Platzgummer's avatar
Valentin Platzgummer committed
266 267
      // create add tiles command.
      auto pTask = std::make_unique<Task>(
Valentin Platzgummer's avatar
Valentin Platzgummer committed
268 269
          std::bind(&Impl::_callAddTiles, this, pTileArray));

Valentin Platzgummer's avatar
Valentin Platzgummer committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      // dispatch command.
      auto ret = _dispatcher.dispatch(std::move(pTask));
      auto addFuture = ret.share();

      // create get progress cmd.
      pTask = std::make_unique<Task>([this, addFuture, pIdArray] {
        addFuture.wait();
        if (addFuture.get().toBool()) {
          return this->_callGetProgress(pIdArray);
        } else {
          return QVariant(false);
        }
      });

      // dispatch command.
      ret = _dispatcher.dispatch(std::move(pTask));
      auto progressFuture = ret.share();
      _futureWatcher.setFuture(progressFuture);
      return progressFuture;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
289 290 291 292 293 294 295 296 297 298 299 300
    }
  }

  std::promise<QVariant> p;
  p.set_value(QVariant(false));
  return p.get_future();
}

std::shared_future<QVariant>
NemoInterface::Impl::removeTiles(const IDArray &idArray) {
  using namespace nemo_interface;

301
  // qDebug() << "removeTiles called";
302

Valentin Platzgummer's avatar
Valentin Platzgummer committed
303 304 305 306 307 308 309 310 311 312 313 314
  if (idArray.size() > 0) {

    // copy known ids
    auto pIdArray = std::make_shared<IDArray>();
    for (const auto id : idArray) {
      const auto it = this->_localTiles.find(id);
      Q_ASSERT(it != _localTiles.end());
      if (Q_LIKELY(it != _localTiles.end())) {
        _localTiles.erase(it);
        pIdArray->push_back(id);
      } else {
        qCDebug(NemoInterfaceLog) << "removeTiles(): unknown id: " << id << ".";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
315
      }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
316
    }
317 318 319
    if (pIdArray->size() > 0) {
      this->_parent->tilesChanged();
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
320 321 322 323

    // ready for send?
    if (pIdArray->size() > 0 && (this->ready() || this->_userSync())) {

Valentin Platzgummer's avatar
Valentin Platzgummer committed
324
      this->_setState(STATE::USER_SYNC);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
      this->_doAction();

      // create command.
      auto cmd = std::make_unique<Task>(
          std::bind(&Impl::_callRemoveTiles, this, pIdArray));

      // dispatch command and return.
      auto ret = _dispatcher.dispatch(std::move(cmd));
      auto sfut = ret.share();
      _futureWatcher.setFuture(sfut);
      return sfut;
    }
  }

  std::promise<QVariant> p;
  p.set_value(QVariant(false));
  return p.get_future();
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
343

Valentin Platzgummer's avatar
Valentin Platzgummer committed
344 345
std::shared_future<QVariant> NemoInterface::Impl::clearTiles() {
  using namespace nemo_interface;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
346

347
  // qDebug() << "clearTiles called";
348

Valentin Platzgummer's avatar
Valentin Platzgummer committed
349
  // clear local tiles (_localTiles)
350 351 352 353
  if (!_localTiles.empty()) {
    this->_localTiles.clear();
    this->_parent->tilesChanged();
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
354

355
  if (this->ready() || this->_userSync()) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
356

Valentin Platzgummer's avatar
Valentin Platzgummer committed
357
    this->_setState(STATE::USER_SYNC);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
358
    this->_doAction();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
359

Valentin Platzgummer's avatar
Valentin Platzgummer committed
360 361 362
    // create command.
    auto pTask =
        std::make_unique<Task>(std::bind(&Impl::_callClearTiles, this));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
363 364

    // dispatch command and return.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
365 366 367 368
    auto ret = _dispatcher.dispatch(std::move(pTask));
    auto sfut = ret.share();
    _futureWatcher.setFuture(sfut);
    return sfut;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
369 370 371 372 373 374
  } else {
    std::promise<QVariant> p;
    p.set_value(QVariant(false));
    return p.get_future();
  }
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
375

376
TileArray NemoInterface::Impl::getTiles(const IDArray &idArray) const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
377 378
  TileArray tileArray;

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  if (this->ready()) {
    for (const auto &id : idArray) {
      const auto it = _remoteTiles.find(id);
      if (it != _remoteTiles.end()) {
        MeasurementTile copy;
        copy.setId(it->second->id());
        copy.setProgress(it->second->progress());
        copy.setPath(it->second->tile());
        tileArray.append(std::move(copy));
      }
    }
  } else {
    for (const auto &id : idArray) {
      const auto it = _localTiles.find(id);
      if (it != _localTiles.end()) {
        MeasurementTile copy;
        copy.setId(it->second->id());
        copy.setProgress(it->second->progress());
        copy.setPath(it->second->tile());
        tileArray.append(std::move(copy));
      }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
400 401 402 403 404 405
    }
  }

  return tileArray;
}

406
TileArray NemoInterface::Impl::getAllTiles() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
407 408
  TileArray tileArray;

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  if (this->ready()) {
    for (const auto &entry : _remoteTiles) {
      auto pTile = entry.second;
      MeasurementTile copy;
      copy.setId(pTile->id());
      copy.setProgress(pTile->progress());
      copy.setPath(pTile->tile());
      tileArray.append(std::move(copy));
    }

  } else {
    for (const auto &entry : _localTiles) {
      auto pTile = entry.second;
      MeasurementTile copy;
      copy.setId(pTile->id());
      copy.setProgress(pTile->progress());
      copy.setPath(pTile->tile());
      tileArray.append(std::move(copy));
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
428 429 430 431 432
  }

  return tileArray;
}

433
LogicalArray NemoInterface::Impl::containsTiles(const IDArray &idArray) const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
434 435 436
  LogicalArray logicalArray;

  for (const auto &id : idArray) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
437 438
    const auto &it = _localTiles.find(id);
    logicalArray.append(it != _localTiles.end());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
439 440 441 442 443
  }

  return logicalArray;
}

444
std::size_t NemoInterface::Impl::size() const { return _localTiles.size(); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
445

446
bool NemoInterface::Impl::empty() const { return _localTiles.empty(); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
447

448
ProgressArray NemoInterface::Impl::getProgress() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
449 450
  ProgressArray progressArray;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
451 452 453 454 455 456 457 458 459 460
  if (this->_isSynchronized()) {
    for (const auto &entry : _remoteTiles) {
      progressArray.append(
          LabeledProgress{entry.second->progress(), entry.second->id()});
    }
  } else {
    for (const auto &entry : _localTiles) {
      progressArray.append(
          LabeledProgress{entry.second->progress(), entry.second->id()});
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
461 462 463 464 465
  }

  return progressArray;
}

466
ProgressArray NemoInterface::Impl::getProgress(const IDArray &idArray) const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
467 468
  ProgressArray progressArray;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  if (this->_isSynchronized()) {
    for (const auto &id : idArray) {
      const auto it = _remoteTiles.find(id);
      if (it != _remoteTiles.end()) {
        progressArray.append(
            LabeledProgress{it->second->progress(), it->second->id()});
      }
    }
  } else {
    for (const auto &id : idArray) {
      const auto it = _localTiles.find(id);
      if (it != _localTiles.end()) {
        progressArray.append(
            LabeledProgress{it->second->progress(), it->second->id()});
      }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
484 485 486 487
    }
  }

  return progressArray;
488 489
}

490
NemoInterface::STATUS NemoInterface::Impl::status() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
491
  return _status(this->_state);
492 493
}

494
bool NemoInterface::Impl::running() const { return _running(this->_state); }
495

496
bool NemoInterface::Impl::ready() const { return _ready(this->_state.load()); }
497

498
bool NemoInterface::Impl::_sysSync() const { return _sysSync(this->_state); }
499

Valentin Platzgummer's avatar
Valentin Platzgummer committed
500
void NemoInterface::Impl::_onFutureWatcherFinished() {
501
  if (this->ready() || this->_userSync() || this->_sysSync()) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
502 503
    auto lastTransactionSuccessfull = _futureWatcher.result().toBool();
    if (!lastTransactionSuccessfull) {
504 505 506
      qCDebug(NemoInterfaceLog)
          << "last transaction unsuccessfull: " << _toString(_lastCall);
      QTimer::singleShot(5000, [this] { this->_trySynchronize(); });
Valentin Platzgummer's avatar
Valentin Platzgummer committed
507
    }
508
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
509 510
}

511 512 513 514 515
void NemoInterface::Impl::_onHeartbeatTimeout() {
  this->_setState(STATE::HEARTBEAT_TIMEOUT);
  this->_doAction();
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
void NemoInterface::Impl::_onRosbridgeStateChanged() {
  auto state = this->_pRosbridge->state();
  if (state == Rosbridge::STATE::CONNECTED) {
    if (this->_state == STATE::START_BRIDGE ||
        this->_state == STATE::WEBSOCKET_TIMEOUT) {
      this->_setState(STATE::WEBSOCKET_DETECTED);
      this->_doAction();
    }
  } else if (state == Rosbridge::STATE::TIMEOUT) {
    if (this->_state == STATE::TRY_TOPIC_SERVICE_SETUP ||
        this->_state == STATE::READY ||
        this->_state == STATE::WEBSOCKET_DETECTED ||
        this->_state == STATE::HEARTBEAT_TIMEOUT) {
      this->_setState(STATE::WEBSOCKET_TIMEOUT);
      this->_doAction();
    }
  }
}

535
bool NemoInterface::Impl::_userSync() const { return _userSync(this->_state); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
536

537
const QString &NemoInterface::Impl::infoString() const { return _infoString; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
538

539 540 541
const QString &NemoInterface::Impl::warningString() const {
  return _warningString;
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
542

543 544
void NemoInterface::Impl::_updateProgress(std::shared_ptr<ProgressArray> pArray,
                                          std::promise<bool> promise) {
545
  // qDebug() << "_updateProgress called";
546 547

  bool error = false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
548
  for (auto itLP = pArray->begin(); itLP != pArray->end();) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
549

Valentin Platzgummer's avatar
Valentin Platzgummer committed
550
    auto it = _remoteTiles.find(itLP->id());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
551

Valentin Platzgummer's avatar
Valentin Platzgummer committed
552 553 554
    if (Q_LIKELY(it != _remoteTiles.end())) {
      it->second->setProgress(itLP->progress());
      ++itLP;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
555 556
    } else {
      qCDebug(NemoInterfaceLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
557 558
          << "_updateProgress(): tile with id " << itLP->id() << " not found.";
      itLP = pArray->erase(itLP);
559
      error = true;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
560 561 562
    }
  }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
563 564 565
  if (pArray->size() > 0) {
    emit _parent->progressChanged(*pArray);
  }
566 567

  promise.set_value(!error);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
568 569
}

570 571
void NemoInterface::Impl::_onHeartbeatReceived(const QNemoHeartbeat &hb,
                                               std::promise<bool> promise) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
572 573 574 575 576 577 578 579
  _lastHeartbeat = hb;
  this->_timeoutTimer.start(NO_HEARTBEAT_TIMEOUT);
  if (this->_state == STATE::TRY_TOPIC_SERVICE_SETUP) {
    this->_setState(STATE::READY);
    this->_doAction();
  } else if (this->_state == STATE::HEARTBEAT_TIMEOUT) {
    this->_setState(STATE::READY);
    this->_doAction();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
580
  }
581
  promise.set_value(true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
}

void NemoInterface::Impl::_setInfoString(const QString &info) {
  if (_infoString != info) {
    _infoString = info;
    emit this->_parent->infoStringChanged();
  }
}

void NemoInterface::Impl::_setWarningString(const QString &warning) {
  if (_warningString != warning) {
    _warningString = warning;
    emit this->_parent->warningStringChanged();
  }
}

void NemoInterface::Impl::_doTopicServiceSetup() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
599
  using namespace ros_bridge::messages;
600 601

  // Subscribe nemo progress.
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  this->_pRosbridge->subscribeTopic(progressTopic, [this](
                                                       const QJsonObject &o) {
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);

    // parse in_message
    rapidjson::Document d;
    d.Parse(msg.toUtf8());
    if (!d.HasParseError()) {
      if (d.HasMember("msg") && d["msg"].IsObject()) {

        // create obj from json
        nemo_msgs::progress_array::ProgressArray progressArray;
        if (nemo_msgs::progress_array::fromJson(d["msg"], progressArray)) {

          // correct range errors of progress
          for (auto &lp : progressArray.progress_array()) {
            bool rangeError = false;
            if (lp.progress() < 0) {
              lp.setProgress(0);
              rangeError = true;
            }
            if (lp.progress() > 100) {
              lp.setProgress(100);
              rangeError = true;
            }

            if (rangeError) {
              qCWarning(NemoInterfaceLog) << "/nemo/progress progress out "
                                             "of range, value was set to: "
                                          << lp.progress();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
632 633
            }
          }
634 635 636 637 638 639 640 641 642 643 644

          auto p = std::make_shared<ProgressArray>();
          *p = std::move(progressArray.progress_array());
          std::promise<bool> promise;
          auto future = promise.get_future();
          bool value = QMetaObject::invokeMethod(
              this->_parent, [this, p, promise = std::move(promise)]() mutable {
                this->_updateProgress(p, std::move(promise));
              });
          Q_ASSERT(value == true);
          future.wait();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
645
        } else {
646 647 648
          qCWarning(NemoInterfaceLog) << "/nemo/progress not able to "
                                         "create ProgressArray form json: "
                                      << msg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
649
        }
650 651 652 653 654 655 656 657 658 659
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/progress no \"msg\" key or wrong type: " << msg;
      }
    } else {
      qCWarning(NemoInterfaceLog) << "/nemo/progress message parse error ("
                                  << d.GetParseError() << "): " << msg;
    }
  });

Valentin Platzgummer's avatar
Valentin Platzgummer committed
660
  // Subscribe heartbeat msg.
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  this->_pRosbridge->subscribeTopic(heartbeatTopic, [this](
                                                        const QJsonObject &o) {
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);

    // parse in_message
    rapidjson::Document d;
    d.Parse(msg.toUtf8());
    if (!d.HasParseError()) {
      if (d.HasMember("msg") && d["msg"].IsObject()) {

        // create obj from json
        nemo_msgs::heartbeat::Heartbeat heartbeat;
        if (nemo_msgs::heartbeat::fromJson(d["msg"], heartbeat)) {
          std::promise<bool> promise;
          auto future = promise.get_future();
          bool value = QMetaObject::invokeMethod(
              this->_parent,
              [this, heartbeat, promise = std::move(promise)]() mutable {
                this->_onHeartbeatReceived(heartbeat, std::move(promise));
              });
          Q_ASSERT(value == true);
          future.wait();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
683
        } else {
684 685 686
          qCWarning(NemoInterfaceLog) << "/nemo/heartbeat not able to "
                                         "create Heartbeat form json: "
                                      << msg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
687
        }
688 689 690 691 692 693 694 695 696
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/heartbeat no \"msg\" key or wrong type: " << msg;
      }
    } else {
      qCWarning(NemoInterfaceLog) << "/nemo/heartbeat message parse error ("
                                  << d.GetParseError() << "): " << msg;
    }
  });
697 698
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
699
void NemoInterface::Impl::_trySynchronize() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
700 701 702
  if ((this->_state == STATE::READY || this->_state == STATE::SYS_SYNC ||
       this->_state == STATE::USER_SYNC) &&
      !_isSynchronized()) {
703 704 705

    qCWarning(NemoInterfaceLog) << "trying to synchronize";

Valentin Platzgummer's avatar
Valentin Platzgummer committed
706
    this->_setState(STATE::SYS_SYNC);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
707 708 709 710 711 712 713
    this->_doAction();

    // create clear cmd.
    auto pTask = std::make_unique<nemo_interface::Task>(
        std::bind(&Impl::_callClearTiles, this));

    // dispatch command.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
714 715
    _dispatcher.clear();
    _dispatcher.stop();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
716 717
    Q_ASSERT(_dispatcher.pendingTasks() == 0);
    auto ret = _dispatcher.dispatch(std::move(pTask));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
718
    auto clearFuture = ret.share();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
719 720 721 722 723 724 725 726

    // create tile array.
    auto pTileArray = std::make_shared<QVector<std::shared_ptr<const Tile>>>();
    for (auto pair : _localTiles) {
      pTileArray->push_back(pair.second);
    }

    // create addTiles cmd.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    pTask =
        std::make_unique<nemo_interface::Task>([this, pTileArray, clearFuture] {
          clearFuture.wait();
          if (clearFuture.get().toBool()) {
            return this->_callAddTiles(pTileArray);
          } else {
            return QVariant(false);
          }
        });

    // dispatch command.
    ret = _dispatcher.dispatch(std::move(pTask));
    auto addFuture = ret.share();

    // create GetAllProgress cmd.
    pTask = std::make_unique<nemo_interface::Task>([this, addFuture] {
      addFuture.wait();
      if (addFuture.get().toBool()) {
        return this->_callGetAllProgress();
      } else {
        return QVariant(false);
      }
    });
Valentin Platzgummer's avatar
Valentin Platzgummer committed
750 751

    // dispatch command.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
752
    ret = _dispatcher.dispatch(std::move(pTask));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
753 754 755 756
    _futureWatcher.setFuture(ret.share());
  }
}

757
bool NemoInterface::Impl::_isSynchronized() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
758 759 760 761 762 763
  return _localTiles.size() > 0 && _remoteTiles.size() > 0 &&
         std::equal(
             _localTiles.begin(), _localTiles.end(), _remoteTiles.begin(),
             [](const auto &a, const auto &b) { return a.first == b.first; });
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
764
void NemoInterface::Impl::_doAction() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
765
  static bool resetDone = false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
766 767
  switch (this->_state) {
  case STATE::STOPPED:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
768
    this->_timeoutTimer.stop();
769
    this->_clearTilesRemote(std::promise<bool>());
770 771
    if (this->_pRosbridge->state() != Rosbridge::STATE::STOPPED) {
      this->_pRosbridge->stop();
772
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
773
    break;
774

Valentin Platzgummer's avatar
Valentin Platzgummer committed
775
  case STATE::START_BRIDGE:
776
    this->_pRosbridge->start();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
777
    break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
778 779
    break;
  case STATE::WEBSOCKET_DETECTED:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
780
    resetDone = false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
781 782 783 784 785
    this->_setState(STATE::TRY_TOPIC_SERVICE_SETUP);
    this->_doAction();
    break;
  case STATE::TRY_TOPIC_SERVICE_SETUP:
    this->_doTopicServiceSetup();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
786
    this->_timeoutTimer.start(NO_HEARTBEAT_TIMEOUT);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
787 788
    break;
  case STATE::READY:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
789 790
    _trySynchronize();
    break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
791 792
  case STATE::USER_SYNC:
  case STATE::SYS_SYNC:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
793 794
    break;
  case STATE::HEARTBEAT_TIMEOUT:
795
    this->_clearTilesRemote(std::promise<bool>());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
796 797 798 799
    break;
  case STATE::WEBSOCKET_TIMEOUT:
    if (!resetDone) {
      resetDone = true;
800 801
      this->_pRosbridge->stop();
      this->_pRosbridge->start();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
802
    }
803 804
    this->_timeoutTimer.stop();
    this->_clearTilesRemote(std::promise<bool>());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
805 806
    break;
  };
807 808
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
809 810
QVariant NemoInterface::Impl::_callAddTiles(
    std::shared_ptr<QVector<std::shared_ptr<const Tile>>> pTileArray) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
811

812
  // qDebug() << "_callAddTiles called";
813

Valentin Platzgummer's avatar
Valentin Platzgummer committed
814 815
  this->_lastCall = CALL_NAME::ADD_TILES;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
  // create json object
  rapidjson::Document request(rapidjson::kObjectType);
  auto &allocator = request.GetAllocator();
  rapidjson::Value jsonTileArray(rapidjson::kArrayType);
  for (const auto &tile : *pTileArray) {
    using namespace ros_bridge::messages;
    rapidjson::Value jsonTile(rapidjson::kObjectType);
    if (!nemo_msgs::tile::toJson(*tile, jsonTile, allocator)) {
      qCDebug(NemoInterfaceLog)
          << "addTiles(): not able to create json object: tile id: "
          << tile->id() << " progress: " << tile->progress()
          << " points: " << tile->tile();
    }
    jsonTileArray.PushBack(jsonTile, allocator);
  } // for
831

Valentin Platzgummer's avatar
Valentin Platzgummer committed
832 833 834
  rapidjson::Value tileKey("in_tile_array");
  request.AddMember(tileKey, jsonTileArray, allocator);

835 836 837 838 839
  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  request.Accept(writer);
  QJsonDocument req = QJsonDocument::fromJson(buffer.GetString());

Valentin Platzgummer's avatar
Valentin Platzgummer committed
840 841 842
  // create response handler.
  auto promise_response = std::make_shared<std::promise<bool>>();
  auto future_response = promise_response->get_future();
843 844 845 846 847 848 849 850 851 852
  auto responseHandler = [promise_response](const QJsonObject &o) mutable {
    // check if transaction was successfull
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);
    rapidjson::Document d;
    d.Parse(msg.toUtf8());
    if (!d.HasParseError()) {
      if (d.HasMember("values") && d["values"].IsObject()) {
        auto values = d["values"].GetObject();
        if (values.HasMember("success") && values["success"].IsBool()) {
          promise_response->set_value(values["success"].GetBool());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
853 854
        } else {
          qCWarning(NemoInterfaceLog)
855
              << "/nemo/add_tiles no \"success\" key or wrong type: " << msg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
856 857
          promise_response->set_value(false);
        }
858 859 860 861 862 863 864 865 866 867 868
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/add_tiles no \"values\" key or wrong type: " << msg;
        promise_response->set_value(false);
      }
    } else {
      qCWarning(NemoInterfaceLog) << "/nemo/add_tiles message parse error ("
                                  << d.GetParseError() << "): " << msg;
      promise_response->set_value(false);
    }
  };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
869 870

  // call service.
871 872
  this->_pRosbridge->callService("/nemo/add_tiles", responseHandler,
                                 req.object());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
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

  // wait for response.
  auto tStart = hrc::now();
  bool abort = true;
  do {
    auto status = future_response.wait_for(std::chrono::milliseconds(100));
    if (status == std::future_status::ready) {
      abort = false;
      break;
    }
  } while (hrc::now() - tStart < maxResponseTime ||
           this->_dispatcher.isInterruptionRequested());

  if (abort) {
    qCWarning(NemoInterfaceLog)
        << "addTiles(): Websocket not responding to request.";
    return QVariant(false);
  }

  // transaction error?
  if (!future_response.get()) {
    return QVariant(false);
  }

  // add remote tiles (_remoteTiles)
898 899 900 901 902 903 904 905
  std::promise<bool> promise;
  auto future = promise.get_future();
  bool value = QMetaObject::invokeMethod(
      this->_parent /* context */,
      [this, pTileArray, promise = std::move(promise)]() mutable {
        this->_addTilesRemote(pTileArray, std::move(promise));
      });
  Q_ASSERT(value == true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
906 907

  // return success
908
  return QVariant(future.get());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
909 910 911 912
}

QVariant
NemoInterface::Impl::_callRemoveTiles(std::shared_ptr<IDArray> pIdArray) {
913
  // qDebug() << "_callRemoveTiles called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
914 915 916

  this->_lastCall = CALL_NAME::REMOVE_TILES;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
917 918 919 920 921 922 923 924 925 926 927
  // create json object
  rapidjson::Document request(rapidjson::kObjectType);
  auto &allocator = request.GetAllocator();
  rapidjson::Value jsonIdArray(rapidjson::kArrayType);
  for (const auto id : *pIdArray) {
    using namespace ros_bridge::messages;
    jsonIdArray.PushBack(rapidjson::Value(id), allocator);
  } // for
  rapidjson::Value tileKey("ids");
  request.AddMember(tileKey, jsonIdArray, allocator);

928 929 930 931 932
  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  request.Accept(writer);
  QJsonDocument req = QJsonDocument::fromJson(buffer.GetString());

Valentin Platzgummer's avatar
Valentin Platzgummer committed
933 934 935
  // create response handler.
  auto promise_response = std::make_shared<std::promise<bool>>();
  auto future_response = promise_response->get_future();
936 937 938 939 940 941 942 943 944 945
  auto responseHandler = [promise_response](const QJsonObject &o) mutable {
    // check if transaction was successfull
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);
    rapidjson::Document d;
    d.Parse(msg.toUtf8());
    if (!d.HasParseError()) {
      if (d.HasMember("values") && d["values"].IsObject()) {
        auto values = d["values"].GetObject();
        if (values.HasMember("success") && values["success"].IsBool()) {
          promise_response->set_value(values["success"].GetBool());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
946 947
        } else {
          qCWarning(NemoInterfaceLog)
948
              << "/nemo/remove_tiles no \"success\" key or wrong type: " << msg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
949 950
          promise_response->set_value(false);
        }
951 952 953 954 955 956 957 958 959 960 961
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/remove_tiles no \"values\" key or wrong type: " << msg;
        promise_response->set_value(false);
      }
    } else {
      qCWarning(NemoInterfaceLog) << "/nemo/remove_tiles message parse error ("
                                  << d.GetParseError() << "): " << msg;
      promise_response->set_value(false);
    }
  };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
962 963

  // call service.
964 965
  this->_pRosbridge->callService("/nemo/remove_tiles", responseHandler,
                                 req.object());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

  // wait for response.
  auto tStart = hrc::now();
  bool abort = true;
  do {
    auto status = future_response.wait_for(std::chrono::milliseconds(100));
    if (status == std::future_status::ready) {
      abort = false;
      break;
    }
  } while (hrc::now() - tStart < maxResponseTime ||
           this->_dispatcher.isInterruptionRequested());

  if (abort) {
    qCWarning(NemoInterfaceLog)
        << "remove_tiles(): Websocket not responding to request.";
    return QVariant(false);
  }

  // transaction error?
  if (!future_response.get()) {
    return QVariant(false);
  }

  // remove remote tiles (_remoteTiles)
991 992 993 994 995 996 997 998
  std::promise<bool> promise;
  auto future = promise.get_future();
  bool value = QMetaObject::invokeMethod(
      this->_parent /* context */,
      [this, pIdArray, promise = std::move(promise)]() mutable {
        this->_removeTilesRemote(pIdArray, std::move(promise));
      });
  Q_ASSERT(value == true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
999 1000

  // return success
1001
  return QVariant(future.get());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1002 1003 1004
}

QVariant NemoInterface::Impl::_callClearTiles() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1005

1006
  // qDebug() << "_callClearTiles called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1007 1008
  this->_lastCall = CALL_NAME::CLEAR_TILES;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1009 1010 1011
  // create response handler.
  auto promise_response = std::make_shared<std::promise<bool>>();
  auto future_response = promise_response->get_future();
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  auto responseHandler = [promise_response](const QJsonObject &o) mutable {
    // check if transaction was successfull
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);
    rapidjson::Document d;
    d.Parse(msg.toUtf8());
    if (!d.HasParseError()) {
      if (d.HasMember("result") && d["result"].IsBool()) {
        promise_response->set_value(d["result"].GetBool());
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/clear_tiles no \"result\" key or wrong type: " << msg;
        promise_response->set_value(false);
      }
    } else {
      qCWarning(NemoInterfaceLog) << "/nemo/clear_tiles message parse error ("
                                  << d.GetParseError() << "): " << msg;
      promise_response->set_value(false);
    }
  };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1031 1032

  // call service.
1033 1034
  this->_pRosbridge->callService("/nemo/clear_tiles", responseHandler,
                                 QJsonObject());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

  // wait for response.
  auto tStart = hrc::now();
  bool abort = true;
  do {
    auto status = future_response.wait_for(std::chrono::milliseconds(100));
    if (status == std::future_status::ready) {
      abort = false;
      break;
    }
  } while (hrc::now() - tStart < maxResponseTime ||
           this->_dispatcher.isInterruptionRequested());

  if (abort) {
    qCWarning(NemoInterfaceLog) << "Websocket not responding to request.";
    return QVariant(false);
  }

  // transaction failed?
  if (!future_response.get()) {
    return QVariant(false);
  }

  // clear remote tiles (_remoteTiles)
1059 1060 1061 1062 1063 1064 1065
  std::promise<bool> promise;
  auto future = promise.get_future();
  bool value = QMetaObject::invokeMethod(
      this->_parent, [this, promise = std::move(promise)]() mutable {
        this->_clearTilesRemote(std::move(promise));
      });
  Q_ASSERT(value == true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1066 1067

  // return success
1068
  return QVariant(future.get());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1069 1070 1071 1072
}

QVariant
NemoInterface::Impl::_callGetProgress(std::shared_ptr<IDArray> pIdArray) {
1073
  // qDebug() << "_callGetProgress called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1074 1075 1076

  this->_lastCall = CALL_NAME::GET_PROGRESS;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  // create json object
  rapidjson::Document request(rapidjson::kObjectType);
  auto &allocator = request.GetAllocator();
  rapidjson::Value jsonIdArray(rapidjson::kArrayType);
  for (const auto id : *pIdArray) {
    using namespace ros_bridge::messages;
    jsonIdArray.PushBack(rapidjson::Value(id), allocator);
  } // for
  rapidjson::Value tileKey("ids");
  request.AddMember(tileKey, jsonIdArray, allocator);

1088 1089 1090 1091 1092
  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  request.Accept(writer);
  QJsonDocument req = QJsonDocument::fromJson(buffer.GetString());

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1093 1094 1095 1096
  // create response handler.
  typedef std::shared_ptr<ProgressArray> ResponseType;
  auto promise_response = std::make_shared<std::promise<ResponseType>>();
  auto future_response = promise_response->get_future();
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  auto responseHandler = [promise_response](const QJsonObject &o) mutable {
    // check if transaction was successfull
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);
    rapidjson::Document d;
    d.Parse(msg.toUtf8());
    if (!d.HasParseError()) {
      if (d.HasMember("values") && d["values"].IsObject()) {
        auto values = d["values"].GetObject();
        ros_bridge::messages::nemo_msgs::progress_array::ProgressArray
            progressArrayMsg;
        if (ros_bridge::messages::nemo_msgs::progress_array::fromJson(
                d["values"], progressArrayMsg)) {
          auto pArray = std::make_shared<ProgressArray>();
          *pArray = std::move(progressArrayMsg.progress_array());
          promise_response->set_value(pArray);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1112 1113
        } else {
          qCWarning(NemoInterfaceLog)
1114 1115
              << "/nemo/get_progress error while creating ProgressArray "
                 "from json.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1116 1117
          promise_response->set_value(nullptr);
        }
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/get_progress no \"values\" key or wrong type: " << msg;
        promise_response->set_value(nullptr);
      }
    } else {
      qCWarning(NemoInterfaceLog) << "/nemo/get_progress message parse error ("
                                  << d.GetParseError() << "): " << msg;
      promise_response->set_value(nullptr);
    }
  };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1129 1130

  // call service.
1131 1132
  this->_pRosbridge->callService("/nemo/get_progress", responseHandler,
                                 req.object());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

  // wait for response.
  auto tStart = hrc::now();
  bool abort = true;
  do {
    auto status = future_response.wait_for(std::chrono::milliseconds(100));
    if (status == std::future_status::ready) {
      abort = false;
      break;
    }
  } while (hrc::now() - tStart < maxResponseTime ||
           this->_dispatcher.isInterruptionRequested());

  if (abort) {
    qCWarning(NemoInterfaceLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
        << "all_remove_tiles(): Websocket not responding to request.";
    return QVariant(false);
  }

  // transaction error?
  auto pArray = future_response.get();
  if (pArray == nullptr) {
    return QVariant(false);
  }

  // remove remote tiles (_remoteTiles)
1159 1160 1161 1162 1163 1164 1165
  std::promise<bool> promise;
  auto future = promise.get_future();
  bool value = QMetaObject::invokeMethod(
      this->_parent, [this, pArray, promise = std::move(promise)]() mutable {
        this->_updateProgress(pArray, std::move(promise));
      });
  Q_ASSERT(value == true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1166 1167

  // return success
1168
  return QVariant(future.get());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1169 1170 1171
}

QVariant NemoInterface::Impl::_callGetAllProgress() {
1172
  // qDebug() << "_callGetAllProgress called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1173 1174 1175 1176 1177 1178 1179

  this->_lastCall = CALL_NAME::GET_ALL_PROGRESS;

  // create response handler.
  typedef std::shared_ptr<ProgressArray> ResponseType;
  auto promise_response = std::make_shared<std::promise<ResponseType>>();
  auto future_response = promise_response->get_future();
1180
  auto responseHandler = [promise_response](const QJsonObject &o) mutable {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1181
    // check if transaction was successfull
1182
    QString msg = QJsonDocument(o).toJson(QJsonDocument::JsonFormat::Compact);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1183
    rapidjson::Document d;
1184
    d.Parse(msg.toUtf8());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    if (!d.HasParseError()) {
      if (d.HasMember("values") && d["values"].IsObject()) {
        ros_bridge::messages::nemo_msgs::progress_array::ProgressArray
            progressArrayMsg;
        if (ros_bridge::messages::nemo_msgs::progress_array::fromJson(
                d["values"], progressArrayMsg)) {
          auto pArray = std::make_shared<ProgressArray>();
          *pArray = std::move(progressArrayMsg.progress_array());
          promise_response->set_value(pArray);
        } else {
          qCWarning(NemoInterfaceLog)
              << "/nemo/all_get_progress error while creating ProgressArray "
                 "from json.";
          promise_response->set_value(nullptr);
        }
      } else {
        qCWarning(NemoInterfaceLog)
            << "/nemo/all_get_progress no \"values\" key or wrong type: "
1203
            << msg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1204 1205 1206 1207 1208
        promise_response->set_value(nullptr);
      }
    } else {
      qCWarning(NemoInterfaceLog)
          << "/nemo/all_get_progress message parse error (" << d.GetParseError()
1209
          << "): " << msg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1210 1211 1212 1213 1214
      promise_response->set_value(nullptr);
    }
  };

  // call service.
1215 1216
  this->_pRosbridge->callService("/nemo/get_all_progress", responseHandler,
                                 QJsonObject());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

  // wait for response.
  auto tStart = hrc::now();
  bool abort = true;
  do {
    auto status = future_response.wait_for(std::chrono::milliseconds(100));
    if (status == std::future_status::ready) {
      abort = false;
      break;
    }
  } while (hrc::now() - tStart < maxResponseTime ||
           this->_dispatcher.isInterruptionRequested());

  if (abort) {
    qCWarning(NemoInterfaceLog)
        << "all_remove_tiles(): Websocket not responding to request.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    return QVariant(false);
  }

  // transaction error?
  auto pArray = future_response.get();
  if (pArray == nullptr) {
    return QVariant(false);
  }

  // remove remote tiles (_remoteTiles)
1243 1244 1245 1246 1247 1248 1249
  std::promise<bool> promise;
  auto future = promise.get_future();
  bool value = QMetaObject::invokeMethod(
      this->_parent, [this, pArray, promise = std::move(promise)]() mutable {
        this->_updateProgress(pArray, std::move(promise));
      });
  Q_ASSERT(value == true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1250 1251

  // return success
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
  return QVariant(future.get());
}

QString NemoInterface::Impl::_toString(NemoInterface::Impl::CALL_NAME name) {
  switch (name) {

  case CALL_NAME::ADD_TILES:
    return QString("ADD_TILES");
  case CALL_NAME::REMOVE_TILES:
    return QString("REMOVE_TILES");
  case CALL_NAME::CLEAR_TILES:
    return QString("CLEAR_TILES");
  case CALL_NAME::GET_PROGRESS:
    return QString("GET_PROGRESS");
  case CALL_NAME::GET_ALL_PROGRESS:
    return QString("GET_ALL_PROGRESS");
  }
  return QString("unknown CALL_NAME");
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1270 1271 1272
}

void NemoInterface::Impl::_addTilesRemote(
1273 1274 1275
    std::shared_ptr<QVector<std::shared_ptr<const Tile>>> pTileArray,
    std::promise<bool> promise) {

1276
  // qDebug() << "_addTilesRemote called";
1277

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1278 1279 1280 1281
  auto pArrayDup = std::make_shared<QVector<std::shared_ptr<Tile>>>();
  for (auto pTile : *pTileArray) {
    pArrayDup->push_back(std::make_shared<Tile>(*pTile));
  }
1282
  _addTilesRemote2(pArrayDup, std::move(promise));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1283 1284
}

1285 1286 1287 1288
void NemoInterface::Impl::_addTilesRemote2(
    std::shared_ptr<QVector<std::shared_ptr<Tile>>> pTileArray,
    std::promise<bool> promise) {

1289
  // qDebug() << "_addTilesRemote2 called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1290 1291

  bool anyChange = false;
1292
  bool error = false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

  for (auto pTile : *pTileArray) {
    auto id = pTile->id();
    auto it = _remoteTiles.find(id);
    if (Q_LIKELY(it == _remoteTiles.end())) {
      auto ret = _remoteTiles.insert(std::make_pair(id, pTile));
      Q_ASSERT(ret.second == true);
      Q_UNUSED(ret);
      anyChange = true;
    } else {
      qCWarning(NemoInterfaceLog)
          << "_addTilesRemote: tile with id " << id << " already added.";
1305 1306 1307
      if (pTile->tile() != it->second->tile()) {
        error = true;
      }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1308 1309 1310 1311 1312 1313 1314 1315 1316
    }
  }

  if (anyChange) {
    if (this->_isSynchronized()) {
      this->_setState(STATE::READY);
      this->_doAction();
    }
  }
1317 1318

  promise.set_value(!error);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1319 1320
}

1321 1322
void NemoInterface::Impl::_removeTilesRemote(std::shared_ptr<IDArray> idArray,
                                             std::promise<bool> promise) {
1323
  // qDebug() << "_removeTilesRemote called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
  bool anyChange = false;

  for (const auto id : *idArray) {
    auto it = _remoteTiles.find(id);
    if (Q_LIKELY(it != _remoteTiles.end())) {
      _remoteTiles.erase(it);
      anyChange = true;
    } else {
      qCWarning(NemoInterfaceLog)
          << "_removeTilesRemote: tile with unknown id " << id << ".";
    }
  }

  if (anyChange) {
    if (this->_isSynchronized()) {
      this->_setState(STATE::READY);
      this->_doAction();
    }
  }
1343 1344

  promise.set_value(true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1345 1346
}

1347
void NemoInterface::Impl::_clearTilesRemote(std::promise<bool> promise) {
1348
  // qDebug() << "_clearTilesRemote called";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1349 1350 1351 1352 1353 1354 1355
  if (_remoteTiles.size() > 0) {
    _remoteTiles.clear();
    if (this->_isSynchronized()) {
      this->_setState(STATE::READY);
      this->_doAction();
    }
  }
1356
  promise.set_value(true);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
}

bool NemoInterface::Impl::_setState(STATE newState) {
  if (newState != this->_state) {
    auto oldState = this->_state.load();
    this->_state = newState;

    qCDebug(NemoInterfaceLog)
        << "state: " << _toString(oldState) << " -> " << _toString(newState);
    auto oldStatus = _status(oldState);
    auto newStatus = _status(newState);
    if (oldStatus != newStatus) {
      emit this->_parent->statusChanged();
    }

    if (_running(oldState) != _running(newState)) {
      emit this->_parent->runningChanged();
    }

1376 1377 1378 1379 1380 1381
    return true;
  } else {
    return false;
  }
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1382 1383 1384 1385
bool NemoInterface::Impl::_ready(NemoInterface::Impl::STATE s) {
  return s == STATE::READY;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1386
bool NemoInterface::Impl::_userSync(NemoInterface::Impl::STATE s) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1387
  return s == STATE::USER_SYNC;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1388 1389 1390
}

bool NemoInterface::Impl::_sysSync(NemoInterface::Impl::STATE s) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1391
  return s == STATE::SYS_SYNC;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1392 1393
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1394 1395 1396 1397
bool NemoInterface::Impl::_running(NemoInterface::Impl::STATE s) {
  return s != STATE::STOPPED;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
NemoInterface::STATUS
NemoInterface::Impl::_status(NemoInterface::Impl::STATE state) {
  NemoInterface::STATUS status;
  switch (state) {
  case STATE::STOPPED:
    status = NemoInterface::STATUS::NOT_CONNECTED;
    break;
  case STATE::START_BRIDGE:
    status = NemoInterface::STATUS::NOT_CONNECTED;
    break;
  case STATE::WEBSOCKET_DETECTED:
    status = NemoInterface::STATUS::WEBSOCKET_DETECTED;
    break;
  case STATE::TRY_TOPIC_SERVICE_SETUP:
    status = NemoInterface::STATUS::WEBSOCKET_DETECTED;
    break;
  case STATE::READY:
    status = NemoInterface::STATUS::READY;
    break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1417 1418
  case STATE::USER_SYNC:
  case STATE::SYS_SYNC:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
    status = NemoInterface::STATUS::SYNC;
    break;
  case STATE::WEBSOCKET_TIMEOUT:
  case STATE::HEARTBEAT_TIMEOUT:
    status = NemoInterface::STATUS::TIMEOUT;
    break;
  }

  return status;
}

QString NemoInterface::Impl::_toString(NemoInterface::Impl::STATE s) {
  switch (s) {
  case STATE::STOPPED:
    return QString("STOPPED");
  case STATE::START_BRIDGE:
    return QString("START_BRIDGE");
  case STATE::WEBSOCKET_DETECTED:
    return QString("WEBSOCKET_DETECTED");
  case STATE::TRY_TOPIC_SERVICE_SETUP:
    return QString("TRY_TOPIC_SERVICE_SETUP");
  case STATE::READY:
    return QString("READY");
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1442
  case STATE::USER_SYNC:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1443
    return QString("SYNC_USER");
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1444
  case STATE::SYS_SYNC:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
    return QString("SYNC_SYS");
  case STATE::WEBSOCKET_TIMEOUT:
    return QString("WEBSOCKET_TIMEOUT");
  case STATE::HEARTBEAT_TIMEOUT:
    return QString("HEARTBEAT_TIMEOUT");
  }
  return "unknown state!";
}

QString NemoInterface::Impl::_toString(NemoInterface::STATUS s) {
  switch (s) {
  case NemoInterface::STATUS::NOT_CONNECTED:
    return QString("NOT_CONNECTED");
  case NemoInterface::STATUS::READY:
    return QString("READY");
  case NemoInterface::STATUS::TIMEOUT:
    return QString("TIMEOUT");
  case NemoInterface::STATUS::WEBSOCKET_DETECTED:
    return QString("WEBSOCKET_DETECTED");
  case NemoInterface::STATUS::SYNC:
    return QString("SYNC");
  }
  return "unknown state!";
}

1470 1471
// ===============================================================
// NemoInterface
1472 1473 1474 1475 1476 1477 1478 1479 1480
NemoInterface::NemoInterface()
    : QObject(), pImpl(std::make_unique<NemoInterface::Impl>(this)) {}

NemoInterface *NemoInterface::createInstance() { return new NemoInterface(); }

NemoInterface *NemoInterface::instance() {
  return GenericSingelton<NemoInterface>::instance(
      NemoInterface::createInstance);
}
1481 1482 1483 1484 1485 1486 1487

NemoInterface::~NemoInterface() {}

void NemoInterface::start() { this->pImpl->start(); }

void NemoInterface::stop() { this->pImpl->stop(); }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1488 1489 1490 1491 1492 1493 1494
std::shared_future<QVariant>
NemoInterface::addTiles(const TileArray &tileArray) {
  TilePtrArray ptrArray;
  for (const auto &tile : tileArray) {
    ptrArray.push_back(const_cast<MeasurementTile *>(&tile));
  }
  return this->pImpl->addTiles(ptrArray);
1495 1496
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1497 1498 1499
std::shared_future<QVariant>
NemoInterface::addTiles(const TilePtrArray &tileArray) {
  return this->pImpl->addTiles(tileArray);
1500 1501
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1502 1503 1504 1505 1506 1507 1508 1509
std::shared_future<QVariant>
NemoInterface::removeTiles(const IDArray &idArray) {
  return this->pImpl->removeTiles(idArray);
}

std::shared_future<QVariant> NemoInterface::clearTiles() {
  return this->pImpl->clearTiles();
}
1510

1511
TileArray NemoInterface::getTiles(const IDArray &idArray) const {
1512 1513 1514
  return this->pImpl->getTiles(idArray);
}

1515 1516 1517
TileArray NemoInterface::getAllTiles() const {
  return this->pImpl->getAllTiles();
}
1518

1519
LogicalArray NemoInterface::containsTiles(const IDArray &idArray) const {
1520 1521 1522
  return this->pImpl->containsTiles(idArray);
}

1523
std::size_t NemoInterface::size() const { return this->pImpl->size(); }
1524

1525
bool NemoInterface::empty() const { return this->pImpl->empty(); }
1526

1527
ProgressArray NemoInterface::getProgress() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1528 1529 1530
  return this->pImpl->getProgress();
}

1531
ProgressArray NemoInterface::getProgress(const IDArray &idArray) const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1532 1533 1534
  return this->pImpl->getProgress(idArray);
}

1535
NemoInterface::STATUS NemoInterface::status() const {
1536 1537 1538 1539 1540 1541 1542
  return this->pImpl->status();
}

QString NemoInterface::statusString() const {
  return statusMap.at(this->pImpl->status());
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
1543 1544 1545 1546 1547
QString NemoInterface::infoString() const { return this->pImpl->infoString(); }

QString NemoInterface::warningString() const {
  return this->pImpl->warningString();
}
1548 1549 1550 1551 1552

QString NemoInterface::editorQml() {
  return QStringLiteral("NemoInterface.qml");
}

1553
bool NemoInterface::running() const { return this->pImpl->running(); }