Browse code

modified authenticate function to have timeout and second retry on failed xhr

louis authored on22/06/2022 17:06:52
Showing8 changed files
... ...
@@ -1,5 +1,5 @@
1 1
 argv = ['/home/louis/.pebble-sdk/SDKs/current/sdk-core/pebble/waf', 'configure']
2
-environ = {'LC_NUMERIC': 'fr_FR.UTF-8', 'QT4_IM_MODULE': 'xim', 'GJS_DEBUG_OUTPUT': 'stderr', 'PEBBLE_TOOLCHAIN_PATH': '/home/louis/pebble-dev/pebble-sdk-4.5-linux64/arm-cs-tools/bin', 'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'QT_IM_MODULE': 'xim', 'LOGNAME': 'louis', 'USER': 'louis', 'PATH': '/home/louis/pebble-dev/pebble-sdk-4.5-linux64/arm-cs-tools/bin:/home/louis/pebble-dev/pebble-sdk-4.5-linux64/bin:/home/louis/pebble-dev/pebble-sdk-4.5-linux64/bin:/sbin:/bin:/usr/bin:/usr/local/bin:/snap/bin', 'LC_PAPER': 'fr_FR.UTF-8', 'HOME': '/home/louis', 'XRDP_SESSION': '1', 'DISPLAY': ':10.0', 'SSH_AGENT_PID': '1779', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'UID': '1000', 'SESSION_MANAGER': 'local/rebble-dev:@/tmp/.ICE-unix/1694,unix/rebble-dev:/tmp/.ICE-unix/1694', 'LC_MEASUREMENT': 'fr_FR.UTF-8', 'QT_ACCESSIBILITY': '1', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'CLUTTER_IM_MODULE': 'xim', 'TEXTDOMAIN': 'im-config', 'XMODIFIERS': '@im=ibus', 'GPG_AGENT_INFO': '/run/user/1000/gnupg/S.gpg-agent:0:1', 'NOCLIMB': '1', 'SHLVL': '2', 'XRDP_SOCKET_PATH': '/var/run/xrdp/sockdir', 'XDG_RUNTIME_DIR': '/run/user/1000', 'GTK_IM_MODULE': 'ibus', 'LC_ADDRESS': 'fr_FR.UTF-8', 'SSH_AUTH_SOCK': '/tmp/ssh-c4le1cSUUPxw/agent.1694', 'VTE_VERSION': '5202', 'IM_CONFIG_PHASE': '2', 'TEXTDOMAINDIR': '/usr/share/locale/', 'GNOME_SHELL_SESSION_MODE': 'ubuntu', 'NO_GCE_CHECK': 'False', 'PULSE_SCRIPT': '/etc/xrdp/pulse/default.pa', 'LESSOPEN': '| /bin/lesspipe %s', 'XDG_SESSION_ID': 'c3', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus', '_': '/home/louis/pebble-dev/pebble-sdk-4.5-linux64/.env/bin/python', 'GNOME_TERMINAL_SERVICE': ':1.73', 'LC_IDENTIFICATION': 'fr_FR.UTF-8', 'LESSCLOSE': '/bin/lesspipe %s %s', 'GTK_MODULES': 'gail:atk-bridge', 'OLDPWD': '/home/louis/pebble-dev/repos/synocam_home_switch/src', 'PYTHONHOME': '/home/louis/.pebble-sdk/SDKs/current/sdk-core/../.env', 'LC_TELEPHONE': 'fr_FR.UTF-8', 'NODE_PATH': '/home/louis/.pebble-sdk/SDKs/current/sdk-core/../node_modules', 'LC_MONETARY': 'fr_FR.UTF-8', 'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop', 'PWD': '/home/louis/pebble-dev/repos/synocam_home_switch', 'COLORTERM': 'truecolor', 'LC_NAME': 'fr_FR.UTF-8', 'XDG_MENU_PREFIX': 'gnome-', 'LC_TIME': 'fr_FR.UTF-8', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:', 'PHONESIM_PATH': '/home/louis/pebble-dev/pebble-sdk-4.5-linux64/.env//bin/pypkjs', 'GJS_DEBUG_TOPICS': 'JS ERROR;JS LOG', 'GNOME_TERMINAL_SCREEN': '/org/gnome/Terminal/screen/21bf7104_d556_46cc_ba74_7bc3817c18f2'}
2
+environ = {'LC_NUMERIC': 'fr_FR.UTF-8', 'QT4_IM_MODULE': 'xim', 'GJS_DEBUG_OUTPUT': 'stderr', 'PEBBLE_TOOLCHAIN_PATH': '/home/louis/pebble-dev/pebble-sdk-4.6-rc2-linux64/arm-cs-tools/bin', 'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'QT_IM_MODULE': 'xim', 'LOGNAME': 'louis', 'USER': 'louis', 'PATH': '/home/louis/pebble-dev/pebble-sdk-4.6-rc2-linux64/arm-cs-tools/bin:/home/louis/pebble-dev/pebble-sdk-4.6-rc2-linux64/bin:/home/louis/pebble-dev/pebble-sdk-4.6-rc2-linux64/bin:/sbin:/bin:/usr/bin:/usr/local/bin:/snap/bin', 'LC_PAPER': 'fr_FR.UTF-8', 'HOME': '/home/louis', 'XRDP_SESSION': '1', 'DISPLAY': ':10.0', 'SSH_AGENT_PID': '2293', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'UID': '1000', 'SESSION_MANAGER': 'local/ubuntu-IDS:@/tmp/.ICE-unix/2149,unix/ubuntu-IDS:/tmp/.ICE-unix/2149', 'LC_MEASUREMENT': 'fr_FR.UTF-8', 'QT_ACCESSIBILITY': '1', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'CLUTTER_IM_MODULE': 'xim', 'TEXTDOMAIN': 'im-config', 'XMODIFIERS': '@im=ibus', 'GPG_AGENT_INFO': '/run/user/1000/gnupg/S.gpg-agent:0:1', 'NOCLIMB': '1', 'SHLVL': '2', 'XRDP_SOCKET_PATH': '/var/run/xrdp/sockdir', 'XDG_RUNTIME_DIR': '/run/user/1000', 'GTK_IM_MODULE': 'ibus', 'LC_ADDRESS': 'fr_FR.UTF-8', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'VTE_VERSION': '5202', 'IM_CONFIG_PHASE': '2', 'TEXTDOMAINDIR': '/usr/share/locale/', 'GNOME_SHELL_SESSION_MODE': 'ubuntu', 'NO_GCE_CHECK': 'False', 'PULSE_SCRIPT': '/etc/xrdp/pulse/default.pa', 'LESSOPEN': '| /bin/lesspipe %s', 'XDG_SESSION_ID': 'c3', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus', '_': '/home/louis/pebble-dev/pebble-sdk-4.6-rc2-linux64/.env/bin/python', 'GNOME_TERMINAL_SERVICE': ':1.68', 'LC_IDENTIFICATION': 'fr_FR.UTF-8', 'LESSCLOSE': '/bin/lesspipe %s %s', 'GTK_MODULES': 'gail:atk-bridge', 'OLDPWD': '/home/louis/pebble-dev/repos/synocam_home_switch/src/pkjs', 'PYTHONHOME': '/home/louis/.pebble-sdk/SDKs/current/sdk-core/../.env', 'LC_TELEPHONE': 'fr_FR.UTF-8', 'NODE_PATH': '/home/louis/.pebble-sdk/SDKs/current/sdk-core/../node_modules', 'LC_MONETARY': 'fr_FR.UTF-8', 'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop', 'PWD': '/home/louis/pebble-dev/repos/synocam_home_switch', 'COLORTERM': 'truecolor', 'LC_NAME': 'fr_FR.UTF-8', 'XDG_MENU_PREFIX': 'gnome-', 'LC_TIME': 'fr_FR.UTF-8', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:', 'PHONESIM_PATH': '/home/louis/pebble-dev/pebble-sdk-4.6-rc2-linux64/.env//bin/pypkjs', 'GJS_DEBUG_TOPICS': 'JS ERROR;JS LOG', 'GNOME_TERMINAL_SCREEN': '/org/gnome/Terminal/screen/384bf1de_cacf_434f_ae77_2153d0d1d5b7'}
3 3
 files = ['/home/louis/pebble-dev/repos/synocam_home_switch/wscript']
4 4
 hash = -2431564940392307765
5 5
 options = {'files': '', 'jobs': 2, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'no_groups': False, 'distcheck_args': None, 'top': '', 'sandbox': False, 'destdir': '', 'keep': 0, 'zones': '', 'debug': False, 'prefix': '/usr/local/', 'timestamp': None, 'download': False, 'force': False, 'targets': '', 'out': ''}
6 6
Binary files a/build/synocam_home_switch.pbw and b/build/synocam_home_switch.pbw differ
7 7
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+!<arch>
0 2
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+!<arch>
0 2
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+#pragma once
2
+#include <stdint.h>
3
+
4
+//
5
+// AUTOGENERATED BY BUILD
6
+// DO NOT MODIFY - CHANGES WILL BE OVERWRITTEN
7
+//
8
+
0 9
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+#pragma once
2
+#include <stdint.h>
3
+
4
+//
5
+// AUTOGENERATED BY BUILD
6
+// DO NOT MODIFY - CHANGES WILL BE OVERWRITTEN
7
+//
8
+
... ...
@@ -9,7 +9,7 @@ const uint32_t inbox_size = 64;
9 9
 const uint32_t outbox_size = 64;
10 10
 
11 11
 
12
-static char s_auth[40];
12
+static char s_auth[60];
13 13
 static char s_api[40];
14 14
 static char s_username[40];
15 15
 static char s_password[40];
... ...
@@ -262,7 +262,7 @@ static void inbox_received_callback(DictionaryIterator *iter, void *context) {
262 262
   Tuple *auth_tuple = dict_find(iter, MESSAGE_KEY_auth);
263 263
 
264 264
   if(auth_tuple) {
265
-    strncpy(s_auth, auth_tuple->value->cstring, 40);
265
+    strncpy(s_auth, auth_tuple->value->cstring, 60);
266 266
     // Display in the TextLayer
267 267
     text_layer_set_text(s_text_layer, s_auth);
268 268
     APP_LOG(APP_LOG_LEVEL_DEBUG, "Auth message received ... ");
... ...
@@ -6,13 +6,15 @@ var messageKeys = require('message_keys');
6 6
 
7 7
 var sid;
8 8
 var status;
9
+var retry;
10
+retry=0;
9 11
 
10 12
 function authenticate() {
11 13
   var response;
12 14
   sid="";
13 15
   console.log('---- authenticate');
14 16
   console.log('-- '+localStorage.getItem('username'));
15
-  console.log('-- '+localStorage.getItem('password'));
17
+  //console.log('-- '+localStorage.getItem('password'));
16 18
   console.log('-- '+localStorage.getItem('server'));
17 19
   if (localStorage.getItem('username')  && localStorage.getItem('password') && localStorage.getItem('server') ){
18 20
     var username=localStorage.getItem('username');
... ...
@@ -20,37 +22,72 @@ function authenticate() {
20 22
     var server=localStorage.getItem('server');
21 23
     var url = server + "/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account="+username+"&passwd="+password+"&session=SurveillanceStation&format=sid";
22 24
     var xhr = new XMLHttpRequest();
23
-  
24
-    xhr.open("GET", url,false);
25
-    xhr.send();
25
+    xhr.timeout = 3000; // time in milliseconds
26 26
 
27
-    if(xhr.status == 200) {
28
-      response = JSON.parse(xhr.responseText);
29
-      if (response.success == true){
30
-        sid = response.data.sid;
31
-      }
32
-    }else {
33
-      console.log('------Request returned error code ' + xhr.status.toString());
34
-    }
35
-  
36
-    if (sid != ""){
37
-      auth = "";
38
-      message = "Welcome to Syno Cam Switch ! ready & authenticated";
39
-           
40
-      // Build message
41
-      var dict = {
42
-        'auth': message,
43
-      };
27
+    xhr.open("GET", url,true);
44 28
     
45
-      // Send the message
46
-      Pebble.sendAppMessage(dict, function(e) {
47
-        console.log('sent');
48
-      }, function() {
49
-        console.log('failed');
50
-      });
51
-       
52
-    }
29
+    xhr.onload = function () {
30
+      if (xhr.readyState === 4) {
31
+        console.log('------authentication request returned');
32
+        if(xhr.status == 200) {
33
+          response = JSON.parse(xhr.responseText);
34
+          if (response.success == true){
35
+            sid = response.data.sid;
36
+          }else{
37
+  	    console.log('------Authentication failed : '+ response.toString());
38
+          }
39
+        }else {
40
+          console.log('------Request returned error code ' + xhr.status);
41
+        }
42
+    
43
+        if (sid != ""){
44
+          auth = "";
45
+          message = "Welcome to Syno Cam Switch ! ready & authenticated";
46
+               
47
+          // Build message
48
+          var dict = {
49
+            'auth': message,
50
+          };
51
+        
52
+          // Send the message
53
+          Pebble.sendAppMessage(dict, function(e) {
54
+            console.log('sent');
55
+          }, function() {
56
+            console.log('failed');
57
+          });
58
+         
59
+        }else{
60
+          console.log('------Unexpected error : authentication is OK but no SID retrieved');
61
+        }
62
+      }
63
+    };
64
+
65
+    xhr.ontimeout = function (e){
66
+      retry++;
67
+      if (retry<=1){
68
+        console.log('------Authentication timed out retrying another time ');
69
+        authenticate();
70
+      }else{
71
+        console.log('------Authentication timed out a second time');
72
+	//send back "timeout" to watch
73
+	auth="";
74
+	message ="Authentication timed out, verify settings and connectivity";
75
+
76
+	// Build message
77
+        var dict = {
78
+          'auth': message,
79
+        };
80
+
81
+        // Send the message
82
+        Pebble.sendAppMessage(dict, function(e) {
83
+          console.log('sent');
84
+        }, function() {
85
+          console.log('failed');
86
+        });
87
+      }
88
+    };
53 89
       
90
+    xhr.send();
54 91
   }else{
55 92
     Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
56 93
   }
... ...
@@ -171,7 +208,7 @@ function switch_home(bool) {
171 208
               console.log('------Request returned error code ' + xhr.status.toString());
172 209
             }
173 210
           }else{
174
-            console.log('---- nothign to do, status already '+status);
211
+            console.log('---- nothing to do, status already '+status);
175 212
             switch (status) {
176 213
               case true:
177 214
                 message = "Your Home Mode is already ON";
... ...
@@ -212,38 +249,6 @@ Pebble.addEventListener('ready',function(e){
212 249
   Pebble.sendAppMessage({'JSReady':1});
213 250
 });
214 251
 
215
-/*
216
-Pebble.addEventListener('webviewclosed', function(e) {
217
-  if (e && !e.response) {
218
-    return;
219
-  }
220
-
221
-  // Get the keys and values from each config item
222
-  var dict = clay.getSettings(e.response);
223
-  //  console.log("server is "+dict[messageKeys.server]);
224
-
225
-  // Send settings values to watch side
226
-  Pebble.sendAppMessage({'server':dict[messageKeys.server]}, function(e) {
227
-    console.log('Sent server config to Pebble');
228
-  }, function(e) {
229
-    console.log('Failed to send server config!');
230
-    console.log(JSON.stringify(e));
231
-  });
232
-  Pebble.sendAppMessage({'username':dict[messageKeys.username]}, function(e) {
233
-    console.log('Sent username config to Pebble');
234
-  }, function(e) {
235
-    console.log('Failed to send username config!');
236
-    console.log(JSON.stringify(e));
237
-  });
238
-  Pebble.sendAppMessage({'password':dict[messageKeys.password]}, function(e) {
239
-    console.log('Sent password config to Pebble');
240
-  }, function(e) {
241
-    console.log('Failed to send password config!');
242
-    console.log(JSON.stringify(e));
243
-  });
244
-});
245
-*/
246
-
247 252
 // Get AppMessage events
248 253
 Pebble.addEventListener('appmessage', function(e) {
249 254
   // Get the dictionary from the message