forked from RodgerLeblanc/Custom-Weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.min.js
297 lines (253 loc) · 9.19 KB
/
app.min.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"use strict";
var useConfig = true;
var openWeatherMapApiKey = 'your_api_key_here';
var request = require('request');
var VectorWatch = require('vectorwatch-sdk');
var vectorWatch = new VectorWatch();
var logger = vectorWatch.logger;
vectorWatch.on('config', function(event, response) {
// your stream was just dragged onto a watch face
logger.info('on config');
if (useConfig) {
var city = response.createAutocomplete('City');
city.setHint('Enter a city (ie: London, UK)');
city.setAsYouType(1);
var scale = response.createGridList('Scale');
scale.setHint('Select temperature scale');
scale.addOption('C');
scale.addOption('F');
var provider = response.createGridList('Provider');
provider.setHint('Select weather provider');
provider.addOption('Yahoo');
provider.addOption('OpenWeatherMap');
}
response.send();
});
vectorWatch.on('options', function(event, response) {
// dynamic options for a specific setting name was requested
logger.info('on options');
switch(event.req.body.settingName) {
case 'City':
var searchTerm = event.getSearchTerm();
response.addOption(searchTerm);
response.send();
break;
}
return response;
});
vectorWatch.on('subscribe', function(event, response) {
// your stream was added to a watch face
logger.info('on subscribe');
response.setValue('CellNinja');
response.send();
try {
// Yahoo servers sometimes returns 503, which breaks the subscription. Call webhook to avoid this.
callWebhook();
} catch(err) {
logger.error('Error on subscribe: ' + err.message);
}
});
vectorWatch.on('unsubscribe', function(event, response) {
// your stream was removed from a watch face
logger.info('on unsubscribe');
response.send();
});
vectorWatch.on('schedule', function(records) {
logger.info('on schedule');
records.forEach(function(record) {
getWeatherFromRecord(record);
});
});
vectorWatch.on('webhook', function(event, response, records) {
logger.info('on webhook');
records.forEach(function(record) {
getWeatherFromRecord(record);
});
response.setContentType('text/plain');
response.statusCode = 200;
response.setContent('OK');
response.send();
});
function callWebhook() {
return new Promise(function (resolve, reject) {
var url = 'https://endpoint.vector.watch/VectorCloud/rest/v1/stream/' + process.env.STREAM_UUID + '/webhook';
logger.info('url: ' + url);
request(url, function (error, httpResponse, body) {
if (error) {
reject('REST call error: ' + error.message + ' for ' + url);
return;
}
if (httpResponse && httpResponse.statusCode != 200) {
reject('REST call error: ' + httpResponse.statusCode + ' for ' + url);
return;
}
resolve(body);
});
});
}
function getWeatherFromRecord(record) {
var settings = getSettings(record.userSettings);
try {
getWeather(settings).then(function(body) {
logger.info('body: ' + JSON.stringify(body));
var streamText = getStreamText(settings, body);
logger.info('streamText: ' + streamText);
record.pushUpdate(streamText);
}).catch(function(e) {
logger.error('Error in on schedule first getWeather: ' + e);
// Retry once again
try {
getWeather(settings).then(function(body) {
logger.info('body: ' + JSON.stringify(body));
var streamText = getStreamText(settings, body);
logger.info('streamText: ' + streamText);
record.pushUpdate(streamText);
}).catch(function(e) {
logger.error('Error in on schedule second getWeather: ' + e);
});
} catch(err) {
logger.error('Double error: ' + err.message);
}
});
} catch(err) {
logger.error('on push - malformed user setting: ' + err.message);
}
}
function getWeather(settings) {
return new Promise(function (resolve, reject) {
var city = settings.City.name;
var scale = settings.Scale.name;
var provider = settings.Provider.name;
var url = encodeURI((provider === 'Yahoo') ?
'https://query.yahooapis.com/v1/public/yql?q=select item.condition ' +
'from weather.forecast where woeid in ' +
'(select woeid from geo.places(1) where ' +
'text="' + city + '") and ' +
'u=\'' + scale + '\'&format=json'
:
'http://api.openweathermap.org/data/2.5/weather' +
'?q=' + city +
'&units=' + unitsToOWMUnits(scale) +
'&appid=' + openWeatherMapApiKey
);
logger.info('url: ' + url);
// https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22London,%20UK%22)%20and%20u=%27c%27&format=json
// http://api.openweathermap.org/data/2.5/weather?q=London,%20UK&units=metric&appid=<your_api_key>
request(url, function (error, httpResponse, body) {
if (error) {
reject('REST call error: ' + error.message + ' for ' + url);
return;
}
if (httpResponse && httpResponse.statusCode != 200) {
reject('REST call error: ' + httpResponse.statusCode + ' for ' + url);
return;
}
try {
body = JSON.parse(body);
resolve(body);
} catch(err) {
reject('Malformed JSON response from ' + url + ': ' + err.message);
}
});
});
}
function getSettings(settings) {
if (!useConfig || (settings.City === undefined) || (settings.Scale === undefined) || (settings.Provider === undefined)) {
settings = JSON.parse('{"City":{"name":"London, UK"},"Scale":{"name":"C"},"Provider":{"name":"OpenWeatherMap"}}');
}
return settings;
}
function unitsToOWMUnits(u) {
return (u.toUpperCase() === 'F') ? "imperial" : "metric";
}
function getStreamText(settings, body) {
var temp = Math.round((settings.Provider.name === 'Yahoo') ?
body.query.results.channel.item.condition.temp :
body.main.temp);
var icon = (settings.Provider.name === 'Yahoo') ?
body.query.results.channel.item.condition.code :
body.weather[0].icon;
var convertedIcon = (settings.Provider.name === 'Yahoo') ?
vectorIconFromYahooIcon(icon) :
vectorIconFromOWMIcon(icon);
if (convertedIcon !== "") convertedIcon += " ";
var scale = settings.Scale.name;
return convertedIcon + temp + "°" + scale;
}
function vectorIconFromYahooIcon(code) {
switch(parseInt(code)) {
case 0: return String.fromCharCode(0xe00a);
case 1:
case 2:
case 3:
case 4: return String.fromCharCode(0xe011);
case 5:
case 6:
case 7: return String.fromCharCode(0xe00e);
case 8:
case 9:
case 10: return String.fromCharCode(0xe00d);
case 11:
case 12: return String.fromCharCode(0xe00b);
case 13:
case 14:
case 15:
case 16: return String.fromCharCode(0xe00f);
case 17: return String.fromCharCode(0xe00d);
case 18: return String.fromCharCode(0xe00e);
case 19: return String.fromCharCode(0xe00a);
case 20:
case 21: return String.fromCharCode(0xe009);
case 22:
case 23:
case 24: return String.fromCharCode(0xe00a);
case 25: return "";
case 26:
case 27:
case 28: return String.fromCharCode(0xe008);
case 29:
case 30: return String.fromCharCode(0xe006);
case 31: return String.fromCharCode(0xe005);
case 32: return String.fromCharCode(0xe004);
case 33: return String.fromCharCode(0xe005);
case 34: return String.fromCharCode(0xe004);
case 35: return String.fromCharCode(0xe00e);
case 36: return String.fromCharCode(0xe004);
case 37:
case 38:
case 39: return String.fromCharCode(0xe011);
case 40: return String.fromCharCode(0xe00b);
case 41:
case 42:
case 43: return String.fromCharCode(0xe00f);
case 44: return String.fromCharCode(0xe006);
case 45: return String.fromCharCode(0xe011);
case 46: return String.fromCharCode(0xe00f);
case 47: return String.fromCharCode(0xe011);
case 3200: return "";
default: return "";
}
}
function vectorIconFromOWMIcon(code) {
switch(code) {
case "01d": return String.fromCharCode(0xe004);
case "01n": return String.fromCharCode(0xe004);
case "02d":
case "02n": return String.fromCharCode(0xe004);
case "03d":
case "03n":
case "04d":
case "04n": return String.fromCharCode(0xe008);
case "09d":
case "09n":
case "10d":
case "10n": return String.fromCharCode(0xe00b);
case "11d":
case "11n": return String.fromCharCode(0xe011);
case "13d":
case "13n": return String.fromCharCode(0xe00f);
case "50d":
case "50n": return String.fromCharCode(0xe009);
default: return "";
}
}