forked from bertieuk/MMM-SolarEdge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-SolarEdgeLite.js
144 lines (123 loc) · 5.04 KB
/
MMM-SolarEdgeLite.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
/*
* Magic Mirror module for displaying minimal SolarEdge data
* Today, yesterday, this month, smaller font
* By Jeroen Peters (jeroenpeters1986), forked from bertieuk https://github.com/bertieuk/MMM-Solar
* MIT Licensed
*/
Module.register("MMM-SolarEdgeLite",{
defaults: {
url: "https://monitoringapi.solaredge.com/site/",
apiKey: "",
siteId: "12345",
refInterval: 1000 * 60 * 5,
basicHeader: false,
language: 'en'
},
/* This one is not very solid, we'll see if we can improve it when the module increases popularity */
// Get the modules text-strings based on user preferred languages
getLangStrings: function(lang, string_title)
{
let strings = [];
if( lang === 'nl' ) {
strings["title"] = "Zonnepanelen";
strings["loading"] = "Bezig met laden...";
strings["titles"] = ["Huidig vermogen:", "Vandaag:", "Gisteren:", "Deze maand:"];
strings["suffixes"] = ["Watt", "kWh", "kWh", "kWh"];
strings["results"] = ["Laden...", "Laden...", "Laden...", "Laden..."];
} else {
strings["title"] = "SolarEdge PV";
strings["loading"] = "Loading...";
strings["titles"] = ["Current power:", "Today:", "Yesterday:", "This month:"];
strings["suffixes"] = ["Watt", "kWh", "kWh", "kWh"];
strings["results"] = ["Loading...", "Loading...", "Loading...", "Loading...",];
}
return strings[string_title];
},
// Start the module
start: function() {
this.titles = this.getLangStrings(this.config.language, 'titles');
this.suffixes = this.getLangStrings(this.config.language, 'suffixes');
this.results = this.getLangStrings(this.config.language, 'results');
this.loaded = false;
this.getSolarData();
if (this.config.basicHeader) {
this.data.header = this.getLangStrings(this.config.language, 'title');
}
const self = this;
//Schedule updates
setInterval(function() {
self.getSolarData();
self.updateDom();
}, this.config.refInterval);
},
// Import additional CSS Styles
getStyles: function() {
return ['solar.css']
},
// Contact node helper for solar data
getSolarData: function() {
this.sendSocketNotification("GET_SEL_DATA", {
config: this.config
});
},
// Handle node helper response
socketNotificationReceived: function(notification, payload) {
if (notification === "SEL_DATA") {
let currentPower = payload.overview.currentPower.power;
if (currentPower > 1000) {
this.results[0] = (currentPower / 1000).toFixed(2) + " kW";
} else {
this.results[0] = currentPower + " Watt";
}
this.results[1] = (payload.overview.lastDayData.energy / 1000).toFixed(2) + " kWh";
this.results[2] = (payload.yesterday / 1000).toFixed(2) + " kWh";
this.results[3] = (payload.overview.lastMonthData.energy / 1000).toFixed(2) + " kWh";
this.loaded = true;
this.updateDom(1000);
}
},
// Override dom generator.
getDom: function() {
let wrapper = document.createElement("div");
if (this.config.apiKey === "" || this.config.siteId === "") {
wrapper.innerHTML = "No configuration!";
return wrapper;
}
//Display loading while waiting for API response
if (!this.loaded) {
wrapper.innerHTML = this.getLangStrings(this.config.language, 'loading');
return wrapper;
}
let tb = document.createElement("table");
if (!this.config.basicHeader) {
let imgDiv = document.createElement("div");
let img = document.createElement("img");
img.align = "absmiddle";
img.src = "/modules/MMM-SolarEdgeLite/solar_white.png";
let sTitle = document.createElement("p");
sTitle.innerHTML = this.getLangStrings(this.config.language, 'title');
sTitle.className += " thin normal";
imgDiv.appendChild(img);
imgDiv.appendChild(sTitle);
let divider = document.createElement("hr");
divider.className += " dimmed";
wrapper.appendChild(imgDiv);
wrapper.appendChild(divider);
}
for (let i = 0; i < this.results.length; i++) {
let row = document.createElement("tr");
let titleTr = document.createElement("td");
let dataTr = document.createElement("td");
titleTr.innerHTML = this.titles[i];
dataTr.innerHTML = this.results[i] + " " + this.suffixes[i];
dataTr.innerHTML = this.results[i];
titleTr.className += " small regular bright";
dataTr.className += " small light normal";
row.appendChild(titleTr);
row.appendChild(dataTr);
tb.appendChild(row);
}
wrapper.appendChild(tb);
return wrapper;
}
});