-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-EnergyMonitor.js
332 lines (268 loc) · 11.1 KB
/
MMM-EnergyMonitor.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
"use strict";
/* Magic Mirror
* Module: MMM-EnergyMonitor
*
* By Beh (hello@beh.wtf)
* MIT Licensed.
*/
Module.register("MMM-EnergyMonitor", {
defaults: {
name: "MMM-EnergyMonitor",
hidden: false,
updateInterval: 3000,
energyStorage: true,
width: "600px",
height: "500px",
lineWidth: "7px",
resetCycles: 4,
logNotifications: false,
wattConversionOptions: {
enabled: true,
threshold: 1200,
numDecimalDigits: 2,
},
iconCssClasses: {
home: "fas fa-home",
grid: "fas fa-plug",
energyStorage: "fas fa-battery-half",
localPowerSource: "fas fa-solar-panel"
}
},
requiresVersion: "2.17.0", // Required version of MagicMirror
start: function () {
this.currentData = {
battery: 0,
home: 0,
grid: 0,
solar: 0
};
this.resetCounter = {
battery: 0,
grid: 0,
solar: 0
};
Log.log("MMM-EnergyMonitor started.");
this.scheduleUpdate();
this.loaded = true;
},
scheduleUpdate: function () {
setInterval(() => {
this.updateDom();
this.trackValueReset();
}, this.config.updateInterval);
},
trackValueReset: function() {
for (const dataType in this.resetCounter) {
if (this.resetCounter.hasOwnProperty(dataType)) {
if(this.resetCounter[dataType] === this.config.resetCycles) {
this.currentData[dataType] = 0;
this.resetCounter[dataType] = 0;
}
this.resetCounter[dataType] += 1;
}
}
},
getDom: function () {
// create element wrapper for show into the module
const wrapper = document.createElement("div");
wrapper.id = "energymonitor-wrapper";
wrapper.style.setProperty("--width", this.config.width);
wrapper.style.setProperty("--height", this.config.height);
wrapper.style.setProperty("--line-width", this.config.lineWidth);
this.addIcons(wrapper);
const solarLine = this.generateSolarLine();
wrapper.appendChild(solarLine);
const homeLine = this.generateHomeLine();
wrapper.appendChild(homeLine);
const gridLine = this.generateGridLine();
wrapper.appendChild(gridLine);
if(this.config.energyStorage) {
const batteryLine = this.generateBatteryLine();
wrapper.appendChild(batteryLine);
}
return wrapper;
},
addIcons: function(wrapper) {
// Solar Icon
const solarPanel = document.createElement("div");
solarPanel.className = "icon horizontal left";
const solarPanelIcon = document.createElement("i");
solarPanelIcon.className = this.config.iconCssClasses.localPowerSource;
solarPanel.appendChild(solarPanelIcon);
wrapper.appendChild(solarPanel);
// Home Icon
const home = document.createElement("div");
home.className = "icon vertical top";
const homeIcon = document.createElement("i");
homeIcon.className = this.config.iconCssClasses.home;
home.appendChild(homeIcon);
wrapper.appendChild(home);
// Plug Icon
const grid = document.createElement("div");
grid.className = "icon horizontal right";
const gridIcon = document.createElement("i");
gridIcon.className = this.config.iconCssClasses.grid;
grid.appendChild(gridIcon);
wrapper.appendChild(grid);
// Battery Icon
const battery = document.createElement("div");
battery.className = "icon vertical bottom";
const batteryIcon = document.createElement("i");
batteryIcon.className = this.config.iconCssClasses.energyStorage;
battery.appendChild(batteryIcon);
wrapper.appendChild(battery);
},
generateSolarLine: function() {
const solarLine = document.createElement("div");
solarLine.classList.add("line", "horizontal", "left");
const solarLabel = document.createElement("div");
solarLabel.id = "solar-label";
solarLabel.classList.add("label");
solarLabel.innerHTML = `${this.getWattString(this.currentData.solar)} <br>`;
solarLabel.innerHTML += this.translate("SOLAR_PRODUCING");
solarLine.appendChild(solarLabel);
if(this.currentData.solar > 0) {
solarLine.classList.add("active");
const solarArrowOut = document.createElement("i");
solarArrowOut.classList.add("arrow", "right", "active");
solarLine.appendChild(solarArrowOut);
} else {
solarLine.classList.add("dimmed");
}
return solarLine;
},
generateHomeLine: function() {
this.calculateHomeConsumption();
const homeLine = document.createElement("div");
homeLine.classList.add("line", "vertical", "up");
const homeLabel = document.createElement("div");
homeLabel.id = "home-label";
homeLabel.classList.add("label");
homeLabel.innerHTML = `${this.getWattString(this.currentData.home)}<br>`;
homeLabel.innerHTML += this.translate("HOME_CONSUMPTION");
homeLine.appendChild(homeLabel);
if(this.currentData.home > 0) {
homeLine.classList.add("active");
const homeArrowIn = document.createElement("div");
homeArrowIn.classList.add("arrow", "up", "active");
homeLine.appendChild(homeArrowIn);
} else {
homeLine.classList.add("dimmed");
}
return homeLine;
},
generateGridLine: function() {
const gridLine = document.createElement("div");
gridLine.classList.add("line", "horizontal", "right");
if(this.currentData.grid !== 0)
gridLine.classList.add("active");
const gridLabel = document.createElement("div");
gridLabel.id = "grid-label";
gridLabel.classList.add("label");
gridLabel.innerHTML = `${this.getWattString(Math.abs(this.currentData.grid))}<br>`;
gridLine.appendChild(gridLabel);
// Positive value means feeding to grid
if(this.currentData.grid > 0) {
gridLabel.innerHTML += this.translate("GRID_BACKFEEDING");
gridLabel.classList.add("font-green");
const gridArrowOut = document.createElement("div");
gridArrowOut.classList.add("arrow", "right", "active");
gridLine.appendChild(gridArrowOut);
} else if(this.currentData.grid < 0) {
gridLabel.innerHTML += this.translate("GRID_CONSUMPTION");
gridLabel.classList.add("font-red");
const gridArrowIn = document.createElement("div");
gridArrowIn.classList.add("arrow", "left", "active");
gridLine.appendChild(gridArrowIn);
} else {
gridLabel.innerHTML += this.translate("GRID_IDLE");
gridLine.classList.add("dimmed");
}
return gridLine;
},
generateBatteryLine: function() {
const batteryLine = document.createElement("div");
batteryLine.classList.add("line", "vertical", "down");
const batteryLabel = document.createElement("div");
batteryLabel.id = "battery-label";
batteryLabel.classList.add("label");
batteryLabel.innerHTML = `${this.getWattString(Math.abs(this.currentData.battery))}<br>`;
batteryLine.appendChild(batteryLabel);
if(this.currentData.battery !== 0)
batteryLine.classList.add("active");
// Positive value means charging battery
if(this.currentData.battery > 0) {
batteryLabel.innerHTML += this.translate("BATTERY_CHARGING");
batteryLabel.classList.add("font-green");
const batteryArrowIn = document.createElement("i");
batteryArrowIn.classList.add("fas", "fa-caret-down", "arrow", "down", "active");
batteryLine.appendChild(batteryArrowIn);
} else if(this.currentData.battery < 0) {
batteryLabel.innerHTML += this.translate("BATTERY_DISCHARGING");
batteryLabel.classList.add("font-red");
const batteryArrowOut = document.createElement("i");
batteryArrowOut.classList.add("fas", "fa-caret-up", "arrow", "up", "active");
batteryLine.appendChild(batteryArrowOut);
} else {
batteryLabel.innerHTML += this.translate("BATTERY_IDLE");
batteryLine.classList.add("dimmed");
}
return batteryLine;
},
calculateHomeConsumption: function() {
this.currentData.home = this.currentData.solar - this.currentData.grid - this.currentData.battery;
},
getWattString: function (value) {
const wattConversionOptions = this.config.wattConversionOptions;
if (wattConversionOptions.enabled && value > wattConversionOptions.threshold) {
return `${(value / 1000).toFixed(wattConversionOptions.numDecimalDigits)} KW`;
}
return `${Math.round(value)} W`;
},
getStyles: function () {
return ["MMM-EnergyMonitor.css", "fontawesome.css"];
},
// Load translations files
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json",
};
},
validateNumberPayload(notification, payload, sender, ) {
if(typeof payload !== "number") {
if(this.logNotifications)
Log.log(`EnergyMonitor received data that is NaN: ${payload} from sender: ${sender.name} via notification: ${notification}`);
return false;
} else {
if(this.logNotifications)
Log.log(`EnergyMonitor received data: ${payload} from sender: ${sender.name} via notification: ${notification}`);
return true;
}
},
notificationReceived(notification, payload, sender) {
if(!this.loaded)
return;
// Unit: Watt | negative: discharge | positive: charge
if (notification === "MMM-EnergyMonitor_ENERGY_STORAGE_POWER_UPDATE") {
if(!this.validateNumberPayload(notification, payload, sender))
return;
this.currentData.battery = payload;
this.resetCounter.battery = 0;
}
// Unit: Watt | negative: consume from grid | positive: feed to grid
if (notification === "MMM-EnergyMonitor_GRID_POWER_UPDATE") {
if(!this.validateNumberPayload(notification, payload, sender))
return;
this.currentData.grid = payload;
this.resetCounter.grid = 0;
}
// Unit: Watt | cannot be negative
if (notification === "MMM-EnergyMonitor_SOLAR_POWER_UPDATE") {
if(!this.validateNumberPayload(notification, payload, sender))
return;
this.currentData.solar = payload < 0 ? 0 : payload ;
this.resetCounter.solar = 0;
}
},
});