-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_helper.js
43 lines (39 loc) · 1 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
/* MagicMirror²
* Module: MMM-AQI
* By Ricardo Gonzalez
* MIT Licensed.
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start () {
Log.log("MMM-AQI helper started ...");
},
/* getAQIData()
* Requests new data from AirNow API.
* Sends data back via socket on succesfull response.
*/
async getAQIData (url) {
const self = this;
try {
const response = await fetch(url);
if (response.ok) {
const data = await response.json();
self.sendSocketNotification("AQI_DATA", {
data,
url,
});
} else {
self.sendSocketNotification("AQI_DATA", {data: null, url});
}
} catch (error) {
self.sendSocketNotification("AQI_DATA", {data: null, url});
}
},
// Subclass socketNotificationReceived received.
socketNotificationReceived (notification, payload) {
if (notification === "GET_AQI") {
this.getAQIData(payload.url);
}
},
});