-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathENPHASEV4.plugin.txt
More file actions
115 lines (103 loc) · 5.17 KB
/
ENPHASEV4.plugin.txt
File metadata and controls
115 lines (103 loc) · 5.17 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
///////// <version>1.0.2</version>
///////// ENPHASEV4 /////////////
///////// Plugin to extract Enphase V4 Solar data for Toon ///////////////
///////// By Oepi-Loepi ///////////////
function base64Encode(input) {
var keySTR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keySTR.charAt(enc1) + keySTR.charAt(enc2) +
keySTR.charAt(enc3) +keySTR.charAt(enc4);
}
return output;
}
function getSolarData(passWord,userName,apiKey,siteid,urlString,totalValue){
if (debugOutput) console.log("*********SolarPanel Start getSolarData")
var base64encoded =base64Encode(userName + ":" + passWord);
if (debugOutput) console.log("*********SolarPanel base64encoded: " + base64encoded)
var params = "refresh_token="+ enphaseV4Refresh_token.trim() + "&grant_type=refresh_token"
if (debugOutput) console.log("*********SolarPanel params : " + params )
var http = new XMLHttpRequest()
var url = "https://api.enphaseenergy.com/oauth/token"
if (debugOutput) console.log("*********SolarPanel url: " + url)
http.open("POST", url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.setRequestHeader('Authorization', 'Basic ' + base64encoded);
http.onreadystatechange = function() { // Call a function when the state changes.
if (debugOutput) console.log("*********SolarPanel http.readyState: " + http.readyState)
if (http.readyState === XMLHttpRequest.DONE) {
if (debugOutput) console.log("*********SolarPanel http.status: " + http.status)
if (http.status === 200 || http.status === 300 || http.status === 302) {
try {
if (debugOutput) console.log("http.status: " + http.status)
if (debugOutput) console.log(http.responseText)
var JsonString = http.responseText
var JsonObject= JSON.parse(JsonString)
enphaseV4Access_token = JsonObject.access_token
enphaseV4Refresh_token = JsonObject.refresh_token
solarPanel_enphaseV4Refresh_token.write(JsonObject.refresh_token)
getStep2(passWord,userName,apiKey,siteid,urlString,totalValue);
}
catch (e){
currentPower = 0
parseReturnData(0,totalValue,todayValue,0,0,0,0, http.status,"error")
}
} else {
if (debugOutput) console.log(http.responseText)
parseReturnData(currentPower,totalValue,0,0,0,0,0, http.status,"error")
}
}
}
http.send(params);
}
function getStep2(passWord,userName,apiKey,siteid,urlString,totalValue){
if (debugOutput) console.log("*********SolarPanel Start getStep2")
var http = new XMLHttpRequest()
var url = "https://api.enphaseenergy.com/api/v4/systems/" + siteid + "/summary?key=" + apiKey
if (debugOutput) console.log("*********SolarPanel url: " + url)
http.open("GET", url, true);
http.setRequestHeader('Authorization', 'Bearer ' + enphaseV4Access_token.trim());
http.onreadystatechange = function() { // Call a function when the state changes.
if (debugOutput) console.log("*********SolarPanel http.readyState: " + http.readyState)
if (http.readyState === XMLHttpRequest.DONE) {
if (debugOutput) console.log("*********SolarPanel http.status: " + http.status)
if (http.status === 200 || http.status === 300 || http.status === 302) {
try {
if (debugOutput) console.log(http.responseText)
var JsonString = http.responseText
var JsonObject= JSON.parse(JsonString)
currentPower = JsonObject.current_power
totalValue = JsonObject.energy_lifetime
var today2 = JsonObject.energy_today
if (debugOutput) console.log("*********SolarPanel currentPower: " + currentPower)
if (debugOutput) console.log("*********SolarPanel today: " + today2)
if (debugOutput) console.log("*********SolarPanel totalValue: " + totalValue)
parseReturnData(currentPower,totalValue,today2,0,0,0,0,http.status,"succes")
}
catch (e){
currentPower = 0
parseReturnData(0,totalValue,todayValue,0,0,0,0, http.status,"error")
}
} else {
if (debugOutput) console.log(http.responseText)
parseReturnData(currentPower,totalValue,0,0,0,0,0, http.status,"error")
}
}
}
http.send();
}