forked from blaxbb/renowatch-pebble-modified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pebble-js-app.js
167 lines (146 loc) · 4.03 KB
/
pebble-js-app.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
//
// Register for an API Key here https://openweathermap.org/api
//
//
var API_KEY = "SECRETKEYHERE";
//
//
//
//
//
var CLEAR_DAY = 0;
var CLEAR_NIGHT = 1;
var WINDY = 2;
var COLD = 3;
var PARTLY_CLOUDY_DAY = 4;
var PARTLY_CLOUDY_NIGHT = 5;
var HAZE = 6;
var CLOUD = 7;
var RAIN = 8;
var SNOW = 9;
var HAIL = 10;
var CLOUDY = 11;
var STORM = 12;
var NA = 13;
function getIcon(id, dayBool)
{
var category = id[0];
switch(category){
case "2":
return STORM;
case "3":
case "5":
return RAIN;
case "6":
return SNOW;
case "7":
return HAZE;
case "8":
if(id == "800")
{
return (dayBool ? CLEAR_DAY : CLEAR_NIGHT);
}
if(id == "801" || id == "802")
{
return (dayBool ? PARTLY_CLOUDY_DAY : PARTLY_CLOUDY_NIGHT);
}
return CLOUD;
}
}
var options = JSON.parse(localStorage.getItem('options'));
//console.log('read options: ' + JSON.stringify(options));
if (options === null) options = { "use_gps" : "true",
"location" : "",
"units" : "fahrenheit",
"invert_color" : "false"};
function getWeatherFromLatLong(latitude, longitude) {
console.log(latitude + ", " + longitude);
var forecastReq = new XMLHttpRequest();
var unitsCode = "auto";
if (options.units == "fahrenheit") unitsCode = "imperial"
else if (options.units == "celsius") unitsCode = "metric"
var forecastUrl = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&units=" + unitsCode + "&appid=" + API_KEY;
forecastReq.open('GET', forecastUrl, true);
forecastReq.onload = function(e)
{
//console.log(e.status);
if(forecastReq.status == 200)
{
var data = JSON.parse(forecastReq.responseText);
if(data)
{
getWeatherForecastIO(data);
}
}
}
forecastReq.send(null);
return;
}
function getWeatherForecastIO(data)
{
var temp = data.main.temp;
console.log("TEMP: " + temp);
console.log(data.weather[0].id);
var time = data.dt;
var sunrise = data.sys.sunrise;
var sunset = data.sys.sunset;
var dayBool = time > sunrise && time < sunset;
var icon = getIcon(data.weather[0].id.toString(), dayBool);
Pebble.sendAppMessage({
"icon" : icon,
"temperature" : Math.round(temp) + "\u00B0",
"invert_color" : (options.invert_color == "true" ? 1 : 0),
});
}
var locationOptions = {
"timeout": 15000,
"maximumAge": 60000
};
function updateWeather() {
navigator.geolocation.getCurrentPosition(locationSuccess,
locationError,
locationOptions);
}
function locationSuccess(pos) {
var coordinates = pos.coords;
getWeatherFromLatLong(coordinates.latitude, coordinates.longitude);
}
function locationError(err) {
console.warn('location error (' + err.code + '): ' + err.message);
Pebble.sendAppMessage({
"icon":11,
"temperature":""
});
}
Pebble.addEventListener('showConfiguration', function(e) {
var uri = 'http://client.flip.net.au/reno/circle.html?' +
'use_gps=' + encodeURIComponent(options.use_gps) +
'&location=' + encodeURIComponent(options.location) +
'&units=' + encodeURIComponent(options.units) +
'&invert_color=' + encodeURIComponent(options.invert_color) +
'&account_token=' + encodeURIComponent(Pebble.getAccountToken());
//console.log('showing configuration at uri: ' + uri);
Pebble.openURL(uri);
});
Pebble.addEventListener('webviewclosed', function(e) {
if (e.response) {
options = JSON.parse(decodeURIComponent(e.response));
localStorage.setItem('options', JSON.stringify(options));
//console.log('storing options: ' + JSON.stringify(options));
updateWeather();
} else {
console.log('no options received');
}
});
Pebble.addEventListener("appmessage", function(e) {
if(e.payload.request_weather) {
console.log("Got weather request from watch.");
updateWeather();
}
});
Pebble.addEventListener("ready", function(e) {
//console.log("connect!" + e.ready);
updateWeather();
console.log(e.type);
});