Commit db5c070a authored by Gus Grubba's avatar Gus Grubba

Handle pairing status

parent 3d4f2d8f
...@@ -72,8 +72,7 @@ PairingManager::_pairingCompleted(QString name) ...@@ -72,8 +72,7 @@ PairingManager::_pairingCompleted(QString name)
_remotePairingMap["NM"] = name; _remotePairingMap["NM"] = name;
emit pairedListChanged(); emit pairedListChanged();
_app->informationMessageBoxOnMainThread("", tr("Paired with %1").arg(name)); _app->informationMessageBoxOnMainThread("", tr("Paired with %1").arg(name));
_status = PairingSuccess; setPairingStatus(PairingSuccess, tr("Pairing Successfull"));
setPairingStatus(tr("Pairing successfull"));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -83,8 +82,7 @@ PairingManager::_connectionCompleted(QString name) ...@@ -83,8 +82,7 @@ PairingManager::_connectionCompleted(QString name)
//QString pwd = _remotePairingMap["PWD"].toString(); //QString pwd = _remotePairingMap["PWD"].toString();
//_toolbox->microhardManager()->switchToConnectionEncryptionKey(pwd); //_toolbox->microhardManager()->switchToConnectionEncryptionKey(pwd);
_app->informationMessageBoxOnMainThread("", tr("Connected to %1").arg(name)); _app->informationMessageBoxOnMainThread("", tr("Connected to %1").arg(name));
_status = PairingConnected; setPairingStatus(PairingConnected, tr("Connection Successfull"));
setPairingStatus(tr("Connection successfull"));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -111,7 +109,6 @@ PairingManager::_startUploadRequest() ...@@ -111,7 +109,6 @@ PairingManager::_startUploadRequest()
QNetworkRequest req; QNetworkRequest req;
req.setUrl(QUrl(_uploadURL)); req.setUrl(QUrl(_uploadURL));
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = _uploadManager->post(req, _uploadData.toUtf8()); QNetworkReply *reply = _uploadManager->post(req, _uploadData.toUtf8());
connect(reply, &QNetworkReply::finished, this, &PairingManager::_uploadFinished); connect(reply, &QNetworkReply::finished, this, &PairingManager::_uploadFinished);
} }
...@@ -146,15 +143,19 @@ PairingManager::_uploadFinished(void) ...@@ -146,15 +143,19 @@ PairingManager::_uploadFinished(void)
} else if (a[0] == "Connected" && a.length() > 1) { } else if (a[0] == "Connected" && a.length() > 1) {
_connectionCompleted(a[1]); _connectionCompleted(a[1]);
} else if (a[0] == "Connection" && a.length() > 1) { } else if (a[0] == "Connection" && a.length() > 1) {
_status = PairingConnectionRejected; setPairingStatus(PairingConnectionRejected, tr("Connection Rejected"));
setPairingStatus(tr("Connection rejected."));
qCDebug(PairingManagerLog) << "Connection error: " << str; qCDebug(PairingManagerLog) << "Connection error: " << str;
} else { } else {
_status = PairingRejected; setPairingStatus(PairingRejected, tr("Pairing Rejected"));
setPairingStatus(tr("Pairing rejected."));
qCDebug(PairingManagerLog) << "Pairing error: " << str; qCDebug(PairingManagerLog) << "Pairing error: " << str;
} }
delete _uploadManager; _uploadManager->deleteLater();
_uploadManager = nullptr;
} else {
if(++_pairRetryCount > 3) {
qCDebug(PairingManagerLog) << "Giving up";
setPairingStatus(PairingError, tr("Too Many Errors"));
_uploadManager->deleteLater();
_uploadManager = nullptr; _uploadManager = nullptr;
} else { } else {
qCDebug(PairingManagerLog) << "Upload error: " + reply->errorString(); qCDebug(PairingManagerLog) << "Upload error: " + reply->errorString();
...@@ -162,6 +163,7 @@ PairingManager::_uploadFinished(void) ...@@ -162,6 +163,7 @@ PairingManager::_uploadFinished(void)
} }
} }
} }
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -181,8 +183,7 @@ PairingManager::_parsePairingJsonFile() ...@@ -181,8 +183,7 @@ PairingManager::_parsePairingJsonFile()
void void
PairingManager::connectToPairedDevice(QString name) PairingManager::connectToPairedDevice(QString name)
{ {
_status = PairingConnecting; setPairingStatus(PairingConnecting, tr("Connecting to %1").arg(name));
setPairingStatus(tr("Connecting to %1").arg(name));
QFile file(_pairingCacheFile(name)); QFile file(_pairingCacheFile(name));
file.open(QIODevice::ReadOnly | QIODevice::Text); file.open(QIODevice::ReadOnly | QIODevice::Text);
QString json = file.readAll(); QString json = file.readAll();
...@@ -198,6 +199,7 @@ PairingManager::pairedDeviceNameList(void) ...@@ -198,6 +199,7 @@ PairingManager::pairedDeviceNameList(void)
while (it.hasNext()) { while (it.hasNext()) {
QFileInfo fileInfo(it.next()); QFileInfo fileInfo(it.next());
list.append(fileInfo.fileName()); list.append(fileInfo.fileName());
qCDebug(PairingManagerLog) << "Listing: " << fileInfo.fileName();
} }
return list; return list;
...@@ -234,10 +236,12 @@ PairingManager::_parsePairingJson(QString jsonEnc) ...@@ -234,10 +236,12 @@ PairingManager::_parsePairingJson(QString jsonEnc)
_jsonDoc = QJsonDocument::fromJson(json.toUtf8()); _jsonDoc = QJsonDocument::fromJson(json.toUtf8());
if (_jsonDoc.isNull()) { if (_jsonDoc.isNull()) {
setPairingStatus(PairingError, tr("Invalid Pairing File"));
qCDebug(PairingManagerLog) << "Failed to create Pairing JSON doc."; qCDebug(PairingManagerLog) << "Failed to create Pairing JSON doc.";
return; return;
} }
if (!_jsonDoc.isObject()) { if (!_jsonDoc.isObject()) {
setPairingStatus(PairingError, tr("Error Parsing Pairing File"));
qCDebug(PairingManagerLog) << "Pairing JSON is not an object."; qCDebug(PairingManagerLog) << "Pairing JSON is not an object.";
return; return;
} }
...@@ -245,6 +249,7 @@ PairingManager::_parsePairingJson(QString jsonEnc) ...@@ -245,6 +249,7 @@ PairingManager::_parsePairingJson(QString jsonEnc)
QJsonObject jsonObj = _jsonDoc.object(); QJsonObject jsonObj = _jsonDoc.object();
if (jsonObj.isEmpty()) { if (jsonObj.isEmpty()) {
setPairingStatus(PairingError, tr("Error Parsing Pairing File"));
qCDebug(PairingManagerLog) << "Pairing JSON object is empty."; qCDebug(PairingManagerLog) << "Pairing JSON object is empty.";
return; return;
} }
...@@ -257,6 +262,7 @@ PairingManager::_parsePairingJson(QString jsonEnc) ...@@ -257,6 +262,7 @@ PairingManager::_parsePairingJson(QString jsonEnc)
} }
if (linkType.length()==0) { if (linkType.length()==0) {
setPairingStatus(PairingError, tr("Error Parsing Pairing File"));
qCDebug(PairingManagerLog) << "Pairing JSON is malformed."; qCDebug(PairingManagerLog) << "Pairing JSON is malformed.";
return; return;
} }
...@@ -452,9 +458,10 @@ PairingManager::pairingLinkTypeStrings(void) ...@@ -452,9 +458,10 @@ PairingManager::pairingLinkTypeStrings(void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void void
PairingManager::_setPairingStatus(QString status) PairingManager::_setPairingStatus(PairingStatus status, QString statusStr)
{ {
_pairingStatus = status; _status = status;
_statusString = statusStr;
emit pairingStatusChanged(); emit pairingStatusChanged();
} }
...@@ -462,7 +469,7 @@ PairingManager::_setPairingStatus(QString status) ...@@ -462,7 +469,7 @@ PairingManager::_setPairingStatus(QString status)
QString QString
PairingManager::pairingStatusStr(void) const PairingManager::pairingStatusStr(void) const
{ {
return _pairingStatus; return _statusString;
} }
#if QGC_GST_MICROHARD_ENABLED #if QGC_GST_MICROHARD_ENABLED
...@@ -471,8 +478,8 @@ void ...@@ -471,8 +478,8 @@ void
PairingManager::startMicrohardPairing() PairingManager::startMicrohardPairing()
{ {
stopPairing(); stopPairing();
_status = PairingActive; _pairRetryCount = 0;
setPairingStatus(tr("Microhard pairing running.")); setPairingStatus(PairingActive, tr("Pairing..."));
_parsePairingJson(_assumeMicrohardPairingJson()); _parsePairingJson(_assumeMicrohardPairingJson());
} }
#endif #endif
...@@ -485,8 +492,7 @@ PairingManager::stopPairing() ...@@ -485,8 +492,7 @@ PairingManager::stopPairing()
pairingNFC.stop(); pairingNFC.stop();
#endif #endif
_stopUpload(); _stopUpload();
_status = PairingIdle; setPairingStatus(PairingIdle, "");
setPairingStatus("");
} }
#if defined QGC_ENABLE_NFC || defined QGC_ENABLE_QTNFC #if defined QGC_ENABLE_NFC || defined QGC_ENABLE_QTNFC
...@@ -495,7 +501,7 @@ void ...@@ -495,7 +501,7 @@ void
PairingManager::startNFCScan() PairingManager::startNFCScan()
{ {
stopPairing(); stopPairing();
emit pairingStatusChanged(); setPairingStatus(PairingActive, tr("Pairing..."));
pairingNFC.start(); pairingNFC.start();
} }
......
...@@ -55,7 +55,8 @@ public: ...@@ -55,7 +55,8 @@ public:
PairingConnecting, PairingConnecting,
PairingConnected, PairingConnected,
PairingRejected, PairingRejected,
PairingConnectionRejected PairingConnectionRejected,
PairingError
}; };
Q_ENUM(PairingStatus) Q_ENUM(PairingStatus)
...@@ -66,7 +67,7 @@ public: ...@@ -66,7 +67,7 @@ public:
PairingStatus pairingStatus() { return _status; } PairingStatus pairingStatus() { return _status; }
int nfcIndex(void) { return _nfcIndex; } int nfcIndex(void) { return _nfcIndex; }
int microhardIndex(void) { return _microhardIndex; } int microhardIndex(void) { return _microhardIndex; }
void setStatusMessage(QString status) { emit setPairingStatus(status); } void setStatusMessage(PairingStatus status, QString statusStr) { emit setPairingStatus(status, statusStr); }
void jsonReceived(QString json) { emit parsePairingJson(json); } void jsonReceived(QString json) { emit parsePairingJson(json); }
#ifdef __android__ #ifdef __android__
static void setNativeMethods(void); static void setNativeMethods(void);
...@@ -95,7 +96,7 @@ signals: ...@@ -95,7 +96,7 @@ signals:
void nameListChanged(); void nameListChanged();
void pairingStatusChanged(); void pairingStatusChanged();
void parsePairingJson(QString json); void parsePairingJson(QString json);
void setPairingStatus(QString pairingStatus); void setPairingStatus(PairingStatus status, QString pairingStatus);
void pairedListChanged(); void pairedListChanged();
private slots: private slots:
...@@ -103,14 +104,15 @@ private slots: ...@@ -103,14 +104,15 @@ private slots:
void _stopUpload(); void _stopUpload();
void _startUploadRequest(); void _startUploadRequest();
void _parsePairingJson(QString jsonEnc); void _parsePairingJson(QString jsonEnc);
void _setPairingStatus(QString pairingStatus); void _setPairingStatus(PairingStatus status, QString pairingStatus);
private: private:
QString _pairingStatus; QString _statusString;
QString _jsonFileName; QString _jsonFileName;
QVariantMap _remotePairingMap; QVariantMap _remotePairingMap;
int _nfcIndex = -1; int _nfcIndex = -1;
int _microhardIndex = -1; int _microhardIndex = -1;
int _pairRetryCount = 0;
PairingStatus _status = PairingIdle; PairingStatus _status = PairingIdle;
AES _aes; AES _aes;
QJsonDocument _jsonDoc{}; QJsonDocument _jsonDoc{};
......
...@@ -56,6 +56,9 @@ Item { ...@@ -56,6 +56,9 @@ Item {
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
if(QGroundControl.pairingManager.pairedDeviceNameList.length > 1) {
connectionPopup.open()
} else {
if(QGroundControl.pairingManager.pairingLinkTypeStrings.length > 1) if(QGroundControl.pairingManager.pairingLinkTypeStrings.length > 1)
pairingPopup.open() pairingPopup.open()
else { else {
...@@ -63,6 +66,7 @@ Item { ...@@ -63,6 +66,7 @@ Item {
} }
} }
} }
}
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
//-- Pairing //-- Pairing
Popup { Popup {
...@@ -91,7 +95,7 @@ Item { ...@@ -91,7 +95,7 @@ Item {
anchors.centerIn: parent anchors.centerIn: parent
Item { width: 1; height: 1; } Item { width: 1; height: 1; }
QGCLabel { QGCLabel {
text: qsTr("No Vehicles Available") text: qsTr("Pair New Vehicle")
font.pointSize: ScreenTools.mediumFontPointSize font.pointSize: ScreenTools.mediumFontPointSize
font.family: ScreenTools.demiboldFontFamily font.family: ScreenTools.demiboldFontFamily
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
...@@ -166,7 +170,7 @@ Item { ...@@ -166,7 +170,7 @@ Item {
anchors.centerIn: parent anchors.centerIn: parent
Item { width: 1; height: 1; } Item { width: 1; height: 1; }
QGCLabel { QGCLabel {
text: qsTr("No Vehicles Available") text: qsTr("Pair New Vehicle")
font.pointSize: ScreenTools.mediumFontPointSize font.pointSize: ScreenTools.mediumFontPointSize
font.family: ScreenTools.demiboldFontFamily font.family: ScreenTools.demiboldFontFamily
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
...@@ -235,7 +239,7 @@ Item { ...@@ -235,7 +239,7 @@ Item {
anchors.centerIn: parent anchors.centerIn: parent
Item { width: 1; height: 1; } Item { width: 1; height: 1; }
QGCLabel { QGCLabel {
text: qsTr("No Vehicles Available") text: qsTr("Pair New Vehicle")
font.pointSize: ScreenTools.mediumFontPointSize font.pointSize: ScreenTools.mediumFontPointSize
font.family: ScreenTools.demiboldFontFamily font.family: ScreenTools.demiboldFontFamily
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
...@@ -287,7 +291,7 @@ Item { ...@@ -287,7 +291,7 @@ Item {
parent: Overlay.overlay parent: Overlay.overlay
x: Math.round((mainWindow.width - width) * 0.5) x: Math.round((mainWindow.width - width) * 0.5)
y: Math.round((mainWindow.height - height) * 0.5) y: Math.round((mainWindow.height - height) * 0.5)
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside closePolicy: cancelButton.visible ? Popup.NoAutoClose : (Popup.CloseOnEscape | Popup.CloseOnPressOutside)
background: Rectangle { background: Rectangle {
anchors.fill: parent anchors.fill: parent
color: qgcPal.globalTheme === QGCPalette.Light ? Qt.rgba(1,1,1,0.95) : Qt.rgba(0,0,0,0.75) color: qgcPal.globalTheme === QGCPalette.Light ? Qt.rgba(1,1,1,0.95) : Qt.rgba(0,0,0,0.75)
...@@ -310,22 +314,46 @@ Item { ...@@ -310,22 +314,46 @@ Item {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
} }
Item { width: 1; height: 1; } Item { width: 1; height: 1; }
QGCColoredImage {
id: busyIndicator
height: ScreenTools.defaultFontPixelHeight * 2
width: height
source: "/qmlimages/MapSync.svg"
sourceSize.height: height
fillMode: Image.PreserveAspectFit
mipmap: true
smooth: true
color: qgcPal.text
visible: cancelButton.visible
anchors.horizontalCenter: parent.horizontalCenter
RotationAnimation on rotation {
loops: Animation.Infinite
from: 360
to: 0
duration: 720
running: busyIndicator.visible
}
}
QGCLabel { QGCLabel {
text: qsTr("List Of Available Devices") text: qsTr("List Of Available Devices")
visible: QGroundControl.pairingManager.pairedDeviceNameList.length > 0 visible: QGroundControl.pairingManager.pairedDeviceNameList.length > 0 && !cancelButton.visible
font.pointSize: ScreenTools.mediumFontPointSize font.pointSize: ScreenTools.mediumFontPointSize
font.family: ScreenTools.demiboldFontFamily font.family: ScreenTools.demiboldFontFamily
} }
Item { width: 1; height: 1; } Item { width: 1; height: 1; }
GridLayout { GridLayout {
columns: 3 columns: 3
visible: QGroundControl.pairingManager.pairedDeviceNameList.length > 0 visible: QGroundControl.pairingManager.pairedDeviceNameList.length > 0 && !cancelButton.visible
columnSpacing: ScreenTools.defaultFontPixelWidth
rowSpacing: ScreenTools.defaultFontPixelHeight * 0.25
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
Repeater { Repeater {
model: QGroundControl.pairingManager.pairedDeviceNameList model: QGroundControl.pairingManager.pairedDeviceNameList
QGCLabel { QGCLabel {
text: modelData text: modelData
Layout.minimumWidth: ScreenTools.defaultFontPixelWidth * 20 Layout.row: index
Layout.column: 0
Layout.minimumWidth:ScreenTools.defaultFontPixelWidth * 14
Layout.fillWidth: true Layout.fillWidth: true
} }
} }
...@@ -333,6 +361,11 @@ Item { ...@@ -333,6 +361,11 @@ Item {
model: QGroundControl.pairingManager.pairedDeviceNameList model: QGroundControl.pairingManager.pairedDeviceNameList
QGCButton { QGCButton {
text: qsTr("Connect") text: qsTr("Connect")
Layout.row: index
Layout.column: 1
onClicked: {
QGroundControl.pairingManager.connectToPairedDevice(modelData)
}
} }
} }
Repeater { Repeater {
...@@ -343,6 +376,8 @@ Item { ...@@ -343,6 +376,8 @@ Item {
sourceSize.height: height sourceSize.height: height
source: "/res/TrashDelete.svg" source: "/res/TrashDelete.svg"
color: qgcPal.colorRed color: qgcPal.colorRed
Layout.row: index
Layout.column: 2
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
...@@ -354,8 +389,8 @@ Item { ...@@ -354,8 +389,8 @@ Item {
} }
Item { width: 1; height: 1; } Item { width: 1; height: 1; }
RowLayout { RowLayout {
id: pairingButtons id: connectedButtons
visible: QGroundControl.pairingManager.status === PairingManager.PairingConnected visible: QGroundControl.pairingManager.pairingStatus === PairingManager.PairingConnected || QGroundControl.pairingManager.pairingStatus === PairingManager.PairingIdle
spacing: ScreenTools.defaultFontPixelWidth * 4 spacing: ScreenTools.defaultFontPixelWidth * 4
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
QGCButton { QGCButton {
...@@ -372,7 +407,7 @@ Item { ...@@ -372,7 +407,7 @@ Item {
} }
} }
QGCButton { QGCButton {
text: qsTr("Go And Fly") text: QGroundControl.pairingManager.pairingStatus === PairingManager.PairingConnected ? qsTr("Go And Fly") : qsTr("Close")
Layout.minimumWidth: _contentWidth * 0.333 Layout.minimumWidth: _contentWidth * 0.333
Layout.fillWidth: true Layout.fillWidth: true
onClicked: { onClicked: {
...@@ -382,12 +417,12 @@ Item { ...@@ -382,12 +417,12 @@ Item {
} }
QGCButton { QGCButton {
id: cancelButton id: cancelButton
visible: QGroundControl.pairingManager.status === PairingManager.PairingActive || QGroundControl.pairingManager.status === PairingManager.PairingConnecting visible: QGroundControl.pairingManager.pairingStatus === PairingManager.PairingActive || QGroundControl.pairingManager.pairingStatus === PairingManager.PairingConnecting
text: qsTr("Cancel") text: qsTr("Cancel")
width: _contentWidth width: _contentWidth
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
onClicked: { onClicked: {
if(QGroundControl.pairingManager.status === PairingManager.PairingActive) if(QGroundControl.pairingManager.pairingStatus === PairingManager.PairingActive)
QGroundControl.pairingManager.stopPairing() QGroundControl.pairingManager.stopPairing()
else { else {
//-- TODO: Cancel connection to paired device //-- TODO: Cancel connection to paired device
...@@ -396,7 +431,7 @@ Item { ...@@ -396,7 +431,7 @@ Item {
} }
} }
QGCButton { QGCButton {
visible: !cancelButton.visible && !pairingButtons.visible visible: !cancelButton.visible && !connectedButtons.visible
text: qsTr("Close") text: qsTr("Close")
width: _contentWidth width: _contentWidth
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment