| ... | ... |
@@ -15,140 +15,140 @@ var client_id = "94880"; |
| 15 | 15 |
var client_secret = "08dc170f0fe38f39dd327bea82a28db4400e6f00"; |
| 16 | 16 |
|
| 17 | 17 |
var firstlocationOptions = {
|
| 18 |
- 'enableHighAccuracy': true, // default = false (quick and dirty mode), can be true (more accurate but need more power and time) |
|
| 19 |
- 'timeout': 60000, //60s timeout to get a first good signal |
|
| 20 |
- 'maximumAge': 0 // no cache |
|
| 18 |
+ 'enableHighAccuracy': true, // default = false (quick and dirty mode), can be true (more accurate but need more power and time) |
|
| 19 |
+ 'timeout': 60000, //60s timeout to get a first good signal |
|
| 20 |
+ 'maximumAge': 0 // no cache |
|
| 21 | 21 |
}; |
| 22 | 22 |
var locationOptions = {
|
| 23 |
- 'enableHighAccuracy': true, // default = false (quick and dirty mode), can be true (more accurate but need more power and time) |
|
| 24 |
- 'timeout': 5000, //5s timeout to get a good signal |
|
| 25 |
- 'maximumAge': 0 // no cache |
|
| 23 |
+ 'enableHighAccuracy': true, // default = false (quick and dirty mode), can be true (more accurate but need more power and time) |
|
| 24 |
+ 'timeout': 5000, //5s timeout to get a good signal |
|
| 25 |
+ 'maximumAge': 0 // no cache |
|
| 26 | 26 |
}; |
| 27 | 27 |
|
| 28 |
-Pebble.addEventListener('showConfiguration', function (e) {
|
|
| 29 |
- clay.config = clayConfig; |
|
| 30 |
- console.log("Clay config is showing...")
|
|
| 31 |
- Pebble.openURL(clay.generateUrl()); |
|
| 28 |
+Pebble.addEventListener('showConfiguration', function(e) {
|
|
| 29 |
+ clay.config = clayConfig; |
|
| 30 |
+ console.log("Clay config is showing...")
|
|
| 31 |
+ Pebble.openURL(clay.generateUrl()); |
|
| 32 | 32 |
}); |
| 33 | 33 |
|
| 34 |
-Pebble.addEventListener('webviewclosed', function (t) {
|
|
| 35 |
- if (!t || t.response) {
|
|
| 36 |
- console.log("Clay config is submitted : " + t.response)
|
|
| 37 |
- try {
|
|
| 38 |
- if (data = JSON.parse(t.response), data.code && data.scope == "read,activity:write") {
|
|
| 39 |
- if (data.state == "bike_companion" && data.scope == "read,activity:write") {
|
|
| 40 |
- getTokens(data.code); |
|
| 41 |
- } else {
|
|
| 42 |
- console.log("Error on response returned : scope is " + grantcode.scope + " and state is " + grantcode.state);
|
|
| 34 |
+Pebble.addEventListener('webviewclosed', function(t) {
|
|
| 35 |
+ if (!t || t.response) {
|
|
| 36 |
+ console.log("Clay config is submitted : " + t.response)
|
|
| 37 |
+ try {
|
|
| 38 |
+ if (data = JSON.parse(t.response), data.code && data.scope == "read,activity:write") {
|
|
| 39 |
+ if (data.state == "bike_companion" && data.scope == "read,activity:write") {
|
|
| 40 |
+ getTokens(data.code); |
|
| 41 |
+ } else {
|
|
| 42 |
+ console.log("Error on response returned : scope is " + grantcode.scope + " and state is " + grantcode.state);
|
|
| 43 |
+ } |
|
| 44 |
+ } else {
|
|
| 45 |
+ clay.getSettings(t.response); |
|
| 46 |
+ console.log("Clay settings in Localstorage looks like " + localStorage.getItem("clay-settings"));
|
|
| 47 |
+ } |
|
| 48 |
+ } catch (t) {
|
|
| 49 |
+ console.log("Oauth parsing error, continue on saving clay settings");
|
|
| 50 |
+ clay.getSettings(t.response); |
|
| 51 |
+ var claysettings = JSON.parse(localStorage.getItem('clay-settings'))
|
|
| 52 |
+ claysettings.strava_enabled = false; |
|
| 53 |
+ localStorage.setItem("clay-settings", JSON.stringify("claysettings"));
|
|
| 54 |
+ console.log("Clay settings in Localstorage looks like " + localStorage.getItem("clay-settings"));
|
|
| 43 | 55 |
} |
| 44 |
- } else {
|
|
| 45 |
- clay.getSettings(t.response); |
|
| 46 |
- console.log("Clay settings in Localstorage looks like " + localStorage.getItem("clay-settings"));
|
|
| 47 |
- } |
|
| 48 |
- } catch (t) {
|
|
| 49 |
- console.log("Oauth parsing error, continue on saving clay settings");
|
|
| 50 |
- clay.getSettings(t.response); |
|
| 51 |
- var claysettings = JSON.parse(localStorage.getItem('clay-settings'))
|
|
| 52 |
- claysettings.strava_enabled = false; |
|
| 53 |
- localStorage.setItem("clay-settings", JSON.stringify("claysettings"));
|
|
| 54 |
- console.log("Clay settings in Localstorage looks like " + localStorage.getItem("clay-settings"));
|
|
| 55 |
- } |
|
| 56 | 56 |
|
| 57 |
- } |
|
| 57 |
+ } |
|
| 58 | 58 |
}); |
| 59 | 59 |
|
| 60 | 60 |
// Calculate the distance from 2 geoloc in degrees. |
| 61 | 61 |
// IMPORTANT : this is a calculation from 2D projection, altitude is not involved |
| 62 | 62 |
// |
| 63 | 63 |
function distance_on_geoid(lat1, lon1, lat2, lon2) {
|
| 64 |
- // Convert degrees to radians |
|
| 65 |
- lat1 = lat1 * Math.PI / 180.0; |
|
| 66 |
- lon1 = lon1 * Math.PI / 180.0; |
|
| 67 |
- lat2 = lat2 * Math.PI / 180.0; |
|
| 68 |
- lon2 = lon2 * Math.PI / 180.0; |
|
| 69 |
- // radius of earth in metres |
|
| 70 |
- r = 6378100; |
|
| 71 |
- // P |
|
| 72 |
- rho1 = r * Math.cos(lat1); |
|
| 73 |
- z1 = r * Math.sin(lat1); |
|
| 74 |
- x1 = rho1 * Math.cos(lon1); |
|
| 75 |
- y1 = rho1 * Math.sin(lon1); |
|
| 76 |
- // Q |
|
| 77 |
- rho2 = r * Math.cos(lat2); |
|
| 78 |
- z2 = r * Math.sin(lat2); |
|
| 79 |
- x2 = rho2 * Math.cos(lon2); |
|
| 80 |
- y2 = rho2 * Math.sin(lon2); |
|
| 81 |
- // Dot product |
|
| 82 |
- dot = (x1 * x2 + y1 * y2 + z1 * z2); |
|
| 83 |
- cos_theta = dot / (r * r); |
|
| 84 |
- theta = Math.acos(cos_theta); |
|
| 85 |
- // Distance in Metres |
|
| 86 |
- return r * theta; |
|
| 64 |
+ // Convert degrees to radians |
|
| 65 |
+ lat1 = lat1 * Math.PI / 180.0; |
|
| 66 |
+ lon1 = lon1 * Math.PI / 180.0; |
|
| 67 |
+ lat2 = lat2 * Math.PI / 180.0; |
|
| 68 |
+ lon2 = lon2 * Math.PI / 180.0; |
|
| 69 |
+ // radius of earth in metres |
|
| 70 |
+ r = 6378100; |
|
| 71 |
+ // P |
|
| 72 |
+ rho1 = r * Math.cos(lat1); |
|
| 73 |
+ z1 = r * Math.sin(lat1); |
|
| 74 |
+ x1 = rho1 * Math.cos(lon1); |
|
| 75 |
+ y1 = rho1 * Math.sin(lon1); |
|
| 76 |
+ // Q |
|
| 77 |
+ rho2 = r * Math.cos(lat2); |
|
| 78 |
+ z2 = r * Math.sin(lat2); |
|
| 79 |
+ x2 = rho2 * Math.cos(lon2); |
|
| 80 |
+ y2 = rho2 * Math.sin(lon2); |
|
| 81 |
+ // Dot product |
|
| 82 |
+ dot = (x1 * x2 + y1 * y2 + z1 * z2); |
|
| 83 |
+ cos_theta = dot / (r * r); |
|
| 84 |
+ theta = Math.acos(cos_theta); |
|
| 85 |
+ // Distance in Metres |
|
| 86 |
+ return r * theta; |
|
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
// Adding leading characters to string for nice displays |
| 90 | 90 |
// |
| 91 | 91 |
function padStart(string, max_length, padding) {
|
| 92 |
- if (string.length > max_length) {
|
|
| 93 |
- return string; |
|
| 94 |
- } else {
|
|
| 95 |
- var new_str = string; |
|
| 96 |
- for (index = string.length; index < max_length; index++) {
|
|
| 97 |
- new_str = padding + new_str; |
|
| 92 |
+ if (string.length > max_length) {
|
|
| 93 |
+ return string; |
|
| 94 |
+ } else {
|
|
| 95 |
+ var new_str = string; |
|
| 96 |
+ for (index = string.length; index < max_length; index++) {
|
|
| 97 |
+ new_str = padding + new_str; |
|
| 98 |
+ } |
|
| 99 |
+ return new_str; |
|
| 98 | 100 |
} |
| 99 |
- return new_str; |
|
| 100 |
- } |
|
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
|
| 104 | 104 |
// Store location in Pebble app local storage |
| 105 | 105 |
// |
| 106 | 106 |
function storeLocation(position, first) {
|
| 107 |
- var latitude = position.coords.latitude; |
|
| 108 |
- var longitude = position.coords.longitude; |
|
| 109 |
- var timestamp = position.timestamp; |
|
| 110 |
- if (first == true) {
|
|
| 111 |
- localStorage.setItem("firstlatitude", latitude);
|
|
| 112 |
- localStorage.setItem("firstlongitude", longitude);
|
|
| 113 |
- localStorage.setItem("firsttimestamp", Date.now());
|
|
| 114 |
- //console.log("-- First timestamp: " + localStorage.getItem("firsttimestamp"));
|
|
| 115 |
- localStorage.setItem("totalcoordinates", 0)
|
|
| 116 |
- } |
|
| 117 |
- |
|
| 118 |
- localStorage.setItem("lastlatitude", latitude);
|
|
| 119 |
- localStorage.setItem("lastlongitude", longitude);
|
|
| 120 |
- localStorage.setItem("lasttimestamp", timestamp);
|
|
| 121 |
- localStorage.setItem("totalcoordinates", parseInt(localStorage.getItem("totalcoordinates")) + 1)
|
|
| 122 |
- // console.log("Stored location " + position.coords.latitude + ',' + position.coords.longitude);
|
|
| 107 |
+ var latitude = position.coords.latitude; |
|
| 108 |
+ var longitude = position.coords.longitude; |
|
| 109 |
+ var timestamp = position.timestamp; |
|
| 110 |
+ if (first == true) {
|
|
| 111 |
+ localStorage.setItem("firstlatitude", latitude);
|
|
| 112 |
+ localStorage.setItem("firstlongitude", longitude);
|
|
| 113 |
+ localStorage.setItem("firsttimestamp", Date.now());
|
|
| 114 |
+ //console.log("-- First timestamp: " + localStorage.getItem("firsttimestamp"));
|
|
| 115 |
+ localStorage.setItem("totalcoordinates", 0)
|
|
| 116 |
+ } |
|
| 117 |
+ |
|
| 118 |
+ localStorage.setItem("lastlatitude", latitude);
|
|
| 119 |
+ localStorage.setItem("lastlongitude", longitude);
|
|
| 120 |
+ localStorage.setItem("lasttimestamp", timestamp);
|
|
| 121 |
+ localStorage.setItem("totalcoordinates", parseInt(localStorage.getItem("totalcoordinates")) + 1)
|
|
| 122 |
+ // console.log("Stored location " + position.coords.latitude + ',' + position.coords.longitude);
|
|
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
// Get location from Pebble app local storage |
| 126 | 126 |
// |
| 127 | 127 |
function getLocation(first) {
|
| 128 | 128 |
|
| 129 |
- if (first == false) {
|
|
| 130 |
- if (localStorage.getItem("lastlatitude") || localStorage.getItem("lastlongitude") || localStorage.getItem("lasttimestamp")) {
|
|
| 131 |
- var la = localStorage.getItem("lastlatitude");
|
|
| 132 |
- var lo = localStorage.getItem("lastlongitude");
|
|
| 133 |
- var ti = localStorage.getItem("lasttimestamp");
|
|
| 134 |
- var co = { "latitude": la, "longitude": lo };
|
|
| 135 |
- var pos = { "coords": co, "timestamp": ti };
|
|
| 136 |
- return pos; |
|
| 137 |
- } else {
|
|
| 138 |
- return null; |
|
| 139 |
- } |
|
| 140 |
- } else {
|
|
| 141 |
- if (localStorage.getItem("firstlatitude") || localStorage.getItem("firstlongitude") || localStorage.getItem("firsttimestamp")) {
|
|
| 142 |
- var la = localStorage.getItem("firstlatitude");
|
|
| 143 |
- var lo = localStorage.getItem("firstlongitude");
|
|
| 144 |
- var ti = localStorage.getItem("firsttimestamp");
|
|
| 145 |
- var co = { "latitude": la, "longitude": lo };
|
|
| 146 |
- var pos = { "coords": co, "timestamp": ti };
|
|
| 147 |
- return pos; |
|
| 129 |
+ if (first == false) {
|
|
| 130 |
+ if (localStorage.getItem("lastlatitude") || localStorage.getItem("lastlongitude") || localStorage.getItem("lasttimestamp")) {
|
|
| 131 |
+ var la = localStorage.getItem("lastlatitude");
|
|
| 132 |
+ var lo = localStorage.getItem("lastlongitude");
|
|
| 133 |
+ var ti = localStorage.getItem("lasttimestamp");
|
|
| 134 |
+ var co = { "latitude": la, "longitude": lo };
|
|
| 135 |
+ var pos = { "coords": co, "timestamp": ti };
|
|
| 136 |
+ return pos; |
|
| 137 |
+ } else {
|
|
| 138 |
+ return null; |
|
| 139 |
+ } |
|
| 148 | 140 |
} else {
|
| 149 |
- return null; |
|
| 141 |
+ if (localStorage.getItem("firstlatitude") || localStorage.getItem("firstlongitude") || localStorage.getItem("firsttimestamp")) {
|
|
| 142 |
+ var la = localStorage.getItem("firstlatitude");
|
|
| 143 |
+ var lo = localStorage.getItem("firstlongitude");
|
|
| 144 |
+ var ti = localStorage.getItem("firsttimestamp");
|
|
| 145 |
+ var co = { "latitude": la, "longitude": lo };
|
|
| 146 |
+ var pos = { "coords": co, "timestamp": ti };
|
|
| 147 |
+ return pos; |
|
| 148 |
+ } else {
|
|
| 149 |
+ return null; |
|
| 150 |
+ } |
|
| 150 | 151 |
} |
| 151 |
- } |
|
| 152 | 152 |
|
| 153 | 153 |
|
| 154 | 154 |
} |
| ... | ... |
@@ -156,53 +156,53 @@ function getLocation(first) {
|
| 156 | 156 |
// Get max speed of the run |
| 157 | 157 |
// |
| 158 | 158 |
function getMaxSpeed(lastSpeed) {
|
| 159 |
- oldmax = localStorage.getItem("maxSpeed") || -1;
|
|
| 160 |
- if (oldmax < lastSpeed) {
|
|
| 161 |
- maxSpeed = lastSpeed |
|
| 162 |
- } else if (oldmax > lastSpeed) {
|
|
| 163 |
- maxSpeed = oldmax |
|
| 164 |
- } else {
|
|
| 165 |
- maxSpeed = oldmax |
|
| 166 |
- } |
|
| 167 |
- localStorage.setItem("maxSpeed", maxSpeed);
|
|
| 168 |
- return maxSpeed |
|
| 159 |
+ oldmax = localStorage.getItem("maxSpeed") || -1;
|
|
| 160 |
+ if (oldmax < lastSpeed) {
|
|
| 161 |
+ maxSpeed = lastSpeed |
|
| 162 |
+ } else if (oldmax > lastSpeed) {
|
|
| 163 |
+ maxSpeed = oldmax |
|
| 164 |
+ } else {
|
|
| 165 |
+ maxSpeed = oldmax |
|
| 166 |
+ } |
|
| 167 |
+ localStorage.setItem("maxSpeed", maxSpeed);
|
|
| 168 |
+ return maxSpeed |
|
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
// split float number into an array of int (null returned instead of 0 for decimal) |
| 172 | 172 |
// |
| 173 | 173 |
function splitFloatNumber(num) {
|
| 174 |
- const intStr = num.toString().split('.')[0];
|
|
| 175 |
- var decimalStr = num.toString().split('.')[1];
|
|
| 176 |
- if (decimalStr === undefined) { decimalStr = 0 } else { decimalStr = decimalStr }
|
|
| 177 |
- return [Number(intStr), Number(decimalStr)]; |
|
| 174 |
+ const intStr = num.toString().split('.')[0];
|
|
| 175 |
+ var decimalStr = num.toString().split('.')[1];
|
|
| 176 |
+ if (decimalStr === undefined) { decimalStr = 0 } else { decimalStr = decimalStr }
|
|
| 177 |
+ return [Number(intStr), Number(decimalStr)]; |
|
| 178 | 178 |
|
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
// Build GPX headers |
| 182 | 182 |
// |
| 183 | 183 |
function GPXHeadersBuilder(timestamp, name, type) {
|
| 184 |
- var headers = '<?xml version="1.0" encoding="UTF-8"?><gpx creator="Pebble with barometer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1"><metadata><time>' + timestamp + '</time></metadata><trk><name>' + name + '</name><type>' + type + '</type><trkseg>'; |
|
| 185 |
- localStorage.setItem("GPX", headers);
|
|
| 186 |
- return true; |
|
| 184 |
+ var headers = '<?xml version="1.0" encoding="UTF-8"?><gpx creator="Pebble with barometer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1"><metadata><time>' + timestamp + '</time></metadata><trk><name>' + name + '</name><type>' + type + '</type><trkseg>'; |
|
| 185 |
+ localStorage.setItem("GPX", headers);
|
|
| 186 |
+ return true; |
|
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
// Build GPX track point |
| 190 | 190 |
// |
| 191 | 191 |
function GPXtrkptBuilder(lat, lon, ele, timestamp) {
|
| 192 |
- var GPX = localStorage.getItem("GPX");
|
|
| 193 |
- var trkpt = '<trkpt lat="' + lat + '" lon="' + lon + '"><ele>' + ele + '</ele><time>' + timestamp + '</time></trkpt>'; |
|
| 194 |
- localStorage.setItem("GPX", GPX + trkpt);
|
|
| 195 |
- return true; |
|
| 192 |
+ var GPX = localStorage.getItem("GPX");
|
|
| 193 |
+ var trkpt = '<trkpt lat="' + lat + '" lon="' + lon + '"><ele>' + ele + '</ele><time>' + timestamp + '</time></trkpt>'; |
|
| 194 |
+ localStorage.setItem("GPX", GPX + trkpt);
|
|
| 195 |
+ return true; |
|
| 196 | 196 |
} |
| 197 | 197 |
|
| 198 | 198 |
// Build GPX footer |
| 199 | 199 |
// |
| 200 | 200 |
function GPXfooterBuilder() {
|
| 201 |
- var GPX = localStorage.getItem("GPX");
|
|
| 202 |
- var footer = '</trkseg></trk></gpx>'; |
|
| 203 |
- var ret = localStorage.setItem("GPX", GPX + footer);
|
|
| 204 |
- //console.log("GPX closed : " + GPX + footer);
|
|
| 205 |
- return ret; |
|
| 201 |
+ var GPX = localStorage.getItem("GPX");
|
|
| 202 |
+ var footer = '</trkseg></trk></gpx>'; |
|
| 203 |
+ var ret = localStorage.setItem("GPX", GPX + footer);
|
|
| 204 |
+ //console.log("GPX closed : " + GPX + footer);
|
|
| 205 |
+ return ret; |
|
| 206 | 206 |
} |
| 207 | 207 |
|
| 208 | 208 |
//------------------------------------------ |
| ... | ... |
@@ -210,439 +210,441 @@ function GPXfooterBuilder() {
|
| 210 | 210 |
//------------------------------------------ |
| 211 | 211 |
|
| 212 | 212 |
function getTokens(code) {
|
| 213 |
- // call to strava api to get tokens in exchange of temp code |
|
| 214 |
- // need to use strava.jonget.fr to proxy request and hide secret |
|
| 215 |
- var url = "https://www.strava.com/oauth/token?client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code + "&grant_type=authorization_code"; |
|
| 216 |
- var xhr = new XMLHttpRequest(); |
|
| 217 |
- xhr.timeout = 10000; // time in milliseconds |
|
| 218 |
- |
|
| 219 |
- xhr.open("POST", url, false);
|
|
| 220 |
- |
|
| 221 |
- xhr.send(); |
|
| 222 |
- |
|
| 223 |
- if (xhr.status === 200) {
|
|
| 224 |
- console.log('------xhr request returned :', xhr.responseText);
|
|
| 225 |
- response_json = JSON.parse(xhr.responseText); |
|
| 226 |
- |
|
| 227 |
- var tokenjson = {
|
|
| 228 |
- access_token: response_json.access_token, |
|
| 229 |
- refresh_token: response_json.refresh_token, |
|
| 230 |
- expiry: response_json.expires_at, |
|
| 231 |
- delay: response_json.expires_in |
|
| 232 |
- }; |
|
| 233 |
- localStorage.setItem("strava_tokens", JSON.stringify(tokenjson));
|
|
| 234 |
- } |
|
| 213 |
+ // call to strava api to get tokens in exchange of temp code |
|
| 214 |
+ // need to use strava.jonget.fr to proxy request and hide secret |
|
| 215 |
+ var url = "https://www.strava.com/oauth/token?client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code + "&grant_type=authorization_code"; |
|
| 216 |
+ var xhr = new XMLHttpRequest(); |
|
| 217 |
+ xhr.timeout = 10000; // time in milliseconds |
|
| 218 |
+ |
|
| 219 |
+ xhr.open("POST", url, false);
|
|
| 220 |
+ |
|
| 221 |
+ xhr.send(); |
|
| 222 |
+ |
|
| 223 |
+ if (xhr.status === 200) {
|
|
| 224 |
+ console.log('------xhr request returned :', xhr.responseText);
|
|
| 225 |
+ response_json = JSON.parse(xhr.responseText); |
|
| 226 |
+ |
|
| 227 |
+ var tokenjson = {
|
|
| 228 |
+ access_token: response_json.access_token, |
|
| 229 |
+ refresh_token: response_json.refresh_token, |
|
| 230 |
+ expiry: response_json.expires_at, |
|
| 231 |
+ delay: response_json.expires_in |
|
| 232 |
+ }; |
|
| 233 |
+ localStorage.setItem("strava_tokens", JSON.stringify(tokenjson));
|
|
| 234 |
+ } |
|
| 235 | 235 |
} |
| 236 | 236 |
|
| 237 | 237 |
function refreshTokens(refresh_token) {
|
| 238 |
- // call to strava api to get tokens in exchange of refresh code |
|
| 239 |
- // need to use strava.jonget.fr to proxy request and hide secret |
|
| 240 |
- var url = "https://www.strava.com/oauth/token?client_id=" + client_id + "&client_secret=" + client_secret + "&refresh_token=" + refresh_token + "&grant_type=refresh_token"; |
|
| 241 |
- var xhr = new XMLHttpRequest(); |
|
| 242 |
- xhr.timeout = 10000; // time in milliseconds |
|
| 243 |
- |
|
| 244 |
- xhr.open("POST", url, false);
|
|
| 245 |
- |
|
| 246 |
- xhr.send(); |
|
| 247 |
- //console.log('------Refresh token - xhr onloaded')
|
|
| 248 |
- if (xhr.status === 200) {
|
|
| 249 |
- console.log('------Refresh token - xhr request returned with ' + xhr.responseText);
|
|
| 250 |
- response_json = JSON.parse(xhr.responseText); |
|
| 251 |
- |
|
| 252 |
- var tokenjson = {
|
|
| 253 |
- access_token: response_json.access_token, |
|
| 254 |
- refresh_token: response_json.refresh_token, |
|
| 255 |
- expiry: response_json.expires_at, |
|
| 256 |
- delay: response_json.expires_in |
|
| 257 |
- }; |
|
| 258 |
- localStorage.setItem("strava_tokens", JSON.stringify(tokenjson));
|
|
| 238 |
+ // call to strava api to get tokens in exchange of refresh code |
|
| 239 |
+ // need to use strava.jonget.fr to proxy request and hide secret |
|
| 240 |
+ var url = "https://www.strava.com/oauth/token?client_id=" + client_id + "&client_secret=" + client_secret + "&refresh_token=" + refresh_token + "&grant_type=refresh_token"; |
|
| 241 |
+ var xhr = new XMLHttpRequest(); |
|
| 242 |
+ xhr.timeout = 10000; // time in milliseconds |
|
| 243 |
+ |
|
| 244 |
+ xhr.open("POST", url, false);
|
|
| 245 |
+ |
|
| 246 |
+ xhr.send(); |
|
| 247 |
+ //console.log('------Refresh token - xhr onloaded')
|
|
| 248 |
+ if (xhr.status === 200) {
|
|
| 249 |
+ console.log('------Refresh token - xhr request returned with ' + xhr.responseText);
|
|
| 250 |
+ response_json = JSON.parse(xhr.responseText); |
|
| 251 |
+ |
|
| 252 |
+ var tokenjson = {
|
|
| 253 |
+ access_token: response_json.access_token, |
|
| 254 |
+ refresh_token: response_json.refresh_token, |
|
| 255 |
+ expiry: response_json.expires_at, |
|
| 256 |
+ delay: response_json.expires_in |
|
| 257 |
+ }; |
|
| 258 |
+ localStorage.setItem("strava_tokens", JSON.stringify(tokenjson));
|
|
| 259 | 259 |
|
| 260 |
- } |
|
| 260 |
+ } |
|
| 261 | 261 |
|
| 262 | 262 |
} |
| 263 | 263 |
|
| 264 | 264 |
// Send GPX to Strava profile |
| 265 | 265 |
function SendToStrava() {
|
| 266 |
- console.log('--- GPX upload to strava');
|
|
| 267 |
- var gpxfile = localStorage.getItem("GPX");
|
|
| 268 |
- |
|
| 269 |
- var tokens = localStorage.getItem("strava_tokens");
|
|
| 270 |
- |
|
| 271 |
- //checking token expiry |
|
| 272 |
- var date = (Date.now()) / 1000 |
|
| 273 |
- |
|
| 274 |
- if (JSON.parse(tokens).expiry < date) {
|
|
| 275 |
- console.log("Strava oAuth token expired, refreshing it")
|
|
| 276 |
- refreshTokens(JSON.parse(tokens).refresh_token); |
|
| 277 |
- } else {
|
|
| 278 |
- console.log("token (" + JSON.parse(tokens).access_token + ") valid, continuing")
|
|
| 279 |
- } |
|
| 280 |
- |
|
| 281 |
- |
|
| 282 |
- var bearer = JSON.parse(tokens).access_token; |
|
| 283 |
- params = {
|
|
| 284 |
- url: "https://www.strava.com/api/v3/uploads", |
|
| 285 |
- method: "POST", |
|
| 286 |
- data: { description: "desc", data_type: "gpx" },
|
|
| 287 |
- files: { file: gpxfile },
|
|
| 288 |
- authorization: "Bearer " + bearer, |
|
| 289 |
- callback: function (e) {
|
|
| 290 |
- var message = ""; |
|
| 291 |
- if (console.log(e.status + " - " + e.txt), 201 == e.status) {
|
|
| 292 |
- message = "Your activity has been created"; |
|
| 293 |
- localStorage.setItem("strava_uploaded", true);
|
|
| 294 |
- } else if (400 == e.status) {
|
|
| 295 |
- message = "An error has occurred. If you've already uploaded the current activity, please delete it in Strava."; |
|
| 296 |
- } else if (401 == e.status) {
|
|
| 297 |
- message = "Error - Unauthorized. Please check your credentials in the settings." |
|
| 298 |
- } else {
|
|
| 266 |
+ console.log('--- GPX upload to strava');
|
|
| 267 |
+ var gpxfile = localStorage.getItem("GPX");
|
|
| 268 |
+ |
|
| 269 |
+ var tokens = localStorage.getItem("strava_tokens");
|
|
| 270 |
+ |
|
| 271 |
+ //checking token expiry |
|
| 272 |
+ var date = (Date.now()) / 1000 |
|
| 273 |
+ |
|
| 274 |
+ if (JSON.parse(tokens).expiry < date) {
|
|
| 275 |
+ console.log("Strava oAuth token expired, refreshing it")
|
|
| 276 |
+ refreshTokens(JSON.parse(tokens).refresh_token); |
|
| 277 |
+ } else {
|
|
| 278 |
+ console.log("token (" + JSON.parse(tokens).access_token + ") valid, continuing")
|
|
| 279 |
+ } |
|
| 280 |
+ |
|
| 281 |
+ |
|
| 282 |
+ var bearer = JSON.parse(tokens).access_token; |
|
| 283 |
+ params = {
|
|
| 284 |
+ url: "https://www.strava.com/api/v3/uploads", |
|
| 285 |
+ method: "POST", |
|
| 286 |
+ data: { description: "desc", data_type: "gpx" },
|
|
| 287 |
+ files: { file: gpxfile },
|
|
| 288 |
+ authorization: "Bearer " + bearer, |
|
| 289 |
+ callback: function(e) {
|
|
| 290 |
+ var message = ""; |
|
| 291 |
+ if (console.log(e.status + " - " + e.txt), 201 == e.status) {
|
|
| 292 |
+ message = "Your activity has been created"; |
|
| 293 |
+ localStorage.setItem("strava_uploaded", true);
|
|
| 294 |
+ } else if (400 == e.status) {
|
|
| 295 |
+ message = "An error has occurred. If you've already uploaded the current activity, please delete it in Strava."; |
|
| 296 |
+ } else if (401 == e.status) {
|
|
| 297 |
+ message = "Error - Unauthorized. Please check your credentials in the settings." |
|
| 298 |
+ } else {
|
|
| 299 |
+ try {
|
|
| 300 |
+ response_json = JSON.parse(e.txt) |
|
| 301 |
+ response_json.error ? (console.log("error:" + response_json.error), message = response_json.error) : response_json.status && (console.log("status:" + response_json.status))
|
|
| 302 |
+ } catch (e) {
|
|
| 303 |
+ console.log("Error log, " + e)
|
|
| 304 |
+ } |
|
| 305 |
+ } |
|
| 306 |
+ message && Pebble.showSimpleNotificationOnPebble("Ventoo SE - Strava", message)
|
|
| 307 |
+ } |
|
| 308 |
+ } |
|
| 309 |
+ var XHR = new XMLHttpRequest; |
|
| 310 |
+ var n = this; |
|
| 311 |
+ //console.log(params.url); |
|
| 312 |
+ XHR.open(params.method, params.url, !0); |
|
| 313 |
+ var body = ""; |
|
| 314 |
+ var boundary = Math.random().toString().substring(2); |
|
| 315 |
+ XHR.setRequestHeader("content-type", "multipart/form-data; charset=utf-8; boundary=" + boundary)
|
|
| 316 |
+ XHR.setRequestHeader("Authorization", params.authorization);
|
|
| 317 |
+ for (var i in params.data) body += "--" + boundary + '\r\nContent-Disposition: form-data; name="' + i + '"\r\n\r\n' + params.data[i] + "\r\n"; |
|
| 318 |
+ for (var i in params.files) body += "--" + boundary + '\r\nContent-Disposition: form-data; name="' + i + '" ; filename=test.gpx\r\n\r\n' + params.files[i] + "\r\n"; |
|
| 319 |
+ body += "--" + boundary + "--\r\n" |
|
| 320 |
+ |
|
| 321 |
+ XHR.onreadystatechange = function() {
|
|
| 299 | 322 |
try {
|
| 300 |
- response_json = JSON.parse(e.txt) |
|
| 301 |
- response_json.error ? (console.log("error:" + response_json.error), message = response_json.error) : response_json.status && (console.log("status:" + response_json.status))
|
|
| 323 |
+ 4 == XHR.readyState && (n.status = XHR.status, n.txt = XHR.responseText, n.xml = XHR.responseXML, params.callback && params.callback(n)) |
|
| 302 | 324 |
} catch (e) {
|
| 303 |
- console.log("Error log, " + e)
|
|
| 325 |
+ console.error("Error2 loading, ", e)
|
|
| 304 | 326 |
} |
| 305 |
- } |
|
| 306 |
- message && Pebble.showSimpleNotificationOnPebble("Ventoo SE - Strava", message)
|
|
| 307 | 327 |
} |
| 308 |
- } |
|
| 309 |
- var XHR = new XMLHttpRequest; |
|
| 310 |
- var n = this; |
|
| 311 |
- //console.log(params.url); |
|
| 312 |
- XHR.open(params.method, params.url, !0); |
|
| 313 |
- var body = ""; |
|
| 314 |
- var boundary = Math.random().toString().substring(2); |
|
| 315 |
- XHR.setRequestHeader("content-type", "multipart/form-data; charset=utf-8; boundary=" + boundary)
|
|
| 316 |
- XHR.setRequestHeader("Authorization", params.authorization);
|
|
| 317 |
- for (var i in params.data) body += "--" + boundary + '\r\nContent-Disposition: form-data; name="' + i + '"\r\n\r\n' + params.data[i] + "\r\n"; |
|
| 318 |
- for (var i in params.files) body += "--" + boundary + '\r\nContent-Disposition: form-data; name="' + i + '" ; filename=test.gpx\r\n\r\n' + params.files[i] + "\r\n"; |
|
| 319 |
- body += "--" + boundary + "--\r\n" |
|
| 320 |
- |
|
| 321 |
- XHR.onreadystatechange = function () {
|
|
| 322 |
- try {
|
|
| 323 |
- 4 == XHR.readyState && (n.status = XHR.status, n.txt = XHR.responseText, n.xml = XHR.responseXML, params.callback && params.callback(n)) |
|
| 324 |
- } catch (e) {
|
|
| 325 |
- console.error("Error2 loading, ", e)
|
|
| 326 |
- } |
|
| 327 |
- } |
|
| 328 |
- XHR.send(body) |
|
| 328 |
+ XHR.send(body) |
|
| 329 | 329 |
} |
| 330 | 330 |
|
| 331 | 331 |
|
| 332 | 332 |
// Send GPX to web server (need configuration on serverside) |
| 333 | 333 |
// TODO : secure it ? |
| 334 | 334 |
function PostToWeb() {
|
| 335 |
- console.log('--- GPX upload to custom web server');
|
|
| 336 |
- var GPX = localStorage.getItem("GPX");
|
|
| 337 |
- var url = JSON.parse(localStorage.getItem('clay-settings')).gpx_web_url + "?name=pebblegpx&type=application/gpx+xml";
|
|
| 338 |
- var xhr = new XMLHttpRequest(); |
|
| 339 |
- xhr.timeout = 10000; // time in milliseconds |
|
| 340 |
- |
|
| 341 |
- xhr.open("POST", url, false);
|
|
| 342 |
- |
|
| 343 |
- //console.log('------ CSV / xhr opened')
|
|
| 344 |
- xhr.onload = function () {
|
|
| 345 |
- //console.log('------xhr onloaded')
|
|
| 346 |
- if (xhr.readyState === 4) {
|
|
| 347 |
- //console.log('------xhr request returned with ' + xhr.status);
|
|
| 348 |
- //console.log(this.responseText); |
|
| 349 |
- localStorage.setItem("custom_uploaded", true);
|
|
| 350 |
- if (xhr.status == 200) {
|
|
| 351 |
- //console.log('--> HTTP 200');
|
|
| 352 |
- return true; |
|
| 353 |
- } else {
|
|
| 354 |
- //console.log('--> HTTP ' + xhr.status);
|
|
| 355 |
- return false; |
|
| 356 |
- } |
|
| 357 |
- } |
|
| 358 |
- }; |
|
| 335 |
+ console.log('--- GPX upload to custom web server');
|
|
| 336 |
+ var GPX = localStorage.getItem("GPX");
|
|
| 337 |
+ var url = JSON.parse(localStorage.getItem('clay-settings')).gpx_web_url + "?name=pebblegpx&type=application/gpx+xml";
|
|
| 338 |
+ var xhr = new XMLHttpRequest(); |
|
| 339 |
+ xhr.timeout = 10000; // time in milliseconds |
|
| 340 |
+ |
|
| 341 |
+ xhr.open("POST", url, false);
|
|
| 342 |
+ |
|
| 343 |
+ //console.log('------ CSV / xhr opened')
|
|
| 344 |
+ xhr.onload = function() {
|
|
| 345 |
+ //console.log('------xhr onloaded')
|
|
| 346 |
+ if (xhr.readyState === 4) {
|
|
| 347 |
+ //console.log('------xhr request returned with ' + xhr.status);
|
|
| 348 |
+ //console.log(this.responseText); |
|
| 349 |
+ localStorage.setItem("custom_uploaded", true);
|
|
| 350 |
+ if (xhr.status == 200) {
|
|
| 351 |
+ //console.log('--> HTTP 200');
|
|
| 352 |
+ return true; |
|
| 353 |
+ } else {
|
|
| 354 |
+ //console.log('--> HTTP ' + xhr.status);
|
|
| 355 |
+ return false; |
|
| 356 |
+ } |
|
| 357 |
+ } |
|
| 358 |
+ }; |
|
| 359 | 359 |
|
| 360 |
- //send CSV in body |
|
| 361 |
- xhr.send(GPX); |
|
| 360 |
+ //send CSV in body |
|
| 361 |
+ xhr.send(GPX); |
|
| 362 | 362 |
|
| 363 | 363 |
} |
| 364 | 364 |
|
| 365 | 365 |
// Send location to web server for instant location (no live tracking) |
| 366 | 366 |
// TODO : secure it ? |
| 367 | 367 |
function instantLocationUpdate(pos) {
|
| 368 |
- console.log('--- Instant location update');
|
|
| 369 |
- // console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
|
|
| 370 |
- |
|
| 371 |
- var url = JSON.parse(localStorage.getItem('clay-settings')).ping_location_url + "?lat=" + pos.coords.latitude + "&long=" + pos.coords.longitude + "&acc=" + pos.coords.accuracy + "×tamp=" + pos.timestamp;
|
|
| 372 |
- var xhr = new XMLHttpRequest(); |
|
| 373 |
- xhr.timeout = 10000; // time in milliseconds |
|
| 374 |
- |
|
| 375 |
- xhr.open("POST", url);
|
|
| 376 |
- |
|
| 377 |
- //console.log('------ instant / xhr opened')
|
|
| 378 |
- xhr.onload = function () {
|
|
| 379 |
- //console.log('------xhr onloaded')
|
|
| 380 |
- if (xhr.readyState === 4) {
|
|
| 381 |
- //console.log('------xhr request returned with ' + xhr.status);
|
|
| 382 |
- //console.log(this.responseText); |
|
| 383 |
- if (xhr.status == 200) {
|
|
| 384 |
- //console.log('--> HTTP 200');
|
|
| 385 |
- return true; |
|
| 386 |
- } else {
|
|
| 387 |
- //console.log('--> HTTP ' + xhr.status);
|
|
| 388 |
- return false; |
|
| 389 |
- } |
|
| 390 |
- } |
|
| 391 |
- }; |
|
| 368 |
+ console.log('--- Instant location update');
|
|
| 369 |
+ // console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
|
|
| 370 |
+ |
|
| 371 |
+ var url = JSON.parse(localStorage.getItem('clay-settings')).ping_location_url + "?lat=" + pos.coords.latitude + "&long=" + pos.coords.longitude + "&acc=" + pos.coords.accuracy + "×tamp=" + pos.timestamp;
|
|
| 372 |
+ var xhr = new XMLHttpRequest(); |
|
| 373 |
+ xhr.timeout = 10000; // time in milliseconds |
|
| 374 |
+ |
|
| 375 |
+ xhr.open("POST", url);
|
|
| 376 |
+ |
|
| 377 |
+ //console.log('------ instant / xhr opened')
|
|
| 378 |
+ xhr.onload = function() {
|
|
| 379 |
+ //console.log('------xhr onloaded')
|
|
| 380 |
+ if (xhr.readyState === 4) {
|
|
| 381 |
+ //console.log('------xhr request returned with ' + xhr.status);
|
|
| 382 |
+ //console.log(this.responseText); |
|
| 383 |
+ if (xhr.status == 200) {
|
|
| 384 |
+ //console.log('--> HTTP 200');
|
|
| 385 |
+ return true; |
|
| 386 |
+ } else {
|
|
| 387 |
+ //console.log('--> HTTP ' + xhr.status);
|
|
| 388 |
+ return false; |
|
| 389 |
+ } |
|
| 390 |
+ } |
|
| 391 |
+ }; |
|
| 392 | 392 |
|
| 393 |
- //send without body |
|
| 394 |
- xhr.send(); |
|
| 393 |
+ //send without body |
|
| 394 |
+ xhr.send(); |
|
| 395 | 395 |
|
| 396 | 396 |
} |
| 397 | 397 |
|
| 398 | 398 |
// called in case of successful geoloc gathering and sends the coordinate to watch |
| 399 | 399 |
// |
| 400 | 400 |
function locationSuccess(new_pos) {
|
| 401 |
- console.log('--- locationSuccess');
|
|
| 402 |
- // console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
|
|
| 403 |
- |
|
| 404 |
- var prev_pos = getLocation(false); |
|
| 405 |
- var first_pos = getLocation(true); |
|
| 406 |
- if (prev_pos === null) {
|
|
| 407 |
- console.log('--- start building gpx');
|
|
| 408 |
- storeLocation(new_pos, true); |
|
| 409 |
- localStorage.setItem("strava_uploaded", false);
|
|
| 410 |
- localStorage.setItem("custom_uploaded", false);
|
|
| 411 |
- |
|
| 412 |
- // Start the GPX file |
|
| 413 |
- GPXHeadersBuilder(new Date(new_pos.timestamp).toISOString(), "test", "18"); |
|
| 414 |
- |
|
| 415 |
- } else {
|
|
| 416 |
- storeLocation(new_pos, false); |
|
| 417 |
- |
|
| 418 |
- // Prepare display on watch |
|
| 419 |
- // now it's only raw data |
|
| 420 |
- // init strings |
|
| 421 |
- var latitudeString = ""; |
|
| 422 |
- var longitudeString = ""; |
|
| 423 |
- var accuracyString = ""; |
|
| 424 |
- var altitudeString = ""; |
|
| 425 |
- var speedString = ""; |
|
| 426 |
- var distanceString = ""; |
|
| 427 |
- |
|
| 428 |
- // get speed from geoloc API isntead of calculate it |
|
| 429 |
- // speed is initially in m/s, get it at km/h |
|
| 430 |
- if (new_pos.coords.speed === null) {
|
|
| 431 |
- var speed = 0; |
|
| 432 |
- } else {
|
|
| 433 |
- var speed = new_pos.coords.speed * 3.6; |
|
| 434 |
- localStorage.setItem("speedsum", parseInt(localStorage.getItem("speedsum")) + speed);
|
|
| 435 |
- } |
|
| 401 |
+ console.log('--- locationSuccess');
|
|
| 402 |
+ // console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
|
|
| 436 | 403 |
|
| 437 |
- // distance since beginning in m |
|
| 438 |
- var dist = distance_on_geoid(prev_pos.coords.latitude, prev_pos.coords.longitude, new_pos.coords.latitude, new_pos.coords.longitude); |
|
| 439 |
- var totaldist = parseInt(localStorage.getItem("dist"));
|
|
| 440 |
- if (!isNaN(dist)) {
|
|
| 441 |
- totaldist = totaldist + parseInt(dist); |
|
| 442 |
- localStorage.setItem("dist", totaldist);
|
|
| 443 |
- } |
|
| 444 |
- distanceString = splitFloatNumber(totaldist / 1000)[0].toString() + "." + splitFloatNumber(totaldist / 1000)[1].toString().substring(0, 3); |
|
| 445 |
- //console.log("total dist is now " + totaldist);
|
|
| 446 |
- |
|
| 447 |
- // avg speed (also when not moving) since beginning |
|
| 448 |
- var avgspeed = parseInt(localStorage.getItem("speedsum")) / parseInt(localStorage.getItem("totalcoordinates"));
|
|
| 449 |
- var avgSpeedString = splitFloatNumber(avgspeed)[0].toString() + "." + splitFloatNumber(avgspeed)[1].toString().substring(0, 1); |
|
| 450 |
- console.log("speedsum=" + parseInt(localStorage.getItem("speedsum")) + " / totalcoordinates=" + parseInt(localStorage.getItem("totalcoordinates")));
|
|
| 451 |
- console.log("--avgspeed=" + avgspeed + " / avgSpeedString=" + avgSpeedString)
|
|
| 452 |
- if (avgSpeedString == "0.N") {
|
|
| 453 |
- avgSpeedString = "0.0"; |
|
| 454 |
- } |
|
| 455 |
- //console.log("avg speed is : " + avgSpeedString);
|
|
| 456 |
- |
|
| 457 |
- // Duration |
|
| 458 |
- |
|
| 459 |
- var duration = new_pos.timestamp - first_pos.timestamp; |
|
| 460 |
- const date = new Date(duration); |
|
| 461 |
- durationString = padStart(date.getUTCHours().toString(), 2, "0") + ":" + padStart(date.getMinutes().toString(), 2, "0") + ":" + padStart(date.getSeconds().toString(), 2, "0"); |
|
| 462 |
- console.log("durationString is : " + durationString);
|
|
| 463 |
- |
|
| 464 |
- //formating for precision and max size |
|
| 465 |
- latitudeString = new_pos.coords.latitude.toString().substring(0, 12); |
|
| 466 |
- longitudeString = new_pos.coords.longitude.toString().substring(0, 12); |
|
| 467 |
- accuracyString = new_pos.coords.accuracy.toString().substring(0, 4); |
|
| 468 |
- //console.log("split num : " + new_pos.coords.altitude);
|
|
| 469 |
- altitudeString = splitFloatNumber(new_pos.coords.altitude)[0].toString().substring(0, 5); |
|
| 470 |
- timestampISO = new Date(new_pos.timestamp).toISOString(); |
|
| 471 |
- |
|
| 472 |
- //console.log("split num : " + speed);
|
|
| 473 |
- if (isNaN(speed)) {
|
|
| 474 |
- speedString = "---"; |
|
| 475 |
- } else {
|
|
| 476 |
- speedString = splitFloatNumber(speed)[0].toString().substring(0, 3) + "." + splitFloatNumber(speed)[1].toString().substring(0, 1); |
|
| 477 |
- if (speedString == "0.N") {
|
|
| 478 |
- speedString = "0.0"; |
|
| 479 |
- } |
|
| 480 |
- |
|
| 481 |
- //console.log("split num : " + getMaxSpeed(speed));
|
|
| 482 |
- maxSpeedString = splitFloatNumber(getMaxSpeed(speed))[0].toString().substring(0, 3); |
|
| 483 |
- } |
|
| 404 |
+ var prev_pos = getLocation(false); |
|
| 405 |
+ var first_pos = getLocation(true); |
|
| 406 |
+ if (prev_pos === null) {
|
|
| 407 |
+ console.log('--- start building gpx');
|
|
| 408 |
+ storeLocation(new_pos, true); |
|
| 409 |
+ localStorage.setItem("strava_uploaded", false);
|
|
| 410 |
+ localStorage.setItem("custom_uploaded", false);
|
|
| 484 | 411 |
|
| 485 |
- //add a new datapoint to GPX file |
|
| 486 |
- GPXtrkptBuilder(latitudeString, longitudeString, altitudeString, timestampISO); |
|
| 412 |
+ // Start the GPX file |
|
| 413 |
+ GPXHeadersBuilder(new Date(new_pos.timestamp).toISOString(), "test", "18"); |
|
| 487 | 414 |
|
| 415 |
+ } else {
|
|
| 416 |
+ storeLocation(new_pos, false); |
|
| 417 |
+ |
|
| 418 |
+ // Prepare display on watch |
|
| 419 |
+ // now it's only raw data |
|
| 420 |
+ // init strings |
|
| 421 |
+ var latitudeString = ""; |
|
| 422 |
+ var longitudeString = ""; |
|
| 423 |
+ var accuracyString = ""; |
|
| 424 |
+ var altitudeString = ""; |
|
| 425 |
+ var speedString = ""; |
|
| 426 |
+ var distanceString = ""; |
|
| 427 |
+ |
|
| 428 |
+ // get speed from geoloc API isntead of calculate it |
|
| 429 |
+ // speed is initially in m/s, get it at km/h |
|
| 430 |
+ if (new_pos.coords.speed === null) {
|
|
| 431 |
+ var speed = 0; |
|
| 432 |
+ } else {
|
|
| 433 |
+ var speed = new_pos.coords.speed * 3.6; |
|
| 434 |
+ localStorage.setItem("speedsum", parseInt(localStorage.getItem("speedsum")) + speed);
|
|
| 435 |
+ } |
|
| 488 | 436 |
|
| 437 |
+ // distance since beginning in m |
|
| 438 |
+ var dist = distance_on_geoid(prev_pos.coords.latitude, prev_pos.coords.longitude, new_pos.coords.latitude, new_pos.coords.longitude); |
|
| 439 |
+ var totaldist = parseInt(localStorage.getItem("dist"));
|
|
| 440 |
+ if (!isNaN(dist)) {
|
|
| 441 |
+ totaldist = totaldist + parseInt(dist); |
|
| 442 |
+ localStorage.setItem("dist", totaldist);
|
|
| 443 |
+ } |
|
| 444 |
+ distanceString = splitFloatNumber(totaldist / 1000)[0].toString() + "." + splitFloatNumber(totaldist / 1000)[1].toString().substring(0, 3); |
|
| 445 |
+ //console.log("total dist is now " + totaldist);
|
|
| 446 |
+ |
|
| 447 |
+ // avg speed (also when not moving) since beginning |
|
| 448 |
+ var avgspeed = parseInt(localStorage.getItem("speedsum")) / parseInt(localStorage.getItem("totalcoordinates"));
|
|
| 449 |
+ var avgSpeedString = splitFloatNumber(avgspeed)[0].toString() + "." + splitFloatNumber(avgspeed)[1].toString().substring(0, 1); |
|
| 450 |
+ console.log("speedsum=" + parseInt(localStorage.getItem("speedsum")) + " / totalcoordinates=" + parseInt(localStorage.getItem("totalcoordinates")));
|
|
| 451 |
+ console.log("--avgspeed=" + avgspeed + " / avgSpeedString=" + avgSpeedString)
|
|
| 452 |
+ if (avgSpeedString == "0.N") {
|
|
| 453 |
+ avgSpeedString = "0.0"; |
|
| 454 |
+ } |
|
| 455 |
+ //console.log("avg speed is : " + avgSpeedString);
|
|
| 456 |
+ |
|
| 457 |
+ // Duration |
|
| 458 |
+ |
|
| 459 |
+ var duration = new_pos.timestamp - first_pos.timestamp; |
|
| 460 |
+ const date = new Date(duration); |
|
| 461 |
+ durationString = padStart(date.getUTCHours().toString(), 2, "0") + ":" + padStart(date.getMinutes().toString(), 2, "0") + ":" + padStart(date.getSeconds().toString(), 2, "0"); |
|
| 462 |
+ console.log("durationString is : " + durationString);
|
|
| 463 |
+ |
|
| 464 |
+ //formating for precision and max size |
|
| 465 |
+ latitudeString = new_pos.coords.latitude.toString().substring(0, 12); |
|
| 466 |
+ longitudeString = new_pos.coords.longitude.toString().substring(0, 12); |
|
| 467 |
+ accuracyString = new_pos.coords.accuracy.toString().substring(0, 4); |
|
| 468 |
+ //console.log("split num : " + new_pos.coords.altitude);
|
|
| 469 |
+ altitudeString = splitFloatNumber(new_pos.coords.altitude)[0].toString().substring(0, 5); |
|
| 470 |
+ timestampISO = new Date(new_pos.timestamp).toISOString(); |
|
| 471 |
+ |
|
| 472 |
+ //console.log("split num : " + speed);
|
|
| 473 |
+ if (isNaN(speed)) {
|
|
| 474 |
+ speedString = "---"; |
|
| 475 |
+ } else {
|
|
| 476 |
+ speedString = splitFloatNumber(speed)[0].toString().substring(0, 3) + "." + splitFloatNumber(speed)[1].toString().substring(0, 1); |
|
| 477 |
+ if (speedString == "0.N") {
|
|
| 478 |
+ speedString = "0.0"; |
|
| 479 |
+ } |
|
| 489 | 480 |
|
| 490 |
- // Build message |
|
| 491 |
- message = "OK"; |
|
| 492 |
- var dict = {
|
|
| 493 |
- 'accuracy': accuracyString, |
|
| 494 |
- 'distance': distanceString, |
|
| 495 |
- 'avg_speed': avgSpeedString, |
|
| 496 |
- 'duration': durationString, |
|
| 497 |
- 'altitude': altitudeString, |
|
| 498 |
- 'speed': speedString, |
|
| 499 |
- 'max_speed': maxSpeedString, |
|
| 500 |
- 'status': message |
|
| 501 |
- }; |
|
| 481 |
+ //console.log("split num : " + getMaxSpeed(speed));
|
|
| 482 |
+ maxSpeedString = splitFloatNumber(getMaxSpeed(speed))[0].toString().substring(0, 3); |
|
| 483 |
+ } |
|
| 502 | 484 |
|
| 503 |
- // Send the message |
|
| 504 |
- Pebble.sendAppMessage(dict, function () {
|
|
| 505 |
- console.log('Message sent successfully: ' + JSON.stringify(dict));
|
|
| 506 |
- }, function (e) {
|
|
| 507 |
- console.log('Message (' + JSON.stringify(dict) + ') failed: ' + JSON.stringify(e));
|
|
| 508 |
- }); |
|
| 509 |
- } |
|
| 485 |
+ //add a new datapoint to GPX file |
|
| 486 |
+ GPXtrkptBuilder(latitudeString, longitudeString, altitudeString, timestampISO); |
|
| 487 |
+ |
|
| 488 |
+ |
|
| 489 |
+ |
|
| 490 |
+ // Build message |
|
| 491 |
+ message = "OK"; |
|
| 492 |
+ var dict = {
|
|
| 493 |
+ 'accuracy': accuracyString, |
|
| 494 |
+ 'distance': distanceString, |
|
| 495 |
+ 'avg_speed': avgSpeedString, |
|
| 496 |
+ 'duration': durationString, |
|
| 497 |
+ 'altitude': altitudeString, |
|
| 498 |
+ 'speed': speedString, |
|
| 499 |
+ 'max_speed': maxSpeedString, |
|
| 500 |
+ 'status': message |
|
| 501 |
+ }; |
|
| 502 |
+ |
|
| 503 |
+ // Send the message |
|
| 504 |
+ Pebble.sendAppMessage(dict, function() {
|
|
| 505 |
+ console.log('Message sent successfully: ' + JSON.stringify(dict));
|
|
| 506 |
+ }, function(e) {
|
|
| 507 |
+ console.log('Message (' + JSON.stringify(dict) + ') failed: ' + JSON.stringify(e));
|
|
| 508 |
+ }); |
|
| 509 |
+ } |
|
| 510 | 510 |
|
| 511 | 511 |
|
| 512 | 512 |
} |
| 513 | 513 |
|
| 514 | 514 |
function locationError(err) {
|
| 515 | 515 |
|
| 516 |
- console.warn('location error (' + err.code + '): ' + err.message);
|
|
| 516 |
+ console.warn('location error (' + err.code + '): ' + err.message);
|
|
| 517 | 517 |
|
| 518 | 518 |
} |
| 519 | 519 |
|
| 520 | 520 |
|
| 521 | 521 |
function start_get_coordinate() {
|
| 522 |
- clearInterval(firstlocationInterval); |
|
| 523 |
- firstlocationInterval = false; |
|
| 524 |
- locationInterval = setInterval(function () {
|
|
| 525 |
- navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions); |
|
| 526 |
- }, 1000); |
|
| 527 |
- |
|
| 528 |
- if (locate_me) {
|
|
| 529 |
- instantLocationInterval = setInterval(function () {
|
|
| 530 |
- navigator.geolocation.getCurrentPosition(instantLocationUpdate, locationError, locationOptions); |
|
| 531 |
- }, 60000); |
|
| 532 |
- } |
|
| 522 |
+ clearInterval(firstlocationInterval); |
|
| 523 |
+ firstlocationInterval = false; |
|
| 524 |
+ locationInterval = setInterval(function() {
|
|
| 525 |
+ navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions); |
|
| 526 |
+ }, 1000); |
|
| 527 |
+ |
|
| 528 |
+ if (locate_me) {
|
|
| 529 |
+ instantLocationInterval = setInterval(function() {
|
|
| 530 |
+ navigator.geolocation.getCurrentPosition(instantLocationUpdate, locationError, locationOptions); |
|
| 531 |
+ }, 60000); |
|
| 532 |
+ } |
|
| 533 | 533 |
|
| 534 | 534 |
} |
| 535 | 535 |
|
| 536 | 536 |
function init() {
|
| 537 | 537 |
|
| 538 |
- clearInterval(locationInterval); |
|
| 539 |
- locationInterval = false; |
|
| 540 |
- clearInterval(instantLocationInterval); |
|
| 541 |
- instantLocationInterval = false; |
|
| 542 |
- firstlocationInterval = setInterval(function () {
|
|
| 543 |
- navigator.geolocation.getCurrentPosition(null, locationError, firstlocationOptions); |
|
| 544 |
- }, 1000); |
|
| 545 |
- |
|
| 546 |
- //console.log("Clay settings = " + localStorage.getItem('clay-settings'));
|
|
| 547 |
- var se = JSON.parse(localStorage.getItem('clay-settings')).strava_enabled;
|
|
| 548 |
- var su = ("true" === localStorage.getItem("strava_uploaded"));
|
|
| 549 |
- var ce = JSON.parse(localStorage.getItem('clay-settings')).gpx_web_enabled;
|
|
| 550 |
- var cu = ("true" === localStorage.getItem("custom_uploaded"));
|
|
| 551 |
- |
|
| 552 |
- locate_me = JSON.parse(localStorage.getItem('clay-settings')).ping_location_enabled;
|
|
| 553 |
- |
|
| 554 |
- |
|
| 555 |
- console.log("Locate_me = " + locate_me);
|
|
| 556 |
- console.log("Strava = " + se + " (" + typeof se + ")/ uploaded = " + su + " (" + typeof su + ")");
|
|
| 557 |
- console.log("Custom web = " + ce + " (" + typeof ce + ")/ uploaded = " + cu + " (" + typeof cu + ")");
|
|
| 558 |
- |
|
| 559 |
- if ((se && !su) || (ce && !cu)) {
|
|
| 560 |
- |
|
| 561 |
- var GPX = localStorage.getItem("GPX");
|
|
| 562 |
- if (!GPX.substring(GPX.length - 6, 6) == "</gpx>") {
|
|
| 563 |
- console.log("WARNING - NO GPX FOOTER : " + GPX.substring(GPX.length - 6, 6))
|
|
| 564 |
- /*var footer = '</trkseg></trk></gpx>'; |
|
| 565 |
- localStorage.setItem("GPX", GPX + footer);*/
|
|
| 566 |
- GPXfooterBuilder(); |
|
| 567 |
- console.log("GPX FOOTER is now : " + GPX.substring(GPX.length - 6, 6))
|
|
| 568 |
- } |
|
| 538 |
+ clearInterval(locationInterval); |
|
| 539 |
+ locationInterval = false; |
|
| 540 |
+ clearInterval(instantLocationInterval); |
|
| 541 |
+ instantLocationInterval = false; |
|
| 542 |
+ firstlocationInterval = setInterval(function() {
|
|
| 543 |
+ navigator.geolocation.getCurrentPosition(null, locationError, firstlocationOptions); |
|
| 544 |
+ }, 1000); |
|
| 545 |
+ |
|
| 546 |
+ //console.log("Clay settings = " + localStorage.getItem('clay-settings'));
|
|
| 547 |
+ var se = JSON.parse(localStorage.getItem('clay-settings')).strava_enabled;
|
|
| 548 |
+ var su = ("true" === localStorage.getItem("strava_uploaded"));
|
|
| 549 |
+ var ce = JSON.parse(localStorage.getItem('clay-settings')).gpx_web_enabled;
|
|
| 550 |
+ var cu = ("true" === localStorage.getItem("custom_uploaded"));
|
|
| 551 |
+ |
|
| 552 |
+ locate_me = JSON.parse(localStorage.getItem('clay-settings')).ping_location_enabled;
|
|
| 553 |
+ |
|
| 554 |
+ |
|
| 555 |
+ console.log("Locate_me = " + locate_me);
|
|
| 556 |
+ console.log("Strava = " + se + " (" + typeof se + ")/ uploaded = " + su + " (" + typeof su + ")");
|
|
| 557 |
+ console.log("Custom web = " + ce + " (" + typeof ce + ")/ uploaded = " + cu + " (" + typeof cu + ")");
|
|
| 558 |
+ |
|
| 559 |
+ if ((se && !su) || (ce && !cu)) {
|
|
| 560 |
+ |
|
| 561 |
+ var GPX = localStorage.getItem("GPX");
|
|
| 562 |
+ console.log("last 6 char of GPX are " + GPX.substring(GPX.length - 6, GPX.length))
|
|
| 563 |
+ if (GPX.substring(GPX.length - 6, GPX.length) !== "</gpx>") {
|
|
| 564 |
+ console.log("WARNING - NO GPX FOOTER ")
|
|
| 565 |
+ /*var footer = '</trkseg></trk></gpx>'; |
|
| 566 |
+ localStorage.setItem("GPX", GPX + footer);*/
|
|
| 567 |
+ GPXfooterBuilder(); |
|
| 568 |
+ GPX = localStorage.getItem("GPX");
|
|
| 569 |
+ console.log("GPX FOOTER is now : " + GPX.substring(GPX.length - 6, GPX.length))
|
|
| 570 |
+ } |
|
| 569 | 571 |
|
| 570 |
- if (se) {
|
|
| 571 |
- console.log("GPX upload needed to Strava")
|
|
| 572 |
- SendToStrava(); |
|
| 573 |
- } |
|
| 572 |
+ if (se) {
|
|
| 573 |
+ console.log("GPX upload needed to Strava")
|
|
| 574 |
+ SendToStrava(); |
|
| 575 |
+ } |
|
| 574 | 576 |
|
| 575 |
- if (ce) {
|
|
| 576 |
- console.log("GPX upload needed to custom server")
|
|
| 577 |
- PostToWeb(); |
|
| 577 |
+ if (ce) {
|
|
| 578 |
+ console.log("GPX upload needed to custom server")
|
|
| 579 |
+ PostToWeb(); |
|
| 580 |
+ } |
|
| 581 |
+ } else {
|
|
| 582 |
+ console.log("clearing var")
|
|
| 583 |
+ localStorage.setItem("maxSpeed", 0);
|
|
| 584 |
+ localStorage.setItem("firstlatitude", "");
|
|
| 585 |
+ localStorage.setItem("firstlongitude", "");
|
|
| 586 |
+ localStorage.setItem("firsttimestamp", "");
|
|
| 587 |
+ localStorage.setItem("lastlatitude", "");
|
|
| 588 |
+ localStorage.setItem("lastlongitude", "");
|
|
| 589 |
+ localStorage.setItem("lasttimestamp", "");
|
|
| 590 |
+ localStorage.setItem("dist", 0)
|
|
| 591 |
+ localStorage.setItem("speedsum", 0)
|
|
| 578 | 592 |
} |
| 579 |
- } else {
|
|
| 580 |
- console.log("clearing var")
|
|
| 581 |
- localStorage.setItem("maxSpeed", 0);
|
|
| 582 |
- localStorage.setItem("firstlatitude", "");
|
|
| 583 |
- localStorage.setItem("firstlongitude", "");
|
|
| 584 |
- localStorage.setItem("firsttimestamp", "");
|
|
| 585 |
- localStorage.setItem("lastlatitude", "");
|
|
| 586 |
- localStorage.setItem("lastlongitude", "");
|
|
| 587 |
- localStorage.setItem("lasttimestamp", "");
|
|
| 588 |
- localStorage.setItem("dist", 0)
|
|
| 589 |
- localStorage.setItem("speedsum", 0)
|
|
| 590 |
- } |
|
| 591 | 593 |
|
| 592 | 594 |
} |
| 593 | 595 |
|
| 594 | 596 |
// Get JS readiness events |
| 595 |
-Pebble.addEventListener('ready', function (e) {
|
|
| 596 |
- console.log('PebbleKit JS is ready');
|
|
| 597 |
- // Update Watch on this |
|
| 598 |
- Pebble.sendAppMessage({ 'JSReady': 1 });
|
|
| 597 |
+Pebble.addEventListener('ready', function(e) {
|
|
| 598 |
+ console.log('PebbleKit JS is ready');
|
|
| 599 |
+ // Update Watch on this |
|
| 600 |
+ Pebble.sendAppMessage({ 'JSReady': 1 });
|
|
| 599 | 601 |
|
| 600 |
- init(); |
|
| 602 |
+ init(); |
|
| 601 | 603 |
}); |
| 602 | 604 |
|
| 603 | 605 |
// Get AppMessage events |
| 604 |
-Pebble.addEventListener('appmessage', function (e) {
|
|
| 605 |
- // Get the dictionary from the message |
|
| 606 |
- var dict = e.payload; |
|
| 607 |
- //console.log(dict[0].toString()); |
|
| 608 |
- switch (dict[0]) {
|
|
| 609 |
- case 'startstop': |
|
| 610 |
- if (!locationInterval == false) {
|
|
| 611 |
- console.log("Stopping the track");
|
|
| 612 |
- clearInterval(locationInterval); |
|
| 613 |
- locationInterval = false; |
|
| 614 |
- clearInterval(instantLocationInterval); |
|
| 615 |
- instantLocationInterval = false; |
|
| 616 |
- firstlocationInterval = setInterval(function () {
|
|
| 617 |
- navigator.geolocation.getCurrentPosition(null, locationError, firstlocationOptions); |
|
| 618 |
- }, 1000); |
|
| 619 |
- } else {
|
|
| 620 |
- console.log("Starting the track");
|
|
| 621 |
- start_get_coordinate(); |
|
| 622 |
- } |
|
| 623 |
- break; |
|
| 624 |
- case 'send': |
|
| 625 |
- if (locate_me) {
|
|
| 626 |
- var prev_pos = getLocation(false); |
|
| 627 |
- instantLocationUpdate(prev_pos); |
|
| 628 |
- } |
|
| 629 |
- |
|
| 630 |
- init(); |
|
| 631 |
- |
|
| 632 |
- /* |
|
| 633 |
- // Send the message |
|
| 634 |
- var dict = {
|
|
| 635 |
- 'status': "EXIT" |
|
| 636 |
- }; |
|
| 637 |
- Pebble.sendAppMessage(dict, function () {
|
|
| 638 |
- console.log('Message sent successfully: ' + JSON.stringify(dict));
|
|
| 639 |
- }, function (e) {
|
|
| 640 |
- console.log('Message (' + JSON.stringify(dict) + ') failed: ' + JSON.stringify(e));
|
|
| 641 |
- }); |
|
| 642 |
- */ |
|
| 643 |
- break; |
|
| 644 |
- default: |
|
| 645 |
- console.log('Sorry. I don\'t understand your request :' + dict[0]);
|
|
| 646 |
- } |
|
| 606 |
+Pebble.addEventListener('appmessage', function(e) {
|
|
| 607 |
+ // Get the dictionary from the message |
|
| 608 |
+ var dict = e.payload; |
|
| 609 |
+ //console.log(dict[0].toString()); |
|
| 610 |
+ switch (dict[0]) {
|
|
| 611 |
+ case 'startstop': |
|
| 612 |
+ if (!locationInterval == false) {
|
|
| 613 |
+ console.log("Stopping the track");
|
|
| 614 |
+ clearInterval(locationInterval); |
|
| 615 |
+ locationInterval = false; |
|
| 616 |
+ clearInterval(instantLocationInterval); |
|
| 617 |
+ instantLocationInterval = false; |
|
| 618 |
+ firstlocationInterval = setInterval(function() {
|
|
| 619 |
+ navigator.geolocation.getCurrentPosition(null, locationError, firstlocationOptions); |
|
| 620 |
+ }, 1000); |
|
| 621 |
+ } else {
|
|
| 622 |
+ console.log("Starting the track");
|
|
| 623 |
+ start_get_coordinate(); |
|
| 624 |
+ } |
|
| 625 |
+ break; |
|
| 626 |
+ case 'send': |
|
| 627 |
+ if (locate_me) {
|
|
| 628 |
+ var prev_pos = getLocation(false); |
|
| 629 |
+ instantLocationUpdate(prev_pos); |
|
| 630 |
+ } |
|
| 631 |
+ |
|
| 632 |
+ init(); |
|
| 633 |
+ |
|
| 634 |
+ /* |
|
| 635 |
+ // Send the message |
|
| 636 |
+ var dict = {
|
|
| 637 |
+ 'status': "EXIT" |
|
| 638 |
+ }; |
|
| 639 |
+ Pebble.sendAppMessage(dict, function () {
|
|
| 640 |
+ console.log('Message sent successfully: ' + JSON.stringify(dict));
|
|
| 641 |
+ }, function (e) {
|
|
| 642 |
+ console.log('Message (' + JSON.stringify(dict) + ') failed: ' + JSON.stringify(e));
|
|
| 643 |
+ }); |
|
| 644 |
+ */ |
|
| 645 |
+ break; |
|
| 646 |
+ default: |
|
| 647 |
+ console.log('Sorry. I don\'t understand your request :' + dict[0]);
|
|
| 648 |
+ } |
|
| 647 | 649 |
|
| 648 | 650 |
}); |
| 649 | 651 |
\ No newline at end of file |