-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-MiOSView.js
319 lines (253 loc) · 9.73 KB
/
MMM-MiOSView.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
/* MiOS Status Viewing Module */
/* Magic Mirror
* Module: MiOS Viewer
*
* By Nick Wootton
* based on SwissTransport module by Benjamin Angst http://www.beny.ch
* and documentstaion from MCV here - http://wiki.micasaverde.com/index.php/UI_Simple
* MIT Licensed.
*/
Module.register("MMM-MiOSView", {
// Define module defaults
defaults: {
updateInterval: 5 * 60 * 1000, // Update every 5 minutes.
animationSpeed: 2000,
fade: true,
fadePoint: 0.25, // Start on 1/4th of the list.
initialLoadDelay: 0, // start delay seconds.
LoadTime: 0,
DataVersion: 0,
DefaultTimeout: 60,
DefaultMinimumDelay: 2000,
CurrentMinimumDelay: 0,
CurrentSleep: 2000,
EngineState: -2, // Meaning we are not connected
NumFailures: 0,
veraURL: '', // URL of Local Vera box
veraCategories: [],
header: 'Vera Status',
debug: false
},
// Define required scripts.
getStyles: function() {
return ["vera.css", "font-awesome.css"];
},
// Define required scripts.
getScripts: function() {
return ["moment.js", this.file('titleCase.js')];
},
//Define header for module.
getHeader: function() {
this.config.header = this.translate("HEADER");
return this.config.header;
},
//Get translations
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
// Set locale.
moment.locale(config.language);
this.VeraData = {};
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
this.url = encodeURI('http://' + this.config.veraURL + ":3480/" + this.getParams());
if (this.config.debug) {
Log.warn('URL Request is: ' + this.url);
}
this.updateVeraInfo(this);
},
// updateVeraInfo
updateVeraInfo: function(self) {
if (this.hidden != true) {
self.sendSocketNotification('GET_MiOSINFO', { 'url': this.url });
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.config.veraURL === "") {
wrapper.innerHTML = this.translate("SET_IP");
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.veraCategories === "") {
wrapper.innerHTML = this.translate("SET_CATEGORIES");
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOAD_MSG");
wrapper.className = "dimmed light small";
return wrapper;
}
// *** Start Building Table
var table = document.createElement("table");
table.className = "small";
//With data returned
if (typeof this.VeraData.devices !== 'undefined' && this.VeraData.devices !== null) {
var myDevices = this.VeraData.devices;
if (this.config.debug) {
Log.info(myDevices);
}
for (var r in myDevices) {
var myDevice = myDevices[r];
//Create row for Current device
var deviceRow = document.createElement("tr");
table.appendChild(deviceRow);
//Status reported
var deviceStatusCell = document.createElement("td");
if ((myDevice.status == 0)) {
deviceStatusCell.className = "status deviceOff";
}
else if ((myDevice.status == 1)) {
deviceStatusCell.className = "status bright deviceOn";
}
else {
deviceStatusCell.className = "";
}
//deviceStatusCell.innerHTML = myDevice.status;
deviceStatusCell.innerHTML = " ";
deviceRow.appendChild(deviceStatusCell);
//device cell
var deviceCurrentCell = document.createElement("td");
deviceCurrentCell.innerHTML = myDevice.name;
if ((myDevice.status == 0)) {
deviceCurrentCell.className = "deviceName";
}
else if ((myDevice.status == 1)) {
deviceCurrentCell.className = "bright deviceName";
}
else {
deviceCurrentCell.className = "deviceName";
}
deviceRow.appendChild(deviceCurrentCell);
}
} else {
var row1 = document.createElement("tr");
table.appendChild(row1);
var messageCell = document.createElement("td");
messageCell.innerHTML = " " + this.VeraData.message + " ";
messageCell.className = "bright";
row1.appendChild(messageCell);
var row2 = document.createElement("tr");
table.appendChild(row2);
var timeCell = document.createElement("td");
timeCell.innerHTML = " " + this.VeraData.timestamp + " ";
timeCell.className = "bright";
row2.appendChild(timeCell);
}
wrapper.appendChild(table);
// *** End building results table
return wrapper;
},
/* processVeraData(data)
* Uses the received data to set the various values.
*/
processVeraData: function(data) {
//Dump data
if (this.config.debug) {
Log.info(data);
}
//Check we have data back from API
if (typeof data !== 'undefined' && data !== null) {
// We got valid data, so introduce the minimum delay in case there's a flood of changes in the Vera
this.config.CurrentMinimumDelay = this.config.DefaultMinimumDelay;
//define object to hold output info
this.VeraData = {};
//Define object for device data
this.VeraData.devices = [];
//Define message holder
this.VeraData.message = null;
//Timestamp
this.VeraData.timestamp = new Date();
//Check we have device array
if (typeof data.devices !== 'undefined' && data.devices !== null) {
//.. and actual values
if (typeof data.devices.length !== 'undefined' && data.devices.length !== 0) {
for (var i=0; i < data.devices.length; i++) {
var deviceInfo = data.devices[i];
if (this.config.veraCategories.indexOf(parseInt(deviceInfo.category)) > -1) {
this.VeraData.devices.push(
{
name: deviceInfo.name,
status: deviceInfo.status
});
}
}
} else {
//No device info returned - set message
this.VeraData.message = "No devices found";
if (this.config.debug) {
Log.error("=======LEVEL 3=========");
Log.error(this.VeraData);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
} else {
//No info returned - set message
this.VeraData.message = "No info about the devices returned";
if (this.config.debug) {
Log.error("=======LEVEL 2=========");
Log.error(this.VeraData);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
} else {
//No data returned - set message
this.VeraData.message = "No data returned";
if (this.config.debug) {
Log.error("=======LEVEL 1=========");
Log.error(this.VeraData);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
/* getParams(compliments)
* Generates an url with url parameters based on the config.
*
* return String - URL params.
*/
getParams: function() {
var params = "data_request?id=lu_sdata";
params += "&loadtime=" + this.config.LoadTime;
params += "&dataversion=" + this.config.DataVersion;
params += "&timeout=" + this.config.DefaultTimeout;
params += "&minimumdelay=" + this.config.CurrentMinimumDelay;
if (this.config.debug) {
Log.warn(params);
}
return params;
},
/* 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;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateVeraInfo(self);
}, nextLoad);
},
// Process data returned
socketNotificationReceived: function(notification, payload) {
if (notification === 'MiOS_DATA' && payload.url === this.url) {
this.processVeraData(payload.data);
this.scheduleUpdate(this.config.updateInterval);
}
}
});