-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSOLISCLOUD1.plugin.txt
More file actions
182 lines (157 loc) · 7.67 KB
/
SOLISCLOUD1.plugin.txt
File metadata and controls
182 lines (157 loc) · 7.67 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
///////// <version>1.0.1</version>
///////// SOLISCLOUD1 /////////////
///////// Plugin to extract SolisCloud Solar data for Toon ///////////////
///////// By Oepi-Loepi ///////////////
function base64 (input) {
var result = '', binData, i;
var base64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''); // Base is 65 in fact :-)
if (typeof input === 'string') for (i = 0, input = input.split(''); i < input.length; i++) input[i] = input[i].charCodeAt(0);
for (i = 0; i < input.length; i += 3) {
binData = (input[i] & 0xFF) << 16 | // FF.00.00
(input[i + 1] & 0xFF) << 8 | // 00.FF.00
(input[i + 2] & 0xFF); // 00.00.FF
result += base64Alphabet[(binData & 0xFC0000) >>> 18] + //11111100.00000000.00000000 = 0xFC0000 = 16515072
base64Alphabet[(binData & 0x03F000) >>> 12] + //00000011.11110000.00000000 = 0x03F000 = 258048
base64Alphabet[( i + 3 >= input.length && (input.length << 1) % 3 === 2 ? 64 :
(binData & 0x000FC0) >>> 6 )] + //00000000.00001111.11000000 = 0x000FC0 = 4032
base64Alphabet[( i + 3 >= input.length && (input.length << 1) % 3 ? 64 :
binData & 0x00003F )]; //00000000.00000000.00111111 = 0x00003F = 63
}
return result;
}
function hexStr2bin(str) {
str = str.replace(/[^0-9^a-f]/ig, ''); // Cutting off the garbage.
if (str.length & 1) return false; // Oh, this is not hex string (len % 2 !== 0).
var result = [], i;
for (i = 0; i < str.length; i += 2) {
result[result.length] = parseInt(str.substr(i, 2), 16);
}
return result;
}
function getSolarData(passWord,userName,apiKey,siteid,urlString,totalValue){
if (debugOutput) console.log("*********SolarPanel Start getSolisStep1")
var body = "{\"pageNo\":1,\"pageSize\":10}"
var keyId= userName
var keySecret = passWord
var port = siteid
var result = ''
var reqDate = new Date()
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var dayArray=['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var strArray=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var wd = dayArray[reqDate.getDay()]
var d = reqDate.getDate()
var m = strArray[reqDate.getMonth()]
var y = reqDate.getFullYear()
var h = reqDate.getUTCHours()
var min = reqDate.getMinutes()
var s = reqDate.getSeconds()
if (h.toString().length == 1) {
h = "0" + h;
}
if (min.toString().length == 1) {
min = "0" + min;
}
if (s.toString().length == 1) {
s = "0" + s;
}
var newDate = wd + ", " + d + " " + m + " " + y + " " + h + ":" + min + ":" + s + " GMT"
var newDateEncoded =encodeURI(newDate)
var encryptedBody= Qt.md5(body)
encryptedBody = base64( hexStr2bin(encryptedBody) )
var encryptedBodyEncoded = encodeURI(encryptedBody)
var onetime = true
var hmackey = keySecret
if (debugOutput) console.log("*********SolarPanel ---------------------------------------------------------------")
if (debugOutput) console.log("*********SolarPanel DateValue: " + newDate)
var doc2 = new XMLHttpRequest();
doc2.open("PUT", "file:///tmp/hmac.txt");
doc2.send(hmackey + ";" + newDate);
var doc4 = new XMLHttpRequest();
doc4.open("PUT", "file:///tmp/tsc.command");
doc4.send("external-solarPanel");
//wacht op een response vanuit het TSC script
solarEnphaseDelay(8000, function() {
if (onetime){
onetime = false
getHmac(totalValue, newDate, port, keyId)
}
})
}
function getHmac(totalValue, newDate, port, keyId){
if (debugOutput) console.log("*********SolarPanel Start getHmac")
var http = new XMLHttpRequest()
var url = "file:///tmp/hmac_output.txt"
if (debugOutput) console.log("*********SolarPanel url: " + url)
http.open("GET", url, true);
http.onreadystatechange = function() { // Call a function when the state changes.
if (http.readyState === XMLHttpRequest.DONE) {
if (http.status === 200 || http.status === 300 || http.status === 302) {
if (debugOutput) console.log("http.status: " + http.status)
if (debugOutput) console.log(http.responseText)
var base64hmac = base64( hexStr2bin(http.responseText) )
if (debugOutput) console.log("*********SolarPanel base64hmac: " + base64hmac)
var auth = "API " + keyId + ":" + base64hmac
var doc2 = new XMLHttpRequest();
doc2.open("PUT", "file:///tmp/solis.txt");
doc2.send(newDate + ";" + auth + ";" + port);
var doc4 = new XMLHttpRequest();
doc4.open("PUT", "file:///tmp/tsc.command");
doc4.send("external-solarPanel");
//wacht op een response vanuit het TSC script
var onetime = true
solarEnphaseDelay(8000, function() {
if (onetime){
onetime = false
parseSolisJson(totalValue)
}
})
} else {
if (http.status === 401){
if (getDataCount == 0){apiKey = ""}
if (getDataCount == 1){apiKey2 = ""}
}
parseReturnData(currentPower,totalValue,todayValue,0,0,0,0, http.status,"error")
}
}
}
http.send();
}
function parseSolisJson(totalValue){
if (debugOutput) console.log("*********SolarPanel Start parseSolisJson")
var http = new XMLHttpRequest()
var url = "file:///tmp/solis_output.txt"
if (debugOutput) console.log("*********SolarPanel url: " + url)
http.open("GET", url, true);
http.onreadystatechange = function() { // Call a function when the state changes.
if (debugOutput) console.log("http.readyState: " + http.readyState)
if (http.readyState === XMLHttpRequest.DONE) {
if (debugOutput) console.log("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 JsonObject= JSON.parse(http.responseText)
var today2=parseInt((JsonObject.data.page.records[0].dayEnergy) * 1000)
totalValue= parseInt((JsonObject.data.page.records[0].allEnergy) * 1000000)
currentPower = parseInt((JsonObject.data.page.records[0].power) * 1000)
if (debugOutput) console.log("*********SolarPanel currentPower: " + currentPower)
if (debugOutput) console.log("*********SolarPanel today2: " + today2)
if (debugOutput) console.log("*********SolarPanel totalValue: " + totalValue)
parseReturnData(currentPower,totalValue,today2,0,0,0,0,http.status,"succes")
}
catch (e){
currentPower = 0
parseReturnData(currentPower,totalValue,todayValue,0,0,0,0, http.status,"error")
}
} else {
if (http.status === 401){
if (getDataCount == 0){apiKey = ""}
if (getDataCount == 1){apiKey2 = ""}
}
parseReturnData(currentPower,totalValue,todayValue,0,0,0,0, http.status,"error")
}
}
}
http.send();
}