Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add menu option for save watch configs #258

Merged
merged 9 commits into from
Oct 24, 2020
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@ More information
----------------

Look at the wiki at https://github.com/openambitproject/openambit/wiki for
more information.
more information.
15 changes: 8 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ for target in libambit movescount openambit openambit-cli
do
cd ${SOURCE_LOCATION}
echo "------building $target------"
mkdir -p ${target}-build
cd ${target}-build
cmake "$@" ../src/${target}
make -j${CORES}
if [ "${DO_INSTALL-0}" == "1" ]; then
echo "------installing $target------"
sudo make install
mkdir -p $target-build
cd $target-build
cmake "$@" ../src/$target
make -j$CORES
if [ "$DO_INSTALL" == "1" ]; then
echo "------installing $target------"
sudo make install
sudo ldconfig
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some unrelated newline/blank changes, maybe you can revert them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will revert them

fi
done

Expand Down
91 changes: 91 additions & 0 deletions src/movescount/movescount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <QEventLoop>
#include <QMutex>
#include <QDebug>
#include <QDir>
#include <QJsonDocument>

#include "logstore.h"

Expand Down Expand Up @@ -204,6 +206,23 @@ int MovesCount::getCustomModeData(ambit_sport_mode_device_settings_t* ambitCusto
return ret;
}

int MovesCount::getWatchModeConfig(ambit_sport_mode_device_settings_t* ambitCustomModes)
{
int ret = -1;

if (&workerThread == QThread::currentThread()) {
ret = getWatchModeDataThread(ambitCustomModes);
}
else {
QMetaObject::invokeMethod(this, "getWatchModeDataThread", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(int, ret),
Q_ARG(ambit_sport_mode_device_settings_t*, ambitCustomModes));
}

return ret;
}


int MovesCount::getAppsData(ambit_app_rules_t* ambitApps)
{
int ret = -1;
Expand All @@ -220,6 +239,22 @@ int MovesCount::getAppsData(ambit_app_rules_t* ambitApps)
return ret;
}

int MovesCount::getWatchAppConfig(ambit_app_rules_t* ambitApps)
{
int ret = -1;

if (&workerThread == QThread::currentThread()) {
ret = getWatchAppConfigThread(ambitApps);
}
else {
QMetaObject::invokeMethod(this, "getWatchAppConfigThread", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(int, ret),
Q_ARG(ambit_app_rules_t*, ambitApps));
}

return ret;
}

QList<MovesCountLogDirEntry> MovesCount::getMovescountEntries(QDate startTime, QDate endTime)
{
QList<MovesCountLogDirEntry> retList;
Expand Down Expand Up @@ -471,6 +506,20 @@ void MovesCount::getDeviceSettingsInThread()
}
}

void writeJson(QByteArray _data, const char* name) {
QFile logfile(name);
logfile.open(QIODevice::WriteOnly | QIODevice::Truncate);

// pretty print JSON
QJsonDocument doc = QJsonDocument::fromJson(_data);
QString formattedJsonString = doc.toJson(QJsonDocument::Indented);

//formattedJsonString.replace("", "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can remove this commented code here


logfile.write(formattedJsonString.toUtf8());
logfile.close();
}

int MovesCount::getCustomModeDataInThread(ambit_sport_mode_device_settings_t *ambitSettings)
{
int ret = -1;
Expand All @@ -491,6 +540,28 @@ int MovesCount::getCustomModeDataInThread(ambit_sport_mode_device_settings_t *am
return ret;
}

int MovesCount::getWatchModeDataThread(ambit_sport_mode_device_settings_t *ambitSettings)
{
int ret = -1;
QNetworkReply *reply;

reply = syncGET("/userdevices/" + device_info.serial, "", true);

if (checkReplyAuthorization(reply)) {
QByteArray _data = reply->readAll();
MovescountSettings settings = MovescountSettings();

writeJson(_data, QString(getenv("HOME")).toUtf8() + "/.openambit/settings.json");

if (jsonParser.parseDeviceSettingsReply(_data, settings) == 0) {
settings.toAmbitData(ambitSettings);
ret = 0;
}
}

return ret;
}

int MovesCount::getAppsDataInThread(ambit_app_rules_t* ambitApps)
{
int ret = -1;
Expand All @@ -509,6 +580,26 @@ int MovesCount::getAppsDataInThread(ambit_app_rules_t* ambitApps)
return ret;
}

int MovesCount::getWatchAppConfigThread(ambit_app_rules_t* ambitApps)
{
int ret = -1;
QNetworkReply *reply;

reply = syncGET("/rules/private", "", true);

if (checkReplyAuthorization(reply)) {
QByteArray _data = reply->readAll();

writeJson(_data, QString(getenv("HOME")).toUtf8() + "/.openambit/apprules.json");

if (jsonParser.parseAppRulesReply(_data, ambitApps) == 0) {
ret = 0;
}
}

return ret;
}

QList<MovesCountLogDirEntry> MovesCount::getMovescountEntriesInThread(QDate startTime, QDate endTime)
{
QNetworkReply *reply;
Expand Down
4 changes: 4 additions & 0 deletions src/movescount/movescount.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class MovesCount : public QObject
int applyPersonalSettingsFromDevice(ambit_personal_settings_t *movesPersonalSettings, ambit_personal_settings_t *devicePersonalSettings);
void getDeviceSettings();
int getCustomModeData(ambit_sport_mode_device_settings_t *ambitCustomModes);
int getWatchModeConfig(ambit_sport_mode_device_settings_t* ambitCustomModes);
int getAppsData(ambit_app_rules_t *ambitApps);
int getWatchAppConfig(ambit_app_rules_t* ambitApps);
QList<MovesCountLogDirEntry> getMovescountEntries(QDate startTime, QDate endTime);

void checkAuthorization();
Expand All @@ -87,7 +89,9 @@ private slots:
int getRoutePointsInThread(ambit_route_t *routes, ambit_personal_settings_t *ps, QString url);
void getDeviceSettingsInThread();
int getCustomModeDataInThread(ambit_sport_mode_device_settings_t *ambitSettings);
int getWatchModeDataThread(ambit_sport_mode_device_settings_t *ambitSettings);
int getAppsDataInThread(ambit_app_rules_t *ambitApps);
int getWatchAppConfigThread(ambit_app_rules_t* ambitApps);
QList<MovesCountLogDirEntry> getMovescountEntriesInThread(QDate startTime, QDate endTime);

void checkAuthorizationInThread();
Expand Down
1 change: 1 addition & 0 deletions src/openambit-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
personal_config.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this gitignore useful in general or just a leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a leftover. I used to store a different APPKEY there, because on Task.cpp I had:

+#include "personal_config.h"
+++ b/src/openambit-cli/Task.cpp
@@ -7,8 +7,11 @@
 #include <movescount/logstore.h>
 #include <movescount/movescountxml.h>
 #include <libambit_int.h>
+#include "personal_config.h"
 
-#define APPKEY                 "HpF9f1qV5qrDJ1hY1QK1diThyPsX10Mh4JvCw9xVQSglJNLdcwr3540zFyLzIC3e"
+#ifndef APPKEY
+  #define APPKEY                 "HpF9f1qV5qrDJ1hY1QK1diThyPsX10Mh4JvCw9xVQSglJNLdcwr3540zFyLzIC3e"
+#endif
 #define MOVESCOUNT_DEFAULT_URL "https://uiservices.movescount.com/"

14 changes: 14 additions & 0 deletions src/openambit/devicemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void DeviceManager::startSync(bool readAllLogs = false)
bool syncOrbit = settings.value("syncSettings/syncOrbit", true).toBool();
bool syncSportMode = settings.value("syncSettings/syncSportMode", false).toBool();
bool syncNavigation = settings.value("syncSettings/syncNavigation", false).toBool();
bool syncWatchJsonConfig= settings.value("syncSettings/syncWatchJsonConfig", false).toBool();
bool syncMovescount = settings.value("movescountSettings/movescountEnable", false).toBool();

mutex.lock();
Expand All @@ -114,6 +115,7 @@ void DeviceManager::startSync(bool readAllLogs = false)
if (syncOrbit) syncParts+=2;
if (syncSportMode) syncParts++;
if (syncMovescount) syncParts++;
if (syncWatchJsonConfig) syncParts++;

if (this->deviceObject != NULL) {
emit this->syncProgressInform(QString(tr("Reading personal settings")), false, true, 0);
Expand Down Expand Up @@ -141,6 +143,18 @@ void DeviceManager::startSync(bool readAllLogs = false)
qDebug() << "End reading log...";
}

if (syncWatchJsonConfig) {
qDebug() << "Start sync watch apps to JSON";
emit this->syncProgressInform(QString(tr("Synchronizing Watch apps and modes config")), false, true, 100*currentSyncPart/syncParts);

ambit_app_rules_t* ambitApps = liblibambit_malloc_app_rules();
movesCount->getWatchAppConfig(ambitApps);

ambit_sport_mode_device_settings_t *ambitDeviceSettings = libambit_malloc_sport_mode_device_settings();
movesCount->getWatchModeConfig(ambitDeviceSettings);
qDebug() << "End sync watch apps to JSON";
}

if (waypoint_sync_res != -1 && syncNavigation) {
qDebug() << "Start reading navigation...";
emit this->syncProgressInform(QString(tr("Synchronizing navigation")), false, true, 100*currentSyncPart/syncParts);
Expand Down
4 changes: 3 additions & 1 deletion src/openambit/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,19 @@ void MainWindow::movesCountSetup()
bool syncOrbit = false;
bool syncSportMode = false;
bool syncNavigation = false;
bool syncWatchJsonConfig = false;
bool movescountEnable = false;

settings.beginGroup("syncSettings");
syncOrbit = settings.value("syncOrbit", true).toBool();
syncSportMode = settings.value("syncSportMode", false).toBool();
syncNavigation = settings.value("syncNavigation", false).toBool();
syncWatchJsonConfig = settings.value("syncWatchJsonConfig", false).toBool();
settings.endGroup();

settings.beginGroup("movescountSettings");
movescountEnable = settings.value("movescountEnable", false).toBool();
if (syncOrbit || syncSportMode || syncNavigation || movescountEnable) {
if (syncOrbit || syncSportMode || syncNavigation || movescountEnable || syncWatchJsonConfig) {
if (movesCount == NULL) {
movesCount = MovesCount::instance();
movesCount->setAppkey(APPKEY);
Expand Down
2 changes: 2 additions & 0 deletions src/openambit/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void SettingsDialog::readSettings()
ui->checkBoxSyncOrbit->setChecked(settings.value("syncOrbit", true).toBool());
ui->checkBoxSyncSportsMode->setChecked(settings.value("syncSportMode", false).toBool());
ui->checkBoxSyncNavigation->setChecked(settings.value("syncNavigation", false).toBool());
ui->checkBoxSyncWatchJsonConfig->setChecked(settings.value("syncWatchJsonConfig", false).toBool());
settings.endGroup();

settings.beginGroup("movescountSettings");
Expand All @@ -103,6 +104,7 @@ void SettingsDialog::writeSettings()
settings.setValue("syncOrbit", ui->checkBoxSyncOrbit->isChecked());
settings.setValue("syncSportMode", ui->checkBoxSyncSportsMode->isChecked());
settings.setValue("syncNavigation", ui->checkBoxSyncNavigation->isChecked());
settings.setValue("syncWatchJsonConfig", ui->checkBoxSyncWatchJsonConfig->isChecked());
settings.endGroup();

settings.beginGroup("movescountSettings");
Expand Down
7 changes: 7 additions & 0 deletions src/openambit/settingsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QCheckBox" name="checkBoxSyncWatchJsonConfig">
<property name="text">
<string>Save configuration for the watch (JSON file)</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down