forked from jclarke0000/MMM-DarkSkyForecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
99 lines (85 loc) · 2.93 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*********************************
Node Helper for MMM-AppleWeatherKit.
This helper is responsible for the data pull from Dark Sky.
At a minimum the API key, Latitude and Longitude parameters
must be provided. If any of these are missing, the request
to Dark Sky 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 "ca", "uk2", "us", or "si"
lang - Any of the languages Dark Sky supports, as listed here: https://darksky.net/dev/docs#response-format
The Dark Sky API request looks like this:
https://api.darksky.net/forecast/API_KEY/LATITUDE,LONGITUDE?units=XXX&lang=YY
*********************************/
var NodeHelper = require("node_helper");
var request = require("request");
var moment = require("moment");
var jwt = require("jsonwebtoken");
var fs = require("fs");
const qs = require("querystring");
module.exports = NodeHelper.create({
start: function () {
console.log(
"====================== Starting node_helper for module [" +
this.name +
"]"
);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "APPLE_WEATHERKIT_REQUEST") {
const {
appleDeveloperTeamId,
appleServiceId,
appleKeyId,
latitude,
longitude,
language,
appleWeatherKitKeyPath,
timezone,
countryCode,
} = payload;
console.log("PAYLOAD", payload);
const tokOpts = {
algorithm: "ES256",
keyid: appleKeyId,
jwtid: `${appleDeveloperTeamId}.${appleServiceId}`,
};
const claims = {
iss: appleDeveloperTeamId,
iat: new Date().getTime() / 1000,
exp: new Date().getTime() / 1000 + 60 * 60 * 24 * 180, // 180 days
sub: appleServiceId,
};
var privateKey = fs.readFileSync(appleWeatherKitKeyPath);
var token = jwt.sign(claims, privateKey, tokOpts);
const queryString = qs.stringify({
timezone,
countryCode,
dataSets: "currentWeather,forecastNextHour,forecastDaily,weatherAlerts",
});
const url = `https://weatherkit.apple.com/api/v1/weather/${language}/${latitude}/${longitude}?${queryString}`;
const req = {
url,
headers: {
Authorization: `Bearer ${token}`,
},
};
request(req, (error, response, body) => {
//console.log(`status code ${response.statusCode}`);
if (!error && response.statusCode == 200) {
var resp = JSON.parse(body);
//console.log(resp);
resp.instanceId = payload.instanceId;
this.sendSocketNotification("APPLE_WEATHERKIT_RESPONSE", resp);
} else {
console.log(
"[MMM-AppleWeatherKit] " +
moment().format("D-MMM-YY HH:mm") +
" ** ERROR ** " +
error
);
}
});
}
},
});