Browse code

Send data received from the watch to the configured URL

Natasha Kerensikova authored on13/03/2016 20:28:58
Showing1 changed files
... ...
@@ -17,6 +17,28 @@
17 17
 var cfg_endpoint = null;
18 18
 var cfg_data_field = null;
19 19
 
20
+var to_send = [];
21
+var sender = new XMLHttpRequest();
22
+
23
+function sendHead() {
24
+   if (to_send.length < 1) return;
25
+   var line = to_send.shift();
26
+   var data = new FormData();
27
+   data.append(cfg_data_field, line);
28
+   sender.open("POST", cfg_endpoint, true);
29
+   sender.send(data);
30
+}
31
+
32
+function enqueue(key, line) {
33
+   to_send.push(line);
34
+   if (to_send.length === 1) sendHead();
35
+}
36
+
37
+function uploadError() { console.log(this.statusText); }
38
+
39
+sender.addEventListener("load", sendHead);
40
+sender.addEventListener("error", uploadError);
41
+
20 42
 Pebble.addEventListener("ready", function() {
21 43
    console.log("Health Export PebbleKit JS ready!");
22 44
    Pebble.sendAppMessage({ "modalMessage": "Not configured" });
... ...
@@ -24,7 +46,8 @@ Pebble.addEventListener("ready", function() {
24 46
 
25 47
 Pebble.addEventListener("appmessage", function(e) {
26 48
    console.log('Received message: ' + JSON.stringify(e.payload));
27
-   if (e.payload.dataKey) {
49
+   if (e.payload.dataKey && e.payload.dataLine) {
50
+      enqueue(e.payload.dataKey, e.payload.dataLine);
28 51
       Pebble.sendAppMessage({ "lastSent": e.payload.dataKey });
29 52
    }
30 53
 });