-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnode_helper.js
More file actions
69 lines (64 loc) · 2.57 KB
/
Copy pathnode_helper.js
File metadata and controls
69 lines (64 loc) · 2.57 KB
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
/*
* Node Helper for MMM-OneCallWeather.
*
* This helper is responsible for the data pull from OpenWeather.
* 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 "metric", "imperial", or "" (blank)
* lang - Any of the languages OpenWeather supports, as listed here: https://openweathermap.org/api/one-call-api#multi
* Dropped axios and request and used fetch
*/
const NodeHelper = require("node_helper");
const Log = require("logger");
const moment = require("moment");
module.exports = NodeHelper.create({
socketNotificationReceived (notification, config) {
if (notification === "OPENWEATHER_ONECALL_GET") {
const self = this;
Log.debug("[MMM-OneCallWeather] node received");
if (config.apikey === null || config.apikey === "") {
Log.debug(`[MMM-OneCallWeather] ${moment().format("D-MMM-YY HH:mm")} ** ERROR ** No API key configured. Get an API key at https://openweathermap.org/api/one-call-api`);
} else if (
config.latitude === null ||
config.latitude === "" ||
config.longitude === null ||
config.longitude === ""
) {
Log.debug(`[MMM-OneCallWeather] ${moment().format("D-MMM-YY HH:mm")} ** ERROR ** Latitude and/or longitude not provided.`);
} else {
const myUrl =
`https://api.openweathermap.org/data/${config.apiVersion}/onecall` +
`?lat=${config.latitude}&lon=${config.longitude}${
config.units === ""
? ""
: `&units=${config.units}`
}&exclude=${config.exclude}&appid=${config.apikey}&lang=${
config.language
}`;
// make request to OpenWeather One Call API
fetch(myUrl)
.then((response) => {
if (response.status === 200) {
return response.json();
}
throw new Error(response.statusText);
})
.then((data) => {
// handle success
Log.debug(`[MMM-OneCallWeather] got request loop ${myUrl}`);
self.sendSocketNotification("OPENWEATHER_ONECALL_DATA", data);
Log.debug("[MMM-OneCallWeather] sent the data back");
})
.catch((error) => {
// handle error
Log.error(`[MMM-OneCallWeather] ${moment().format("D-MMM-YY HH:mm")} ** ERROR ** ${error}`);
});
}
}
}
});