Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Support manual entries which GPS data is in a Route #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ const template = (data, name) => {
<time>${new Date(data.created_at).toISOString()}</time>
</metadata>
<trk>
<name>${dayTime} ${name}</name>${
<name>${data.notes ? data.notes : `${dayTime} ${name}`}</name>${
data.description
? `
<desc>${data.description}</desc>`
: ''
}${
type
? `
<type>${type}</type>
`.trim()
Copy link
Owner

Choose a reason for hiding this comment

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

Probably better to keep them trimmed to prevent empty lines?

Copy link
Author

@adrienkaiser adrienkaiser Sep 11, 2019

Choose a reason for hiding this comment

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

Yes but that also removes the starting new line, and the <type> tag ends up on the same line as the previous tag.
I actually just "manually trimmed" the line by removing the trailing new line : end of string character is just after the closing tag </type> instead of being on the next line.

Copy link
Owner

Choose a reason for hiding this comment

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

Makes sense now, thanks!

<type>${type}</type>`
: ''
}
<trkseg>
Expand All @@ -168,8 +172,12 @@ const template = (data, name) => {

return `
<trkpt lon="${gpsItem.longitude}" lat="${gpsItem.latitude}">
<ele>${gpsItem.altitude}</ele>
<time>${new Date(gpsItem.timestamp).toISOString()}</time>${
<ele>${gpsItem.altitude}</ele>${
gpsItem.timestamp
? `
<time>${new Date(gpsItem.timestamp).toISOString()}</time>`
: ''
}${
heartRate
? `
<extensions>
Expand Down Expand Up @@ -209,7 +217,7 @@ const template = (data, name) => {
};

const fileName = (data, name) => {
const createdDate = new Date(data.created_at);
const createdDate = new Date(data.start_time);
Copy link
Owner

Choose a reason for hiding this comment

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

This will break at other activities. Can we do it in a way to handle both created_at & start_time?

Copy link
Author

Choose a reason for hiding this comment

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

We could add a condition on the start_time variable, but I don't see how it will break for other activities.
It seems that start_time exists in all session files, with/without live tracking, with/without GPS path. For live tracked sessions, created_at and start_time have the same value.
In addition, it's already used in your code : https://github.com/glennreyes/runtastic-gpx/blob/master/index.js#L116

Copy link
Owner

Choose a reason for hiding this comment

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

Let's do created_at || start_time here since we cannot assume that start_time will always equal the created date. So we fallback to start_time if the creation date doesn't exist.

const month = createdDate.getMonth() + 1;
const day = createdDate.getDate();

Expand Down Expand Up @@ -258,6 +266,31 @@ const start = async ([exportPath, outputPath = `${process.cwd()}/export`]) => {
.promisify(fs.readFile)(gpsPath)
.catch(console.error);
}
else if(item.route_id) { // if session has no GPS, check if it is attached to a route which has GPS
const routePath = `${exportPath}/Routes/${item.route_id}.json`;
const routeGpsPath = `${exportPath}/Routes/GPS-data/${item.route_id}.json`;

// get route name and description to add to session description
if (fs.existsSync(routePath)) {
routeFile = await util
.promisify(fs.readFile)(routePath)
.catch(console.error);
if (routeFile) {
route = parse(routeFile);
item.description = `No GPS data. Route: ${route.name} / ${route.description.replace(/\r\n/g, ' - ')}`;
}
}

// get route GPS and add start/end timestamps
if (fs.existsSync(routeGpsPath)) {
gpsFile = await util
.promisify(fs.readFile)(routeGpsPath)
.catch(console.error);
gpsFile = gpsFile.toString('utf8'); // convert to string to add start and end timestamps
gpsFile = gpsFile.replace('[{', `[{"timestamp":"${new Date(item.start_time).toISOString()}",`);
gpsFile = gpsFile.replace('}]', `,"timestamp":"${new Date(item.end_time).toISOString()}"}]`);
}
}

if (fs.existsSync(heartRatePath)) {
heartRateFile = await util
Expand Down