-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Hive.js
492 lines (454 loc) · 16.6 KB
/
MMM-Hive.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/* global Module */
/* Magic Mirror
* Module: MMM-Hive
*
* By Stuart McNally
* MIT Licensed.
*/
Module.register('MMM-Hive',{
defaults: {
animationSpeed: 1000,
updateInterval: 10 * 60000, //update temperature every 10 minutes, value in milliseconds
refreshInterval: 1000 * 60 * 10, //refresh once a minute
lang: config.language,
initialLoadDelay: 0,
username: '',
password: '',
postcode: '',
insideText: 'Inside:',
outsideText: 'Outside:',
targetTempText: 'Target Temperature:',
hotWaterText: 'Hot Water:',
thermBattText: 'Thermostat Battery:',
insideIconSet: 'fa fa-home',
hBoostOffText: 'Not Active',
hwBoostOffText: 'Off',
boostRow: false,
showNext: true,
showHotWater: false,
showBattery: false,
batteryIcon: false,
highestTemp: '30',
highTemp: '25',
lowTemp: '20',
lowestTemp: '15',
animatedLoading: true,
temperatureSuffix: '°C',
nodeName: 'heating',
outsideUrl: 'http://weather-prod.bgchprod.info/weather?postcode=',
insideUrl: 'https://beekeeper-uk.hivehome.com/1.0',
loginUrl: 'https://beekeeper-uk.hivehome.com/1.0',
loginUrlPlus: '/cognito/refresh-token',
insideUrlProducts: '/products',
insideUrlDevices: '/devices',
debug: false,
token: '',
jsonLocation: '/home/pi/node_modules/amazon-user-pool-srp-client/HiveTokens.json',
hiveHeaders: {
'Content-Type': 'application/json',
}
},
getStyles: function() {
return ["font-awesome.css","MMM-Hive.css"];
},
start: function() {
Log.info('Starting module: ' + this.name);
moment.locale(config.language);
this.outsideT = null;
this.insideT = null;
this.insideS = null;
this.insideTarget = null;
this.hotStateT = null;
this.batteryT = null;
this.hwBoost = null;
this.hBoost = null;
this.loaded = false;
this.error = false;
this.error401 = false;
this.errorPostcode = false;
this.dataID = null;
this.sendSocketNotification('CONFIG', this.config);
this.sendSocketNotification('DATA', null);
this.dataTimer();
},
dataTimer: function() {
var self = this;
Log.log("dataTime Started")
this.dataID = setInterval(function() { self.sendSocketNotification('DATA', null); }, this.config.updateInterval);
},
processData: function(FLAG, result) {
var self = this;
if (this.loaded === false) { self.updateDom(self.config.animationSpeed) ; }
this.loaded = true;
if (FLAG === "INSIDE"){
for (var i=0; i<result.length; i++) {
if (result[i].type === this.config.nodeName) {
var insideTemp = result[i].props.temperature;
this.insideT = insideTemp;
var heatingState = result[i].state.status || "unknown";
this.insideS = heatingState;
var heatingBoost = result[i].state.boost || this.config.hBoostOffText;
this.hBoost = heatingBoost;
var heatingTarget = targetHeatTemperature = result[i].state.target || "unknown";
this.insideTarget = heatingTarget
//var hotState = result[1].state.status;
//this.hotStateT = hotState
}
if (result[i].type === 'hotwater') {
var hotState = result[i].state.status;
this.hotStateT = hotState
var hotWaterBoost = result[i].state.boost || this.config.hwBoostOffText;
this.hwBoost = hotWaterBoost;
}
}}
else if (FLAG === "DEVICES"){
for (var i=0; i<result.length; i++) {
if (result[i].type === 'thermostatui') {
var battery = result[i].props.battery;
this.batteryT = battery;
}}
}
else if (FLAG === "OUTSIDE"){
var outsideTemp = result[0].weather.temperature.value;
this.outsideT = outsideTemp;
}
},
getDom: function() {
var information = document.createElement("div");
if (this.error401) {
information.innerHTML = "Error logging into the Hive website.</br>Please ensure that your Hive Username / Password</br>are set in the config.js file.";
information.className = "light small";
return information;
}
if (this.errorPostcode) {
information.innerHTML = "Error logging into the Hive website.</br>Please ensure that your Post Code</br>is set and correct in the config.js file.";
information.className = "light small";
return information;
}
if (!this.loaded) {
if (this.config.animatedLoading == true) {
information.className = "fa fa-spinner fa-pulse fa-3x fa-fw light small";
return information;
}
else {
information.innerHTML = "Loading...";
information.className = "light small";
return information;
}
}
// if (this.error) {
// information.innerHTML = "Error loading data";
// information.className = "light small";
// return information;
// }
var table = document.createElement("table");
//heating
var temperatureRow = document.createElement("tr");
var outsideIcon = document.createElement("td");
if (this.outsideT >= this.config.highestTemp) {
outsideIcon.setAttribute("aria-hidden","true");
outsideIcon.className = "fa fa-thermometer-full small dimmed";
outsideIcon.style.cssText="color:red;";
temperatureRow.appendChild(outsideIcon);
}
else if (this.outsideT >= this.config.highTemp) {
outsideIcon.setAttribute("aria-hidden","true");
outsideIcon.className = "fa fa-thermometer-three-quarters small dimmed";
temperatureRow.appendChild(outsideIcon);
}
else if (this.outsideT >= this.config.lowTemp) {
outsideIcon.setAttribute("aria-hidden","true");
outsideIcon.className = "fa fa-thermometer-half small dimmed";
temperatureRow.appendChild(outsideIcon);
}
else if (this.outsideT >= this.config.lowestTemp) {
outsideIcon.setAttribute("aria-hidden","true");
outsideIcon.className = "fa fa-thermometer-quarter small dimmed";
temperatureRow.appendChild(outsideIcon);
}
else if (this.outsideT < this.config.lowestTemp) {
outsideIcon.setAttribute("aria-hidden","true");
outsideIcon.className = "fa fa-thermometer-empty small dimmed";
temperatureRow.appendChild(outsideIcon);
}
var outsideCell = document.createElement("td");
outsideCell.innerHTML = this.config.outsideText + " " + this.outsideT + this.config.temperatureSuffix;
outsideCell.className = "small light";
outsideCell.style.cssText="color:white;";
temperatureRow.appendChild(outsideCell)
var buffer = document.createElement("td");
buffer.innerHTML = " "
temperatureRow.appendChild(buffer)
var onOffIcon = document.createElement("td");
//if (this.insideS == "OFF") {
if (this.insideT >= this.insideTarget) {
onOffIcon.setAttribute("aria-hidden","true");
onOffIcon.className = this.config.insideIconSet + " small dimmed";
temperatureRow.appendChild(onOffIcon);
}
//else if (this.insideS == "ON") {
else if (this.insideT < this.insideTarget) {
onOffIcon.setAttribute("aria-hidden","true");
onOffIcon.className = this.config.insideIconSet + " small";
onOffIcon.style.cssText="color:red;";
temperatureRow.appendChild(onOffIcon);
}
var insideCell = document.createElement("td");
insideCell.innerHTML = this.config.insideText + " " + this.insideT + this.config.temperatureSuffix;
insideCell.className = "light small";
insideCell.style.cssText="color:white;";
temperatureRow.appendChild(insideCell);
if (this.config.batteryIcon == true) {
var battIcon = document.createElement("td");
if (this.batteryT >= 95) {
battIcon.setAttribute("aria-hidden","true");
battIcon.className = "fa fa-battery-full fa-rotate-270 xsmall dimmed";
temperatureRow.appendChild(battIcon);
}
else if (this.batteryT <= 5) {
battIcon.setAttribute("aria-hidden","true");
battIcon.className = "fa fa-battery-empty fa-rotate-270 xsmall dimmed";
battIcon.style.cssText="color:red;";
temperatureRow.appendChild(battIcon);
}
else if (this.batteryT <= 25) {
battIcon.setAttribute("aria-hidden","true");
battIcon.className = "fa fa-battery-quarter fa-rotate-270 xsmall dimmed";
temperatureRow.appendChild(battIcon);
}
else if (this.batteryT <= 50) {
battIcon.setAttribute("aria-hidden","true");
battIcon.className = "fa fa-battery-half fa-rotate-270 xsmall dimmed";
temperatureRow.appendChild(battIcon);
}
else if (this.batteryT <= 94) {
battIcon.setAttribute("aria-hidden","true");
battIcon.className = "fa fa-battery-three-quarters fa-rotate-270 xsmall dimmed";
temperatureRow.appendChild(battIcon);
}
}
//
//heating end
//hotwater
var hotWaterRow = document.createElement("tr");
if (this.config.showHotWater == true && this.config.showBattery == false) {
var hotWaterCell = document.createElement("td");
hotWaterCell.colSpan = 5;
hotWaterCell.innerHTML = this.config.hotWaterText + " " + this.hotStateT;
if (this.hotStateT == "ON") {
hotWaterCell.style.cssText="color:red;";
hotWaterCell.className = "light xsmall";}
else if (this.hotStateT == "OFF") {
hotWaterCell.className = "light xsmall";}
hotWaterRow.appendChild(hotWaterCell);
}
else if (this.config.showHotWater == true && this.config.showBattery == true) {
var hotWaterCell = document.createElement("td");
hotWaterCell.colSpan = 5;
hotWaterCell.innerHTML = this.config.hotWaterText + " " + this.hotStateT + " || " + this.config.thermBattText + " " + this.batteryT + "%";
if (this.hotStateT == "ON") {
hotWaterCell.style.cssText="color:red;";
hotWaterCell.className = "light xsmall";}
else if (this.hotStateT == "OFF") {
hotWaterCell.className = "light xsmall";}
hotWaterRow.appendChild(hotWaterCell);
}
else if (this.config.showHotWater == false && this.config.showBattery == true) {
var hotWaterCell = document.createElement("td");
hotWaterCell.colSpan = 5;
hotWaterCell.innerHTML = this.config.thermBattText + " " + this.batteryT + "%";
hotWaterCell.className = "light xsmall";
hotWaterRow.appendChild(hotWaterCell);
}
//hotwater end
//target temp
var targetTemperatureRow = document.createElement("tr");
if (this.config.showNext == true) {
var targetTemperatureCell = document.createElement("td");
targetTemperatureCell.colSpan = 5;
if (this.insideTarget <= 1) {
var frostCell = document.createElement("td");
frostCell.setAttribute("aria-hidden","true");
frostCell.className = "fa fa-snowflake-o";
frostCell.style.cssText="color:white;";
targetTemperatureCell.innerHTML = "Frost Protect Enabled ";
targetTemperatureCell.className = "light xsmall";
targetTemperatureCell.appendChild(frostCell);
targetTemperatureCell.insertBefore(frostCell, targetTemperatureCell.firstChild);
targetTemperatureRow.appendChild(targetTemperatureCell);
}
else {
targetTemperatureCell.innerHTML = this.config.targetTempText + " " + this.insideTarget + this.config.temperatureSuffix;
targetTemperatureCell.className = "light xsmall";
targetTemperatureRow.appendChild(targetTemperatureCell);
}}
//target temp end
//boost row
var boostRow = document.createElement("tr");
if (this.config.boostRow == true) {
var hboostCell = document.createElement("td");
hboostCell.colSpan = 3;
if (this.hBoost == this.config.hwBoostOffText) {
hboostCell.innerHTML = "H Boost: " + this.hBoost;
hboostCell.className = "light xsmall";
boostRow.appendChild(hboostCell);
}
else {
hboostCell.innerHTML = "H Boost - Time: " + this.hBoost;
hboostCell.className = "light xsmall";
hboostCell.style.cssText="color:red;";
boostRow.appendChild(hboostCell);
}
var hwboostCell = document.createElement("td");
hwboostCell.colSpan = 3;
if (this.hwBoost == this.config.hwBoostOffText) {
hwboostCell.innerHTML = "HW Boost: " + this.hwBoost;
hwboostCell.className = "light xsmall";
boostRow.appendChild(hwboostCell);
}
else {
hwboostCell.innerHTML = "HW Boost - Time: " + this.hwBoost;
hwboostCell.className = "light xsmall";
hwboostCell.style.cssText="color:red;";
boostRow.appendChild(hwboostCell);
}
}
//boost row end
table.appendChild(temperatureRow);
table.appendChild(targetTemperatureRow);
//table.appendChild(hotWaterRow);
//table.appendChild(boostRow);
return table;
},
socketNotificationReceived: function(notification, payload) {
var dt = new Date();
var utcDate = dt.toUTCString();
if (notification === "INSIDE") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
this.processData("INSIDE", JSON.parse(payload));
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
}
else {
this.processData("INSIDE", JSON.parse(payload));
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
}
}
//testing
else if (notification === "DEVICES") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
this.processData("DEVICES", JSON.parse(payload));
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
}
else {
this.processData("DEVICES", JSON.parse(payload));
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
}
}
//testing end
else if (notification === "OUTSIDE") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
this.processData("OUTSIDE", JSON.parse("[" + payload + "]"));
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
}
else {
this.processData("OUTSIDE", JSON.parse("[" + payload + "]"));
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
}
}
else if (notification === "401_ERROR" || notification === "400_ERROR") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = true;
}
else {
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = true;
}
}
else if (notification === "POSTCODE_ERROR") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
this.errorPostcode = true;
}
else {
this.updateDom(this.config.animationSpeed);
this.loaded = true;
this.error = false;
this.error401 = false;
this.errorPostcode = true;
}
}
else if (notification === "INSIDE_ERROR" || notification === "OUTSIDE_ERROR") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
clearInterval(this.dataID);
this.dataID = null;
this.updateDom(this.config.animationSpeed);
this.dataTimer();
this.loaded = true;
this.error = true;
this.error401 = false;
}
else {
clearInterval(this.dataID);
this.dataID = null;
this.updateDom(this.config.animationSpeed);
this.dataTimer();
this.loaded = true;
this.error = true;
this.error401 = false;
}
}
else if (notification === "UNKNOWN_ERROR") {
if (this.config.debug == true) {
Log.log(utcDate + " " + this.name + " received notification: " + notification + " - Payload: " + payload);
clearInterval(this.dataID);
this.dataID = null;
this.updateDom(this.config.animationSpeed);
this.dataTimer();
this.loaded = true;
this.error = true;
this.error401 = false;
}
else {
clearInterval(this.dataID);
this.dataID = null;
this.updateDom(this.config.animationSpeed);
this.dataTimer();
this.loaded = true;
this.error = true;
this.error401 = false;
}
}
},
});