forked from christopherpthomas/MMM-OctoMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-OctoMon.js
356 lines (293 loc) · 10.3 KB
/
MMM-OctoMon.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
//MMM-OctoMon.js:
/* Magic Mirror
* Module: MMM-OctoMon
*
* By Chris Thomas
* MIT Licensed.
*/
Module.register("MMM-OctoMon",{
// Default module config.
defaults: {
elecApiUrl: "",
gasApiUrl: "",
api_key: "",
updateInterval: 60000*60,
displayDays: 7,
elecMedium: 10,
elecHigh: 20,
elecCostKWH: 0.1372,
elecCostSC: 0.25,
gasMedium: 0.5,
gasHigh: 1,
gasCostKWH: 0.0331,
gasCostSC: 0.168,
decimalPlaces: 2,
showUpdateTime: true,
retryDelay: 5000,
animationSpeed: 2000,
},
start: function() {
Log.log("start()");
var self = this;
var elecDataRequest=null;
var gasDataRequest=null;
this.elecLoaded=false;
this.gasLoaded=false;
this.getElecData(2);
this.getGasData(2);
setInterval(function() {
self.getElecData(2);
self.getGasData(2);
}, this.config.updateInterval);
},
getElecData: function(retries) {
Log.log("getElecData(retries=" + retries + ")");
var self = this;
var hash = btoa(this.config.api_key + ":");
if(this.config.elecApiUrl!="")
{
var elecDataRequest = new XMLHttpRequest();
elecDataRequest.open("GET", this.config.elecApiUrl, true);
elecDataRequest.setRequestHeader("Authorization","Basic " + hash);
elecDataRequest.onreadystatechange = function() {
Log.log("getElecData() readyState=" + this.readyState);
if (this.readyState === 4) {
Log.log("getElecData() status=" + this.status);
if (this.status === 200) {
self.processElecData(JSON.parse(this.response));
retries=0;
} else if (this.status === 401) {
self.elecLoaded = false;
self.updateDom(self.config.animationSpeed);
Log.error(self.name, "getElecData() 401 error. status=" + this.status);
} else {
self.elecLoaded = false;
self.updateDom(self.config.animationSpeed);
Log.error(self.name, "getElecData() Could not load data. status=" + this.status);
}
if (retries>0) {
retries=retries-1;
self.scheduleElecRetry(retries);
}
}
};
elecDataRequest.send();
}
},
getGasData: function(retries) {
Log.log("getGasData(retries=" + retries + ")");
var self = this;
var hash = btoa(this.config.api_key + ":");
if(this.config.gasApiUrl!="")
{
var gasDataRequest = new XMLHttpRequest();
gasDataRequest.open("GET", this.config.gasApiUrl, true);
gasDataRequest.setRequestHeader("Authorization","Basic " + hash);
gasDataRequest.onreadystatechange = function() {
Log.log("getGasData() readyState=" + this.readyState);
if (this.readyState === 4) {
Log.log("getGasData() status=" + this.status);
if (this.status === 200) {
self.processGasData(JSON.parse(this.response));
retries=0;
} else if (this.status === 401) {
self.gasLoaded = false;
self.updateDom(self.config.animationSpeed);
Log.error(self.name, "getGasData() 401 error. " + this.status);
} else {
self.gasLoaded = false;
self.updateDom(self.config.animationSpeed);
Log.error(self.name, "getGasData() Could not load data. status=" + this.status);
}
if (retries>0) {
retries=retries-1;
self.scheduleGasRetry(retries);
}
}
};
gasDataRequest.send();
}
},
scheduleElecRetry: function(retries) {
Log.log("scheduleElecRetry() retries=" + retries);
var self = this;
setTimeout(function() {
self.getElecData(retries);
}, self.config.retryDelay);
},
scheduleGasRetry: function(retries) {
Log.log("scheduleGasRetry() retries=" + retries);
var self = this;
setTimeout(function() {
self.getGasData(retries);
}, self.config.retryDelay);
},
// Override dom generator.
getDom: function() {
Log.log("getDom()");
var wrapper = document.createElement("div");
var errors = "";
if ((this.config.gasApiUrl === "" || typeof this.config.gasApiUrl === 'undefined') && (this.config.elecApiUrl === "" || typeof this.config.elecApiUrl === 'undefined')) {
errors = errors + "Both gasApiUrl and elecApiUrl not set in config. At least one required.</br>";
}
if (this.config.api_key === "") {
errors = errors + "API Key (api_key) not set in config.</br>";
}
if(errors != "") {
wrapper.innerHTML = errors;
wrapper.className = "dimmed light small";
return wrapper;
}
if(this.elecLoaded == false && this.gasLoaded==false) {
wrapper.innerHTML = "Querying Server...";
wrapper.className = "dimmed light small";
return wrapper;
}
var table = document.createElement("table");
table.className="small";
var headerrow = document.createElement("tr");
var headerdatelabel = document.createElement("td");
headerdatelabel.innerHTML = ""; //or you could display a date column header: "<span class=\"fa fa-calendar-alt small\"></span> Date";
headerdatelabel.className = "small";
headerdatelabel.style.verticalAlign = "top";
headerdatelabel.style.textAlign = "center";
var headereleclabel = document.createElement("td");
headereleclabel.innerHTML = "<span class=\"fa fa-plug small\"></span> Elec";
headereleclabel.className = "small";
headereleclabel.style.verticalAlign = "top";
headereleclabel.style.textAlign = "center";
var headergaslabel = document.createElement("td");
headergaslabel.innerHTML = "<span class=\"fa fa-burn small\"></span> Gas";
headergaslabel.className = "small";
headergaslabel.style.verticalAlign = "top";
headergaslabel.style.textAlign = "center";
headerrow.appendChild(headerdatelabel);
if(this.elecDataRequest) headerrow.appendChild(headereleclabel);
if(this.gasDataRequest) headerrow.appendChild(headergaslabel);
table.appendChild(headerrow);
var i=0;
var intLoop=0;
var intDays=this.config.displayDays; //how many days of history to show
var dteLoop = new Date();//start today and go backwards
if(true)
{
//if true, actually, start from first day's worth of available data
//the api only seems to be able to return data from two days ago
//so this skips over 'today' and 'yesterday' that have no displayable data yet
var elecdate;
if (this.elecDataRequest) {
if(typeof this.elecDataRequest.results[0] !== 'undefined') {
elecdate = new Date(this.elecDataRequest.results[0].interval_start);
}
}
var gasdate;
if (this.gasDataRequest) {
if(typeof this.gasDataRequest.results[0] !== 'undefined') {
gasdate = new Date(this.gasDataRequest.results[0].interval_start);
}
}
if(typeof elecdate == 'undefined') {
elecdate = new Date();
}
if(typeof gasdate == 'undefined') {
gasdate = new Date();
}
//which is the closest date to today? start the loop there.
if(elecdate>=gasdate)
{
dteLoop=elecdate;
}
else
{
dteLoop=gasdate;
}
}
var strDays=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
for(intLoop=0;intLoop<intDays;intLoop++)
{
var thisrow = document.createElement("tr");
var thisdatelabel = document.createElement("td");
thisdatelabel.innerHTML = strDays[dteLoop.getDay()] + " " + dteLoop.toLocaleDateString();
thisdatelabel.className = "small";
var thiseleclabel = document.createElement("td");
thiseleclabel.innerHTML = "---";
thiseleclabel.className = "small";
thiseleclabel.style.textAlign = "center";
var thisgaslabel = document.createElement("td");
thisgaslabel.innerHTML = "---";
thisgaslabel.className = "small";
thisgaslabel.style.textAlign = "center";
//we're looking for gas and elec results for this day
if (this.elecDataRequest) {
for(i=0;i<intDays;i++) {
if(typeof this.elecDataRequest.results[i] !== 'undefined') {
var edate = new Date(this.elecDataRequest.results[i].interval_start);
if(edate.toLocaleDateString() == dteLoop.toLocaleDateString()) {
var strCol = "white";//could be green
var intVal = this.elecDataRequest.results[i].consumption.toFixed(this.config.decimalPlaces);
if(intVal>=this.config.elecMedium)strCol="color:orange";
if(intVal>=this.config.elecHigh)strCol="color:red";
strUse = intVal + " kWh";
strCost="";
if(this.config.elecCostKWH>0)
strCost = "£" + (Math.round(((intVal * this.config.elecCostKWH)+this.config.elecCostSC) * 100)/100).toFixed(2);
//display electricity energy usage and cost here
thiseleclabel.innerHTML = " <span style=\"" + strCol + "\">" + strUse + " " + strCost + "</span>";
thiseleclabel.style.textAlign = "right";
}
}
}
}
if (this.gasDataRequest) {
for(i=0;i<intDays;i++) {
if(typeof this.gasDataRequest.results[i] !== 'undefined') {
var edate = new Date(this.gasDataRequest.results[i].interval_start);
if(edate.toLocaleDateString() == dteLoop.toLocaleDateString()) {
var strCol = "white";//could be green
var intVal = this.gasDataRequest.results[i].consumption.toFixed(this.config.decimalPlaces);
if(intVal>=this.config.gasMedium)strCol="color:orange";
if(intVal>=this.config.gasHigh)strCol="color:red";
strUse = intVal + " kWh";
strCost = "";
if(this.config.gasCostKWH>0)
strCost = "£" + (Math.round(((intVal * this.config.gasCostKWH)+this.config.gasCostSC) * 100)/100).toFixed(2);
//display gas energy usage and cost here
thisgaslabel.innerHTML = " <span style=\"" + strCol + "\">" + strUse + " " + strCost + "</span>";
thisgaslabel.style.textAlign = "right";
}
}
}
}
thisrow.appendChild(thisdatelabel);
if(this.elecDataRequest) thisrow.appendChild(thiseleclabel);
if(this.gasDataRequest) thisrow.appendChild(thisgaslabel);
table.appendChild(thisrow);
dteLoop.setDate(dteLoop.getDate() - 1); //go back to the next day
}
wrapper.appendChild(table);
return wrapper;
},
getHeader: function() {
var adate = new Date();
//Log.log("getHeader() " + adate.toLocaleTimeString());
if(this.config.showUpdateTime == true) {
return this.data.header + " " + adate.toLocaleTimeString();
} else {
return this.data.header;
}
},
processElecData: function(data) {
Log.log("processElecData()");
var self = this;
this.elecDataRequest = data;
this.elecLoaded = true;
self.updateDom(self.config.animationSpeed);
},
processGasData: function(data) {
Log.log("processGasData()");
var self = this;
this.gasDataRequest = data;
this.gasLoaded = true;
self.updateDom(self.config.animationSpeed);
},
});