-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_helper.js
60 lines (49 loc) · 1.37 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
'use strict';
/* Magic Mirror
* Module: MMM-MarineWeather
*
* Magic Mirror By Michael Teeuw https://magicmirror.builders
* MIT Licensed.
*
* Module MMM-MarineWeather By Grena https://github.com/grenagit
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const fetch = require('node-fetch');
module.exports = NodeHelper.create({
getParams: function() {
var self = this;
var currentDate = Math.floor(Date.now() / 1000)
var params = "?";
params += "lat=" + self.config.latitude + "&lng=" + self.config.longitude;
params += "¶ms=" + self.config.params.join();
params += "&source=" + self.config.dataSource;
params += "&start=" + currentDate + "&end=" + currentDate ;
return params;
},
getData: function() {
var self = this;
fetch(self.config.apiBase + self.config.MWEndpoint + self.getParams(), {
method: 'GET',
headers: {'Authorization': self.config.appid}
})
.then(function(response) {
if (response.status === 200) {
return response.json();
} else {
self.sendSocketNotification("ERROR", response.status);
}
})
.then(function(body) {
self.sendSocketNotification("DATA", body);
});
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'CONFIG') {
self.config = payload;
self.sendSocketNotification("STARTED", true);
self.getData();
}
}
});