-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-EnphaseSolar.js
184 lines (155 loc) · 6.75 KB
/
MMM-EnphaseSolar.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
/*
* Magic Mirror
* Module: MMM-EnphaseSolar
* By Matt Thurling
*
* MIT Licensed.
*/
Module.register("MMM-EnphaseSolar",{
defaults: {
gatewayHost: "",
token: "",
refreshInterval: 1000 * 60 * 1, // 1 minute
displayCurrentProduction: true,
displayCurrentUsage: true,
displayNetOutput: true,
displayTodaysProduction: true,
displayTodaysUsage: true,
displayLastUpdate: true,
displayLastUpdateFormat: "ddd HH:mm:ss",
debug: false,
},
start: function() {
Log.info("Starting module: " + this.name);
this.currentProduction = {
title: this.translate('CURRENT_PRODUCTION') + ":",
suffix: this.translate('SUFFIX_KILOWATT'),
value: this.translate('LOADING')
};
this.currentUsage = {
title: this.translate('CURRENT_USAGE') + ":",
suffix: this.translate('SUFFIX_KILOWATT'),
value: this.translate('LOADING')
};
this.netOutput = {
importingTitle: this.translate('IMPORTING') + ":",
exportingTitle: this.translate('EXPORTING') + ":",
suffix: this.translate('SUFFIX_KILOWATT'),
value: this.translate('LOADING')
};
this.todaysProduction = {
title: this.translate('PRODUCED_TODAY') + ":",
suffix: this.translate('SUFFIX_KILOWATTHOUR'),
value: this.translate('LOADING')
};
this.todaysUsage = {
title: this.translate('USED_TODAY') + ":",
suffix: this.translate('SUFFIX_KILOWATTHOUR'),
value: this.translate('LOADING')
}
this.lastUpdated = Date.now() / 1000;
this.loaded = false;
this.getEnphaseSolarData();
var self = this;
setInterval(function() {
self.getEnphaseSolarData();
self.updateDom();
}, this.config.refreshInterval);
},
//Import additional CSS Styles
getStyles: function() {
return ['MMM-EnphaseSolar.css']
},
getEnphaseSolarData: function() {
Log.info("MMM-EnphaseSolar: getting data");
this.sendSocketNotification("GET_ENPHASE_SOLAR", {
config: this.config,
sessionId: this.sessionId
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === "ENPHASE_SOLAR_DATA") {
this.sessionId = payload.sessionId;
for (const productionData of payload.production) {
if (productionData.type === "eim") {
// current production can be slightly negative when nothing is being produced so zero it in that case
this.currentProduction.value = productionData.wNow < 0 ? 0 : (productionData.wNow / 1000).toFixed(2);
this.todaysProduction.value = (productionData.whToday / 1000).toFixed(2);
this.lastUpdated = productionData.readingTime;
}
}
for (const consumptionData of payload.consumption) {
if (consumptionData.measurementType === "total-consumption") {
this.currentUsage.value = (consumptionData.wNow / 1000).toFixed(2);
this.todaysUsage.value = (consumptionData.whToday / 1000).toFixed(2);
} else if (consumptionData.measurementType === "net-consumption") {
this.netOutput.value = (consumptionData.wNow / 1000).toFixed(2);
}
}
this.loaded = true;
this.updateDom();
}
},
getDom: function() {
var wrapper = document.createElement("div");
if (!this.config.gatewayHost || !this.config.token) {
wrapper.innerHTML = this.translate('ERROR_MISSING_CONFIG');
return wrapper;
}
//Display loading while waiting for API response
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
return wrapper;
}
var tableElement = document.createElement("table");
if (this.config.displayCurrentProduction) {
tableElement.appendChild(this.addRow(this.currentProduction.title, this.currentProduction.value, this.currentProduction.suffix, 'current-production'));
}
if (this.config.displayCurrentUsage) {
tableElement.appendChild(this.addRow(this.currentUsage.title, this.currentUsage.value, this.currentUsage.suffix, 'current-usage'));
}
if (this.config.displayNetOutput) {
var netOutputTitle;
var netOutputClass;
if (this.netOutput.value > 0) {
netOutputTitle = this.netOutput.importingTitle;
netOutputClass = 'net-output-importing';
} else {
netOutputTitle = this.netOutput.exportingTitle;
netOutputClass = 'net-output-exporting';
}
tableElement.appendChild(this.addRow(netOutputTitle, Math.abs(this.netOutput.value), this.netOutput.suffix, netOutputClass));
}
if (this.config.displayTodaysProduction) {
tableElement.appendChild(this.addRow(this.todaysProduction.title, this.todaysProduction.value, this.todaysProduction.suffix, 'todays-production'));
}
if (this.config.displayTodaysUsage) {
tableElement.appendChild(this.addRow(this.todaysUsage.title, this.todaysUsage.value, this.todaysUsage.suffix, 'todays-usage'));
}
wrapper.appendChild(tableElement);
if (this.config.displayLastUpdate) {
var updateinfo = document.createElement("div");
updateinfo.className = "xsmall light";
updateinfo.innerHTML = this.translate('LAST_UPDATED') + ": " + moment.unix(this.lastUpdated).format(this.config.displayLastUpdateFormat);
wrapper.appendChild(updateinfo);
}
return wrapper;
},
addRow: function(title, value, suffix, className) {
var rowElement = document.createElement("tr");
var titleElement = document.createElement("td");
titleElement.innerHTML = title;
titleElement.className = "title-" + className + " medium regular bright";
rowElement.appendChild(titleElement);
var dataElement = document.createElement("td");
dataElement.innerHTML = value + " " + suffix;
dataElement.className = "data-" + className + " medium light normal";
rowElement.appendChild(dataElement);
return rowElement;
},
getTranslations() {
return {
en: 'translations/en.json',
};
},
});