-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-SolarEdge-InverterMonitor.js
executable file
·406 lines (323 loc) · 11 KB
/
MMM-SolarEdge-InverterMonitor.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* global Module */
/* Magic Mirror
* Module: MMM-SolarEdge-InverterMonitor
*
* By Enrique Cardona
* MIT Licensed.
*/
// Colors
const limeToGreen = [[212, 226, 132], [0, 173, 14]];
const yellowToRed = [[255, 238, 82], [173, 0, 14]];
const blueToRed = [[38, 0, 255], [255, 0, 0]];
const blueToPurple = [[38, 0, 255], [174, 0, 255]];
const resourcesPath = "/modules/MMM-SolarEdge-InverterMonitor/vendor/";
Module.register("MMM-SolarEdge-InverterMonitor", {
defaults: {
updateInterval: 10000,
retryDelay: 5000,
server: "http://localhost:8081/data?k=1234",
powerRange: [-2500, 2200],
showTemperature: true,
debug: false
// FIXME animation
//,
// animated: false
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
var self = this;
var dataRequest = null;
var dataNotification = null;
//Flag for check if module is loaded
this.loaded = false;
// Schedule update timer.
self.getData();
setInterval(function () {
self.updateDom();
}, this.config.updateInterval);
},
/*
* getData
*
*/
getData: function () {
var self = this;
var urlApi = self.config.server;
var retry = true;
var dataRequest = new XMLHttpRequest();
dataRequest.open("GET", urlApi, true);
dataRequest.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
self.processData(JSON.parse(this.response));
} else if (this.status === 401) {
self.updateDom(self.config.animationSpeed);
Log.error(self.name, this.status);
retry = false;
} else {
Log.error(self.name, "Could not load data.");
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
dataRequest.send();
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad;
var self = this;
setTimeout(function () {
self.getData();
}, nextLoad);
},
getDom: function () {
var self = this;
// create element wrapper for show into the module
var wrapper = document.createElement("div");
// If this.dataRequest is not empty
if (this.dataRequest) {
let header = document.createElement("header");
header.className = "module-header"
// Use translate function
// this id defined in translations files
header.innerHTML = this.translate("TITLE");
wrapper.appendChild(header);
// Icons
let iconDiv = document.createElement("div");
iconDiv.className = "icons"
let iconSolar = document.createElement("img");
iconSolar.src = resourcesPath + "/itim2101/solar-energy-" + (
this.dataRequest.Production_AC_Power_Net_WH > 0 ? "positive" : "off") + ".svg";
iconDiv.appendChild(iconSolar);
let iconSolarHome = document.createElement("div");
iconSolarHome.className = "progress-bar stripes";
let spanSolarHome = document.createElement("span");
spanSolarHome.style.width = "100%";
spanSolarHome.className = (this.dataRequest.Production_AC_Power_Net_WH <= 0 ? "off" : "positive");
iconSolarHome.appendChild(spanSolarHome);
iconDiv.appendChild(iconSolarHome);
let iconHome = document.createElement("img");
iconHome.src = resourcesPath + "/itim2101/home-" +
(this.dataRequest.Consumption_AC_Power_Meter > 0 ? "positive" : "negative")
+ ".svg";
iconDiv.appendChild(iconHome);
let iconHomeNet = document.createElement("div");
iconHomeNet.className = "progress-bar stripes";
let spanHomeNet = document.createElement("span");
spanHomeNet.style.width = "100%";
spanHomeNet.className = (this.dataRequest.Consumption_AC_Power_Meter < 0 ? "inverse negative" : "positive");
iconHomeNet.appendChild(spanHomeNet);
iconDiv.appendChild(iconHomeNet);
let icon = document.createElement("img");
icon.src = resourcesPath + "/itim2101/electric-tower-"
+
(this.dataRequest.Consumption_AC_Power_Meter < 0 ? "negative" : "off")
+ ".svg";
iconDiv.appendChild(icon);
wrapper.appendChild(iconDiv);
// end iconDiv
// alt meters
var wrapperMeters = document.createElement("div");
self.showMeter(wrapperMeters, "METER", "Wh", this.dataRequest.Consumption_AC_Power_Meter,
self.config.powerRange[0], self.config.powerRange[1], limeToGreen, yellowToRed, "large", false);
if(true || this.dataRequest.Production_AC_Power_Net_WH >0) {
self.showMeter(wrapperMeters, "PRODUCTION", "Wh", this.dataRequest.Production_AC_Power_Net_WH,
self.config.powerRange[0], self.config.powerRange[1], limeToGreen, yellowToRed, "normal", true);
self.showMeter(wrapperMeters, "CONSUMPTION", "Wh", this.dataRequest.Consumption_AC_Power_Net_WH,
self.config.powerRange[0], self.config.powerRange[1], limeToGreen, yellowToRed, "normal", true);
}
wrapper.appendChild(wrapperMeters);
// end alt meters
if (self.config.showTemperature) {
let tempWrapper = document.createElement("p");
tempWrapper.className = "status";
tempWrapper.innerHTML =
" " + this.translate("TEMPERATURE") + ": "
+ this.dataRequest.Temperature_C + "ºC";
wrapper.appendChild(tempWrapper);
}
// status
let statusWrapper = document.createElement("p");
statusWrapper.className = "status" +
(this.dataRequest.Consumption_AC_Power_Meter < 0 ?
" consuming" : " dumping");
statusWrapper.innerHTML = this.translate("STATUS")
+ ": " +
(this.dataRequest.Consumption_AC_Power_Meter < 0 ?
(this.dataRequest.Production_AC_Power_Net_WH > 0 ?
"<strong class='inssuficient'>" + this.translate("INSSUFICIENT") + "</strong>" :
"<strong class='consuming'>" + this.translate("CONSUMING") + "</strong>"
)
:
"<strong class='dumping'>" + this.translate("DUMPING") + "</strong>");
wrapper.appendChild(statusWrapper);
// end status
}
return wrapper;
},
showBar: function (wrapper, element, unit, data, minValue, maxValue, positiveColors, negativeColors) {
let center =
(1 - Math.abs(minValue / (minValue - maxValue))) * 100;
let factor = 100 * (data < 0 ?
Math.min(data / minValue, 1)
:
Math.min(data / maxValue, 1));
let percent =
(data < 0 ? (factor * (1 - center / 100)) :
(factor * center / 100));
if (self.config.debug) {
console.log("Element " + element + " - Center: " + center + "- Factor: " + factor + " %: " + percent)
}
let warning = data > maxValue || data < minValue;
let colorRed = (data < 0 ?
// little
negativeColors[0][0] +
// relative
Math.round(((negativeColors[1][0] - negativeColors[0][0]) * factor) / 100)
:
// little
positiveColors[0][0] +
// relative
Math.round(((positiveColors[1][0] - positiveColors[0][0]) * factor) / 100)
);
let colorGreen = (data < 0 ?
// little
negativeColors[0][1] +
// relative
Math.round(((negativeColors[1][1] - negativeColors[0][1]) * factor) / 100)
:
// little
positiveColors[0][1] +
// relative
Math.round(((positiveColors[1][1] - positiveColors[0][1]) * factor) / 100)
);
let colorBlue = (data < 0 ?
// little
negativeColors[0][2] +
// relative
Math.round(((negativeColors[1][2] - negativeColors[0][2]) * factor) / 100)
:
// little
positiveColors[0][2] +
// relative
Math.round(((positiveColors[1][2] - positiveColors[0][2]) * factor) / 100)
);
let labelWrapper = document.createElement("p");
labelWrapper.className = "label" + (warning ? " warning" : "");
labelWrapper.style.color = "rgb(" + colorRed + ", " + colorGreen + ", " + colorBlue + ")";
labelWrapper.innerHTML = this.translate(element) + ": " + data + " " + unit;
wrapper.appendChild(labelWrapper);
let newWrapper = document.createElement("div");
newWrapper.id = "bar-div-" + element;
newWrapper.className = "progress-bar stripes";
let spanWrapper = document.createElement("span");
spanWrapper.style.width = percent + "%";
if (data < 0) {
spanWrapper.style.marginRight = center + "%";
} else {
spanWrapper.style.marginLeft = (100 - center) + "%";
}
spanWrapper.className = (data < 0 ? "inverse" : "");
// FIXME animation
//= [
// self.config.animated ? "animated" : "",
// (data < 0 ? "inverse" : "")];
spanWrapper.style.backgroundColor = "rgb(" + colorRed + ", " + colorGreen + ", " + colorBlue + ")";
newWrapper.appendChild(spanWrapper);
wrapper.appendChild(newWrapper);
},
showMeter: function (wrapper, element, unit, data, minValue, maxValue, positiveColors, negativeColors, clazz, showTitle) {
var divMeters = document.createElement("div");
divMeters.className = clazz + " bright";
let factor = 100 * (data < 0 ?
Math.min(data / minValue, 1)
:
Math.min(data / maxValue, 1));
let warning = data > maxValue || data < minValue;
let colorRed = (data < 0 ?
// little
negativeColors[0][0] +
// relative
Math.round(((negativeColors[1][0] - negativeColors[0][0]) * factor) / 100)
:
// little
positiveColors[0][0] +
// relative
Math.round(((positiveColors[1][0] - positiveColors[0][0]) * factor) / 100)
);
let colorGreen = (data < 0 ?
// little
negativeColors[0][1] +
// relative
Math.round(((negativeColors[1][1] - negativeColors[0][1]) * factor) / 100)
:
// little
positiveColors[0][1] +
// relative
Math.round(((positiveColors[1][1] - positiveColors[0][1]) * factor) / 100)
);
let colorBlue = (data < 0 ?
// little
negativeColors[0][2] +
// relative
Math.round(((negativeColors[1][2] - negativeColors[0][2]) * factor) / 100)
:
// little
positiveColors[0][2] +
// relative
Math.round(((positiveColors[1][2] - positiveColors[0][2]) * factor) / 100)
);
let labelWrapper = document.createElement("span");
labelWrapper.className = "label" + (warning ? " warning" : "");
labelWrapper.style.color = "rgb(" + colorRed + ", " + colorGreen + ", " + colorBlue + ")";
labelWrapper.innerHTML = (showTitle?this.translate(element) + ": " : "") + data + " " + unit;
divMeters.appendChild(labelWrapper);
wrapper.appendChild(divMeters);
},
getScripts: function () {
return [];
},
getStyles: function () {
return [
"MMM-SolarEdge-InverterMonitor.css",
];
},
// Load translations files
getTranslations: function () {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
},
processData: function (data) {
var self = this;
this.dataRequest = data;
if (this.loaded === false) {
self.updateDom(self.config.animationSpeed);
}
this.loaded = true;
// the data if load
// send notification to helper
this.sendSocketNotification("MMM-SolarEdge-InverterMonitor-NOTIFICATION_TEST", data);
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-SolarEdge-InverterMonitor-NOTIFICATION_TEST") {
// set dataNotification
this.dataNotification = payload;
this.updateDom();
}
},
});