Skip to content

Commit

Permalink
Add application openambit-routes for sending routes from files to the…
Browse files Browse the repository at this point in the history
… watch

Implement reading and parsing JSON from files
to be able to send routes from locally prepared files
to the watch
  • Loading branch information
centic9 committed Sep 10, 2022
1 parent 5eeb988 commit e2bff57
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ add_subdirectory(src/libambit)
add_subdirectory(src/movescount)
add_subdirectory(src/openambit)
add_subdirectory(src/openambit-cli)
add_subdirectory(src/openambit-routes)
add_subdirectory(src/unittest)

if (NOT ${LIBAMBIT_FOUND})
add_dependencies(movescount ambit)
add_dependencies(openambit ambit)
add_dependencies(openambit-cli ambit)
add_dependencies(openambit-routes ambit)
add_dependencies(unittest ambit)
endif ()
if (NOT ${MOVESCOUNT_FOUND})
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [ -r /proc/cpuinfo ]; then
CORES=$(cat /proc/cpuinfo | grep processor | wc -l)
fi

for target in libambit movescount openambit openambit-cli unittest
for target in libambit movescount openambit openambit-cli openambit-routes unittest
do
cd ${SOURCE_LOCATION}
echo "------building $target------"
Expand Down
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ case "$application" in
openambit) builddir=${application}-build;;
ambitconsole) builddir=example-build;;
openambit-cli) builddir=${application}-build;;
openambit-routes) builddir=${application}-build;;
unittest) builddir=${application}-build;;
*)
echo "$application: not supported" >&2
Expand Down
89 changes: 87 additions & 2 deletions src/movescount/movescount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QMutex>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QJsonDocument>

#include "logstore.h"
Expand Down Expand Up @@ -172,6 +173,52 @@ int MovesCount::getRoute(ambit_route_t *route, ambit_personal_settings_t *ps, QS
return ret;
}

int MovesCount::getRouteFromFile(ambit_route_t *route, ambit_personal_settings_t *ps, QString url, QString directory)
{
// find filename based on directory and URL
// routes/310212
QString routeId = url.remove("routes/");

QStringList filters;
// routes_573247115_SMD22.v1.d1.Sexten-Innichen-Toblach-Marchkinkele-Winnebach-Sexten.55km.1550hm.json
filters << QString("routes_").append(routeId).append("_*");

QDir dir = QDir(directory);
QStringList dirs = dir.entryList(filters);
if (dirs.empty())
{
qDebug() << "Did not find " << filters << " at " << directory;
return -1;
}

QString fileName = dirs.at(0);

// both file-names start similar, so we need to make sure we use
// the non-points-one which we want to read here
if (fileName.contains("_points_")) {
fileName = dirs.at(1);
}

QFile routeFile(QString(directory).append("/").append(fileName));
qDebug() << "Reading route from " << routeFile;

if (!routeFile.open(QIODevice::ReadOnly)){
qDebug() << "Route file " << routeFile << " not available for reading!";
return -1;
}

QByteArray _data = routeFile.readAll();

int ret = -1;
if (_data.length() > 0) {
jsonParser.parseRoute(_data, route, ps, NULL, directory);

ret = _data.length();
}

return ret;
}

int MovesCount::getRoutePoints(ambit_route_t *route, ambit_personal_settings_t *ps, QString url)
{
int ret = -1;
Expand All @@ -188,6 +235,44 @@ int MovesCount::getRoutePoints(ambit_route_t *route, ambit_personal_settings_t *
return ret;
}

int MovesCount::getRoutePointsFromFile(ambit_route_t *route, ambit_personal_settings_t *ps, QString url, QString directory)
{
// routes/310212
QString routeId = url.remove("routes/").remove("/points");

QStringList filters;
// routes_573247115_points_SMD22.v1.d1.Sexten-Innichen-Toblach-Marchkinkele-Winnebach-Sexten.55km.1550hm.json
filters << QString("routes_").append(routeId).append("_points_*");

QDir dir = QDir(directory);
QStringList dirs = dir.entryList(filters);

if (dirs.empty())
{
qDebug() << "Did not find " << filters << " at " << directory;
return -1;
}

const QString &fileName = dirs.at(0);
QFile routePointsFile(QString(directory).append("/").append(fileName));

qDebug() << "Reading points from " << routePointsFile;

if (!routePointsFile.open(QIODevice::ReadOnly)){
qDebug() << "Points file " << routePointsFile << " not available for reading!";
return -1;
}

QByteArray _data = routePointsFile.readAll();

int ret = -1;
if (_data.length() > 0) {
ret = jsonParser.parseRoutePoints(_data, route, ps);
}

return ret;
}

void MovesCount::getDeviceSettings()
{
QMetaObject::invokeMethod(this, "getDeviceSettingsInThread", Qt::AutoConnection);
Expand Down Expand Up @@ -382,7 +467,7 @@ int MovesCount::getPersonalSettingsInThread(ambit_personal_settings_t *settings,
if (_data.length() > 0) {
writeJson(_data, QString(getenv("HOME")).toUtf8() + "/.openambit/personal_settings.json");

jsonParser.parsePersonalSettings(_data, settings, this);
jsonParser.parsePersonalSettings(_data, settings, this, NULL);
ret = _data.length();
}
}
Expand All @@ -402,7 +487,7 @@ int MovesCount::getRouteInThread(ambit_route_t *route, ambit_personal_settings_t
QByteArray _data = reply->readAll();

if (_data.length() > 0) {
jsonParser.parseRoute(_data, route, ps, this);
jsonParser.parseRoute(_data, route, ps, this, NULL);

QString file = QString(getenv("HOME")).toUtf8() + QString("/.openambit/") + url.replace("/", "_") + "_" + QString::fromLatin1(route->name) + ".json";
writeJson(_data, file.toStdString().c_str());
Expand Down
2 changes: 2 additions & 0 deletions src/movescount/movescount.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class MovesCount : public QObject
int getOrbitalData(u_int8_t **data);
int getPersonalSettings(ambit_personal_settings_t *settings, bool onlychangedsettings);
int getRoute(ambit_route_t *routes, ambit_personal_settings_t *ps, QString url);
int getRouteFromFile(ambit_route_t *routes, ambit_personal_settings_t *ps, QString url, QString directory);
int getRoutePoints(ambit_route_t *routes, ambit_personal_settings_t *ps, QString url);
int getRoutePointsFromFile(ambit_route_t *routes, ambit_personal_settings_t *ps, QString url, QString directory);
int applyPersonalSettingsFromDevice(ambit_personal_settings_t *movesPersonalSettings, ambit_personal_settings_t *devicePersonalSettings);
void getDeviceSettings();
int getCustomModeData(ambit_sport_mode_device_settings_t *ambitCustomModes);
Expand Down
33 changes: 26 additions & 7 deletions src/movescount/movescountjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ int MovesCountJSON::parseLogReply(QByteArray &input, QString &moveId)
return -1;
}

int MovesCountJSON::parsePersonalSettings(QByteArray &input, ambit_personal_settings_t *ps, MovesCount *movescount)
int MovesCountJSON::parsePersonalSettings(QByteArray &input, ambit_personal_settings_t *ps, MovesCount *movescount,
const QString &routeDirectory)
{

if (input.length() <= 0) {
Expand All @@ -100,7 +101,13 @@ int MovesCountJSON::parsePersonalSettings(QByteArray &input, ambit_personal_sett
ps->routes.count = routes.size();
ps->routes.data = libambit_route_alloc(ps->routes.count);
for(int x=0; x<routes.size(); x++) {
movescount->getRoute(&(ps->routes.data[x]), ps, routes.value(x));
if (movescount == NULL) {
// handle reading the file from routeDirectory
MovesCount *movesCountLocal = MovesCount::instance();
movesCountLocal->getRouteFromFile(&(ps->routes.data[x]), ps, routes.value(x), routeDirectory);
} else {
movescount->getRoute(&(ps->routes.data[x]), ps, routes.value(x));
}
}

//Sort routes on name
Expand Down Expand Up @@ -194,7 +201,8 @@ bool MovesCountJSON::copyDataString(const QVariant& entry, char *data, size_t ma
return true;
}

int MovesCountJSON::parseRoute(QByteArray &input, ambit_route_t *route, ambit_personal_settings_t *ps, MovesCount *movescount)
int MovesCountJSON::parseRoute(QByteArray &input, ambit_route_t *route, ambit_personal_settings_t *ps, MovesCount *movescount,
const QString &directory)
{
if (input.length() <= 0) {
return -1;
Expand All @@ -219,7 +227,16 @@ int MovesCountJSON::parseRoute(QByteArray &input, ambit_route_t *route, ambit_pe
int ret;

if(result["RoutePointsURI"].type() == QVariant::String) {
if((ret = movescount->getRoutePoints(route, ps, result["RoutePointsURI"].toString())) < 2) {
if (movescount == NULL) {
if((ret = movescount->getRoutePointsFromFile(route, ps, result["RoutePointsURI"].toString(), directory)) < 2) {
if(route->points != NULL) {
free(route->points);
route->points = NULL;
}
route->points_count = 0;
return -1;
}
} else if((ret = movescount->getRoutePoints(route, ps, result["RoutePointsURI"].toString())) < 2) {
if(route->points != NULL) {
free(route->points);
route->points = NULL;
Expand All @@ -238,9 +255,6 @@ int MovesCountJSON::parseRoute(QByteArray &input, ambit_route_t *route, ambit_pe
route->altitude_asc= result["AscentAltitude"].toUInt();
route->distance = result["Distance"].toUInt();

//TODO: Remove
//libambit_debug_route_print(route);

return (int)route->points_count;
}

Expand Down Expand Up @@ -1164,6 +1178,11 @@ QVariantMap MovesCountJSON::parseJsonMap(const QByteArray& input, bool& ok) cons
QJsonParseError err;
QJsonDocument json = QJsonDocument::fromJson(input, &err);
ok = !json.isNull();

if (!ok) {
qDebug() << "Error when parsing JSON: " << err.errorString() << " at offset " << QString::number(err.offset);
}

return json.object().toVariantMap();
}

Expand Down
6 changes: 4 additions & 2 deletions src/movescount/movescountjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class MovesCountJSON : public QObject
int parseFirmwareVersionReply(QByteArray &input, u_int8_t fw_version[3]);
int parseLogReply(QByteArray &input, QString &moveId);
int parseLogDirReply(QByteArray &input, QList<MovesCountLogDirEntry> &entries);
int parsePersonalSettings(QByteArray &input, ambit_personal_settings_t *ps, MovesCount *movescount);
int parseRoute(QByteArray &input, ambit_route_t *routes, ambit_personal_settings_t *ps, MovesCount *movescount);
int parsePersonalSettings(QByteArray &input, ambit_personal_settings_t *ps, MovesCount *movescount,
const QString &routeDirectory);
int parseRoute(QByteArray &input, ambit_route_t *routes, ambit_personal_settings_t *ps, MovesCount *movescount,
const QString &directory);
int parseRoutePoints(QByteArray &input, ambit_route_t *routes, ambit_personal_settings_t *ps);
bool appendWaypoint(uint16_t count, ambit_personal_settings_t *ps, char *route_name, char *waypoint_name, int32_t lat, int32_t lon, uint16_t altitude, uint8_t type);
int parseDeviceSettingsReply(QByteArray &input, MovescountSettings &movescountSettings);
Expand Down
4 changes: 3 additions & 1 deletion src/openambit-cli/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ void startSync(ambit_object_t *deviceObject, ambit_personal_settings_t *currentP

ambit_personal_settings_t *movecountPersonalSettings = libambit_personal_settings_alloc();

// TODO: if configured, read personal settings and routes from files instead of MovesCount

qDebug() << "Get Personal Settings";
if((movesCount->getPersonalSettings(movecountPersonalSettings, true)) != -1) {
movesCount->applyPersonalSettingsFromDevice(movecountPersonalSettings, currentPersonalSettings);
Expand All @@ -274,7 +276,7 @@ void startSync(ambit_object_t *deviceObject, ambit_personal_settings_t *currentP

task->hasError();
}
qDebug() << "End reading navigation...";
qDebug() << "End reading navigation, had " << currentPersonalSettings->routes.count << " changed routes...";

libambit_personal_settings_free(movecountPersonalSettings);
}
Expand Down
46 changes: 46 additions & 0 deletions src/openambit-routes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 2.8.5)
project (OPENAMBITROUTES CXX)

set (OPENAMBIT_VERSION 0.5)

# Where to lookup modules
set(CMAKE_MODULE_PATH "${OPENAMBITROUTES_SOURCE_DIR}/cmake")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

find_package(libambit REQUIRED)
find_package(Movescount REQUIRED)
IF (!WIN32)
find_package(UDev REQUIRED)
ELSE (!WIN32)
set(UDEV_LIBS "")
ENDIF (!WIN32)
find_package(Qt5Core REQUIRED)

include(GNUInstallDirs)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${LIBAMBIT_INCLUDE_DIR}
..
)

link_directories(
${LIBAMBIT_LIBS_DIR}
${MOVESCOUNT_LIBS_DIR}
)

set(OPENAMBITROUTES_HDRS Task.h)

set(OPENAMBITROUTES_SRCS main.cpp Task.cpp)

set(CMAKE_AUTOMOC ON)

add_definitions(-DAPP_VERSION="${OPENAMBIT_VERSION}")

add_executable(openambit-routes ${OPENAMBITROUTES_HDRS} ${OPENAMBITROUTES_SRCS})

target_link_libraries(openambit-routes ${LIBAMBIT_LIBS} ${MOVESCOUNT_LIBS} ${UDEV_LIBS} Qt5::Core )

install(TARGETS openambit-routes DESTINATION ${CMAKE_INSTALL_BINDIR})
Loading

0 comments on commit e2bff57

Please sign in to comment.