[Pkg-running-devel] [openambit] 57/131: Added upload of logs to movescount when autorization completed.

Christian Perrier bubulle at moszumanska.debian.org
Thu Jul 17 20:19:11 UTC 2014


This is an automated email from the git hooks/post-receive script.

bubulle pushed a commit to branch master
in repository openambit.

commit b54095d3790c9b40eda29ec85e9c43166d97ddbd
Author: Emil Ljungdahl <emil at kratern.se>
Date:   Mon Jan 20 21:17:06 2014 +0100

    Added upload of logs to movescount when autorization completed.
---
 src/openambit/mainwindow.cpp            |   4 +-
 src/openambit/movescount.cpp            | 143 ++++++++++++++++++++++++++++----
 src/openambit/movescount.h              |  28 +++++--
 src/openambit/movescountjson.cpp        |  37 ++++++++-
 src/openambit/movescountjson.h          |   3 +
 src/openambit/movescountlogdirentry.cpp |  24 ++++++
 src/openambit/movescountlogdirentry.h   |  26 ++++++
 src/openambit/openambit.pro             |   6 +-
 8 files changed, 241 insertions(+), 30 deletions(-)

diff --git a/src/openambit/mainwindow.cpp b/src/openambit/mainwindow.cpp
index 1027943..bfb0872 100644
--- a/src/openambit/mainwindow.cpp
+++ b/src/openambit/mainwindow.cpp
@@ -392,7 +392,9 @@ void MainWindow::movesCountSetup()
             connect(movesCount, SIGNAL(newerFirmwareExists(QByteArray)), this, SLOT(newerFirmwareExists(QByteArray)), Qt::QueuedConnection);
             connect(movesCount, SIGNAL(movesCountAuth(bool)), this, SLOT(movesCountAuth(bool)), Qt::QueuedConnection);
         }
-        movesCount->setUsername(settings.value("email").toString());
+        if (movescountEnable) {
+            movesCount->setUsername(settings.value("email").toString());
+        }
     }
     settings.endGroup();
 }
diff --git a/src/openambit/movescount.cpp b/src/openambit/movescount.cpp
index 405045d..ffa08de 100644
--- a/src/openambit/movescount.cpp
+++ b/src/openambit/movescount.cpp
@@ -24,6 +24,8 @@
 #include <QEventLoop>
 #include <QMutex>
 
+#include "logstore.h"
+
 #define AUTH_CHECK_TIMEOUT 5000 /* ms */
 #define GPS_ORBIT_DATA_MIN_SIZE 30000 /* byte */
 
@@ -60,6 +62,8 @@ void MovesCount::setAppkey(QString appkey)
 void MovesCount::setUsername(QString username)
 {
     this->username = username;
+
+    checkAuthorization();
 }
 
 void MovesCount::setUserkey(QString userkey)
@@ -134,6 +138,33 @@ int MovesCount::getDeviceSettings()
     return -1;
 }
 
+QList<MovesCountLogDirEntry> MovesCount::getMovescountEntries(QDate startTime, QDate endTime)
+{
+    QNetworkReply *reply;
+    QList<MovesCountLogDirEntry> retList;
+
+    reply = syncGET("/moves/private", "startdate=" + startTime.toString("yyyy-MM-dd") + "&enddate=" + endTime.toString("yyyy-MM-dd"), true);
+
+    if (checkReplyAuthorization(reply)) {
+        QByteArray _data = reply->readAll();
+
+        if (jsonParser.parseLogDirReply(_data, retList) != 0) {
+            // empty list if parse failed
+            retList.clear();
+        }
+    }
+
+    return retList;
+}
+
+void MovesCount::checkAuthorization()
+{
+    if (authCheckReply == NULL) {
+        authCheckReply = asyncGET("/members/private", "", true);
+        connect(authCheckReply, SIGNAL(finished()), this, SLOT(authCheckFinished()));
+    }
+}
+
 void MovesCount::checkLatestFirmwareVersion()
 {
     if (firmwareCheckReply == NULL) {
@@ -169,6 +200,15 @@ void MovesCount::writeLog(LogEntry *logEntry)
     }
 }
 
+void MovesCount::authCheckFinished()
+{
+    if (authCheckReply != NULL) {
+        checkReplyAuthorization(authCheckReply);
+        authCheckReply->deleteLater();
+        authCheckReply = NULL;
+    }
+}
+
 void MovesCount::firmwareReplyFinished()
 {
     u_int8_t fw_version[4];
@@ -195,10 +235,95 @@ void MovesCount::recheckAuthorization()
     getDeviceSettings();
 }
 
+void MovesCount::handleAuthorizationSignal(bool authorized)
+{
+    if (authorized) {
+        checkUploadedLogs();
+    }
+}
+
 MovesCount::MovesCount() :
-    firmwareCheckReply(NULL)
+    authorized(false), uploadedCheckRunning(false), authCheckReply(NULL), firmwareCheckReply(NULL)
 {
     this->manager = new QNetworkAccessManager(this);
+
+    this->moveToThread(&workerThread);
+    workerThread.start();
+
+    connect(this, SIGNAL(movesCountAuth(bool)), this, SLOT(handleAuthorizationSignal(bool)));
+}
+
+MovesCount::~MovesCount()
+{
+    workerThread.exit();
+    workerThread.wait();
+}
+
+bool MovesCount::checkReplyAuthorization(QNetworkReply *reply)
+{
+    if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
+        authorized = false;
+        emit movesCountAuth(false);
+        QTimer::singleShot(AUTH_CHECK_TIMEOUT, this, SLOT(recheckAuthorization()));
+    }
+    else if(reply->error() == QNetworkReply::NoError) {
+        authorized = true;
+        emit movesCountAuth(true);
+    }
+
+    return authorized;
+}
+
+void MovesCount::checkUploadedLogs()
+{
+    QDateTime firstUnknown = QDateTime::currentDateTime();
+    QDateTime lastUnknown = QDateTime::fromTime_t(0);
+    QList<LogEntry*> missingEntries;
+
+    if (!uploadedCheckRunning) {
+        uploadedCheckRunning = true;
+
+        QList<LogStore::LogDirEntry> entries = logStore.dir();
+        foreach(LogStore::LogDirEntry entry, entries) {
+            LogEntry *logEntry = logStore.read(entry);
+            if (logEntry->movescountId.length() == 0) {
+                missingEntries.append(logEntry);
+                if (logEntry->time < firstUnknown) {
+                    firstUnknown = logEntry->time;
+                }
+                if (logEntry->time > lastUnknown) {
+                    lastUnknown = logEntry->time;
+                }
+            }
+            else {
+                delete logEntry;
+            }
+        }
+
+        if (missingEntries.count() > 0) {
+            QList<MovesCountLogDirEntry> movescountEntries = getMovescountEntries(firstUnknown.date(), lastUnknown.date());
+            foreach(MovesCountLogDirEntry entry, movescountEntries) {
+                foreach(LogEntry *logEntry, missingEntries) {
+                    if (entry.time == logEntry->time) {
+                        missingEntries.removeOne(logEntry);
+                        logStore.storeMovescountId(logEntry->device, logEntry->time, entry.moveId);
+                        delete logEntry;
+                        break;
+                    }
+                }
+            }
+
+            // Delete remaining entries
+            while (missingEntries.count() > 0) {
+                LogEntry *logEntry = missingEntries.first();
+                writeLog(logEntry);
+                missingEntries.removeOne(logEntry);
+                delete logEntry;
+            }
+        }
+
+        uploadedCheckRunning = false;
+    }
 }
 
 QNetworkReply *MovesCount::asyncGET(QString path, QString additionalHeaders, bool auth)
@@ -261,19 +386,3 @@ QNetworkReply *MovesCount::syncPOST(QString path, QString additionalHeaders, QBy
 
     return reply;
 }
-
-
-bool MovesCount::checkReplyAuthorization(QNetworkReply *reply)
-{
-    if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
-        authorized = false;
-        emit movesCountAuth(false);
-        QTimer::singleShot(AUTH_CHECK_TIMEOUT, this, SLOT(recheckAuthorization()));
-    }
-    else if(reply->error() == QNetworkReply::NoError) {
-        authorized = true;
-        emit movesCountAuth(true);
-    }
-
-    return authorized;
-}
diff --git a/src/openambit/movescount.h b/src/openambit/movescount.h
index fd29340..045481b 100644
--- a/src/openambit/movescount.h
+++ b/src/openambit/movescount.h
@@ -23,12 +23,18 @@
 #define MOVESCOUNT_H
 
 #include <QObject>
+#include <QList>
+#include <QThread>
+#include <QTimer>
 #include <QtNetwork/QNetworkAccessManager>
 #include <QtNetwork/QNetworkReply>
-#include <QTimer>
+
 #include <libambit.h>
+
 #include "logentry.h"
+#include "logstore.h"
 #include "movescountjson.h"
+#include "movescountlogdirentry.h"
 
 class MovesCount : public QObject
 {
@@ -47,24 +53,30 @@ public:
     int getOrbitalData(u_int8_t **data);
     int getPersonalSettings(ambit_personal_settings_t *settings);
     int getDeviceSettings();
+    QList<MovesCountLogDirEntry> getMovescountEntries(QDate startTime, QDate endTime);
 signals:
     void newerFirmwareExists(QByteArray fw_version);
     void movesCountAuth(bool authorized);
     void logMoveID(QString device, QDateTime time, QString moveID);
     
 public slots:
+    void checkAuthorization();
     void checkLatestFirmwareVersion();
     void writePersonalSettings(ambit_personal_settings_t *settings);
     void writeLog(LogEntry *logEntry);
 
 private slots:
+    void authCheckFinished();
     void firmwareReplyFinished();
     void recheckAuthorization();
+    void handleAuthorizationSignal(bool authorized);
 
 private:
     MovesCount();
-    MovesCount(const MovesCount &);
-    MovesCount& operator=(const MovesCount &);
+    ~MovesCount();
+
+    bool checkReplyAuthorization(QNetworkReply *reply);
+    void checkUploadedLogs();
 
     QNetworkReply *asyncGET(QString path, QString additionalHeaders, bool auth);
     QNetworkReply *syncGET(QString path, QString additionalHeaders, bool auth);
@@ -72,9 +84,8 @@ private:
     QNetworkReply *asyncPOST(QString path, QString additionalHeaders, QByteArray &postData, bool auth);
     QNetworkReply *syncPOST(QString path, QString additionalHeaders, QByteArray &postData, bool auth);
 
-    bool checkReplyAuthorization(QNetworkReply *reply);
-
-    bool authorized = false;
+    bool authorized;
+    bool uploadedCheckRunning;
 
     QString baseAddress;
     QString appkey;
@@ -86,8 +97,13 @@ private:
 
     QNetworkAccessManager *manager;
     QNetworkReply *firmwareCheckReply;
+    QNetworkReply *authCheckReply;
 
     MovesCountJSON jsonParser;
+
+    LogStore logStore;
+
+    QThread workerThread;
 };
 
 #endif // MOVESCOUNT_H
diff --git a/src/openambit/movescountjson.cpp b/src/openambit/movescountjson.cpp
index cac393f..8fe30b7 100644
--- a/src/openambit/movescountjson.cpp
+++ b/src/openambit/movescountjson.cpp
@@ -78,6 +78,31 @@ int MovesCountJSON::parseLogReply(QByteArray &input, QString &moveId)
     return -1;
 }
 
+int MovesCountJSON::parseLogDirReply(QByteArray &input, QList<MovesCountLogDirEntry> &entries)
+{
+    QJson::Parser parser;
+    QVariantList logList;
+
+    bool ok;
+
+    if (input.length() <= 0) {
+        return -1;
+    }
+
+    logList = parser.parse(input, &ok).toList();
+
+    if (ok) {
+        foreach(QVariant entryVar, logList) {
+            QVariantMap entry = entryVar.toMap();
+            entries.append(MovesCountLogDirEntry(entry["MoveID"].toString(), QDateTime::fromString(entry["LocalStartTime"].toString(), "yyyy-MM-ddThh:mm:ss.zzz"), entry["ActivityID"].toUInt()));
+        }
+
+        return 0;
+    }
+
+    return -1;
+}
+
 int MovesCountJSON::generateLogData(LogEntry *logEntry, QByteArray &output)
 {
     QJson::Serializer serializer;
@@ -219,7 +244,8 @@ int MovesCountJSON::generateLogData(LogEntry *logEntry, QByteArray &output)
     content.insert("Duration", (double)logEntry->logEntry->header.duration/1000.0);
     content.insert("Energy", logEntry->logEntry->header.energy_consumption);
     content.insert("FlatTime", QVariant::Invalid);
-    content.insert("HighAltitude", (double)logEntry->logEntry->header.altitude_max);
+    if (logEntry->logEntry->header.altitude_max >= -1000 && logEntry->logEntry->header.altitude_max <= 10000)
+        content.insert("HighAltitude", (double)logEntry->logEntry->header.altitude_max);
     if (IBIContent.count() > 0) {
         uncompressedData = serializer.serialize(IBIContent);
         compressData(uncompressedData, compressedData);
@@ -228,7 +254,8 @@ int MovesCountJSON::generateLogData(LogEntry *logEntry, QByteArray &output)
         content.insert("IBIData", IBIDataMap);                        /* compressed */
     }
     content.insert("LocalStartTime", dateTimeString(localBaseTime));
-    content.insert("LowAltitude", (double)logEntry->logEntry->header.altitude_min);
+    if (logEntry->logEntry->header.altitude_min >= -1000 && logEntry->logEntry->header.altitude_min <= 10000)
+        content.insert("LowAltitude", (double)logEntry->logEntry->header.altitude_min);
     content.insert("Marks", marksContent);
     content.insert("MaxCadence", QVariant::Invalid);
     content.insert("MaxSpeed", (double)logEntry->logEntry->header.speed_max/3600.0);
@@ -312,7 +339,8 @@ bool MovesCountJSON::writePeriodicSample(ambit_log_sample_t *sample, QVariantMap
             output.insert("EVPE", value->u.evpe);
             break;
         case ambit_log_sample_periodic_type_altitude:
-            output.insert("Altitude", (double)value->u.altitude);
+            if (value->u.altitude >= -1000 && value->u.altitude <= 10000)
+                output.insert("Altitude", (double)value->u.altitude);
             break;
         case ambit_log_sample_periodic_type_abspressure:
             output.insert("AbsPressure", (int)round((double)value->u.abspressure/10.0));
@@ -327,7 +355,8 @@ bool MovesCountJSON::writePeriodicSample(ambit_log_sample_t *sample, QVariantMap
             output.insert("BatteryCharge", (double)value->u.charge/100.0);
             break;
         case ambit_log_sample_periodic_type_gpsaltitude:
-            output.insert("GPSAltitude", value->u.gpsaltitude);
+            if (value->u.gpsaltitude >= -1000 && value->u.gpsaltitude <= 10000)
+                output.insert("GPSAltitude", value->u.gpsaltitude);
             break;
         case ambit_log_sample_periodic_type_gpsheading:
             output.insert("GPSHeading", (double)value->u.gpsheading/10000000);
diff --git a/src/openambit/movescountjson.h b/src/openambit/movescountjson.h
index 04b6c44..caf7de2 100644
--- a/src/openambit/movescountjson.h
+++ b/src/openambit/movescountjson.h
@@ -26,7 +26,9 @@
 #include <QList>
 #include <QVariantMap>
 #include <libambit.h>
+
 #include "logentry.h"
+#include "movescountlogdirentry.h"
 
 class MovesCountJSON : public QObject
 {
@@ -36,6 +38,7 @@ public:
 
     int parseFirmwareVersionReply(QByteArray &input, u_int8_t fw_version[4]);
     int parseLogReply(QByteArray &input, QString &moveId);
+    int parseLogDirReply(QByteArray &input, QList<MovesCountLogDirEntry> &entries);
 
     int generateLogData(LogEntry *logEntry, QByteArray &output);
     
diff --git a/src/openambit/movescountlogdirentry.cpp b/src/openambit/movescountlogdirentry.cpp
new file mode 100644
index 0000000..6dd919e
--- /dev/null
+++ b/src/openambit/movescountlogdirentry.cpp
@@ -0,0 +1,24 @@
+#include "movescountlogdirentry.h"
+
+MovesCountLogDirEntry::MovesCountLogDirEntry(QString moveId, QDateTime time, u_int8_t activityId, QObject *parent) :
+    QObject(parent), moveId(moveId), time(time), activityId(activityId)
+{
+}
+
+MovesCountLogDirEntry::MovesCountLogDirEntry(const MovesCountLogDirEntry &other) :
+    QObject(other.parent()), moveId(other.moveId), time(other.time), activityId(other.activityId)
+{
+}
+
+MovesCountLogDirEntry::~MovesCountLogDirEntry()
+{
+}
+
+MovesCountLogDirEntry& MovesCountLogDirEntry::operator=(const MovesCountLogDirEntry &rhs)
+{
+    moveId = rhs.moveId;
+    time = rhs.time;
+    activityId = rhs.activityId;
+
+    return *this;
+}
diff --git a/src/openambit/movescountlogdirentry.h b/src/openambit/movescountlogdirentry.h
new file mode 100644
index 0000000..3fc0bbd
--- /dev/null
+++ b/src/openambit/movescountlogdirentry.h
@@ -0,0 +1,26 @@
+#ifndef MOVESCOUNTLOGDIRENTRY_H
+#define MOVESCOUNTLOGDIRENTRY_H
+
+#include <QObject>
+#include <QDateTime>
+
+class MovesCountLogDirEntry : public QObject
+{
+    Q_OBJECT
+public:
+    explicit MovesCountLogDirEntry(QString moveId, QDateTime time, u_int8_t activityId, QObject *parent = 0);
+    MovesCountLogDirEntry(const MovesCountLogDirEntry &other);
+    ~MovesCountLogDirEntry();
+
+    MovesCountLogDirEntry& operator=(const MovesCountLogDirEntry &rhs);
+
+    QString moveId;
+    QDateTime time;
+    u_int8_t activityId;
+signals:
+
+public slots:
+
+};
+
+#endif // MOVESCOUNTLOGDIRENTRY_H
diff --git a/src/openambit/openambit.pro b/src/openambit/openambit.pro
index d667be7..92eab30 100644
--- a/src/openambit/openambit.pro
+++ b/src/openambit/openambit.pro
@@ -23,7 +23,8 @@ SOURCES += main.cpp\
     movescountjson.cpp \
     movescount.cpp \
     udevlistener.cpp \
-    confirmbetadialog.cpp
+    confirmbetadialog.cpp \
+    movescountlogdirentry.cpp
 
 HEADERS  += mainwindow.h \
     devicemanager.h \
@@ -35,7 +36,8 @@ HEADERS  += mainwindow.h \
     movescountjson.h \
     movescount.h \
     udevlistener.h \
-    confirmbetadialog.h
+    confirmbetadialog.h \
+    movescountlogdirentry.h
 
 FORMS    += mainwindow.ui \
     settingsdialog.ui \

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-running/openambit.git



More information about the Pkg-running-devel mailing list