Browse code

authenticate at app start

Louis authored on14/03/2019 22:27:39
Showing4 changed files
1 1
Binary files a/build/synocam_home_switch.pbw and b/build/synocam_home_switch.pbw differ
... ...
@@ -12,11 +12,12 @@
12 12
         "displayName": "DSCam Home-Switch",
13 13
         "enableMultiJS": true,
14 14
         "messageKeys": [
15
+            "auth",
15 16
             "status",
16 17
             "server",
17 18
             "username",
18 19
             "password",
19
-	    "JSReady"
20
+	        "JSReady"
20 21
         ],
21 22
         "projectType": "native",
22 23
         "resources": {
... ...
@@ -32,6 +32,7 @@ static uint32_t size ;
32 32
 time_t current_time;
33 33
   
34 34
 typedef enum {
35
+  auth,
35 36
   status,
36 37
   username,
37 38
   password,
... ...
@@ -202,27 +203,34 @@ static void update_time() {
202 203
 }
203 204
 
204 205
 void comm_is_ready() {
205
-  // Get information about the window
206
-  //Layer *window_layer = window_get_root_layer(s_window);
207
-  //GRect bounds = layer_get_bounds(window_layer);
208 206
 
209
-  // Create a text layer and set the text
210
-  //const GEdgeInsets label_insets = {.right = ACTION_BAR_WIDTH, .left = ACTION_BAR_WIDTH / 2};
211
-  //s_text_layer = text_layer_create(grect_inset(bounds, label_insets));
212
-  text_layer_set_text(s_text_layer, "Welcome to Syno Cam Switch ! ready");
213
-
214
-  // Set the font and text alignment
215
-  //text_layer_set_font(s_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
216
-  //text_layer_set_text_alignment(s_text_layer, GTextAlignmentCenter);
207
+  // Authenticate to get token from syno
217 208
 
218
-  // Add the text layer to the window
219
-  //layer_add_child(window_get_root_layer(s_window), text_layer_get_layer(s_text_layer));
220
-
221
-  // Enable text flow and paging on the text layer, with a slight inset of 10, for round screens
222
-  //text_layer_enable_screen_text_flow_and_paging(s_text_layer, 10);
209
+  // Declare the dictionary's iterator
210
+  DictionaryIterator *out_iter;
211
+  
212
+  // Prepare the outbox buffer for this message
213
+  AppMessageResult result = app_message_outbox_begin(&out_iter);
214
+  
215
+  if(result == APP_MSG_OK) {
216
+    // Add an item to ask for weather data
217
+    msg = "auth";
218
+    dict_write_cstring(out_iter, auth, msg);
219
+  
220
+    // Send this message
221
+    result = app_message_outbox_send();
222
+    if(result != APP_MSG_OK) {
223
+      APP_LOG(APP_LOG_LEVEL_ERROR, "Error sending the outbox: %d", (int)result);
224
+    }else{
225
+      text_layer_set_text(s_text_layer, "Authenticating...");
226
+    }
227
+  } else {
228
+    // The outbox cannot be used right now
229
+    APP_LOG(APP_LOG_LEVEL_ERROR, "Error preparing the outbox: %d", (int)result);
230
+  }
223 231
 
224
-  // Push the window, setting the window animation to 'true'
225
-  //window_stack_push(s_window, true);
232
+  // set the text
233
+  text_layer_set_text(s_text_layer, "Welcome to Syno Cam Switch ! ready");
226 234
 
227 235
   // click provider
228 236
   window_set_click_config_provider(s_window, click_config_provider);
... ...
@@ -5,6 +5,52 @@ var clay = new Clay(clayConfig);
5 5
 var sid;
6 6
 var status;
7 7
 
8
+function authenticate() {
9
+  var response;
10
+  sid="";
11
+  console.log('---- authenticate');
12
+  if (localStorage.getItem('username')  && localStorage.getItem('password') && localStorage.getItem('server') ){
13
+    var username=localStorage.getItem('username');
14
+    var password=localStorage.getItem('password');
15
+    var server=localStorage.getItem('server');
16
+    var url = server + "/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account="+username+"&passwd="+password+"&session=SurveillanceStation&format=sid";
17
+    var xhr = new XMLHttpRequest();
18
+  
19
+    xhr.open("GET", url,false);
20
+    xhr.send();
21
+
22
+    if(xhr.status == 200) {
23
+      response = JSON.parse(xhr.responseText);
24
+      if (response.success == true){
25
+        sid = response.data.sid;
26
+      }
27
+    }else {
28
+      console.log('------Request returned error code ' + xhr.status.toString());
29
+    }
30
+  
31
+    if (sid != ""){
32
+      auth = "";
33
+      message = "Welcome to Syno Cam Switch ! ready & authenticated";
34
+           
35
+      // Build message
36
+      var dict = {
37
+        'auth': message,
38
+      };
39
+    
40
+      // Send the message
41
+      Pebble.sendAppMessage(dict, function(e) {
42
+        console.log('sent');
43
+      }, function() {
44
+        console.log('failed');
45
+      });
46
+       
47
+    }
48
+      
49
+  }else{
50
+    Pebble.showSimpleNotificationOnPebble("DSCam H-S", "You need to set your Synology account and server.");
51
+  }
52
+}
53
+
8 54
 function get_status() {
9 55
   var response;
10 56
   sid="";
... ...
@@ -197,6 +243,9 @@ Pebble.addEventListener('appmessage', function(e) {
197 243
   var dict = e.payload;
198 244
   
199 245
   switch (dict[0]) {
246
+    case 'auth':
247
+      authenticate();
248
+      break;
200 249
     case 'get':
201 250
       get_status();
202 251
       break;