Browse code

added instant location for "find_me" webpage

louis.jonget authored on10/10/2022 15:49:15
Showing2 changed files
1 1
Binary files a/build/bike_companion.pbw and b/build/bike_companion.pbw differ
... ...
@@ -151,7 +151,6 @@ function SendToStrava() {
151 151
 
152 152
   /* -------------------- */
153 153
   var array = [GPX]; // an array consisting of a single string
154
-  console.log('------array for blob: ' + array)
155 154
   var blob = new Blob(array, { type: "application/gpx+xml" }); // the blob
156 155
 
157 156
   // creating multipart/form-data to be sent
... ...
@@ -169,7 +168,8 @@ function SendToStrava() {
169 168
   xhr.open("POST", url, false);
170 169
   xhr.setRequestHeader("Authorization", "Bearer d8927033b3996efe1e5a4e62425bc2aff8f635b0");
171 170
 
172
-  console.log('------xhr opened with authorization')
171
+  console.log('------GPX / xhr opened with authorization')
172
+  console.log('------array for blob: ' + array)
173 173
   xhr.onload = function () {
174 174
     console.log('------xhr onload')
175 175
     if (xhr.readyState === 4) {
... ...
@@ -185,6 +185,7 @@ function SendToStrava() {
185 185
     }
186 186
   };
187 187
 
188
+  // send with data in body
188 189
   xhr.send(data);
189 190
   /* -------------------- */
190 191
 
... ...
@@ -194,7 +195,7 @@ function SendToStrava() {
194 195
 // Build CSV
195 196
 function CSV(pos) {
196 197
   var CSV = localStorage.getItem("CSV");
197
-  var datapoint = pos.timestamp + "," + pos.coords.lat + "," + pos.coords.long + "," + pos.coords.altitude + "," + pos.coords.accuracy + "," + pos.coords.altitudeAccuracy + "," + pos.coords.heading + "," + pos.coords.speed;
198
+  var datapoint = pos.timestamp + "," + pos.coords.latitude + "," + pos.coords.longitude + "," + pos.coords.altitude + "," + pos.coords.accuracy + "," + pos.coords.altitudeAccuracy + "," + pos.coords.heading + "," + pos.coords.speed + "\n";
198 199
 
199 200
   localStorage.setItem("CSV", CSV + datapoint);
200 201
 
... ...
@@ -204,13 +205,13 @@ function CSV(pos) {
204 205
 function PostToWeb() {
205 206
   var CSV = localStorage.getItem("CSV");
206 207
 
207
-  var url = "https://192.168.170.33/strava/upload.php";
208
+  var url = "https://jonget.fr/strava/upload.php?name=pebblecsv&type=gpx/xml";
208 209
   var xhr = new XMLHttpRequest();
209 210
   xhr.timeout = 10000; // time in milliseconds
210 211
 
211 212
   xhr.open("POST", url, false);
212 213
 
213
-  console.log('------xhr opened')
214
+  console.log('------ CSV / xhr opened')
214 215
   xhr.onload = function () {
215 216
     console.log('------xhr onload')
216 217
     if (xhr.readyState === 4) {
... ...
@@ -231,6 +232,40 @@ function PostToWeb() {
231 232
 
232 233
 }
233 234
 
235
+// Send location to web server for instant location (no tracking)
236
+// TODO : secure it ?
237
+function instantLocationUpdate(pos) {
238
+
239
+  console.log('--- Instant location update');
240
+  // console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
241
+
242
+  var url = "https://jonget.fr/find_me/update.php?lat=" + pos.coords.latitude + "&long=" + pos.coords.longitude + "&acc=" + pos.coords.accuracy + "&timestamp=" + pos.timestamp;
243
+  var xhr = new XMLHttpRequest();
244
+  xhr.timeout = 10000; // time in milliseconds
245
+
246
+  xhr.open("POST", url);
247
+
248
+  console.log('------ instant / xhr opened')
249
+  xhr.onload = function () {
250
+    console.log('------xhr onload')
251
+    if (xhr.readyState === 4) {
252
+      console.log('------xhr request returned with ' + xhr.status);
253
+      console.log(this.responseText);
254
+      if (xhr.status == 200) {
255
+        //console.log('--> HTTP 200');
256
+        return true;
257
+      } else {
258
+        //console.log('--> HTTP ' + xhr.status);
259
+        return false;
260
+      }
261
+    }
262
+  };
263
+
264
+  //send without body
265
+  xhr.send();
266
+
267
+}
268
+
234 269
 
235 270
 // Adding leading characters to string for nice displays
236 271
 //
... ...
@@ -250,7 +285,7 @@ function padStart(string, max_length, padding) {
250 285
 //
251 286
 function locationSuccess(new_pos) {
252 287
   console.log('--- locationSuccess');
253
-  console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
288
+  // console.log(" location is " + new_pos.coords.latitude + ',' + new_pos.coords.longitude + ' , acc. ' + new_pos.coords.accuracy);
254 289
 
255 290
   var prev_pos = getLocation();
256 291
   storeLocation(new_pos);
... ...
@@ -269,7 +304,15 @@ function locationSuccess(new_pos) {
269 304
     //clear watch of new position to avoid overlap
270 305
     //navigator.geolocation.clearWatch(geoloc_id);
271 306
     //console.log('--- watch geoloc cleared');
272
-    var speed = speed_from_distance_and_time(prev_pos, new_pos);
307
+    //var speed = speed_from_distance_and_time(prev_pos, new_pos);
308
+
309
+    //get speed from geoloc API isntead of calculate it
310
+    // speed is initially in m/s, get it at km/h
311
+    if (new_pos.coords.speed === null) {
312
+      var speed = 0;
313
+    } else {
314
+      var speed = new_pos.coords.speed * 3.6;
315
+    }
273 316
 
274 317
     // Prepare display on watch
275 318
     // now it's only raw data
... ...
@@ -300,13 +343,14 @@ function locationSuccess(new_pos) {
300 343
     if (gpx_to_strava) {
301 344
       //add a new datapoint to GPX file
302 345
       GPXtrkptBuilder(latitudeString, longitudeString, altitudeString, timestampISO);
346
+      console.log('GPX: ' + localStorage.getItem("GPX"));
303 347
     }
304 348
     if (csv_to_web) {
305 349
       // Start the csv file
306 350
       CSV(new_pos);
351
+      console.log('CSV: ' + localStorage.getItem("CSV"));
307 352
     }
308 353
 
309
-    console.log('GPX: ' + localStorage.getItem("GPX"));
310 354
 
311 355
     // Build message
312 356
     message = "OK";
... ...
@@ -357,12 +401,17 @@ function get_coordinate() {
357 401
     navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
358 402
   }, 5000);
359 403
 
404
+  instantLocationInterval = setInterval(function () {
405
+    navigator.geolocation.getCurrentPosition(instantLocationUpdate, locationError, locationOptions);
406
+  }, 60000);
407
+
360 408
 }
361 409
 
362 410
 function init() {
363 411
   // local storage init,
364 412
   // todo : clear only temporary values, not clay config (and do it on exit ?)
365 413
   localStorage.clear();
414
+  localStorage.setItem("CSV", "");
366 415
   // clear any other var to do
367 416
   clearInterval(locationInterval);
368 417