Browse code

retry on auth error

louis.jonget authored on16/03/2023 14:09:28
Showing3 changed files
1 1
Binary files a/build/bike_companion.pbw and b/build/bike_companion.pbw differ
... ...
@@ -373,13 +373,22 @@ static void inbox_received_callback(DictionaryIterator *iter, void *context) {
373 373
     */
374 374
 
375 375
     //APP_LOG(APP_LOG_LEVEL_DEBUG, "to display : %s ",s_msg);
376
-
377
-    text_layer_set_text(s_upperleft_text_layer, s_altitude);
378
-    text_layer_set_text(s_upperright_text_layer, s_distance);
379
-    text_layer_set_text(s_main_text_layer, s_speed);
380
-    text_layer_set_text(s_lowerleft_text_layer, s_avg_speed);
381
-    text_layer_set_text(s_lowerright_text_layer, s_max_speed);
382
-    text_layer_set_text(s_lower_text_layer, s_duration);
376
+    if(strcmp(s_status, "L200")==0){
377
+      text_layer_set_text(s_upperleft_text_layer, s_altitude);
378
+      text_layer_set_text(s_upperright_text_layer, s_distance);
379
+      text_layer_set_text(s_main_text_layer, s_speed);
380
+      text_layer_set_text(s_lowerleft_text_layer, s_avg_speed);
381
+      text_layer_set_text(s_lowerright_text_layer, s_max_speed);
382
+      text_layer_set_text(s_lower_text_layer, s_duration);
383
+    }else if(strcmp(s_status, "S200")==0){
384
+      text_layer_set_text(s_lower_text_layer, "upload OK");
385
+      vibes_short_pulse();
386
+    }else if(strcmp(s_status, "S401")==0){
387
+      text_layer_set_text(s_lower_text_layer, "auth err, retrying");
388
+    }else{
389
+      text_layer_set_text(s_lower_text_layer, s_status);
390
+      vibes_double_pulse();
391
+    }
383 392
   }else{
384 393
     //APP_LOG(APP_LOG_LEVEL_DEBUG, "not status message... ");
385 394
   }
... ...
@@ -10,7 +10,7 @@ var firstlocationInterval = false;
10 10
 var locationInterval = false;
11 11
 var instantLocationInterval = false;
12 12
 
13
-// TODO to remove for security
13
+// TODO to move to C for security
14 14
 var client_id = "94880";
15 15
 var client_secret = "08dc170f0fe38f39dd327bea82a28db4400e6f00";
16 16
 
... ...
@@ -181,7 +181,7 @@ function splitFloatNumber(num) {
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>';
184
+  var headers = '<?xml version="1.0" encoding="UTF-8"?><gpx creator="Pebble" 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 185
   localStorage.setItem("GPX", headers);
186 186
   return true;
187 187
 }
... ...
@@ -201,7 +201,7 @@ function GPXfooterBuilder() {
201 201
   var GPX = localStorage.getItem("GPX");
202 202
   var footer = '</trkseg></trk></gpx>';
203 203
   var ret = localStorage.setItem("GPX", GPX + footer);
204
-  //console.log("GPX closed : " + GPX + footer);
204
+  console.log("GPX closed : " + localStorage.getItem("GPX"));
205 205
   return ret;
206 206
 }
207 207
 
... ...
@@ -289,21 +289,35 @@ function SendToStrava() {
289 289
     callback: function (e) {
290 290
       var message = "";
291 291
       if (console.log(e.status + " - " + e.txt), 201 == e.status) {
292
-        message = "Your activity has been created";
292
+        message = "S200";
293 293
         localStorage.setItem("strava_uploaded", true);
294 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.";
295
+        message = "S400";
296 296
       } else if (401 == e.status) {
297
-        message = "Error - Unauthorized. Please check your credentials in the settings."
297
+        //token expired, retrying
298
+        message = "S401"
299
+        SendToStrava()
298 300
       } else {
299 301
         try {
300 302
           response_json = JSON.parse(e.txt)
301 303
           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
+        } catch (err) {
305
+          console.log("Error log, " + err)
304 306
         }
307
+        message = "S500";
305 308
       }
306
-      message && Pebble.showSimpleNotificationOnPebble("Ventoo SE - Strava", message)
309
+      //message && Pebble.showSimpleNotificationOnPebble("Ventoo SE - Strava", message)
310
+      
311
+      // Build message
312
+      var dict = {
313
+        'status': message
314
+      };
315
+      // Send the message
316
+      Pebble.sendAppMessage(dict, function () {
317
+        console.log('Message sent successfully: ' + JSON.stringify(dict));
318
+      }, function (e) {
319
+        console.log('Message (' + JSON.stringify(dict) + ') failed: ' + JSON.stringify(e));
320
+      });
307 321
     }
308 322
   }
309 323
   var XHR = new XMLHttpRequest;
... ...
@@ -357,7 +371,7 @@ function PostToWeb() {
357 371
     }
358 372
   };
359 373
 
360
-  //send CSV in body
374
+  //send GPX in body
361 375
   xhr.send(GPX);
362 376
 
363 377
 }
... ...
@@ -488,7 +502,7 @@ function locationSuccess(new_pos) {
488 502
 
489 503
 
490 504
     // Build message
491
-    message = "OK";
505
+    message = "L200";
492 506
     var dict = {
493 507
       'accuracy': accuracyString,
494 508
       'distance': distanceString,
... ...
@@ -559,12 +573,14 @@ function init() {
559 573
   if ((se && !su) || (ce && !cu)) {
560 574
 
561 575
     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))
576
+    console.log("last 6 char of GPX are " + GPX.substring(GPX.length - 6, GPX.length))
577
+    if (GPX.substring(GPX.length - 6, GPX.length) !== "</gpx>") {
578
+        console.log("WARNING - NO GPX FOOTER ")
579
+            /*var footer = '</trkseg></trk></gpx>';
580
+            localStorage.setItem("GPX", GPX + footer);*/
581
+        GPXfooterBuilder();
582
+        GPX = localStorage.getItem("GPX");
583
+        console.log("GPX FOOTER is now : " + GPX.substring(GPX.length - 6, GPX.length))
568 584
     }
569 585
 
570 586
     if (se) {
... ...
@@ -613,9 +629,9 @@ Pebble.addEventListener('appmessage', function (e) {
613 629
         locationInterval = false;
614 630
         clearInterval(instantLocationInterval);
615 631
         instantLocationInterval = false;
616
-        firstlocationInterval = setInterval(function () {
632
+        /*firstlocationInterval = setInterval(function () {
617 633
           navigator.geolocation.getCurrentPosition(null, locationError, firstlocationOptions);
618
-        }, 1000);
634
+        }, 1000);*/
619 635
       } else {
620 636
         console.log("Starting the track");
621 637
         start_get_coordinate();
... ...
@@ -626,20 +642,9 @@ Pebble.addEventListener('appmessage', function (e) {
626 642
         var prev_pos = getLocation(false);
627 643
         instantLocationUpdate(prev_pos);
628 644
       }
629
-
645
+      
630 646
       init();
631 647
 
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 648
       break;
644 649
     default:
645 650
       console.log('Sorry. I don\'t understand your request :' + dict[0]);