Browse code

Merge branch 'master' of ssh://jonget.fr:5522/volume1/web/gitlist.1.0.2/repos/synocam_home_switch/

Louis Jonget authored on11/03/2024 17:55:51
Showing2 changed files
1 1
deleted file mode 100644
2 2
Binary files a/build/dscam-h-s.pbw and /dev/null differ
... ...
@@ -8,143 +8,144 @@ var messageKeys = require('message_keys');
8 8
 var sid;
9 9
 var status;
10 10
 var retry;
11
-retry = 1;
11
+retry = 0;
12 12
 
13 13
 
14 14
 function dec2hex(s) {
15
-  return (s < 15.5 ? '0' : '') + Math.round(s).toString(16);
15
+    return (s < 15.5 ? '0' : '') + Math.round(s).toString(16);
16 16
 }
17 17
 
18 18
 function hex2dec(s) {
19
-  return parseInt(s, 16);
19
+    return parseInt(s, 16);
20 20
 }
21 21
 
22 22
 function base32tohex(base32) {
23
-  var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
24
-  var bits = "";
25
-  var hex = "";
23
+    var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
24
+    var bits = "";
25
+    var hex = "";
26 26
 
27
-  for (var i = 0; i < base32.length; i++) {
28
-    var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
29
-    bits += leftpad(val.toString(2), 5, '0');
30
-  }
31
-  //console.log('-- bits : ' + bits)
27
+    for (var i = 0; i < base32.length; i++) {
28
+        var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
29
+        bits += leftpad(val.toString(2), 5, '0');
30
+    }
31
+    //console.log('-- bits : ' + bits)
32 32
 
33
-  for (var i = 0; i + 4 <= bits.length; i += 4) {
34
-    var chunk = bits.substr(i, 4);
35
-    hex = hex + parseInt(chunk, 2).toString(16);
36
-  }
37
-  return hex;
33
+    for (var i = 0; i + 4 <= bits.length; i += 4) {
34
+        var chunk = bits.substring(i, i + 4);
35
+        hex = hex + parseInt(chunk, 2).toString(16);
36
+    }
37
+    return hex;
38 38
 
39 39
 }
40 40
 
41 41
 function leftpad(str, len, pad) {
42
-  if (len + 1 >= str.length) {
43
-    str = Array(len + 1 - str.length).join(pad) + str;
44
-  }
45
-  return str;
42
+    if (len + 1 >= str.length) {
43
+        str = Array(len + 1 - str.length).join(pad) + str;
44
+    }
45
+    return str;
46 46
 }
47 47
 
48 48
 function getOtp() {
49
-  key = base32tohex(JSON.parse(localStorage.getItem('clay-settings')).OTP_seed)
50
-  //console.log('-- seed:' + JSON.parse(localStorage.getItem('clay-settings')).OTP_seed)
51
-  //console.log('-- key:' + key)
52
-  var epoch = Math.round(new Date().getTime() / 1000.0);
53
-  var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0');
54
-  // updated for jsSHA v2.0.0 - http://caligatio.github.io/jsSHA/
55
-  var shaObj = new jsSHA("SHA-1", "HEX");
56
-  shaObj.setHMACKey(key, "HEX");
57
-  shaObj.update(time);
58
-  var hmac = shaObj.getHMAC("HEX");
59
-  //console.log('-- hmac:' + hmac)
60
-  var offset = hex2dec(hmac.substring(hmac.length - 1));
61
-  //console.log('--offset:' + offset)
62
-  var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec('7fffffff')) + '';
63
-  otp = (otp).substr(otp.length - 6, 6);
64
-  return otp
49
+    key = base32tohex(JSON.parse(localStorage.getItem('clay-settings')).OTP_seed)
50
+        //console.log('-- seed:' + JSON.parse(localStorage.getItem('clay-settings')).OTP_seed)
51
+        //console.log('-- key:' + key)
52
+    var epoch = Math.round(new Date().getTime() / 1000.0);
53
+    var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0');
54
+    // updated for jsSHA v2.0.0 - http://caligatio.github.io/jsSHA/
55
+    var shaObj = new jsSHA("SHA-1", "HEX");
56
+    shaObj.setHMACKey(key, "HEX");
57
+    shaObj.update(time);
58
+    var hmac = shaObj.getHMAC("HEX");
59
+    //console.log('-- hmac:' + hmac)
60
+    var offset = hex2dec(hmac.substring(hmac.length - 1));
61
+    //console.log('--offset:' + offset)
62
+    var otp = (hex2dec(hmac.substring(offset * 2, offset * 2 + 8)) & hex2dec('7fffffff')) + '';
63
+    otp = (otp).substring(otp.length - 6, otp.length);
64
+    return otp
65 65
 }
66 66
 
67 67
 function xhr_to_syno(method, url_path, onload_function, max_retry) {
68
-  console.log('------xhr start')
69
-  status = "";
70
-  if (JSON.parse(localStorage.getItem('clay-settings')).server) {
71
-    var server = JSON.parse(localStorage.getItem('clay-settings')).server
72
-    url = server + url_path;
73
-    var xhr = new XMLHttpRequest();
74
-    xhr.timeout = 6000; // time in milliseconds
75
-
76
-    xhr.open(method, url, true);
77
-    console.log('------xhr opened')
78
-    xhr.onload = function () {
79
-      console.log('------xhr onload')
80
-      if (xhr.readyState === 4) {
81
-        retry = 1;
82
-        console.log('------xhr request returned');
83
-        if (xhr.status == 200) {
84
-          onload_function(xhr);
85
-          return true;
86
-        } else {
87
-          console.log('------xhr request returned error code ' + xhr.status);
88
-          message = "Error (XHR"+xhr.status+")";
89
-          // Build message
90
-          var dict = {
91
-            'status': message,
92
-          };
93
-
94
-          // Send the message
95
-          Pebble.sendAppMessage(dict, function (e) {
96
-            console.log('sent');
97
-          }, function () {
98
-            console.log('failed');
99
-          });
100
-          return false;
101
-        }
102
-      }
103
-    };
104
-
105
-    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, retrying...";
111
-
112
-        // Build message
113
-        var dict = {
114
-          'status': message,
68
+    console.log('------xhr start')
69
+    status = "";
70
+    if (JSON.parse(localStorage.getItem('clay-settings')).server) {
71
+        var server = JSON.parse(localStorage.getItem('clay-settings')).server
72
+        url = server + url_path;
73
+        var xhr = new XMLHttpRequest();
74
+        xhr.timeout = 60000; // time in milliseconds
75
+
76
+        xhr.open(method, url, true);
77
+        console.log('------xhr opened')
78
+        xhr.onload = function() {
79
+            console.log('------xhr onload')
80
+            if (xhr.readyState === 4) {
81
+                retry = 0;
82
+                console.log('------xhr request returned');
83
+                if (xhr.status == 200) {
84
+                    onload_function(xhr);
85
+                    return true;
86
+                } else {
87
+                    console.log('------xhr request returned error code ' + xhr.status);
88
+                    message = "Error (XHR" + xhr.status + ")";
89
+                    // Build message
90
+                    var dict = {
91
+                        'status': message,
92
+                    };
93
+
94
+                    // Send the message
95
+                    Pebble.sendAppMessage(dict, function(e) {
96
+                        console.log('sent');
97
+                    }, function() {
98
+                        console.log('failed');
99
+                    });
100
+                    return false;
101
+                }
102
+            }
115 103
         };
116 104
 
117
-        // Send the message
118
-        Pebble.sendAppMessage(dict, function (e) {
119
-          console.log('sent');
120
-        }, function () {
121
-          console.log('failed');
122
-        });
123
-        xhr_to_syno(method, url_path, onload_function, max_retry);
124
-      } else {
125
-        console.log('------xhr timed out ' + retry + ' times');
126
-        //send back "timeout" to watch
127
-        message = "Time out too many times, verify settings and connectivity";
128
-
129
-        // Build message
130
-        var dict = {
131
-          'status': message,
105
+        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...";
111
+
112
+                // Build message
113
+                var dict = {
114
+                    'status': message,
115
+                };
116
+
117
+                // Send the message
118
+                Pebble.sendAppMessage(dict, function(e) {
119
+                    console.log('sent');
120
+                }, function() {
121
+                    console.log('failed');
122
+                });
123
+
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
+
130
+                // Build message
131
+                var dict = {
132
+                    'status': message,
133
+                };
134
+
135
+                // Send the message
136
+                Pebble.sendAppMessage(dict, function(e) {
137
+                    console.log('sent');
138
+                }, function() {
139
+                    console.log('failed');
140
+                });
141
+                return false;
142
+            }
132 143
         };
144
+        xhr.send(null);
133 145
 
134
-        // Send the message
135
-        Pebble.sendAppMessage(dict, function (e) {
136
-          console.log('sent');
137
-        }, function () {
138
-          console.log('failed');
139
-        });
140
-        return false;
141
-      }
142
-    };
143
-    xhr.send(null);
144
-
145
-  } else {
146
-    Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
147
-  }
146
+    } else {
147
+        Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
148
+    }
148 149
 
149 150
 }
150 151
 
... ...
@@ -224,53 +225,53 @@ function authenticate() {
224 225
 
225 226
 
226 227
 function get_status() {
227
-  var response;
228
-
229
-  if (sid != "") {
230
-    status = "";
231
-    console.log('---- get_status');
232
-
233
-    var url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=GetInfo&_sid=" + sid;
234
-    var method = "GET";
235
-
236
-    onload_function = function (xhr) {
237
-      response = JSON.parse(xhr.responseText);
238
-      if (response.success == true) {
239
-        status = response.data.on;
240
-        var message;
241
-        switch (status) {
242
-          case true:
243
-            message = "Home mode is ON (camera is off)";
244
-            break;
245
-          case false:
246
-            message = "Home mode is OFF (camera is on)";
247
-            break;
248
-          default:
249
-            message = "something happened, try again ! (IMPOSSIBLE)";
228
+    var response;
229
+
230
+    if (sid != "") {
231
+        status = "";
232
+        console.log('---- get_status');
233
+
234
+        var url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=GetInfo&_sid=" + sid;
235
+        var method = "GET";
236
+
237
+        onload_function = function(xhr) {
238
+            response = JSON.parse(xhr.responseText);
239
+            if (response.success == true) {
240
+                status = response.data.on;
241
+                var message;
242
+                switch (status) {
243
+                    case true:
244
+                        message = "Home mode is ON (camera is off)";
245
+                        break;
246
+                    case false:
247
+                        message = "Home mode is OFF (camera is on)";
248
+                        break;
249
+                    default:
250
+                        message = "something happened, try again ! (IMPOSSIBLE)";
251
+                }
252
+            } else {
253
+                message = "something happened, try again ! (G200)";
254
+            }
255
+            // Build message
256
+            var dict = {
257
+                'status': message,
258
+            };
259
+
260
+            // Send the message
261
+            Pebble.sendAppMessage(dict, function(e) {
262
+                console.log('sent');
263
+            }, function() {
264
+                console.log('failed');
265
+            });
250 266
         }
251
-      }else{
252
-        message = "something happened, try again ! (G200)";
253
-      }
254
-      // Build message
255
-      var dict = {
256
-        'status': message,
257
-      };
258
-
259
-      // Send the message
260
-      Pebble.sendAppMessage(dict, function (e) {
261
-        console.log('sent');
262
-      }, function () {
263
-        console.log('failed');
264
-      });
265
-    }
266 267
 
267
-    max_retry = 10;
268
-    xhr_to_syno(method, url_path, onload_function, max_retry);
268
+        max_retry = 10;
269
+        xhr_to_syno(method, url_path, onload_function, max_retry);
269 270
 
270
-  } else {
271
-    authenticate();
272
-    get_status();
273
-  }
271
+    } else {
272
+        authenticate();
273
+        get_status();
274
+    }
274 275
 
275 276
 }
276 277
 
... ...
@@ -317,26 +318,22 @@ function timed_switch_home(duration) {
317 318
 
318 319
 }
319 320
 
320
-function switch_home(bool) {
321
+function timed_switch_home(duration) {
321 322
   var response;
322 323
   console.log('---- authenticate');
323 324
   if (sid != "") {
324
-    console.log('---- switching home mode to ' + bool);
325
-    url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=" + bool + "&_sid=" + 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;
326 332
     method = "GET"
327 333
     onload_switch_function = function (xhr) {
328 334
       response = JSON.parse(xhr.responseText);
329 335
       if (response.success == true) {
330
-        switch (bool) {
331
-          case true:
332
-            message = "Home mode is ON (camera is off)";
333
-            break;
334
-          case false:
335
-            message = "Home mode is OFF (camera is on)";
336
-            break;
337
-          default:
338
-            message = "something happened, try again ! (IMPOSSIBLE)";
339
-        }
336
+          message = "Home mode is ON until "+end_time;
340 337
       }else{
341 338
         message = "something happened, try again ! (S200)";
342 339
       }
... ...
@@ -345,10 +342,10 @@ function switch_home(bool) {
345 342
         'status': message,
346 343
       };
347 344
 
348
-      // Send the message
345
+      // Send the message Home mode is ON for 900 seconds
349 346
       Pebble.sendAppMessage(dict, function (e) {
350
-        console.log('sent');
351
-      }, function () {
347
+        console.log('sent \"'+message+'\"');
348
+      }, function() {
352 349
         console.log('failed');
353 350
       });
354 351
     }
... ...
@@ -363,13 +360,59 @@ function switch_home(bool) {
363 360
 
364 361
 }
365 362
 
363
+function switch_home(bool) {
364
+    var response;
365
+    console.log('---- authenticate');
366
+    if (sid != "") {
367
+        console.log('---- switching home mode to ' + bool);
368
+        url_path = "/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=" + bool + "&_sid=" + sid;
369
+        method = "GET"
370
+        onload_switch_function = function(xhr) {
371
+            response = JSON.parse(xhr.responseText);
372
+            if (response.success == true) {
373
+                switch (bool) {
374
+                    case true:
375
+                        message = "Home mode is ON (camera is off)";
376
+                        break;
377
+                    case false:
378
+                        message = "Home mode is OFF (camera is on)";
379
+                        break;
380
+                    default:
381
+                        message = "something happened, try again ! (IMPOSSIBLE)";
382
+                }
383
+            } else {
384
+                message = "something happened, try again ! (S200)";
385
+            }
386
+            // Build message
387
+            dict = {
388
+                'status': message,
389
+            };
390
+
391
+            // Send the message
392
+            Pebble.sendAppMessage(dict, function(e) {
393
+                console.log('sent');
394
+            }, function() {
395
+                console.log('failed');
396
+            });
397
+        }
398
+        max_retry = 10;
399
+        xhr_to_syno(method, url_path, onload_switch_function, max_retry);
400
+
401
+
402
+    } else {
403
+        authenticate();
404
+        switch_home(bool);
405
+    }
406
+
407
+}
408
+
366 409
 // Get JS readiness events
367
-Pebble.addEventListener('ready', function (e) {
368
-  console.log("---- local storage:");
369
-  console.log("user " + JSON.parse(localStorage.getItem('clay-settings')).username);
370
-  console.log('PebbleKit JS is ready');
371
-  // Update Watch on this
372
-  Pebble.sendAppMessage({ 'JSReady': 1 });
410
+Pebble.addEventListener('ready', function(e) {
411
+    console.log("---- local storage:");
412
+    console.log("user " + JSON.parse(localStorage.getItem('clay-settings')).username);
413
+    console.log('PebbleKit JS is ready');
414
+    // Update Watch on this
415
+    Pebble.sendAppMessage({ 'JSReady': 1 });
373 416
 });
374 417
 
375 418
 // Get AppMessage events