-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Verisure.js
141 lines (114 loc) · 3.32 KB
/
MMM-Verisure.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
/* global Module */
/* Magic Mirror
* Module: MMM-Verisure
*
* By Martin Burheim Tingstad
* MIT Licensed.
*/
Module.register("MMM-Verisure",{
defaults: {
// Read config and secrets from environment variables
refreshInterval: 1000 * 60 * 30, // refresh every 30 minutes
updateInterval: 1000 * 60 * 30, // update every 30 minutes
timeFormat: config.timeFormat,
lang: config.language,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500,
position: 'top_left'
},
start: function() {
var self = this;
let data = [];
let installations = [];
var dataNotification = null;
// Flag for check if module is loaded
this.loaded = false;
// Schedule update timer.
this.sendSocketNotification("CONFIG", this.config);
setInterval(function() {
self.updateDom();
}, this.config.updateInterval);
},
// Define required scripts.
getScripts: function() {
return [];
},
getStyles: function() {
return [];
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad ;
var self = this;
setTimeout(function() {
self.getData();
}, nextLoad);
},
sensorsDiv: function(sensors) {
let temperaturerDiv = document.createElement("div");
for(var sensorcounter = 0; sensorcounter < sensors.length; sensorcounter++) {
var sensor = sensors[sensorcounter];
var sensorDiv = document.createElement("div");
sensorDiv.innerHTML = sensor.device.area + ": " + sensor.temperatureValue + "°C";
temperaturerDiv.append(sensorDiv);
}
return temperaturerDiv;
},
getDom: function() {
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
return wrapper;
}
var installations = document.createElement("div");
var toAdd = document.createDocumentFragment();
for (var i = 0; i < this.installations.length; i++) {
var installation = this.installations[i];
var installationDiv = document.createElement("div");
installationDiv.id = 'installation' + i;
installationDiv.classList.add("medium");
installationDiv.classList.add("bright");
installationDiv.classList.add("light");
installationDiv.innerHTML = installation.name;
installationDiv.id = 'installation'+i;
toAdd.appendChild(installationDiv);
toAdd.appendChild(this.sensorsDiv(installation.sensors));
}
installations.appendChild(toAdd);
wrapper.appendChild(installations);
return wrapper;
},
processData: function(data) {
var self = this;
this.installations = data;
this.loaded = true;
if (this.loaded === false) {
self.updateDom(self.config.animationSpeed);
}
this.loaded = true;
// the data if load
// send notification to helper
this.sendSocketNotification("DATA", data);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STARTED") {
this.updateDom();
}
else if(notification === "DATA") {
payload.position = 'test';
// set dataNotification
this.dataNotification = payload;
this.processData(payload);
this.updateDom();
}
},
});