TaisyncManager.cc 22.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "TaisyncManager.h"
#include "TaisyncHandler.h"
#include "SettingsManager.h"
#include "QGCApplication.h"
Gus Grubba's avatar
Gus Grubba committed
14
#include "QGCCorePlugin.h"
15 16
#include "VideoManager.h"

Gus Grubba's avatar
Gus Grubba committed
17 18
#include <QSettings>

Gus Grubba's avatar
Gus Grubba committed
19
static const char *kTAISYNC_GROUP       = "Taisync";
20 21
static const char *kRADIO_MODE          = "RadioMode";
static const char *kRADIO_CHANNEL       = "RadioChannel";
Gus Grubba's avatar
Gus Grubba committed
22 23 24
static const char *kVIDEO_OUTPUT        = "VideoOutput";
static const char *kVIDEO_MODE          = "VideoMode";
static const char *kVIDEO_RATE          = "VideoRate";
Gus Grubba's avatar
Gus Grubba committed
25 26 27
static const char *kLOCAL_IP            = "LocalIP";
static const char *kREMOTE_IP           = "RemoteIP";
static const char *kNET_MASK            = "NetMask";
Gus Grubba's avatar
Gus Grubba committed
28 29 30
static const char *kRTSP_URI            = "RTSPURI";
static const char *kRTSP_ACCOUNT        = "RTSPAccount";
static const char *kRTSP_PASSWORD       = "RTSPPassword";
31

32 33 34 35 36 37
//-----------------------------------------------------------------------------
TaisyncManager::TaisyncManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
{
    connect(&_workTimer, &QTimer::timeout, this, &TaisyncManager::_checkTaisync);
    _workTimer.setSingleShot(true);
Gus Grubba's avatar
Gus Grubba committed
38 39
    QSettings settings;
    settings.beginGroup(kTAISYNC_GROUP);
Gus Grubba's avatar
Gus Grubba committed
40 41 42 43 44 45
    _localIPAddr  = settings.value(kLOCAL_IP,       QString("192.168.199.33")).toString();
    _remoteIPAddr = settings.value(kREMOTE_IP,      QString("192.168.199.16")).toString();
    _netMask      = settings.value(kNET_MASK,       QString("255.255.255.0")).toString();
    _rtspURI      = settings.value(kRTSP_URI,       QString("rtsp://192.168.0.2")).toString();
    _rtspAccount  = settings.value(kRTSP_ACCOUNT,   QString("admin")).toString();
    _rtspPassword = settings.value(kRTSP_PASSWORD,  QString("12345678")).toString();
Gus Grubba's avatar
Gus Grubba committed
46
    settings.endGroup();
47 48
}

49 50
//-----------------------------------------------------------------------------
TaisyncManager::~TaisyncManager()
Gus Grubba's avatar
Gus Grubba committed
51 52 53 54 55 56 57
{
    _close();
}

//-----------------------------------------------------------------------------
void
TaisyncManager::_close()
58 59 60 61 62 63 64 65 66 67 68 69
{
    if(_taiSettings) {
        _taiSettings->close();
        _taiSettings->deleteLater();
        _taiSettings = nullptr;
    }
#if defined(__ios__) || defined(__android__)
    if (_taiTelemetery) {
        _taiTelemetery->close();
        _taiTelemetery->deleteLater();
        _taiTelemetery = nullptr;
    }
Gus Grubba's avatar
Gus Grubba committed
70 71 72 73 74
    if(_telemetrySocket) {
        _telemetrySocket->close();
        _telemetrySocket->deleteLater();
        _telemetrySocket = nullptr;
    }
75 76 77 78 79 80 81 82 83 84
    if (_taiVideo) {
        _taiVideo->close();
        _taiVideo->deleteLater();
        _taiVideo = nullptr;
    }
#endif
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
85
TaisyncManager::_reset()
86
{
Gus Grubba's avatar
Gus Grubba committed
87
    _close();
Gus Grubba's avatar
Gus Grubba committed
88 89
    _isConnected = false;
    emit connectedChanged();
Gus Grubba's avatar
Gus Grubba committed
90 91
    _linkConnected = false;
    emit linkConnectedChanged();
Gus Grubba's avatar
Gus Grubba committed
92 93 94 95 96
    if(!_appSettings) {
        _appSettings = _toolbox->settingsManager()->appSettings();
        connect(_appSettings->enableTaisync(),      &Fact::rawValueChanged, this, &TaisyncManager::_setEnabled);
        connect(_appSettings->enableTaisyncVideo(), &Fact::rawValueChanged, this, &TaisyncManager::_setVideoEnabled);
    }
97 98 99
    _setEnabled();
}

Gus Grubba's avatar
Gus Grubba committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
//-----------------------------------------------------------------------------
FactMetaData*
TaisyncManager::_createMetadata(const char* name, QStringList enums)
{
    FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, name, this);
    QQmlEngine::setObjectOwnership(metaData, QQmlEngine::CppOwnership);
    metaData->setShortDescription(name);
    metaData->setLongDescription(name);
    metaData->setRawDefaultValue(QVariant(0));
    metaData->setHasControl(true);
    metaData->setReadOnly(false);
    for(int i = 0; i < enums.size(); i++) {
        metaData->addEnumInfo(enums[i], QVariant(i));
    }
    metaData->setRawMin(0);
    metaData->setRawMin(enums.size() - 1);
    return metaData;
}

Gus Grubba's avatar
Gus Grubba committed
119 120 121 122 123
//-----------------------------------------------------------------------------
void
TaisyncManager::setToolbox(QGCToolbox* toolbox)
{
    QGCTool::setToolbox(toolbox);
124 125
    {
        //-- Radio Mode
Gus Grubba's avatar
Gus Grubba committed
126 127 128 129 130
        QStringList enums;
        enums.append(tr("Auto"));
        enums.append(tr("Manual"));
        FactMetaData* metaData = _createMetadata(kRADIO_MODE, enums);
        _radioMode = new Fact(kTAISYNC_GROUP, metaData, this);
131
        QQmlEngine::setObjectOwnership(_radioMode, QQmlEngine::CppOwnership);
Gus Grubba's avatar
Gus Grubba committed
132 133 134
        _radioModeList.append("auto");
        _radioModeList.append("manual");
        connect(_radioMode, &Fact::_containerRawValueChanged, this, &TaisyncManager::_radioSettingsChanged);
135 136 137
    }
    {
        //-- Radio Channel
Gus Grubba's avatar
Gus Grubba committed
138
        QStringList enums;
139
        for(int i = 0; i < 14; i++) {
Gus Grubba's avatar
Gus Grubba committed
140
            enums.append(QString("ch%1").arg(i));
141
        }
Gus Grubba's avatar
Gus Grubba committed
142 143
        FactMetaData* metaData = _createMetadata(kRADIO_CHANNEL, enums);
        _radioChannel = new Fact(kTAISYNC_GROUP, metaData, this);
144
        QQmlEngine::setObjectOwnership(_radioChannel, QQmlEngine::CppOwnership);
Gus Grubba's avatar
Gus Grubba committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        connect(_radioChannel, &Fact::_containerRawValueChanged, this, &TaisyncManager::_radioSettingsChanged);
    }
    {
        //-- Video Output
        QStringList enums;
        enums.append(tr("Stream"));
        enums.append(tr("HDMI Port"));
        FactMetaData* metaData = _createMetadata(kVIDEO_OUTPUT, enums);
        _videoOutput = new Fact(kTAISYNC_GROUP, metaData, this);
        QQmlEngine::setObjectOwnership(_videoOutput, QQmlEngine::CppOwnership);
        _videoOutputList.append("phone");
        _videoOutputList.append("hdmi");
        connect(_videoOutput, &Fact::_containerRawValueChanged, this, &TaisyncManager::_videoSettingsChanged);
    }
    {
        //-- Video Mode
        QStringList enums;
        enums.append("H264");
        enums.append("H265");
        FactMetaData* metaData = _createMetadata(kVIDEO_MODE, enums);
        _videoMode = new Fact(kTAISYNC_GROUP, metaData, this);
        QQmlEngine::setObjectOwnership(_videoMode, QQmlEngine::CppOwnership);
        connect(_videoMode, &Fact::_containerRawValueChanged, this, &TaisyncManager::_videoSettingsChanged);
    }
    {
        //-- Video Rate
        QStringList enums;
        enums.append(tr("Low"));
        enums.append(tr("Medium"));
        enums.append(tr("High"));
        FactMetaData* metaData = _createMetadata(kVIDEO_RATE, enums);
        _videoRate = new Fact(kTAISYNC_GROUP, metaData, this);
        QQmlEngine::setObjectOwnership(_videoRate, QQmlEngine::CppOwnership);
        _videoRateList.append("low");
        _videoRateList.append("middle");
        _videoRateList.append("high");
        connect(_videoRate, &Fact::_containerRawValueChanged, this, &TaisyncManager::_videoSettingsChanged);
182
    }
Gus Grubba's avatar
Gus Grubba committed
183
    //-- Start it all
Gus Grubba's avatar
Gus Grubba committed
184 185 186
    _reset();
}

Gus Grubba's avatar
Gus Grubba committed
187 188 189 190
//-----------------------------------------------------------------------------
bool
TaisyncManager::setRTSPSettings(QString uri, QString account, QString password)
{
191
    if(_taiSettings && _isConnected) {
Gus Grubba's avatar
Gus Grubba committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        if(_taiSettings->setRTSPSettings(uri, account, password)) {
            _rtspURI      = uri;
            _rtspAccount  = account;
            _rtspPassword = password;
            QSettings settings;
            settings.beginGroup(kTAISYNC_GROUP);
            settings.setValue(kRTSP_URI,      _rtspURI);
            settings.setValue(kRTSP_ACCOUNT,  _rtspAccount);
            settings.setValue(kRTSP_PASSWORD, _rtspPassword);
            settings.endGroup();
            emit rtspURIChanged();
            emit rtspAccountChanged();
            emit rtspPasswordChanged();
            _needReboot = true;
            emit needRebootChanged();
            return true;
        }
Gus Grubba's avatar
Gus Grubba committed
209 210 211 212 213 214
    }
    return false;
}

//-----------------------------------------------------------------------------
bool
Gus Grubba's avatar
Gus Grubba committed
215
TaisyncManager::setIPSettings(QString localIP_, QString remoteIP_, QString netMask_)
Gus Grubba's avatar
Gus Grubba committed
216
{
Gus Grubba's avatar
Gus Grubba committed
217 218 219
    bool res = false;
    if(_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_) {
        //-- If we are connected to the Taisync
Gus Grubba's avatar
Gus Grubba committed
220
        if(_isConnected) {
Gus Grubba's avatar
Gus Grubba committed
221 222 223
            if(_taiSettings) {
                //-- Change IP settings
                res = _taiSettings->setIPSettings(localIP_, remoteIP_, netMask_);
Gus Grubba's avatar
Gus Grubba committed
224 225 226 227
                if(res) {
                    _needReboot = true;
                    emit needRebootChanged();
                }
Gus Grubba's avatar
Gus Grubba committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            }
        } else {
            //-- We're not connected. Record the change and restart.
            _localIPAddr  = localIP_;
            _remoteIPAddr = remoteIP_;
            _netMask      = netMask_;
            _reset();
            res = true;
        }
        if(res) {
            QSettings settings;
            settings.beginGroup(kTAISYNC_GROUP);
            settings.setValue(kLOCAL_IP, localIP_);
            settings.setValue(kREMOTE_IP, remoteIP_);
            settings.setValue(kNET_MASK, netMask_);
            settings.endGroup();
        }
    } else {
        //-- Nothing to change
        res = true;
Gus Grubba's avatar
Gus Grubba committed
248
    }
Gus Grubba's avatar
Gus Grubba committed
249
    return res;
Gus Grubba's avatar
Gus Grubba committed
250 251
}

252 253
//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
254
TaisyncManager::_radioSettingsChanged(QVariant)
255
{
256
    if(_taiSettings && _isConnected) {
Gus Grubba's avatar
Gus Grubba committed
257
        _workTimer.stop();
Gus Grubba's avatar
Gus Grubba committed
258 259 260
        _taiSettings->setRadioSettings(
            _radioModeList[_radioMode->rawValue().toInt()],
            _radioChannel->enumStringValue());
Gus Grubba's avatar
Gus Grubba committed
261 262
        _reqMask |= REQ_RADIO_SETTINGS;
        _workTimer.start(3000);
Gus Grubba's avatar
Gus Grubba committed
263
    }
264 265 266 267
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
268
TaisyncManager::_videoSettingsChanged(QVariant)
269
{
270
    if(_taiSettings && _isConnected) {
Gus Grubba's avatar
Gus Grubba committed
271
        _workTimer.stop();
Gus Grubba's avatar
Gus Grubba committed
272 273 274 275
        _taiSettings->setVideoSettings(
            _videoOutputList[_videoOutput->rawValue().toInt()],
            _videoMode->enumStringValue(),
            _videoRateList[_videoRate->rawValue().toInt()]);
Gus Grubba's avatar
Gus Grubba committed
276 277
        _reqMask |= REQ_VIDEO_SETTINGS;
        _workTimer.start(500);
Gus Grubba's avatar
Gus Grubba committed
278
    }
279 280
}

281 282 283 284 285 286
//-----------------------------------------------------------------------------
void
TaisyncManager::_setEnabled()
{
    bool enable = _appSettings->enableTaisync()->rawValue().toBool();
    if(enable) {
Gus Grubba's avatar
Gus Grubba committed
287 288 289 290 291 292
        if(!_taiSettings) {
            _taiSettings = new TaisyncSettings(this);
            connect(_taiSettings, &TaisyncSettings::updateSettings, this, &TaisyncManager::_updateSettings);
            connect(_taiSettings, &TaisyncSettings::connected,      this, &TaisyncManager::_connected);
            connect(_taiSettings, &TaisyncSettings::disconnected,   this, &TaisyncManager::_disconnected);
        }
293
#if defined(__ios__) || defined(__android__)
Gus Grubba's avatar
Gus Grubba committed
294 295 296 297 298 299 300 301 302 303
        if(!_taiTelemetery) {
            _taiTelemetery = new TaisyncTelemetry(this);
            QObject::connect(_taiTelemetery, &TaisyncTelemetry::bytesReady, this, &TaisyncManager::_readTelemBytes);
            _telemetrySocket = new QUdpSocket(this);
            _telemetrySocket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption,    64 * 1024);
            _telemetrySocket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 64 * 1024);
            QObject::connect(_telemetrySocket, &QUdpSocket::readyRead, this, &TaisyncManager::_readUDPBytes);
            _telemetrySocket->bind(QHostAddress::LocalHost, 0, QUdpSocket::ShareAddress);
            _taiTelemetery->start();
        }
304
#endif
Gus Grubba's avatar
Gus Grubba committed
305
        _reqMask = static_cast<uint32_t>(REQ_ALL);
306 307
        _workTimer.start(1000);
    } else {
Gus Grubba's avatar
Gus Grubba committed
308
        //-- Stop everything
309
        _workTimer.stop();
Gus Grubba's avatar
Gus Grubba committed
310
        _close();
311 312
    }
    _enabled = enable;
313 314
    //-- Now handle video support
    _setVideoEnabled();
315 316
}

Gus Grubba's avatar
Gus Grubba committed
317 318 319 320 321 322 323 324 325 326
//-----------------------------------------------------------------------------
void
TaisyncManager::_restoreVideoSettings(Fact* setting)
{
    SettingsFact* pFact = dynamic_cast<SettingsFact*>(setting);
    if(pFact) {
        pFact->setVisible(qgcApp()->toolbox()->corePlugin()->adjustSettingMetaData(VideoSettings::settingsGroup, *setting->metaData()));
    }
}

327 328 329 330
//-----------------------------------------------------------------------------
void
TaisyncManager::_setVideoEnabled()
{
331
    //-- Check both if video is enabled and Taisync support itself is enabled as well.
332
    bool enable = _appSettings->enableTaisyncVideo()->rawValue().toBool() && _appSettings->enableTaisync()->rawValue().toBool();
333
    if(enable) {
Gus Grubba's avatar
Gus Grubba committed
334 335 336 337 338 339
        //-- Set it up the way we need it do be.
        VideoSettings* pVSettings = qgcApp()->toolbox()->settingsManager()->videoSettings();
        pVSettings->setVisible(false);
        pVSettings->udpPort()->setRawValue(5600);
        //-- TODO: this AR must come from somewhere
        pVSettings->aspectRatio()->setRawValue(1024.0 / 768.0);
Gus Grubba's avatar
Gus Grubba committed
340
        pVSettings->videoSource()->setRawValue(QString(VideoSettings::videoSourceUDPH264));
341
#if defined(__ios__) || defined(__android__)
Gus Grubba's avatar
Gus Grubba committed
342 343 344 345 346 347
        if(!_taiVideo) {
            //-- iOS and Android receive raw h.264 and need a different pipeline
            qgcApp()->toolbox()->videoManager()->setIsTaisync(true);
            _taiVideo = new TaisyncVideoReceiver(this);
            _taiVideo->start();
        }
348 349
#endif
    } else {
350
        //-- Restore video settings.
351 352 353 354 355 356 357 358
#if defined(__ios__) || defined(__android__)
        qgcApp()->toolbox()->videoManager()->setIsTaisync(false);
        if (_taiVideo) {
            _taiVideo->close();
            _taiVideo->deleteLater();
            _taiVideo = nullptr;
        }
#endif
Gus Grubba's avatar
Gus Grubba committed
359 360 361 362 363
        VideoSettings* pVSettings = qgcApp()->toolbox()->settingsManager()->videoSettings();
        _restoreVideoSettings(pVSettings->videoSource());
        _restoreVideoSettings(pVSettings->aspectRatio());
        _restoreVideoSettings(pVSettings->udpPort());
        pVSettings->setVisible(true);
364 365 366 367 368
    }
    _enableVideo = enable;
}

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
#if defined(__ios__) || defined(__android__)
void
TaisyncManager::_readTelemBytes(QByteArray bytesIn)
{
    //-- Send telemetry from vehicle to QGC (using normal UDP)
    _telemetrySocket->writeDatagram(bytesIn, QHostAddress::LocalHost, TAISYNC_TELEM_TARGET_PORT);
}
#endif

//-----------------------------------------------------------------------------
#if defined(__ios__) || defined(__android__)
void
TaisyncManager::_readUDPBytes()
{
    if (!_telemetrySocket || !_taiTelemetery) {
        return;
    }
    //-- Read UDP data from QGC
    while (_telemetrySocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(static_cast<int>(_telemetrySocket->pendingDatagramSize()));
        _telemetrySocket->readDatagram(datagram.data(), datagram.size());
        //-- Send it to vehicle
        _taiTelemetery->writeBytes(datagram);
    }
}
#endif

//-----------------------------------------------------------------------------
398 399 400 401 402 403
void
TaisyncManager::_connected()
{
    qCDebug(TaisyncLog) << "Taisync Settings Connected";
    _isConnected = true;
    emit connectedChanged();
Gus Grubba's avatar
Gus Grubba committed
404 405
    _needReboot = false;
    emit needRebootChanged();
406 407
}

Gus Grubba's avatar
Gus Grubba committed
408 409 410 411 412 413 414
//-----------------------------------------------------------------------------
void
TaisyncManager::_disconnected()
{
    qCDebug(TaisyncLog) << "Taisync Settings Disconnected";
    _isConnected = false;
    emit connectedChanged();
Gus Grubba's avatar
Gus Grubba committed
415 416 417 418
    _needReboot = false;
    emit needRebootChanged();
    _linkConnected = false;
    emit linkConnectedChanged();
Gus Grubba's avatar
Gus Grubba committed
419 420 421
    _reset();
}

422 423 424 425 426 427
//-----------------------------------------------------------------------------
void
TaisyncManager::_checkTaisync()
{
    if(_enabled) {
        if(!_isConnected) {
Gus Grubba's avatar
Gus Grubba committed
428 429 430 431
            if(_taiSettings) {
                if(!_taiSettings->isServerRunning()) {
                    _taiSettings->start();
                }
Gus Grubba's avatar
Gus Grubba committed
432
            }
433
        } else {
Gus Grubba's avatar
Gus Grubba committed
434
            //qCDebug(TaisyncVerbose) << bin << _reqMask;
Gus Grubba's avatar
Gus Grubba committed
435 436 437 438 439 440 441 442 443 444
            while(true) {
                if (_reqMask & REQ_LINK_STATUS) {
                    _taiSettings->requestLinkStatus();
                    break;
                }
                if (_reqMask & REQ_DEV_INFO) {
                    _taiSettings->requestDevInfo();
                    break;
                }
                if (_reqMask & REQ_FREQ_SCAN) {
Gus Grubba's avatar
Gus Grubba committed
445
                    _reqMask &= ~static_cast<uint32_t>(REQ_FREQ_SCAN);
Gus Grubba's avatar
Gus Grubba committed
446 447 448 449 450 451 452 453 454 455 456 457
                    _taiSettings->requestFreqScan();
                    break;
                }
                if (_reqMask & REQ_VIDEO_SETTINGS) {
                    _taiSettings->requestVideoSettings();
                    break;
                }
                if (_reqMask & REQ_RADIO_SETTINGS) {
                    _taiSettings->requestRadioSettings();
                    break;
                }
                if (_reqMask & REQ_RTSP_SETTINGS) {
Gus Grubba's avatar
Gus Grubba committed
458
                    _reqMask &= ~static_cast<uint32_t>(REQ_RTSP_SETTINGS);
Gus Grubba's avatar
Gus Grubba committed
459 460 461 462
                    _taiSettings->requestRTSPURISettings();
                    break;
                }
                if (_reqMask & REQ_IP_SETTINGS) {
Gus Grubba's avatar
Gus Grubba committed
463
                    _reqMask &= ~static_cast<uint32_t>(REQ_IP_SETTINGS);
Gus Grubba's avatar
Gus Grubba committed
464 465 466
                    _taiSettings->requestIPSettings();
                    break;
                }
Gus Grubba's avatar
Gus Grubba committed
467 468 469 470 471 472 473 474 475 476 477
                //-- Check link status
                if(_timeoutTimer.elapsed() > 3000) {
                    //-- Give up and restart
                    _disconnected();
                    break;
                }
                //-- If it's been too long since we last heard, ping it.
                if(_timeoutTimer.elapsed() > 1000) {
                    _taiSettings->requestLinkStatus();
                    break;
                }
Gus Grubba's avatar
Gus Grubba committed
478
                break;
479 480
            }
        }
Gus Grubba's avatar
Gus Grubba committed
481
        _workTimer.start(_isConnected ? 500 : 5000);
482 483 484 485 486 487 488
    }
}

//-----------------------------------------------------------------------------
void
TaisyncManager::_updateSettings(QByteArray jSonData)
{
Gus Grubba's avatar
Gus Grubba committed
489
    _timeoutTimer.start();
Gus Grubba's avatar
Gus Grubba committed
490
    qCDebug(TaisyncVerbose) << jSonData;
491 492 493 494 495 496 497 498 499
    QJsonParseError jsonParseError;
    QJsonDocument doc = QJsonDocument::fromJson(jSonData, &jsonParseError);
    if (jsonParseError.error != QJsonParseError::NoError) {
        qWarning() <<  "Unable to parse Taisync response:" << jsonParseError.errorString() << jsonParseError.offset;
        return;
    }
    QJsonObject jObj = doc.object();
    //-- Link Status?
    if(jSonData.contains("\"flight\":")) {
Gus Grubba's avatar
Gus Grubba committed
500
        _reqMask &= ~static_cast<uint32_t>(REQ_LINK_STATUS);
501
        bool tlinkConnected  = jObj["flight"].toString() == "online";
Gus Grubba's avatar
Gus Grubba committed
502 503 504 505 506 507 508 509 510 511 512 513 514
        if(tlinkConnected != _linkConnected) {
           _linkConnected = tlinkConnected;
           emit linkConnectedChanged();
        }
        QString tlinkVidFormat  = jObj["videoformat"].toString(_linkVidFormat);
        int     tdownlinkRSSI   = jObj["radiorssi"].toInt(_downlinkRSSI);
        int     tuplinkRSSI     = jObj["hdrssi"].toInt(_uplinkRSSI);
        if(_linkVidFormat != tlinkVidFormat || _downlinkRSSI != tdownlinkRSSI || _uplinkRSSI != tuplinkRSSI) {
            _linkVidFormat  = tlinkVidFormat;
            _downlinkRSSI   = tdownlinkRSSI;
            _uplinkRSSI     = tuplinkRSSI;
            emit linkChanged();
        }
515 516
    //-- Device Info?
    } else if(jSonData.contains("\"firmwareversion\":")) {
Gus Grubba's avatar
Gus Grubba committed
517
        _reqMask &= ~static_cast<uint32_t>(REQ_DEV_INFO);
Gus Grubba's avatar
Gus Grubba committed
518 519 520 521 522 523 524 525 526
        QString tfwVersion      = jObj["firmwareversion"].toString(_fwVersion);
        QString tserialNumber   = jObj["sn"].toString(_serialNumber);
        if(tfwVersion != _fwVersion || tserialNumber != _serialNumber) {
            _fwVersion      = tfwVersion;
            _serialNumber   = tserialNumber;
            emit infoChanged();
        }
    //-- Radio Settings?
    } else if(jSonData.contains("\"freq\":")) {
Gus Grubba's avatar
Gus Grubba committed
527
        _reqMask &= ~static_cast<uint32_t>(REQ_RADIO_SETTINGS);
Gus Grubba's avatar
Gus Grubba committed
528 529 530 531 532
        int idx = _radioModeList.indexOf(jObj["mode"].toString(_radioMode->enumStringValue()));
        if(idx >= 0) _radioMode->_containerSetRawValue(idx);
        idx = _radioChannel->valueIndex(jObj["freq"].toString(_radioChannel->enumStringValue()));
        if(idx < 0) idx = 0;
        _radioChannel->_containerSetRawValue(idx);
533 534
    //-- Video Settings?
    } else if(jSonData.contains("\"maxbitrate\":")) {
Gus Grubba's avatar
Gus Grubba committed
535
        _reqMask &= ~static_cast<uint32_t>(REQ_VIDEO_SETTINGS);
Gus Grubba's avatar
Gus Grubba committed
536 537 538 539 540 541 542 543
        int idx;
        idx = _videoMode->valueIndex(jObj["mode"].toString(_videoMode->enumStringValue()));
        if(idx < 0) idx = 0;
        _videoMode->_containerSetRawValue(idx);
        idx = _videoRateList.indexOf(jObj["maxbitrate"].toString(_videoMode->enumStringValue()));
        if(idx >= 0) _videoRate->_containerSetRawValue(idx);
        idx = _videoOutputList.indexOf(jObj["decode"].toString(_videoOutput->enumStringValue()));
        if(idx >= 0) _videoOutput->_containerSetRawValue(idx);
Gus Grubba's avatar
Gus Grubba committed
544 545 546
    //-- IP Address Settings?
    } else if(jSonData.contains("\"usbEthIp\":")) {
        QString value;
Gus Grubba's avatar
Gus Grubba committed
547
        bool changed = false;
Gus Grubba's avatar
Gus Grubba committed
548 549 550
        value = jObj["ipaddr"].toString(_localIPAddr);
        if(value != _localIPAddr) {
            _localIPAddr = value;
Gus Grubba's avatar
Gus Grubba committed
551
            changed = true;
Gus Grubba's avatar
Gus Grubba committed
552 553 554 555 556
            emit localIPAddrChanged();
        }
        value = jObj["netmask"].toString(_netMask);
        if(value != _netMask) {
            _netMask = value;
Gus Grubba's avatar
Gus Grubba committed
557
            changed = true;
Gus Grubba's avatar
Gus Grubba committed
558 559 560 561 562
            emit netMaskChanged();
        }
        value = jObj["usbEthIp"].toString(_remoteIPAddr);
        if(value != _remoteIPAddr) {
            _remoteIPAddr = value;
Gus Grubba's avatar
Gus Grubba committed
563
            changed = true;
Gus Grubba's avatar
Gus Grubba committed
564 565
            emit remoteIPAddrChanged();
        }
Gus Grubba's avatar
Gus Grubba committed
566 567 568 569 570 571 572 573
        if(changed) {
            QSettings settings;
            settings.beginGroup(kTAISYNC_GROUP);
            settings.setValue(kLOCAL_IP,  _localIPAddr);
            settings.setValue(kREMOTE_IP, _remoteIPAddr);
            settings.setValue(kNET_MASK,  _netMask);
            settings.endGroup();
        }
Gus Grubba's avatar
Gus Grubba committed
574 575 576
    //-- RTSP URI Settings?
    } else if(jSonData.contains("\"rtspURI\":")) {
        QString value;
Gus Grubba's avatar
Gus Grubba committed
577
        bool changed = false;
Gus Grubba's avatar
Gus Grubba committed
578 579 580
        value = jObj["rtspURI"].toString(_rtspURI);
        if(value != _rtspURI) {
            _rtspURI = value;
Gus Grubba's avatar
Gus Grubba committed
581
            changed = true;
Gus Grubba's avatar
Gus Grubba committed
582 583 584 585 586
            emit rtspURIChanged();
        }
        value = jObj["account"].toString(_rtspAccount);
        if(value != _rtspAccount) {
            _rtspAccount = value;
Gus Grubba's avatar
Gus Grubba committed
587
            changed = true;
Gus Grubba's avatar
Gus Grubba committed
588 589 590 591 592
            emit rtspAccountChanged();
        }
        value = jObj["passwd"].toString(_rtspPassword);
        if(value != _rtspPassword) {
            _rtspPassword = value;
Gus Grubba's avatar
Gus Grubba committed
593
            changed = true;
Gus Grubba's avatar
Gus Grubba committed
594 595
            emit rtspPasswordChanged();
        }
Gus Grubba's avatar
Gus Grubba committed
596 597 598 599 600 601 602 603
        if(changed) {
            QSettings settings;
            settings.beginGroup(kTAISYNC_GROUP);
            settings.setValue(kRTSP_URI,      _rtspURI);
            settings.setValue(kRTSP_ACCOUNT,  _rtspAccount);
            settings.setValue(kRTSP_PASSWORD, _rtspPassword);
            settings.endGroup();
        }
604 605
    }
}