Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pebble-js-app.src.js « js « src - github.com/ClusterM/pebble-my-data.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 730a17970791e9799ff5699e34f33d9979c251f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// -*-coding: utf-8 -*-
// vim: sw=2 ts=2 expandtab ai

var MSG = {
  PERIODIC_UPDATE: 0,
  SHORT_PRESS_UPDATE: 1,
  LONG_PRESS_UPDATE: 2,
  JSON_RESPONSE: 3,
  CONFIG: 4,
  ERROR: 5
};

var config = {};

function http_request(url) {
  var req = new XMLHttpRequest();

  req.open('GET', url, true);
  req.onload = function(e) {

    if (req.readyState == 4) {
      if(req.status == 200) {
        try {
          var response = JSON.parse(req.responseText);
          //console.log("success: " + JSON.stringify(response));
          response["msg_type"] = MSG.JSON_RESPONSE;
          Pebble.sendAppMessage(response);

        } catch(e) {
          console.log("json parse error");
          Pebble.sendAppMessage({ "msg_type": MSG.ERROR });
        }

      } else {
        console.log("fetch error");
        Pebble.sendAppMessage({ "msg_type": MSG.ERROR });
      }
    }

  }

  req.send(null);
}

function fetch_data(url) {
  if (config["config_location"]) {
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        function(position) {
          var latitude = position.coords.latitude;
          var longitude = position.coords.longitude;

          var s = (url.indexOf("?")===-1)?"?":"&";
          http_request(url + s + 'lat=' + latitude + '&lon=' + longitude);
        },
        function(error) {
          //error error.message
          /*
            TODO inform user about error
            PERMISSION_DENIED (numeric value 1)
            POSITION_UNAVAILABLE (numeric value 2)
            TIMEOUT (numeric value 3)
          */
          http_request(url);
        },
        { maximumAge: 1800000 } // 30 minutes
      );
    } else {
      //error geolocation not supported
      http_request(url);
    }
  } else {
    http_request(url);
  }
}

Pebble.addEventListener("ready",
  function(e) {
    console.log("JavaScript app ready and running!");

    var json = window.localStorage.getItem('pebble-my-data');

    if (typeof json === 'string') {
      try {
        config = JSON.parse(json);
        //console.log("loaded config = " + JSON.stringify(config));
        config["msg_type"] = MSG.CONFIG;
        Pebble.sendAppMessage(config); // send current config to pebble

      } catch(e) {
        console.log("stored config json parse error");
        Pebble.sendAppMessage({ "msg_type": MSG.ERROR });
      }
    }
  }
);

Pebble.addEventListener("appmessage",
  function(e) {
    //console.log("received message: " + JSON.stringify(e.payload));

    config["msg_type"] = MSG.CONFIG;
    Pebble.sendAppMessage(config); // send current config to pebble

    if (config["url"]) {
      var url = config["url"];
      var s = (url.indexOf("?")===-1)?"?":"&";

      if (e.payload["refresh"] == MSG.SHORT_PRESS_UPDATE) {
        url = url + s + "short=1";

      } else if (e.payload["refresh"] == MSG.LONG_PRESS_UPDATE) {
        url = url + s + "long=1";
      }

      fetch_data(url);

    } else {
      Pebble.sendAppMessage({ "msg_type": MSG.JSON_RESPONSE, "content": "URL not defined, check settings in Pebble App" });
    }
  }
);

Pebble.addEventListener('showConfiguration', function () {
  if (config["url"]) {
    url = config["url"];
  } else {
    url = "";
  }
  //console.log("put options = " + JSON.stringify(config));

  Pebble.openURL('data:text/html,'+encodeURI('_HTMLMARKER_<!--.html'.replace('_CONFIG_', JSON.stringify(config), 'g')));
});

Pebble.addEventListener("webviewclosed", function(e) {
  if ((typeof e.response === 'string') && (e.response.length > 0)) {
    config = JSON.parse(decodeURIComponent(e.response));
    //console.log("got options = " + JSON.stringify(config));

    window.localStorage.setItem('pebble-my-data', e.response);

    config["msg_type"] = MSG.CONFIG;
    //console.log("push config = " + JSON.stringify(config));
    Pebble.sendAppMessage(config); // send current config to pebble

    if (config["url"]) {
      fetch_data(config["url"]);

    } else {
      Pebble.sendAppMessage({ "msg_type": MSG.JSON_RESPONSE, "content": "URL not defined, check settings in Pebble App" });
    }
  }
});