-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-VartaESS.js
212 lines (172 loc) · 7.01 KB
/
MMM-VartaESS.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
"use strict";
/* global Module */
/* Magic Mirror
* Module: MMM-VartaESS
*
* By Beh (hello@beh.wtf)
* MIT Licensed.
*/
Module.register("MMM-VartaESS", {
defaults: {
name: "MMM-VartaESS",
header: "Energy Storage",
hidden: false,
ip: "192.168.0.55",
port: 502,
clientId: 10,
updateInterval: 3000,
width: 250,
showBatteryDisplay: true,
colors: true,
wattConversionOptions: {
enabled: true,
threshold: 1200,
numDecimalDigits: 2,
},
broadcastBatteryPower: false,
broadcastGridPower: false,
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
this.data.header = this.config.header;
this.currentData = null;
this.scheduleTimer = null;
this.loaded = false;
this.error = null;
this.sendSocketNotification("MMM-VartaESS_INIT", this.config);
Log.info("MMM-VartaESS started.");
},
getDom: function () {
// create element wrapper for show into the module
const wrapper = document.createElement("div");
wrapper.id = "vartaess-wrapper";
wrapper.style.width = `${this.config.width}px`;
if (this.currentData === null && !this.loaded) {
wrapper.className = "small light dimmed";
wrapper.innerHTML = `${this.translate("LOADING")}...`;
return wrapper;
}
if (this.error !== null && !this.loaded) {
wrapper.className = "small light dimmed";
wrapper.innerHTML = `${this.translate(this.error)}...`;
return wrapper;
}
if (this.config.showBatteryDisplay) {
const batteryDisplay = this.getBatteryDisplay();
wrapper.appendChild(batteryDisplay);
}
// Table for displaying Values
const tableWrapper = document.createElement("div");
tableWrapper.id = "table-wrapper";
const table = this.generateDataTable();
tableWrapper.appendChild(table);
wrapper.appendChild(tableWrapper);
return wrapper;
},
generateDataTable: function () {
const table = document.createElement("table");
const stateDescription = `${this.translate("STATE")}:`;
let stateValue = this.translate(this.currentData.state);
if (this.error !== null) {
stateValue = this.translate(this.error);
}
this.appendTableRow(stateDescription, stateValue, table);
const socDescription = `${this.translate("CHARGE")}:`;
const socValue = `${this.currentData.soc} %`;
this.appendTableRow(socDescription, socValue, table);
const gridPowerDescription = `${this.translate("GRID")}: `;
const gpValue = this.currentData.gridPower;
const gridPowerValue = `${this.getWattString(Math.abs(gpValue))} (${
gpValue < 0 ? this.translate("CONSUMPTION_FROM_GRID") : this.translate("BACKFEED_TO_GRID")
})`;
this.appendTableRow(gridPowerDescription, gridPowerValue, table);
const activePowerDescription = `${this.translate("BATTERY")}:`;
let batteryChargingStateLabel = "";
const apValue = this.currentData.activePower;
if (apValue !== 0) {
batteryChargingStateLabel = ` (${
apValue < 0 ? this.translate("BATTERY_DISCHARGE") : this.translate("BATTERY_CHARGE")
})`;
}
const activePowerValue = `${this.getWattString(Math.abs(apValue))}${batteryChargingStateLabel}`;
this.appendTableRow(activePowerDescription, activePowerValue, table);
return table;
},
appendTableRow: function (description, value, table) {
const row = document.createElement("tr");
const descriptionColumn = document.createElement("td");
descriptionColumn.textContent = description;
row.appendChild(descriptionColumn);
const valueColumn = document.createElement("td");
valueColumn.textContent = value;
row.appendChild(valueColumn);
table.appendChild(row);
},
getBatteryDisplay: function () {
const batteryWrapper = document.createElement("div");
batteryWrapper.id = "battery-wrapper";
// Battery should have 85% of module width
const wrapperWidth = Math.round(this.config.width * 0.85);
batteryWrapper.style.width = `${wrapperWidth}px`;
const batteryState = document.createElement("div");
batteryState.id = "battery-state";
const soc = this.currentData.soc;
if (this.config.colors) {
if (soc < 25) {
batteryState.classList.add("battery-state-red");
} else if (soc < 75) {
batteryState.classList.add("battery-state-yellow");
} else {
batteryState.classList.add("battery-state-green");
}
}
// Internal display should also narrower than battery to preserve internal border
// Calculation from right: 8px "battery nose" + 4px border + 5px internal border = 17px
const maxWidth = wrapperWidth - 18; // Dunno, why 18 seems to look light. 1px got lost somewhere, maybe while rounding
const factor = soc / 100;
const width = Math.round(maxWidth * factor);
batteryState.style.width = `${width}px`;
// We will leave this here for debugging
// Log.info(`Module width: ${this.config.width} | wrapper width: ${wrapperWidth} | width: ${width} | maxWidth: ${maxWidth} | factor: ${factor}`);
batteryWrapper.appendChild(batteryState);
return batteryWrapper;
},
getWattString: function (value) {
const wattConversionOptions = this.config.wattConversionOptions;
if (wattConversionOptions.enabled && value > wattConversionOptions.threshold) {
return `${(value / 1000).toFixed(wattConversionOptions.numDecimalDigits)} KW`;
}
return `${value} W`;
},
getStyles: function () {
return ["MMM-VartaESS.css"];
},
// Load translations files
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json",
};
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-VartaESS_INITIALIZED") {
this.loaded = true;
}
if (notification === "MMM-VartaESS_ERROR") {
this.error = payload;
this.updateDom();
}
if (notification === "MMM-VartaESS_DATA") {
if (this.config.broadcastBatteryPower) {
this.sendNotification("MMM-EnergyMonitor_ENERGY_STORAGE_POWER_UPDATE", payload.activePower);
}
if (this.config.broadcastGridPower) {
this.sendNotification("MMM-EnergyMonitor_GRID_POWER_UPDATE", payload.gridPower);
}
this.error = null;
this.currentData = payload;
this.updateDom();
}
},
});