APMRemoteParamsDownloader.cc 6.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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
/*=====================================================================

 QGroundControl Open Source Ground Control Station

 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

 This file is part of the QGROUNDCONTROL project

 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

 ======================================================================*/

#include "APMRemoteParamsDownloader.h"

#include <QMessageBox>
#include <QDesktopServices>
#include <QFile>
#include <QFileDialog>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTimer>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QCryptographicHash>
#include <QApplication>

#include "APMRemoteParamsDownloader.h"

#define FRAME_PARAMS_LIST QUrl("https://api.github.com/repos/diydrones/ardupilot/contents/Tools/Frame_params")
#define FRAME_PARAMS_URL "https://raw.github.com/diydrones/ardupilot/master/Tools/Frame_params/"

static QString dataLocation;

APMRemoteParamsDownloader::APMRemoteParamsDownloader(const QString& file) :
    m_fileToDownload(file),
    m_networkReply(NULL),
    m_downloadedParamFile(NULL)
{
    dataLocation = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0)
           + QDir::separator() + qApp->applicationName();
    refreshParamList();
}

QString APMRemoteParamsDownloader::statusText() const
{
    return m_statusText;
}
void APMRemoteParamsDownloader::setStatusText(const QString& text)
{
    m_statusText = text;
}

void APMRemoteParamsDownloader::refreshParamList()
{
    setStatusText(tr("Refresh Param file list"));

    QUrl url = FRAME_PARAMS_LIST;
    m_networkReply->deleteLater();
    m_networkReply = m_networkAccessManager.get(QNetworkRequest(url));
    connect(m_networkReply, SIGNAL(finished()), this, SLOT(httpParamListFinished()));
    connect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));
}

/* Returned Json Example
    "_links": {
        "git":"https://api.github.com/repos/diydrones/ardupilot/git/blobs/a7074e606d695566f9a8c87724ad52e5e3baba7d",
        "html":"https://github.com/diydrones/ardupilot/blob/master/Tools/Frame_params/Parrot_Bebop.param",
        "self":"https://api.github.com/repos/diydrones/ardupilot/contents/Tools/Frame_params/Parrot_Bebop.param?ref=master"
    },
    "download_url":"https://raw.githubusercontent.com/diydrones/ardupilot/master/Tools/Frame_params/Parrot_Bebop.param",
    "git_url":"https://api.github.com/repos/diydrones/ardupilot/git/blobs/a7074e606d695566f9a8c87724ad52e5e3baba7d",
    "html_url":"https://github.com/diydrones/ardupilot/blob/master/Tools/Frame_params/Parrot_Bebop.param",
    "name":"Parrot_Bebop.param","path":"Tools/Frame_params/Parrot_Bebop.param",""
    "sha":"a7074e606d695566f9a8c87724ad52e5e3baba7d",
    "size":533,
    "type":"file",
    "url":"https://api.github.com/repos/diydrones/ardupilot/contents/Tools/Frame_params/Parrot_Bebop.param?ref=master"
*/
void APMRemoteParamsDownloader::startFileDownloadRequest()
{
    QUrl url;

    QJsonObject obj;

    // Find the correct file from the json file list.
    while(curr != end) {
        obj = (*curr).toObject();
        url = QUrl(obj["download_url"].toString());
        QString name = obj["name"].toString();
        if (name == m_fileToDownload) {
            break;
        }
        curr++;
    }
    if (curr == end)
        return;

    QDir parameterDir(dataLocation);
    if (!parameterDir.exists())
        parameterDir.mkpath(dataLocation);

    QString filename = parameterDir.absoluteFilePath(obj["name"].toString());

    if(m_downloadedParamFile)
        m_downloadedParamFile->deleteLater();
    m_downloadedParamFile = new QFile(filename);
    m_downloadedParamFile->open(QIODevice::WriteOnly);

    m_networkReply = m_networkAccessManager.get(QNetworkRequest(url));
    connect(m_networkReply, SIGNAL(finished()), this, SLOT(httpFinished()));
    connect(m_networkReply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
    connect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDataReadProgress(qint64,qint64)));
    curr++;
}

void APMRemoteParamsDownloader::httpFinished()
{
    m_downloadedParamFile->flush();
    m_downloadedParamFile->close();

    if (m_networkReply->error()) {
        m_downloadedParamFile->remove();
        setStatusText(tr("Download failed: %1.").arg(m_networkReply->errorString()));
    }

    m_networkReply->deleteLater();
    m_networkReply = NULL;
    delete m_downloadedParamFile;
    m_downloadedParamFile = NULL;

    emit finished();
}

void APMRemoteParamsDownloader::httpReadyRead()
{
    if (m_downloadedParamFile)
        m_downloadedParamFile->write(m_networkReply->readAll());
}

void APMRemoteParamsDownloader::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
    Q_UNUSED(bytesRead);
    Q_UNUSED(totalBytes);
}

void APMRemoteParamsDownloader::httpParamListFinished()
{
    if (m_networkReply->error()) {
        qDebug() <<  "Download failed:" << m_networkReply->errorString();
        return;
    }
    processDownloadedVersionObject(m_networkReply->readAll());
    startFileDownloadRequest();
}

void APMRemoteParamsDownloader::processDownloadedVersionObject(const QByteArray &listObject)
{
    QJsonParseError jsonErrorChecker;
    QJsonDocument jsonDocument = QJsonDocument::fromJson(listObject, &jsonErrorChecker);
    if (jsonErrorChecker.error != QJsonParseError::NoError) {
        qDebug() << "Json error while parsing document:" << jsonErrorChecker.errorString();
        return;
    }

    m_documentArray = jsonDocument.array();
    curr = m_documentArray.constBegin();
    end = m_documentArray.constEnd();
}