| ... | ... |
@@ -71,19 +71,66 @@ function xhr_to_syno(method, url_path, onload_function, max_retry) {
|
| 71 | 71 |
var server = JSON.parse(localStorage.getItem('clay-settings')).server
|
| 72 | 72 |
url = server + url_path; |
| 73 | 73 |
var xhr = new XMLHttpRequest(); |
| 74 |
- xhr.timeout = 60000; // time in milliseconds |
|
| 74 |
+ xhr.timeout = 20000; // time in milliseconds |
|
| 75 | 75 |
|
| 76 | 76 |
xhr.open(method, url, true); |
| 77 | 77 |
console.log('------xhr opened')
|
| 78 |
- xhr.onload = function() {
|
|
| 79 |
- console.log('------xhr onload')
|
|
| 78 |
+ xhr.onreadystatechange = function() {
|
|
| 79 |
+ console.log('------xhr onreadystatechange')
|
|
| 80 | 80 |
if (xhr.readyState === 4) {
|
| 81 | 81 |
retry = 0; |
| 82 |
- console.log('------xhr request returned');
|
|
| 82 |
+ console.log('------xhr readyState 4');
|
|
| 83 | 83 |
if (xhr.status == 200) {
|
| 84 |
- onload_function(xhr); |
|
| 85 |
- return true; |
|
| 86 |
- } else {
|
|
| 84 |
+ console.log('------xhr status 200 ');
|
|
| 85 |
+ onload_function(xhr); |
|
| 86 |
+ return true; |
|
| 87 |
+ } else if(xhr.status == 407){ //blocked by proxy (and syno Firewall too)
|
|
| 88 |
+ console.log('------xhr status 407 blocked by proxy (and syno Firewall too) ');
|
|
| 89 |
+ onload_function(xhr); |
|
| 90 |
+ return false; |
|
| 91 |
+ } else if(xhr.status >= 400){ //wrong credentials
|
|
| 92 |
+ console.log('------xhr status 401 wrong credentials ');
|
|
| 93 |
+ onload_function(xhr); |
|
| 94 |
+ return false; |
|
| 95 |
+ } else if(xhr.status == 0){ //timeout
|
|
| 96 |
+ retry++; |
|
| 97 |
+ if (retry < max_retry) {
|
|
| 98 |
+ console.log('------xhr timed out retrying another time ');
|
|
| 99 |
+ //send back "timeout" to watch |
|
| 100 |
+ message = "Time out (" + retry + "), retrying...";
|
|
| 101 |
+ |
|
| 102 |
+ // Build message |
|
| 103 |
+ var dict = {
|
|
| 104 |
+ 'status': message, |
|
| 105 |
+ }; |
|
| 106 |
+ |
|
| 107 |
+ // Send the message |
|
| 108 |
+ Pebble.sendAppMessage(dict, function(e) {
|
|
| 109 |
+ console.log('sent');
|
|
| 110 |
+ }, function() {
|
|
| 111 |
+ console.log('failed');
|
|
| 112 |
+ }); |
|
| 113 |
+ |
|
| 114 |
+ setTimeout(function() { xhr_to_syno(method, url_path, onload_function, max_retry) }, 60000 * retry);
|
|
| 115 |
+ } else {
|
|
| 116 |
+ console.log('------xhr timed out ' + retry + ' times');
|
|
| 117 |
+ //send back "timeout" to watch |
|
| 118 |
+ message = "Time out " + retry + " times, verify settings and connectivity"; |
|
| 119 |
+ |
|
| 120 |
+ // Build message |
|
| 121 |
+ var dict = {
|
|
| 122 |
+ 'status': message, |
|
| 123 |
+ }; |
|
| 124 |
+ |
|
| 125 |
+ // Send the message |
|
| 126 |
+ Pebble.sendAppMessage(dict, function(e) {
|
|
| 127 |
+ console.log('sent');
|
|
| 128 |
+ }, function() {
|
|
| 129 |
+ console.log('failed');
|
|
| 130 |
+ }); |
|
| 131 |
+ return false; |
|
| 132 |
+ } |
|
| 133 |
+ } else { //timeout ?
|
|
| 87 | 134 |
console.log('------xhr request returned error code ' + xhr.status);
|
| 88 | 135 |
message = "Error (XHR" + xhr.status + ")"; |
| 89 | 136 |
// Build message |
| ... | ... |
@@ -103,123 +150,87 @@ function xhr_to_syno(method, url_path, onload_function, max_retry) {
|
| 103 | 150 |
}; |
| 104 | 151 |
|
| 105 | 152 |
xhr.ontimeout = function(e) {
|
| 106 |
- retry++; |
|
| 107 |
- if (retry < max_retry) {
|
|
| 108 |
- console.log('------xhr timed out retrying another time ');
|
|
| 109 |
- //send back "timeout" to watch |
|
| 110 |
- message = "Time out (" + retry + "), retrying...";
|
|
| 153 |
+ |
|
| 154 |
+ }; |
|
| 155 |
+ xhr.send(null); |
|
| 111 | 156 |
|
| 112 |
- // Build message |
|
| 113 |
- var dict = {
|
|
| 114 |
- 'status': message, |
|
| 115 |
- }; |
|
| 157 |
+ } else {
|
|
| 158 |
+ Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
|
|
| 159 |
+ } |
|
| 116 | 160 |
|
| 117 |
- // Send the message |
|
| 118 |
- Pebble.sendAppMessage(dict, function(e) {
|
|
| 119 |
- console.log('sent');
|
|
| 120 |
- }, function() {
|
|
| 121 |
- console.log('failed');
|
|
| 122 |
- }); |
|
| 161 |
+} |
|
| 123 | 162 |
|
| 124 |
- setTimeout(function() { xhr_to_syno(method, url_path, onload_function, max_retry) }, 60000 * retry);
|
|
| 125 |
- } else {
|
|
| 126 |
- console.log('------xhr timed out ' + retry + ' times');
|
|
| 127 |
- //send back "timeout" to watch |
|
| 128 |
- message = "Time out " + retry + " times, verify settings and connectivity"; |
|
| 129 | 163 |
|
| 164 |
+function authenticate() {
|
|
| 165 |
+ var response; |
|
| 166 |
+ sid = ""; |
|
| 167 |
+ console.log('---- authenticate');
|
|
| 168 |
+ if (JSON.parse(localStorage.getItem('clay-settings')).username && JSON.parse(localStorage.getItem('clay-settings')).password) {
|
|
| 169 |
+ var username = JSON.parse(localStorage.getItem('clay-settings')).username;
|
|
| 170 |
+ var password = JSON.parse(localStorage.getItem('clay-settings')).password;
|
|
| 171 |
+ console.log('-- username:' + username);
|
|
| 172 |
+ console.log('-- password:' + password.substring(0, 1) + '...');
|
|
| 173 |
+ var url_path = "/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=6&account=" + username + "&passwd=" + password + "&session=SurveillanceStation&format=sid"; |
|
| 174 |
+ if (JSON.parse(localStorage.getItem('clay-settings')).OTP_enabled) {
|
|
| 175 |
+ var otp_code = getOtp() |
|
| 176 |
+ console.log('-- otp_code is :' + otp_code)
|
|
| 177 |
+ url_path = url_path + "&otp_code=" + otp_code |
|
| 178 |
+ } |
|
| 179 |
+ var method = "GET"; |
|
| 180 |
+ onload_function = function(xhr) {
|
|
| 181 |
+ response = JSON.parse(xhr.responseText); |
|
| 182 |
+ if (response.success == true) {
|
|
| 183 |
+ sid = response.data.sid; |
|
| 184 |
+ console.log('------Authentication succeeded');
|
|
| 185 |
+ if (sid != "") {
|
|
| 186 |
+ message = "Welcome to Syno Cam Switch ! ready & authenticated"; |
|
| 187 |
+ // Build message |
|
| 188 |
+ var dict = {
|
|
| 189 |
+ 'auth': message, |
|
| 190 |
+ }; |
|
| 191 |
+ // Send the message |
|
| 192 |
+ Pebble.sendAppMessage(dict, function(e) {
|
|
| 193 |
+ console.log('sent');
|
|
| 194 |
+ }, function() {
|
|
| 195 |
+ console.log('failed');
|
|
| 196 |
+ }); |
|
| 197 |
+ timed_switch_home(15*60); |
|
| 198 |
+ } else {
|
|
| 199 |
+ console.log('------Unexpected error : authentication is OK but no SID retrieved');
|
|
| 200 |
+ message = "Auth error, no SID"; |
|
| 201 |
+ // Build message |
|
| 202 |
+ var dict = {
|
|
| 203 |
+ 'auth': message, |
|
| 204 |
+ }; |
|
| 205 |
+ // Send the message |
|
| 206 |
+ Pebble.sendAppMessage(dict, function(e) {
|
|
| 207 |
+ console.log('sent');
|
|
| 208 |
+ }, function() {
|
|
| 209 |
+ console.log('failed');
|
|
| 210 |
+ }); |
|
| 211 |
+ } |
|
| 212 |
+ } else {
|
|
| 213 |
+ console.log('------Authentication failed : ' + JSON.stringify(response));
|
|
| 214 |
+ message = "Authentication failed"; |
|
| 130 | 215 |
// Build message |
| 131 | 216 |
var dict = {
|
| 132 |
- 'status': message, |
|
| 217 |
+ 'auth': message, |
|
| 133 | 218 |
}; |
| 134 |
- |
|
| 135 | 219 |
// Send the message |
| 136 | 220 |
Pebble.sendAppMessage(dict, function(e) {
|
| 137 | 221 |
console.log('sent');
|
| 138 | 222 |
}, function() {
|
| 139 | 223 |
console.log('failed');
|
| 140 | 224 |
}); |
| 141 |
- return false; |
|
| 142 | 225 |
} |
| 143 | 226 |
}; |
| 144 |
- xhr.send(null); |
|
| 145 |
- |
|
| 227 |
+ max_retry = 10; |
|
| 228 |
+ xhr_to_syno(method, url_path, onload_function, max_retry); |
|
| 146 | 229 |
} else {
|
| 230 |
+ console.log("--- failed to get settings");
|
|
| 147 | 231 |
Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
|
| 148 | 232 |
} |
| 149 | 233 |
|
| 150 |
-} |
|
| 151 |
- |
|
| 152 |
- |
|
| 153 |
-function authenticate() {
|
|
| 154 |
- var response; |
|
| 155 |
- sid = ""; |
|
| 156 |
- console.log('---- authenticate');
|
|
| 157 |
- if (JSON.parse(localStorage.getItem('clay-settings')).username && JSON.parse(localStorage.getItem('clay-settings')).password) {
|
|
| 158 |
- var username = JSON.parse(localStorage.getItem('clay-settings')).username;
|
|
| 159 |
- var password = JSON.parse(localStorage.getItem('clay-settings')).password;
|
|
| 160 |
- console.log('-- username:' + username);
|
|
| 161 |
- console.log('-- password:' + password.substring(0, 1) + '...');
|
|
| 162 |
- var url_path = "/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=6&account=" + username + "&passwd=" + password + "&session=SurveillanceStation&format=sid"; |
|
| 163 |
- if (JSON.parse(localStorage.getItem('clay-settings')).OTP_enabled) {
|
|
| 164 |
- var otp_code = getOtp() |
|
| 165 |
- console.log('-- otp_code is :' + otp_code)
|
|
| 166 |
- url_path = url_path + "&otp_code=" + otp_code |
|
| 167 |
- } |
|
| 168 |
- var method = "GET"; |
|
| 169 |
- onload_function = function (xhr) {
|
|
| 170 |
- response = JSON.parse(xhr.responseText); |
|
| 171 |
- if (response.success == true) {
|
|
| 172 |
- sid = response.data.sid; |
|
| 173 |
- console.log('------Authentication succeeded');
|
|
| 174 |
- if (sid != "") {
|
|
| 175 |
- message = "Welcome to Syno Cam Switch ! ready & authenticated"; |
|
| 176 |
- // Build message |
|
| 177 |
- var dict = {
|
|
| 178 |
- 'auth': message, |
|
| 179 |
- }; |
|
| 180 |
- // Send the message |
|
| 181 |
- Pebble.sendAppMessage(dict, function (e) {
|
|
| 182 |
- console.log('sent');
|
|
| 183 |
- }, function () {
|
|
| 184 |
- console.log('failed');
|
|
| 185 |
- }); |
|
| 186 |
- timed_switch_home(15*60); |
|
| 187 |
- } else {
|
|
| 188 |
- console.log('------Unexpected error : authentication is OK but no SID retrieved');
|
|
| 189 |
- message = "Auth error, no SID"; |
|
| 190 |
- // Build message |
|
| 191 |
- var dict = {
|
|
| 192 |
- 'auth': message, |
|
| 193 |
- }; |
|
| 194 |
- // Send the message |
|
| 195 |
- Pebble.sendAppMessage(dict, function (e) {
|
|
| 196 |
- console.log('sent');
|
|
| 197 |
- }, function () {
|
|
| 198 |
- console.log('failed');
|
|
| 199 |
- }); |
|
| 200 |
- } |
|
| 201 |
- } else {
|
|
| 202 |
- console.log('------Authentication failed : ' + JSON.stringify(response));
|
|
| 203 |
- message = "Authentication failed"; |
|
| 204 |
- // Build message |
|
| 205 |
- var dict = {
|
|
| 206 |
- 'auth': message, |
|
| 207 |
- }; |
|
| 208 |
- // Send the message |
|
| 209 |
- Pebble.sendAppMessage(dict, function (e) {
|
|
| 210 |
- console.log('sent');
|
|
| 211 |
- }, function () {
|
|
| 212 |
- console.log('failed');
|
|
| 213 |
- }); |
|
| 214 |
- } |
|
| 215 |
- }; |
|
| 216 |
- max_retry = 10; |
|
| 217 |
- xhr_to_syno(method, url_path, onload_function, max_retry); |
|
| 218 |
- } else {
|
|
| 219 |
- console.log("--- failed to get settings");
|
|
| 220 |
- Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
|
|
| 221 |
- } |
|
| 222 |
- |
|
| 223 | 234 |
|
| 224 | 235 |
} |
| 225 | 236 |
|
| ... | ... |
@@ -318,48 +329,6 @@ function timed_switch_home(duration) {
|
| 318 | 329 |
|
| 319 | 330 |
} |
| 320 | 331 |
|
| 321 |
-function timed_switch_home(duration) {
|
|
| 322 |
- var response; |
|
| 323 |
- console.log('---- authenticate');
|
|
| 324 |
- if (sid != "") {
|
|
| 325 |
- var epoch = Math.round(new Date().getTime() / 1000.0); |
|
| 326 |
- var start_ts = epoch + 10 |
|
| 327 |
- var end_ts = duration + start_ts |
|
| 328 |
- var d= new Date(end_ts*1000) |
|
| 329 |
- var end_time = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds(); |
|
| 330 |
- console.log('---- switching home mode for ' + duration + 'seconds, until '+end_time);
|
|
| 331 |
- url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=SaveOneTimeSwitch&onetime_enable_on=true&onetime_disable_on=true&onetime_enable_time=" + start_ts + "&onetime_disable_time=" + end_ts + "&_sid=" + sid; |
|
| 332 |
- method = "GET" |
|
| 333 |
- onload_switch_function = function (xhr) {
|
|
| 334 |
- response = JSON.parse(xhr.responseText); |
|
| 335 |
- if (response.success == true) {
|
|
| 336 |
- message = "Home mode is ON until "+end_time; |
|
| 337 |
- }else{
|
|
| 338 |
- message = "something happened, try again ! (S200)"; |
|
| 339 |
- } |
|
| 340 |
- // Build message |
|
| 341 |
- dict = {
|
|
| 342 |
- 'status': message, |
|
| 343 |
- }; |
|
| 344 |
- |
|
| 345 |
- // Send the message Home mode is ON for 900 seconds |
|
| 346 |
- Pebble.sendAppMessage(dict, function (e) {
|
|
| 347 |
- console.log('sent \"'+message+'\"');
|
|
| 348 |
- }, function() {
|
|
| 349 |
- console.log('failed');
|
|
| 350 |
- }); |
|
| 351 |
- } |
|
| 352 |
- max_retry = 10; |
|
| 353 |
- xhr_to_syno(method, url_path, onload_switch_function, max_retry); |
|
| 354 |
- |
|
| 355 |
- |
|
| 356 |
- } else {
|
|
| 357 |
- authenticate(); |
|
| 358 |
- switch_home(bool); |
|
| 359 |
- } |
|
| 360 |
- |
|
| 361 |
-} |
|
| 362 |
- |
|
| 363 | 332 |
function switch_home(bool) {
|
| 364 | 333 |
var response; |
| 365 | 334 |
console.log('---- authenticate');
|
| ... | ... |
@@ -416,26 +385,25 @@ Pebble.addEventListener('ready', function(e) {
|
| 416 | 385 |
}); |
| 417 | 386 |
|
| 418 | 387 |
// Get AppMessage events |
| 419 |
-Pebble.addEventListener('appmessage', function (e) {
|
|
| 420 |
- // Get the dictionary from the message |
|
| 421 |
- var dict = e.payload; |
|
| 422 |
- console.log(dict[0].toString()); |
|
| 423 |
- switch (dict[0]) {
|
|
| 424 |
- case 'auth': |
|
| 425 |
- authenticate(); |
|
| 426 |
- break; |
|
| 427 |
- case 'get': |
|
| 428 |
- get_status(); |
|
| 429 |
- break; |
|
| 430 |
- case 'home_on': |
|
| 431 |
- switch_home(true); |
|
| 432 |
- //timed_switch_home(15*60) |
|
| 433 |
- break; |
|
| 434 |
- case 'home_off': |
|
| 435 |
- switch_home(false); |
|
| 436 |
- break; |
|
| 437 |
- default: |
|
| 438 |
- console.log('Sorry. I don\'t understand your request :' + dict[0]);
|
|
| 439 |
- } |
|
| 388 |
+Pebble.addEventListener('appmessage', function(e) {
|
|
| 389 |
+ // Get the dictionary from the message |
|
| 390 |
+ var dict = e.payload; |
|
| 391 |
+ console.log(dict[0].toString()); |
|
| 392 |
+ switch (dict[0]) {
|
|
| 393 |
+ case 'auth': |
|
| 394 |
+ authenticate(); |
|
| 395 |
+ break; |
|
| 396 |
+ case 'get': |
|
| 397 |
+ get_status(); |
|
| 398 |
+ break; |
|
| 399 |
+ case 'home_on': |
|
| 400 |
+ switch_home(true); |
|
| 401 |
+ break; |
|
| 402 |
+ case 'home_off': |
|
| 403 |
+ switch_home(false); |
|
| 404 |
+ break; |
|
| 405 |
+ default: |
|
| 406 |
+ console.log('Sorry. I don\'t understand your request :' + dict[0]);
|
|
| 407 |
+ } |
|
| 440 | 408 |
|
| 441 | 409 |
}); |
| 442 | 410 |
\ No newline at end of file |