-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
357 lines (314 loc) · 8.49 KB
/
index.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const fs = require('fs');
const {
parseString
} = require('xml2js');
const moment = require('moment');
const readline = require('readline');
const request = require('request-promise');
const q = require('daskeyboard-applet');
const logger = q.logger;
const Colors = Object.freeze({
CLEAR: '#FFFF00',
CLOUDY: '#FF00FF',
SHOWER: '#0000FF',
SNOW: '#FFFFFF',
STORM: '#FF0000',
SUNNY: '#FFFF00'
})
const Units = Object.freeze({
metric: 'metric',
imperial: 'imperial'
});
const MAX_SEARCH_RESULTS = 1000;
/**
* Loads the weather cities from an installed text file
*/
async function loadCities() {
logger.info(`Retrieving cities...`);
return new Promise((resolve, reject) => {
const lines = [];
const reader = readline.createInterface({
input: fs.createReadStream('cities.txt'),
crlfDelay: Infinity
});
reader.on('line', (line) => {
lines.push(line);
});
reader.on('close', () => {
// remove duplicates, ignoring the header on first line
const dedupe = lines.slice(1).sort().filter((line, i, allLines) => {
return (i == 0 || allLines[i - 1] != line);
});
resolve(dedupe);
});
reader.on('error', error => {
reject(error);
})
})
}
/**
* Process a list of zones into a list of options
* @param {Array<*>} lines
*/
function processCities(lines) {
const options = [];
for (line of lines) {
const values = line.split("\t");
const url = values[17].trim() || values[16].trim() || values[15].trim();
const urlParts = url.split('//')[1].split('/');
options.push({
key: url,
value: `${values[3]}, ${urlParts[3].replace(/_/g, ' ')} (${values[10]})`,
})
}
return options;
}
/**
* Retrieve forecast XML from the service
* @param {String} forecastUrl
*/
async function retrieveForecast(forecastUrl) {
logger.info("Getting forecast via URL: " + forecastUrl);
return request.get({
url: forecastUrl,
json: false
}).then(async body => {
return new Promise((resolve, reject) => {
parseString(body, function (err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
});
}
/**
* Represents a single forecast period within a day
*/
class Period {
constructor({
from,
to,
number,
symbol = {},
precipitation = {},
windDirection = {},
windSpeed = {},
temperature = {},
pressure = {}
}) {
this.from = from;
this.to = to;
this.number = number;
this.symbol = symbol;
this.precipitation = precipitation;
this.windDirection = windDirection;
this.windSpeed = windSpeed;
this.temperature = temperature;
this.pressure = pressure;
}
}
Period.revive = function (json) {
const meta = json['$'];
return new Period({
from: meta.from,
to: meta.to,
number: meta.period,
symbol: json.symbol[0]['$'],
precipitation: json.precipitation[0]['$'],
windDirection: json.windDirection[0]['$'],
windSpeed: json.windSpeed[0]['$'],
temperature: json.temperature[0]['$'],
pressure: json.pressure[0]['$'],
});
}
/**
* Represents a day's worth of forecasts
*/
class Day {
constructor(date, periods) {
this.date = date;
this.periods = periods;
}
}
/**
* Process raw JSON forecast data into Days and Periods
* @param {String} data
*/
function processForecast(data) {
const periods = data.weatherdata.forecast[0].tabular[0].time;
const days = [];
let currentDate = '';
let thisDay = null;
for (periodJson of periods) {
let period = Period.revive(periodJson);
let thisDate = period.from.split('T')[0];
if (thisDate !== currentDate) {
currentDate = thisDate;
if (thisDay) {
days.push(thisDay);
}
thisDay = new Day(thisDate, [period]);
} else {
thisDay.periods.push(period);
}
if (thisDay && thisDay.length) {
days.push(thisDay)
}
}
return days;
}
/**
* Choose the most relevant forecast period within a day.
* @param {Day} day
*/
function choosePeriod(day) {
let periods = [...day.periods];
// strip the overnight period
if (periods.length === 4) {
periods = periods.slice(1);
}
// strip the late night period
if (periods.length > 1) {
periods = periods.slice(0, periods.length - 1);
}
// if only one period, then we return it
if (periods.length === 1) {
return periods[0];
}
// return the period with more precipitation
return (periods[0].precipitation.value > periods[1].precipitation.value) ?
periods[0] : periods[1];
}
const periodNameMap = {
'0': 'Overnight',
'1': 'Morning',
'2': 'Afternoon',
'3': 'Evening',
}
function generatePeriodText(period, units) {
const temperature = (units == Units.imperial) ?
Math.round(period.temperature.value * 1.8 + 32) + '°F' :
period.temperature.value + '°C';
return `${periodNameMap[period.number]}: ${period.symbol.name}, ${temperature}`;
}
/**
* Chooses the appropriate signal color for a period
* @param {Period} period
*/
function chooseColor(period) {
const text = period.symbol.name.toLowerCase();
if (text.includes('snow')) {
return Colors.SNOW;
} else if (text.includes('storm')) {
return Colors.STORM;
} else if (text.includes('rain') || text.includes('shower')) {
return Colors.SHOWER;
} else if (text.includes('cloud')) {
return Colors.CLOUDY;
} else {
return Colors.CLEAR;
}
}
class WeatherForecast extends q.DesktopApp {
constructor() {
super();
this.cityName = null;
// run every 30 min
this.pollingInterval = 30 * 60 * 1000;
}
async applyConfig() { }
async options(fieldId, search) {
return loadCities().then(cities => {
return processCities(cities)
}).then(options => {
// filter the cities if needed
search = (search || '').trim();
search = unescape(search);
if (search.length > 0) {
return options.filter(option => {
return option.value.toLowerCase().includes(search);
}).slice(0, MAX_SEARCH_RESULTS);
} else {
return options.slice(0, MAX_SEARCH_RESULTS);
}
}).catch(error => {
logger.error(error);
return [];
});
}
/**
* Generate a signal from a forecast day
* @param {Array<Day>} days
*/
generateSignal(days) {
days = days.slice(0, this.getWidth());
const messages = [];
for (let day of days) {
const dateMessage = `<div style="color: red;"><strong>${moment(day.date).format('dddd, MMMM Do')}:</strong></div>`;
messages.push(dateMessage);
messages.push(`<div>`);
day.periods.forEach((period, index) => {
messages.push(`${generatePeriodText(period, this.config.units)}`);
if (index !== day.periods.length - 1) {
messages.push('-');
}
});
messages.push(`</div></br>`);
}
const signal = new q.Signal({
points: [
days.map(day => {
return new q.Point(chooseColor(choosePeriod(day)))
})
],
name: `${this.config.cityId_LABEL}`,
message: messages.join("\n"),
});
return signal;
}
async run() {
logger.info("Weather international running.");
const forecastUrl = this.config.cityId;
const cityName = this.config.cityId_LABEL || this.config.cityId;
if (forecastUrl) {
logger.info("My forecast URL is : " + forecastUrl);
logger.info("My city name is: " + cityName);
return retrieveForecast(forecastUrl)
.then(body => {
return this.generateSignal(processForecast(body));
})
.catch((error) => {
logger.error(`Error while getting forecast data: ${error}`);
if(`${error.message}`.includes("getaddrinfo")){
// Do not send signal when getting internet connection error
// return q.Signal.error(
// 'The Weather forecast International service returned an error. <b>Please check your internet connection</b>.'
// );
}else{
return q.Signal.error([`The Weather forecast International service returned an error. Detail: ${error}`]);
}
})
} else {
logger.info("No cityId configured.");
return null;
}
}
}
module.exports = {
Colors: Colors,
Units: Units,
Day: Day,
Period: Period,
WeatherForecast: WeatherForecast,
chooseColor: chooseColor,
choosePeriod: choosePeriod,
generatePeriodText: generatePeriodText,
loadCities: loadCities,
processCities: processCities,
processForecast: processForecast,
retrieveForecast: retrieveForecast,
}
const applet = new WeatherForecast();