-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMMM-darksky-hourly.js
343 lines (290 loc) · 9.56 KB
/
MMM-darksky-hourly.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
Module.register("MMM-darksky-hourly", {
defaults: {
apiKey: "",
apiBase: "https://api.darksky.net/forecast",
units: config.units,
language: config.language,
twentyFourHourTime: true,
showCurrentWeather: true,
showTextSummary: true,
showPrecipitationPossibilityInRow: true,
showDayInRow: true,
showIconInRow: true,
fadeForecast: true,
updateInterval: 10 * 60 * 1000, // every 5 minutes
animationSpeed: 1000,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500,
tempDecimalPlaces: 0, // round temperatures to this many decimal places
geoLocationOptions: {
enableHighAccuracy: true,
timeout: 5000
},
latitude: null,
longitude: null,
maxHoursForecast: 8, // maximum number of days to show in forecast
skipHours: 0,
unitTable: {
'default': 'auto',
'metric': 'si',
'imperial': 'us'
},
iconTable: {
'clear-day': 'wi-day-sunny',
'clear-night': 'wi-night-clear',
'rain': 'wi-rain',
'snow': 'wi-snow',
'sleet': 'wi-sleet',
'wind': 'wi-cloudy-gusts',
'fog': 'wi-fog',
'cloudy': 'wi-cloudy',
'partly-cloudy-day': 'wi-day-cloudy',
'partly-cloudy-night': 'wi-night-alt-cloudy',
'hail': 'wi-hail',
'thunderstorm': 'wi-thunderstorm',
'tornado': 'wi-tornado'
},
debug: false
},
getTranslations: function () {
return false;
},
getScripts: function () {
return [
'jsonp.js',
'moment.js'
];
},
getStyles: function () {
return ["weather-icons.css", "MMM-darksky-hourly.css"];
},
shouldLookupGeolocation: function () {
return this.config.latitude == null &&
this.config.longitude == null;
},
start: function () {
Log.info("Starting module: " + this.name);
if (this.shouldLookupGeolocation()) {
this.getLocation();
}
this.scheduleUpdate(this.config.initialLoadDelay);
},
updateWeather: function () {
if (this.geoLocationLookupFailed) {
return;
}
if (this.shouldLookupGeolocation() && !this.geoLocationLookupSuccess) {
this.scheduleUpdate(1000); // try again in one second
return;
}
var units = this.config.unitTable[this.config.units] || 'auto';
var url = this.config.apiBase+'/'+this.config.apiKey+'/'+this.config.latitude+','+this.config.longitude+'?units='+units+'&lang='+this.config.language;
if (this.config.data) {
// for debugging
this.processWeather(this.config.data);
} else {
getJSONP(url, this.processWeather.bind(this), this.processWeatherError.bind(this));
}
},
processWeather: function (data) {
if (this.config.debug) {
console.log('weather data', data);
}
this.loaded = true;
this.weatherData = data;
this.temp = this.roundTemp(this.weatherData.currently.temperature);
this.updateDom(this.config.animationSpeed);
this.scheduleUpdate();
},
processWeatherError: function (error) {
if (this.config.debug) {
console.log('process weather error', error);
}
// try later
this.scheduleUpdate();
},
notificationReceived: function(notification, payload, sender) {
switch(notification) {
case "DOM_OBJECTS_CREATED":
break;
}
},
getDom: function() {
var wrapper = document.createElement("div");
if (this.config.apiKey === "") {
wrapper.innerHTML = "Please set the correct forcast.io <i>apiKey</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.geoLocationLookupFailed) {
wrapper.innerHTML = "Geolocaiton lookup failed, please set <i>latitude</i> and <i>longitude</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = "dimmed light small";
return wrapper;
}
var currentWeather = this.weatherData.currently;
var hourly = this.weatherData.hourly;
if(this.config.showCurrentWeather) {
var large = document.createElement("div");
large.className = "large light";
var icon = currentWeather ? currentWeather.icon : hourly.icon;
var iconClass = this.config.iconTable[icon];
var icon = document.createElement("span");
icon.className = 'big-icon wi ' + iconClass;
large.appendChild(icon);
var temperature = document.createElement("span");
temperature.className = "bright";
temperature.innerHTML = " " + this.temp + "°";
large.appendChild(temperature);
wrapper.appendChild(large);
}
if (this.config.showTextSummary) {
var summaryText = hourly.summary;
var summary = document.createElement("div");
summary.className = "small dimmed summary";
summary.innerHTML = summaryText;
wrapper.appendChild(summary);
}
wrapper.appendChild(this.renderWeatherForecast());
return wrapper;
},
// Get current day from time
getDayFromTime: function (time) {
var dt = new Date(time * 1000);
return moment.weekdaysShort(dt.getDay());
},
// Get current hour from time
// Depending on config returns either
// - 23:00 - 24 hour format
// - 11pm - 12 hour format
getHourFromTime: function (time) {
var dt = new Date(time * 1000);
var hour = dt.getHours();
if (this.config.twentyFourHourTime) {
return hour + ":00";
}
else {
var twentyFourHourFormat = "am";
if (hour > 11) {
twentyFourHourFormat = "pm";
}
hour = hour % 12;
hour = (hour == 0) ? 12 : hour;
return hour + twentyFourHourFormat;
}
},
// A bunch of these make up the meat
// In each row we can should display
// - time, icon, precip, temp
renderForecastRow: function (data, addClass) {
// Start off with our row
var row = document.createElement("tr");
row.className = "forecast-row" + (addClass ? " " + addClass : "");
// time - hours
var hourTextSpan = document.createElement("span");
hourTextSpan.className = "forecast-hour"
hourTextSpan.innerHTML = this.getHourFromTime(data.time)
// icon
var iconClass = this.config.iconTable[data.icon];
var icon = document.createElement("span");
icon.className = 'wi weathericon ' + iconClass;
// precipitation
// extra check here is due to darksky precip being optional
var precipPossibility = document.createElement("span");
precipPossibility.innerHTML = "N/A"
if (data.precipProbability != null) {
precipPossibility.innerHTML = Math.round(data.precipProbability * 100) + "%";
}
precipPossibility.className = "precipitation"
// temperature
var temp = data.temperature;
temp = Math.round(temp);
var temperature = document.createElement("span");
temperature.innerHTML = temp + "°";
temperature.className = "temp";
// Add what's necessary and return it
row.appendChild(hourTextSpan)
if (this.config.showIconInRow) { row.appendChild(icon); }
if (this.config.showPrecipitationPossibilityInRow) { row.appendChild(precipPossibility) }
row.appendChild(temperature)
return row;
},
renderWeatherForecast: function () {
// Placeholders
var numHours = this.config.maxHoursForecast;
var skip = this.config.skipHours + 1;
// Truncate for the data we need
var filteredHours =
this.weatherData.hourly.data.filter( function(d, i) { return ((i <= (numHours * skip)) && (i % skip == 0)); });
// Setup what we'll be displaying
var display = document.createElement("table");
display.className = "forecast";
var days = [];
// Cycle through and populate our table
for (let i = 1; i < filteredHours.length; i++) {
// insert day here if necessary
var hourData = filteredHours[i];
var addClass = "";
if(this.config.fadeForecast) {
if(i+2 == filteredHours.length) {
addClass = "dark";
}
if(i+1 == filteredHours.length) {
addClass = "darker";
}
}
var row = this.renderForecastRow(hourData, addClass);
let day = this.getDayFromTime(hourData.time);
let daySpan = document.createElement("span");
if (days.indexOf(day) == -1) {
daySpan.innerHTML = day;
days.push(day);
}
if (this.config.showDayInRow) { row.prepend(daySpan); }
display.appendChild(row);
}
return display;
},
getLocation: function () {
var self = this;
navigator.geolocation.getCurrentPosition(
function (location) {
if (self.config.debug) {
console.log("geolocation success", location);
}
self.config.latitude = location.coords.latitude;
self.config.longitude = location.coords.longitude;
self.geoLocationLookupSuccess = true;
},
function (error) {
if (self.config.debug) {
console.log("geolocation error", error);
}
self.geoLocationLookupFailed = true;
self.updateDom(self.config.animationSpeed);
},
this.config.geoLocationOptions);
},
// Round the temperature based on tempDecimalPlaces
roundTemp: function (temp) {
var scalar = 1 << this.config.tempDecimalPlaces;
temp *= scalar;
temp = Math.round( temp );
temp /= scalar;
return temp;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.updateWeather();
}, nextLoad);
}
});