-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
GPX file support. Use attributes for magic numbers. #2
Open
heleon19
wants to merge
2
commits into
alvarolorentedev:master
Choose a base branch
from
heleon19:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const authenticate = require('endomondo-unofficial-api').authenticate; | ||
const workouts = require('endomondo-unofficial-api').workouts; | ||
const workoutGet = require('endomondo-unofficial-api').workout.get; | ||
const workoutSet = require('endomondo-unofficial-api').workout.set | ||
const fixTracks = require('endomondo-unofficial-api').workout.fix | ||
const track = require('endomondo-unofficial-api').attributes.track; | ||
const sport = require('endomondo-unofficial-api').attributes.sport; | ||
const gpx = require('endomondo-unofficial-api').gpx; | ||
|
||
/* example of a valid track point set */ | ||
const trackPoints = [ | ||
{ | ||
time: Date.parse("2017-05-12 15:39:25 UTC"), | ||
longitude: 8.2555906, | ||
latitude: 47.3491986, | ||
altitude: 444.1, | ||
inst: track.START | ||
}, | ||
{ | ||
time: Date.parse("2017-05-12 15:39:27 UTC"), | ||
longitude: 8.1555906, | ||
latitude: 47.2491986, | ||
altitude: 444.1, | ||
}, | ||
{ | ||
time: Date.parse("2017-05-12 15:39:30 UTC"), | ||
longitude: 8.1665917, | ||
latitude: 47.3492244, | ||
altitude: 444.1, | ||
inst: track.END | ||
} | ||
]; | ||
|
||
const main = async() => { | ||
|
||
/* read tracks from gpx file */ | ||
let tracks = await new Promise((resolve) => gpx.parseGpxFromFile("Current.gpx", (err, data) => { | ||
if(err) { | ||
console.log("Could not parse GPX file"); | ||
reject(err); | ||
} | ||
resolve(data); | ||
})); | ||
|
||
/* connect to endomondo */ | ||
const auth = await authenticate({email: "<username>", password: "<password>"}); | ||
console.log(auth); | ||
|
||
/* fix / split tracks if long delay between two points */ | ||
tracks = fixTracks(tracks, 24 * 60, 0.1); | ||
console.log("Numbor of tracks: " + tracks.length); | ||
//return; | ||
|
||
for(let i = 0; i < tracks.length; i++) { | ||
const track = tracks[i]; | ||
|
||
/* create new workout, write first couple of points */ | ||
const step = 20; // write 20 points in one request | ||
const newWork = await workoutSet({ | ||
authToken: auth.authToken, | ||
sport:sport.CYCLING, | ||
distance: 0, | ||
duration: 0, | ||
points: track.slice(0, step) | ||
}); | ||
//console.log(newWork); | ||
|
||
/* write remaining points to existing workout */ | ||
for(let idx = 0; idx < track.length; idx += step) | ||
{ | ||
const ps = track.slice(idx, idx + step); | ||
const updWork = await workoutSet({ | ||
authToken: auth.authToken, | ||
userId: auth.userId, | ||
workoutId: newWork.workoutId, | ||
sport: sport.CYCLING, | ||
distance: ps[ps.length - 1].dist.all, | ||
duration: (ps[ps.length - 1].time - track[0].time) / 1000, | ||
points: ps | ||
}); | ||
console.log(idx + "-" + (idx + ps.length) + ": " + JSON.stringify(ps[ps.length - 1])); | ||
|
||
/* wait befor sending next points */ | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
} | ||
} | ||
|
||
/* get a list of all workouts */ | ||
const works = await workouts({authToken: auth.authToken}); | ||
console.log(works); | ||
|
||
/* show latest workout */ | ||
const work = await workoutGet({authToken: auth.authToken, workoutId: works.data[0].id}); | ||
console.log(work); | ||
|
||
}; | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Track commands are used to indicate current tracking state for tracking points. */ | ||
var track = { | ||
START: 2, | ||
PAUSE: 0, | ||
RESTART: 1, | ||
END: 3 | ||
}; | ||
|
||
/* Sport codes */ | ||
var sport = { | ||
RUNNING: 0, | ||
CYCLING: 2 | ||
}; | ||
|
||
module.exports = { | ||
track: track, | ||
sport: sport | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict' | ||
|
||
var gpxParse = require("gpx-parse"); | ||
var attributes = require('./attributes'), | ||
sportAttr = attributes.sport, | ||
trackAttr = attributes.track | ||
|
||
var EARTH_RADIUS_KM = 6371.0; | ||
|
||
function deg2rad(deg) { | ||
return (deg * Math.PI / 180); | ||
} | ||
|
||
function rad2deg(rad) { | ||
return (rad * 180 / Math.PI); | ||
} | ||
|
||
function distanceEarth(lat1, lon1, lat2, lon2) { | ||
var lat1r, lon1r, lat2r, lon2r, u, v; | ||
lat1r = deg2rad(lat1); | ||
lon1r = deg2rad(lon1); | ||
lat2r = deg2rad(lat2); | ||
lon2r = deg2rad(lon2); | ||
u = Math.sin((lat2r - lat1r)/2); | ||
v = Math.sin((lon2r - lon1r)/2); | ||
return 2.0 * EARTH_RADIUS_KM * Math.asin(Math.sqrt(u * u + Math.cos(lat1r) * Math.cos(lat2r) * v * v)); | ||
}; | ||
|
||
function parseGpxFromFile(file, cb) { | ||
new Promise((resolve, reject) => { | ||
gpxParse.parseGpxFromFile(file, function(error, data) { | ||
if(error) { | ||
reject(error); | ||
} | ||
var tracks = data.tracks.map(function(track) { | ||
var points = []; | ||
track.segments.map(function(segment) { | ||
var _dist = 0; | ||
var last = segment[0]; | ||
var ps = segment.map((s) => { | ||
var l = last; | ||
last = s; | ||
var dist = distanceEarth(l.lat, l.lon, s.lat, s.lon); | ||
_dist += dist; | ||
return { | ||
time: Date.parse(s.time), | ||
longitude: s.lon, | ||
latitude: s.lat, | ||
altitude: s.elevation, | ||
dist: {last: dist, all: _dist}, // km | ||
speed: (dist * 1000 / ((Date.parse(s.time) - Date.parse(l.time)) / 1000)) // m/s | ||
} | ||
}); | ||
if(ps.length > 1) { | ||
ps[0].inst = trackAttr.RESTART; //mark first point as restart | ||
} | ||
if(ps.length > 0) { | ||
ps[ps.length - 1].inst = trackAttr.PAUSE; //mark last point as pause | ||
} | ||
points = points.concat(ps); | ||
}); | ||
if(points.length > 1) { | ||
points[0].inst = trackAttr.START; //mark first point as start | ||
} | ||
if(points.length > 0) { | ||
points[points.length - 1].inst = trackAttr.END; //mark last point as end | ||
} | ||
return points; | ||
}); | ||
resolve(tracks); | ||
}); | ||
}).then((tracks) => cb(null, tracks), function(error) { | ||
cb(error); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
parseGpxFromFile: parseGpxFromFile | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains quite a bit of logic, that i am sure its already done by some package (i think i have used in a different project. If you want we can talk about it or i can look at it to refactor on the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok for me to not include this file. There might be packages to get the distance of two coordinates.