-
Notifications
You must be signed in to change notification settings - Fork 28
/
node_helper.js
74 lines (55 loc) · 2.74 KB
/
node_helper.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
/*********************************
Node Helper for MMM-OpenWeatherForecast.
This helper is responsible for the data pull from OpenWeather's
One Call API. At a minimum the API key, Latitude and Longitude
parameters must be provided. If any of these are missing, the
request to OpenWeather will not be executed, and instead an error
will be output the the MagicMirror log.
Additional, this module supplies two optional parameters:
units - one of "standard", "metric" or "imperial"
lang - Any of the languages OpenWeather supports, as listed here: https://openweathermap.org/api/one-call-api#multi
The OpenWeather OneCall API request looks like this:
http://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&lang={lang}&exclude=minutely&units={units}&lang={lang}
*********************************/
var NodeHelper = require("node_helper");
var axios = require('axios').default; //replaces the deprecated Request library
var moment = require("moment");
module.exports = NodeHelper.create({
start: function() {
console.log("====================== Starting node_helper for module [" + this.name + "]");
},
socketNotificationReceived: function(notification, payload){
if (notification === "OPENWEATHER_FORECAST_GET") {
var self = this;
if (payload.apikey == null || payload.apikey == "") {
console.log( "[MMM-OpenWeatherForecast] " + moment().format("D-MMM-YY HH:mm") + " ** ERROR ** No API key configured. Get an API key at https://darksky.net" );
} else if (payload.latitude == null || payload.latitude == "" || payload.longitude == null || payload.longitude == "") {
console.log( "[MMM-OpenWeatherForecast] " + moment().format("D-MMM-YY HH:mm") + " ** ERROR ** Latitude and/or longitude not provided." );
} else {
//make request to OpenWeather One Call API
var url = "https://api.openweathermap.org/data/2.5/onecall?" +
"lat=" + payload.latitude +
"&lon=" + payload.longitude +
"&exclude=" + "minutely" +
"&appid=" + payload.apikey +
"&units=" + payload.units +
"&lang=" + payload.language;
/*
Update 28-Feb-2021:
The old standby Request library has been deprecated.
So data fetch is now done with Axios.
*/
axios.get(url)
.then(function (response) {
// handle success
response.data.instanceId = payload.instanceId;
self.sendSocketNotification("OPENWEATHER_FORECAST_DATA", response.data);
})
.catch(function (error) {
// handle error
console.log( "[MMM-OpenWeatherForecast] " + moment().format("D-MMM-YY HH:mm") + " ** ERROR ** " + error );
});
}
}
},
});