Browse code

all calls to xhr function with retry feature

Louis authored on12/12/2022 20:09:06
Showing4 changed files
... ...
@@ -1,8 +1,8 @@
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.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': '2174', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'UID': '1000', 'SESSION_MANAGER': 'local/ubuntu-IDS:@/tmp/.ICE-unix/2087,unix/ubuntu-IDS:/tmp/.ICE-unix/2087', '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.70', 'LC_IDENTIFICATION': 'fr_FR.UTF-8', 'LESSCLOSE': '/bin/lesspipe %s %s', 'GTK_MODULES': 'gail:atk-bridge', 'OLDPWD': '/home/louis/pebble-dev/repo_remote/synocam_home_switch/build', '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/repo_remote/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/e2f7f31b_8f0d_4168_972d_1f5ac7d37cd2'}
3
-files = ['/home/louis/pebble-dev/repo_remote/synocam_home_switch/wscript']
4
-hash = -2431564940392307765
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
-out_dir = '/home/louis/pebble-dev/repo_remote/synocam_home_switch/build'
7
-run_dir = '/home/louis/pebble-dev/repo_remote/synocam_home_switch'
8
-top_dir = '/home/louis/pebble-dev/repo_remote/synocam_home_switch'
2
+environ = {'WSL_INTEROP': '/run/WSL/10_interop', 'PEBBLE_TOOLCHAIN_PATH': '/home/louis/pebble-dev/pebble-sdk-4.5-linux64/arm-cs-tools/bin', 'WSL_DISTRO_NAME': 'Ubuntu-18.04', 'HOSTTYPE': 'x86_64', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'LOGNAME': 'louis', 'USER': 'louis', 'NO_GCE_CHECK': 'False', '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:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/Python310/Scripts/:/mnt/c/Program Files/Python310/:/mnt/c/Program Files/Python38/Scripts/:/mnt/c/Program Files/Python38/:/mnt/c/Program Files/Python39/Scripts/:/mnt/c/Program Files/Python39/:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/Program Files (x86)/PuTTY/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/nodejs/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/Python27/:/mnt/c/Python27/Scripts/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/Calibre2/:/mnt/c/Users/Sandy/scoop/shims:/mnt/c/Users/Sandy/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Sandy/AppData/Local/Programs/radare2:/mnt/c/Users/Sandy/AppData/Roaming/npm:/mnt/c/Users/Sandy/AppData/Local/Microsoft/WindowsApps:/snap/bin', 'LIBGL_ALWAYS_INDIRECT': '1', 'HOME': '/home/louis', 'WT_SESSION': 'c4adac7f-8a53-4050-b1bb-0b475940217c', 'DISPLAY': '172.27.224.1:0', '_': '/home/louis/pebble-dev/pebble-sdk-4.5-linux64/.env/bin/python', 'LANG': 'C.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'NAME': 'DESKTOP-PSQDLD8', 'XDG_DATA_DIRS': '/usr/local/share:/usr/share:/var/lib/snapd/desktop', 'OLDPWD': '/home/louis/repos', 'PYTHONHOME': '/home/louis/.pebble-sdk/SDKs/current/sdk-core/../.env', 'NODE_PATH': '/home/louis/.pebble-sdk/SDKs/current/sdk-core/../node_modules', 'WT_PROFILE_ID': '{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}', 'SHLVL': '2', 'PWD': '/home/louis/repos/synocam_home_switch', 'WSLENV': 'WT_SESSION::WT_PROFILE_ID', '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', 'NOCLIMB': '1'}
3
+files = ['/mnt/c/Users/Sandy/repos/synocam_home_switch/wscript']
4
+hash = 4221772378543028085
5
+options = {'files': '', 'jobs': 4, '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
+out_dir = '/mnt/c/Users/Sandy/repos/synocam_home_switch/build'
7
+run_dir = '/mnt/c/Users/Sandy/repos/synocam_home_switch'
8
+top_dir = '/mnt/c/Users/Sandy/repos/synocam_home_switch'
9 9
new file mode 100644
... ...
@@ -0,0 +1,3 @@
1
+{
2
+    "C_Cpp.errorSquiggles": "Disabled"
3
+}
0 4
\ No newline at end of file
1 5
Binary files a/build/synocam_home_switch.pbw and b/build/synocam_home_switch.pbw differ
... ...
@@ -10,328 +10,275 @@ var retry;
10 10
 retry = 1;
11 11
 
12 12
 function xhr_to_syno(method, url_path, onload_function, max_retry) {
13
-  console.log('------xhr start')
14
-  status = "";
15
-  if (JSON.parse(localStorage.getItem('clay-settings')).server) {
16
-    var server = JSON.parse(localStorage.getItem('clay-settings')).server
17
-    url = server + url_path;
18
-    var xhr = new XMLHttpRequest();
19
-    xhr.timeout = 4000; // time in milliseconds
20
-
21
-    xhr.open(method, url, true);
22
-    console.log('------xhr opened')
23
-    xhr.onload = function () {
24
-      console.log('------xhr onload')
25
-      if (xhr.readyState === 4) {
26
-        retry = 1;
27
-        console.log('------xhr request returned');
28
-        if (xhr.status == 200) {
29
-          onload_function(xhr);
30
-          return true;
31
-        } else {
32
-          console.log('------xhr request returned error code ' + xhr.status);
33
-          return false;
34
-        }
35
-      }
36
-    };
37
-
38
-    xhr.ontimeout = function (e) {
39
-      retry++;
40
-      if (retry < max_retry) {
41
-        console.log('------xhr timed out retrying another time ');
42
-        xhr_to_syno(method, url_path, onload_function, max_retry);
43
-      } else {
44
-        console.log('------xhr timed out ' + retry + ' time');
45
-        //send back "timeout" to watch
46
-        message = "Call to server timed out, verify settings and connectivity";
47
-
48
-        // Build message
49
-        var dict = {
50
-          'status': message,
13
+    console.log('------xhr start')
14
+    status = "";
15
+    if (JSON.parse(localStorage.getItem('clay-settings')).server) {
16
+        var server = JSON.parse(localStorage.getItem('clay-settings')).server
17
+        url = server + url_path;
18
+        var xhr = new XMLHttpRequest();
19
+        xhr.timeout = 4000; // time in milliseconds
20
+
21
+        xhr.open(method, url, true);
22
+        console.log('------xhr opened on ' + url + ' # retry=' + retry)
23
+        xhr.onload = function() {
24
+            console.log('------xhr onload')
25
+            if (xhr.readyState === 4) {
26
+                retry = 1;
27
+                console.log('------xhr request returned');
28
+                if (xhr.status == 200) {
29
+                    onload_function(xhr);
30
+                    return true;
31
+                } else {
32
+                    console.log('------xhr request returned error code ' + xhr.status);
33
+                    return false;
34
+                }
35
+            }
51 36
         };
52 37
 
53
-        // Send the message
54
-        Pebble.sendAppMessage(dict, function (e) {
55
-          console.log('sent');
56
-        }, function () {
57
-          console.log('failed');
58
-        });
59
-        return false;
60
-      }
61
-    };
62
-    xhr.send(null);
63
-
64
-  } else {
65
-    Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
66
-  }
67
-
68
-}
38
+        xhr.ontimeout = function(e) {
39
+            retry++;
40
+            if (retry < max_retry) {
41
+                console.log('------xhr timed out retrying another time ');
42
+                xhr_to_syno(method, url_path, onload_function, max_retry);
43
+            } else {
44
+                console.log('------xhr timed out ' + retry + ' time');
45
+                //send back "timeout" to watch
46
+                message = "Call to server timed out, verify settings and connectivity";
69 47
 
48
+                // Build message
49
+                var dict = {
50
+                    'status': message,
51
+                };
70 52
 
71
-function authenticate() {
72
-  var response;
73
-  sid = "";
74
-  console.log('---- authenticate');
75
-  if (JSON.parse(localStorage.getItem('clay-settings')).username && JSON.parse(localStorage.getItem('clay-settings')).password) {
76
-    var username = JSON.parse(localStorage.getItem('clay-settings')).username;
77
-    var password = JSON.parse(localStorage.getItem('clay-settings')).password;
78
-    console.log('-- username:' + username);
79
-    console.log('-- password:' + password);
80
-    var url_path = "/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account=" + username + "&passwd=" + password + "&session=SurveillanceStation&format=sid";
81
-    var method = "GET";
82
-    onload_function = function (xhr) {
83
-      response = JSON.parse(xhr.responseText);
84
-      if (response.success == true) {
85
-        sid = response.data.sid;
86
-        console.log('------Authentication succeeded');
87
-        if (sid != "") {
88
-          message = "Welcome to Syno Cam Switch ! ready & authenticated";
89
-          // Build message
90
-          var dict = {
91
-            'auth': message,
92
-          };
93
-          // Send the message
94
-          Pebble.sendAppMessage(dict, function (e) {
95
-            console.log('sent');
96
-          }, function () {
97
-            console.log('failed');
98
-          });
99
-        } else {
100
-          console.log('------Unexpected error : authentication is OK but no SID retrieved');
101
-        }
102
-      } else {
103
-        console.log('------Authentication failed : ' + response.toString());
104
-      }
105
-    };
106
-    max_retry = 1;
107
-    xhr_to_syno(method, url_path, onload_function, max_retry);
108
-  } else {
109
-    console.log("--- failed to get settings");
110
-    Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
111
-  }
53
+                // Send the message
54
+                Pebble.sendAppMessage(dict, function(e) {
55
+                    console.log('sent');
56
+                }, function() {
57
+                    console.log('failed');
58
+                });
59
+                return false;
60
+            }
61
+        };
62
+        xhr.send(null);
112 63
 
64
+    } else {
65
+        Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
66
+    }
113 67
 
114 68
 }
115 69
 
116 70
 
117
-function get_status() {
118
-  var response;
119
-
120
-  if (sid != ""){
121
-    status = "";
122
-    console.log('---- get_status');
123
-
124
-    var url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=GetInfo&_sid=" + sid;
125
-    var method = "GET";
126
-
127
-    onload_function = function (xhr) { }
128
-
129
-    max_retry = 1;
130
-    xhr_to_syno(method, url_path, onload_function, max_retry);
131
-
132
-    var xhr = new XMLHttpRequest();
133
-    xhr.timeout = 3000; // time in milliseconds
134
-
135
-    xhr.open("GET", url, true);
136
-
137
-    xhr.onload = function () {
138
-      if (xhr.readyState === 4) {
139
-        retry = 0;
140
-        console.log('------Get-status request returned');
141
-        if (xhr.status == 200) {
142
-          response = JSON.parse(xhr.responseText);
143
-          if (response.success == true) {
144
-            status = response.data.on;
145
-            var message;
146
-            switch (status) {
147
-              case true:
148
-                message = "Your Home mode is ON";
149
-                break;
150
-              case false:
151
-                message = "Your Home mode is OFF";
152
-                break;
153
-              default:
154
-                message = "home mode is unknown !";
71
+function authenticate() {
72
+    var response;
73
+    sid = "";
74
+    console.log('---- authenticate');
75
+    if (JSON.parse(localStorage.getItem('clay-settings')).username && JSON.parse(localStorage.getItem('clay-settings')).password) {
76
+        var username = JSON.parse(localStorage.getItem('clay-settings')).username;
77
+        var password = JSON.parse(localStorage.getItem('clay-settings')).password;
78
+        console.log('-- username:' + username);
79
+        console.log('-- password:' + password);
80
+        var url_path = "/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account=" + username + "&passwd=" + password + "&session=SurveillanceStation&format=sid";
81
+        var method = "GET";
82
+        onload_function = function(xhr) {
83
+            response = JSON.parse(xhr.responseText);
84
+            if (response.success == true) {
85
+                sid = response.data.sid;
86
+                console.log('------Authentication succeeded');
87
+                if (sid != "") {
88
+                    message = "Welcome to Syno Cam Switch ! ready & authenticated";
89
+                    // Build message
90
+                    var dict = {
91
+                        'auth': message,
92
+                    };
93
+                    // Send the message
94
+                    Pebble.sendAppMessage(dict, function(e) {
95
+                        console.log('sent');
96
+                    }, function() {
97
+                        console.log('failed');
98
+                    });
99
+                } else {
100
+                    console.log('------Unexpected error : authentication is OK but no SID retrieved');
101
+                }
102
+            } else {
103
+                console.log('------Authentication failed : ' + response.toString());
155 104
             }
156
-            // Build message
157
-            var dict = {
158
-              'status': message,
159
-            };
160
-
161
-            // Send the message
162
-            Pebble.sendAppMessage(dict, function (e) {
163
-              console.log('sent');
164
-            }, function () {
165
-              console.log('failed');
166
-            });
167
-          }
168
-        } else {
169
-          console.log('------Request returned error code ' + xhr.status);
170
-        }
171
-      }
172
-    }
173
-
174
-    xhr.ontimeout = function (e) {
175
-      retry++;
176
-      if (retry <= 1) {
177
-        console.log('------Get-Status timed out retrying another time ');
178
-        get_status();
179
-      } else {
180
-        console.log('------Get-Status timed out a second time');
181
-        //send back "timeout" to watch
182
-        message = "Get-Status timed out, verify settings and connectivity";
183
-
184
-        // Build message
185
-        var dict = {
186
-          'status': message,
187 105
         };
188
-
189
-        // Send the message
190
-        Pebble.sendAppMessage(dict, function (e) {
191
-          console.log('sent');
192
-        }, function () {
193
-          console.log('failed');
194
-        });
195
-      }
106
+        max_retry = 10;
107
+        xhr_to_syno(method, url_path, onload_function, max_retry);
108
+    } else {
109
+        console.log("--- failed to get settings");
110
+        Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
196 111
     }
197
-
198
-    xhr.send();
199
-
112
+}
200 113
 
201 114
 
202
-  } else {
203
-    authenticate();
204
-    get_status();
205
-  }
115
+function get_status() {
116
+    var response;
206 117
 
207
-}
118
+    if (sid != "") {
119
+        status = "";
120
+        console.log('---- get_status');
208 121
 
122
+        var url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=GetInfo&_sid=" + sid;
123
+        var method = "GET";
209 124
 
210
-function switch_home(bool) {
211
-  var response;
212
-  console.log('---- authenticate');
213
-  if (sid != ""){
214
-    status = "";
215
-    console.log('---- get_status');
216
-    if (JSON.parse(localStorage.getItem('clay-settings')).server) {
217
-      var server = JSON.parse(localStorage.getItem('clay-settings')).server;
218
-      var xhr = new XMLHttpRequest();
219
-
220
-      url = server + "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=GetInfo&_sid="+sid;
221
-
222
-      xhr.open("GET", url,false);
223
-      xhr.send();
224
-
225
-      if(xhr.status == 200) {
226
-        response = JSON.parse(xhr.responseText);
227
-        if (response.success == true){
228
-          status = response.data.on;
229
-          console.log('------ status:'+status);
230
-          var message;
231
-          var dict;
232
-          if ( status != bool){
233
-            console.log('---- switching home mode to '+ bool);
234
-            url = server + "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on="+bool+"&_sid="+sid;
235
-
236
-            xhr.open("GET", url,false);
237
-            xhr.send();
238
-
239
-            if(xhr.status == 200) {
240
-              response = JSON.parse(xhr.responseText);
241
-              if (response.success == true){
242
-                status=bool;
125
+        onload_function = function(xhr) {
126
+            response = JSON.parse(xhr.responseText);
127
+            if (response.success == true) {
128
+                status = response.data.on;
129
+                var message;
243 130
                 switch (status) {
244
-                  case true:
245
-                    message = "You just set Home mode ON";
246
-                    break;
247
-                  case false:
248
-                    message = "You just set Home mode OFF";
249
-                    break;
250
-                  default:
251
-                    message = "something happened, try again !";
131
+                    case true:
132
+                        message = "Your Home mode is ON";
133
+                        break;
134
+                    case false:
135
+                        message = "Your Home mode is OFF";
136
+                        break;
137
+                    default:
138
+                        message = "home mode is unknown !";
252 139
                 }
253 140
                 // Build message
254
-                dict = {
255
-                  'status': message,
141
+                var dict = {
142
+                    'status': message,
256 143
                 };
257 144
 
258 145
                 // Send the message
259 146
                 Pebble.sendAppMessage(dict, function(e) {
260
-                  console.log('sent');
147
+                    console.log('sent');
261 148
                 }, function() {
262
-                  console.log('failed');
149
+                    console.log('failed');
263 150
                 });
264
-              }
265
-            }else {
266
-              console.log('------Request returned error code ' + xhr.status.toString());
267 151
             }
268
-          }else{
269
-            console.log('---- nothing to do, status already '+status);
270
-            switch (status) {
271
-              case true:
272
-                message = "Your Home Mode is already ON";
273
-                break;
274
-              case false:
275
-                message = "Your Home Mode is already OFF";
276
-                break;
277
-              default:
278
-                message = "something happened, try again !";
152
+        }
153
+
154
+        max_retry = 10;
155
+        xhr_to_syno(method, url_path, onload_function, max_retry);
156
+
157
+    } else {
158
+        authenticate();
159
+        get_status();
160
+    }
161
+
162
+}
163
+
164
+
165
+function switch_home(bool) {
166
+    var response;
167
+    console.log('---- authenticate');
168
+    if (sid != "") {
169
+        status = "";
170
+        console.log('---- get_status');
171
+        url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=GetInfo&_sid=" + sid;
172
+        var method = "GET";
173
+
174
+        onload_status_function = function(xhr) {
175
+            response = JSON.parse(xhr.responseText);
176
+            if (response.success == true) {
177
+                status = response.data.on;
178
+                console.log('------ status:' + status);
179
+                var message;
180
+                var dict;
181
+                if (status != bool) {
182
+                    console.log('---- switching home mode to ' + bool);
183
+                    url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=" + bool + "&_sid=" + sid;
184
+                    method = "GET"
185
+                    onload_switch_function = function(xhr) {
186
+                        response = JSON.parse(xhr.responseText);
187
+                        if (response.success == true) {
188
+                            status = bool;
189
+                            switch (status) {
190
+                                case true:
191
+                                    message = "You just set Home mode ON";
192
+                                    break;
193
+                                case false:
194
+                                    message = "You just set Home mode OFF";
195
+                                    break;
196
+                                default:
197
+                                    message = "something happened, try again !";
198
+                            }
199
+                            // Build message
200
+                            dict = {
201
+                                'status': message,
202
+                            };
203
+
204
+                            // Send the message
205
+                            Pebble.sendAppMessage(dict, function(e) {
206
+                                console.log('sent');
207
+                            }, function() {
208
+                                console.log('failed');
209
+                            });
210
+                        }
211
+                    }
212
+                    max_retry = 10;
213
+                    xhr_to_syno(method, url_path, onload_switch_function, max_retry);
214
+
215
+                } else {
216
+                    console.log('---- nothing to do, status already ' + status);
217
+                    switch (status) {
218
+                        case true:
219
+                            message = "Your Home Mode is already ON";
220
+                            break;
221
+                        case false:
222
+                            message = "Your Home Mode is already OFF";
223
+                            break;
224
+                        default:
225
+                            message = "something happened, try again !";
226
+                    }
227
+                    // Build message
228
+                    dict = {
229
+                        'status': message,
230
+                    };
231
+
232
+                    // Send the message
233
+                    Pebble.sendAppMessage(dict, function(e) {
234
+                        console.log('sent');
235
+                    }, function() {
236
+                        console.log('failed');
237
+                    });
238
+
239
+                }
279 240
             }
280
-            // Build message
281
-            dict = {
282
-              'status': message,
283
-            };
284
-
285
-            // Send the message
286
-            Pebble.sendAppMessage(dict, function(e) {
287
-              console.log('sent');
288
-            }, function() {
289
-              console.log('failed');
290
-            });
291
-          }
292 241
         }
293
-      }else {
294
-        console.log('------Request returned error code ' + xhr.status.toString());
295
-      }
296
-    }else{
297
-      Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
242
+
243
+        max_retry = 10;
244
+        xhr_to_syno(method, url_path, onload_status_function, max_retry);
245
+
246
+    } else {
247
+        authenticate();
248
+        switch_home(bool);
298 249
     }
299
-  } else {
300
-    authenticate();
301
-    switch_home(bool);
302
-  }
303 250
 
304 251
 }
305 252
 
306 253
 // Get JS readiness events
307
-Pebble.addEventListener('ready',function(e){
308
-  console.log("---- local storage:");
309
-  console.log("user " + JSON.parse(localStorage.getItem('clay-settings')).username);
310
-  console.log('PebbleKit JS is ready');
311
-  // Update Watch on this
312
-  Pebble.sendAppMessage({'JSReady':1});
254
+Pebble.addEventListener('ready', function(e) {
255
+    console.log("---- local storage:");
256
+    console.log("user " + JSON.parse(localStorage.getItem('clay-settings')).username);
257
+    console.log('PebbleKit JS is ready');
258
+    // Update Watch on this
259
+    Pebble.sendAppMessage({ 'JSReady': 1 });
313 260
 });
314 261
 
315 262
 // Get AppMessage events
316 263
 Pebble.addEventListener('appmessage', function(e) {
317
-  // Get the dictionary from the message
318
-  var dict = e.payload;
319
-  console.log(dict[0].toString());
320
-  switch (dict[0]) {
321
-    case 'auth':
322
-      authenticate();
323
-      break;
324
-    case 'get':
325
-      get_status();
326
-      break;
327
-    case 'home_on':
328
-      switch_home(true);
329
-      break;
330
-    case 'home_off':
331
-      switch_home(false);
332
-      break;
333
-    default:
334
-      console.log('Sorry. I don\'t understand your request :'+ dict[0]);
335
-  }
264
+    // Get the dictionary from the message
265
+    var dict = e.payload;
266
+    console.log(dict[0].toString());
267
+    switch (dict[0]) {
268
+        case 'auth':
269
+            authenticate();
270
+            break;
271
+        case 'get':
272
+            get_status();
273
+            break;
274
+        case 'home_on':
275
+            switch_home(true);
276
+            break;
277
+        case 'home_off':
278
+            switch_home(false);
279
+            break;
280
+        default:
281
+            console.log('Sorry. I don\'t understand your request :' + dict[0]);
282
+    }
336 283
 
337
-});
284
+});
338 285
\ No newline at end of file