-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
42 lines (35 loc) · 1.14 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
/* global module */
/* Magic Mirror
* Node Helper: MMM-TidesUK
*
* By Simon Cowdell
* Thanks to KristjanESPERANTO for the code from MMM-Quiz
* MIT Licensed.
* Rewrite and simplification to avoid request.
*/
var NodeHelper = require('node_helper')
module.exports = NodeHelper.create({
async socketNotificationReceived(notification, payload) {
const self = this
if (notification === "GET_TIDES") {
let url = "https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/" + payload.StationID + "/TidalEvents?duration=3";
if (payload.difficulties)
url += `&difficulties=${payload.difficulties}`
if (payload.tags)
url += `&tags=${payload.tags}`
try {
const response = await fetch(url, {
headers: {
'Host': 'admiraltyapi.azure-api.net',
'Ocp-Apim-Subscription-Key': payload.AdmiraltyKey,
'Content-Type': 'application/json'
}})
const body = await response.json();
self.sendSocketNotification('TIDAL_RESULT', body)
} catch (error) {
self.sendSocketNotification("TIDES_FAILED", error);
}
}
},
})
});