-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
98 lines (93 loc) · 4.26 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
/*
* Magic Mirror
* Module: MMM-EnphaseSolar
* By Matt Thurling
*
* MIT Licensed.
*/
var https = require('https');
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if(notification === "GET_ENPHASE_SOLAR") {
new Promise(resolve => {
if (payload.sessionId) {
if (payload.config.debug) {
console.debug("MMM-EnphaseSolar: already have a session token");
}
return resolve(payload.sessionId);
}
console.info("MMM-EnphaseSolar: Getting a session token");
const tokenOptions = {
host: payload.config.gatewayHost,
method: 'GET',
path: '/auth/check_jwt',
rejectUnauthorized: false,
headers: {
Host: payload.config.gatewayHost,
Authorization: ' Bearer ' + payload.config.token,
}
};
const tokenReq = https.request(tokenOptions, (response) => {
var sessionId;
if (response.statusCode == 200) {
const setCookie = response.headers['set-cookie'][0];
sessionId = Object.fromEntries(setCookie.split('; ').map(v => v.split(/=(.+)/))).sessionId;
if (sessionId) {
payload.sessionId = sessionId;
} else {
console.error("MMM-EnphaseSolar: Failed to get session id! Set-Cookie was: " + setCookie);
}
} else {
console.error("MMM-EnphaseSolar: Failed to authorize with local gateway! Response code: " + response.statusCode);
}
response.on('data', function() {
});
response.on('end', function() {
resolve(sessionId);
});
});
tokenReq.on('error', (error) => {
console.error("MMM-EnphaseSolar: Failed to authorize with local gateway! error: " + error);
});
tokenReq.end();
}).then(sessionId => {
const options = {
host: payload.config.gatewayHost,
method: 'GET',
path: '/production.json?details1',
rejectUnauthorized: false,
headers: {
'Accept': 'application/json',
'Cookie': 'sessionId=' + payload.sessionId,
}
};
const dataReq = https.request(options, (response) => {
if (response.statusCode != 200) {
console.error("MMM-EnphaseSolar: data request error: " + response.statusCode);
// clear session id since its expiry may have been the cause of the failure, will retrieve a new one on next refresh
self.sendSocketNotification("ENPHASE_SOLAR_DATA", {sessionId: "", production: [], consumption: []});
}
response.on('data', (data) => {
let jsonData;
try {
jsonData = JSON.parse(data);
jsonData.sessionId = sessionId;
self.sendSocketNotification("ENPHASE_SOLAR_DATA", jsonData);
} catch(e) {
console.error("MMM-EnphaseSolar: Unable to parse JSON, data in response was: " + data);
}
});
});
dataReq.on('error', (error) => {
console.error("MMM-EnphaseSolar: Failed to retrieve solar data! error: " + error);
});
dataReq.end();
});
}
},
});