/
MMM-ISY.js
445 lines (405 loc) · 20.6 KB
/
MMM-ISY.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
/* global Module */
/* jshint esversion: 6 */
/* Magic Mirror
* Module: MMM-ISY
*
* By shbatm
* MIT Licensed.
*/
Module.register("MMM-ISY", {
defaults: {
maxWidth: '98%',
floorplan: 'floorplan.svg',
nodes: {},
variables: {}, // Format: "var_type_varID": { onVal: 1, offVal: 0, flash: true },
thermostats: {},
enableControls: false
},
deviceListCount: 0,
requiresVersion: "2.1.3", // Required version of MagicMirror
deviceList: null,
start: function() {
var self = this;
//Flag for check if module is loaded
this.loaded = false;
this.connected = false;
},
getDom: function() {
var self = this;
var $outerWrapper = $("<div>", {
id: "isyOuterWrapper",
"class": "isyOuterWrapper bootstrap",
style: "width:" + this.config.maxWidth + ";"
}).append($("<div>", {
id: "isyInnerWrapper",
"class": "isyInnerWrapper dimmed light small"
}));
if (!this.loaded && !this.connected) {
$outerWrapper.find("#isyInnerWrapper").append("<span id='loadingText'>Loading ISY Floorplan ...</span>").append(this.controlTemplates.loading);
// Get the floorplan graphic
console.log("Getting floorplan graphic: " + this.config.floorplan);
$outerWrapper.find("#isyInnerWrapper").svg({
loadURL: self.file(self.config.floorplan),
changeSize: true,
onLoad: function(svg) {
// Remove Loading Classes and Text
$(this).removeClass("dimmed light small");
$(this).find("#loadingText").remove();
// Store the SVG reference for later
self.svg = $(this).svg('get');
// Get a list of all the Insteon (id=i1234561 for Insteon ID 12.34.56 1)
// and v5 Node Server (id=n0... for Node Server n001_node_name) nodes in the graphic
self.$isyNodes = $('[id^=i_],[id^=n0],[id^=var_]', self.svg.root());
// Get any node customizations from the config
self.$isyNodes.each(function(index) {
if ($(this)[0].id in self.config.nodes) {
self.$isyNodes[index].isyConfig = self.config.nodes[$(this)[0].id];
}
if ($(this)[0].id in self.config.variables) {
self.$isyNodes[index].isyConfig = self.config.variables[$(this)[0].id];
}
});
}
});
}
return $outerWrapper[0];
},
initialFloorplanUpdate: function() {
var self = this;
this.$isyNodes.each(function(index) {
// Handle Devices
if ($(this)[0].id in self.deviceList.dev) {
// Device was found in device list.
self.updateDevice(self.deviceList.dev[$(this)[0].id], "dev", true, $(this));
}
// Handle Insteon Thermostats
if ($(this)[0].id in self.deviceList.tst) {
// console.log("Thermostat " + $(this)[0].id + " found.");
self.updateThermostat(self.deviceList.tst[$(this)[0].id], true, $(this));
}
// Handle Variables
if ($(this)[0].id in self.deviceList.var) {
// Variable was found in the variable list.
self.updateDevice(self.deviceList.var[$(this)[0].id], "var", true, $(this));
}
});
},
updateThermostat: function(tstat, initial = false, $dev = undefined) {
var that = this;
if (!initial) {
$dev = $(`#${tstat.svgId}`, this.svg.root());
if ($dev.length === 0) { return; }
}
var dev = $dev[0];
// Tag the device with the ISY information for use later
dev.isyNode = tstat;
dev.isyKind = "tstat";
$(`#${tstat.svgId}_CT`, this.svg.root()).text(tstat.fstatus.currTemp);
if (this.config.thermostats[tstat.svgId.replace(/[0-9]$/, "")].showSetPoints) {
$(`#${tstat.svgId}_CSP`, this.svg.root()).text(tstat.fstatus.coolSetPoint);
$(`#${tstat.svgId}_HSP`, this.svg.root()).text(tstat.fstatus.heatSetPoint);
$(`#${tstat.svgId}_RH`, this.svg.root()).text(tstat.fstatus.humidity);
} else {
$(`#${tstat.svgId}_setpoints`, this.svg.root()).hide();
}
$(`#${tstat.svgId}_ST`, this.svg.root()).removeClass("Cooling Heating Idle").addClass(tstat.fstatus.currentStatus);
},
updateDevice: function(payload, kind, initial = false, $dev = undefined) {
var that = this;
if (!initial) {
$dev = $(`#${payload.svgId}`, this.svg.root());
if ($dev.length === 0) { return; }
}
var dev = $dev[0];
if (kind === "dev") {
// console.log("MMM-ISY Tracked Device Changed: " + payload.svgId);
} else if (kind === "var") {
// console.log("MMM-ISY Tracked Variable Changed: " + payload.svgId);
payload.currentState = payload.value;
} else {
return;
}
var toCamelCase = (str) => (/[-_\s]/.test(str)) ? (str.toLowerCase().replace(/[\b_-](\w)/g, (m, p1, i) => p1.toUpperCase())) : str;
// Tag the device with the ISY information for use later
dev.isyNode = payload;
dev.isyKind = kind;
if (initial && this.config.enableControls) {
// $dev.click(this.deviceClicked);
var ctrlTemplate;
if ("deviceType" in dev.isyNode) {
if (dev.isyNode.deviceType === "nodeServerNode" && toCamelCase(dev.isyNode.nodeDefId) in this.controlTemplates) {
ctrlTemplate = this.controlTemplates[toCamelCase(dev.isyNode.nodeDefId)];
} else if (toCamelCase(dev.isyNode.deviceType) in this.controlTemplates) {
ctrlTemplate = this.controlTemplates[toCamelCase(dev.isyNode.deviceType)];
}
}
if (typeof ctrlTemplate !== "undefined") {
$dev.popover({
html: true,
title: function() {
var template = `<span id="popover-title" class="white-text">${dev.isyNode.name}</span>
<button type="button" class="close" id="btnClose" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" class="white-text">×</span></button>`;
return template;
},
content: ctrlTemplate,
container: "#isyInnerWrapper",
placement: "auto"
}).on('inserted.bs.popover', function(evt) {
var $popup = $('#' + $(evt.target).attr('aria-describedby'));
// var $popup = $(this).next('.popover');
$popup.find('button').click(function(e) {
$popup.popover('hide');
if (this.id !== "btnClose") { that.handleControlEvent(evt.target, this.id) };
});
$popup.find('#brightness').ionRangeSlider({
min: 0,
max: 100,
from: Math.round(dev.isyNode.currentState / 255.0 * 100),
hide_min_max: true,
postfix: "%",
scope: evt,
onFinish: function(data) {
that.handleControlEventWithDebounce(this.target, data.from, 500);
}
});
});
}
}
if (typeof dev.isyConfig === 'object') {
// This device has special configuration options
if ("customStatusText" in dev.isyConfig && payload.currentState in dev.isyConfig.customStatusText) {
$dev_text = $(`#${payload.svgId}_tspan`, this.svg.root());
if ($dev_text.length !== 0) {
$dev_text.text(dev.isyConfig.customStatusText[payload.currentState]);
if (("offVal" in dev.isyConfig && payload.currentState == dev.isyConfig.offVal) || payload.currentState === 0) {
$dev_text.removeClass("isyON").addClass("isyOFF");
} else if (("onVal" in dev.isyConfig && payload.currentState == dev.isyConfig.onVal) || payload.currentState > 0) {
$dev_text.removeClass("isyOFF").addClass("isyON");
}
}
}
if ("onVal" in dev.isyConfig && payload.currentState == dev.isyConfig.onVal) {
if ("flash" in dev.isyConfig && dev.isyConfig.flash) {
$dev.addClass("isyFlashNode");
}
$dev.removeClass("isyOFF").addClass("isyON");
if (!initial && "notifyOn" in dev.isyConfig) {
this.sendNotification(dev.isyConfig.notifyOn.notification, dev.isyConfig.notifyOn.payload);
}
return;
} else if ("offVal" in dev.isyConfig && payload.currentState == dev.isyConfig.offVal) {
if ("flash" in dev.isyConfig && dev.isyConfig.flash) {
$dev.removeClass("isyFlashNode");
}
$dev.removeClass("isyON").addClass("isyOFF");
if (!initial && "notifyOff" in dev.isyConfig) {
this.sendNotification(dev.isyConfig.notifyOff.notification, dev.isyConfig.notifyOff.payload);
}
return;
} else if ("inverted" in dev.isyConfig && dev.isyConfig.inverted) {
if (payload.currentState === 0) {
$dev.removeClass("isyOFF").addClass("isyON");
} else if (payload.currentState > 0) {
$dev.removeClass("isyON").addClass("isyOFF");
}
return;
}
}
// Still here, so the status has not been set yet:
if (typeof payload.currentState === "number") {
if (payload.currentState > 0) {
$dev.removeClass("isyOFF").addClass("isyON");
} else {
$dev.removeClass("isyON").addClass("isyOFF");
}
}
return;
},
getScripts: function() {
return [this.file('js/jquery-3.2.1.min.js'),
this.file('js/jquery.svg.min.js'),
this.file('js/jquery.svganim.min.js'),
this.file('js/jquery.svgdom.min.js'),
this.file('js/popper.min.js'),
this.file('js/bootstrap.min.js')
];
},
getStyles: function() {
return [this.name + ".css", 'font-awesome.css',
this.file('css/bootstrap-div.min.css'),
this.file('css/bootstrap-div-tweaks.css')
];
},
getControlSubsystem: function() {
return [
this.file('js/ion.rangeSlider.min.js'),
this.file('css/ion.rangeSlider.css'),
this.file('css/ion.rangeSlider.skinHTML5.css'),
this.file('node_modules/mdi/css/materialdesignicons.min.css')
];
},
handleControlEvent: function(device, command) {
command = (typeof command === "string") ? command.replace(/btn/g, '') : command;
var payload = { id: device.id, node: device.isyNode, kind: device.isyKind, config: device.isyConfig, cmd: command };
if (device.isyKind === "dev" || device.isyKind === "tstat") {
this.sendSocketNotification("DEVICE_CMD", payload);
} else if (device.isyKind === "var") {
this.sendSocketNotification("VARIABLE_CMD", payload);
}
},
handleControlEventWithDebounce(device, command, wait) {
clearTimeout(this.debounceTimeout);
this.debounceTimeout = setTimeout(() => {
this.debounceTimeout = null;
this.handleControlEvent(device, command);
}, wait);
},
debounceTimeout: null,
// socketNotificationReceived from helper
socketNotificationReceived: function(notification, payload) {
var self = this;
// console.log("MMM-ISY received notification: " + notification);
if (notification === 'DEVICE_LIST_RECEIVED') {
$('#isyLoadingModal').modal('hide');
this.deviceList = {};
this.deviceList = payload;
this.connected = true;
this.initialFloorplanUpdate();
}
if (notification === 'DEVICE_CHANGED') {
this.deviceList.dev[payload.svgId] = payload;
this.updateDevice(payload, "dev");
}
if (notification === 'THERMOSTAT_CHANGED') {
this.deviceList.tst[payload.svgId] = payload;
// Handle Insteon Thermostats
this.updateThermostat(payload);
}
if (notification === 'VARIABLE_CHANGED') {
this.deviceList.var[payload.svgId] = payload;
this.updateDevice(payload, "var");
}
if (notification === 'PROGRAM_CMD_RESULT') {
if (!payload) {
this.sendNotification("SHOW_ALERT", {
title: "ISY Alert",
message: `ISY Program Command Unsucceessful!`,
imageFA: "bell-slash",
timer: 2000,
});
}
}
},
notificationReceived: function(notification, payload, sender) {
if (notification === 'ALL_MODULES_STARTED') {
}
if (notification === 'DOM_OBJECTS_CREATED') {
// Show Loading Modal
$('#isyLoadingModal').modal({ backdrop: 'static' }).on("hidden.bs.modal", function(e) {
$('#isyInnerWrapper').removeClass("modal-open");
});
$('#isyInnerWrapper').addClass("modal-open");
$('.modal-backdrop').appendTo('#isyInnerWrapper');
$('body').removeClass('modal-open');
if (this.config.enableControls) {
this.loadDependencies("getControlSubsystem", () => {
console.log("MMM-ISY Control Subsystem Loaded.");
this.sendSocketNotification("INITIALIZE_ISY", this.config);
});
} else {
this.sendSocketNotification("INITIALIZE_ISY", this.config);
}
}
if (notification === 'ISY_PROGRAM_CMD') {
this.sendSocketNotification("PROGRAM_CMD", payload);
// Payload structure: { id: 'pgmID', command: 'cmd'}
// Possible values for cmd: run|runThen|runElse|stop|enable|disable|enableRunAtStartup|disableRunAtStartup
}
},
deviceClicked: function(evt) {
console.log(evt);
},
controlTemplates: {
loading: `<div class="modal fade" id="isyLoadingModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body text-center text-white">
<h4>CONNECTING TO ISY...</h4>
<span class="fa fa-connectdevelop fa-spin fa-3x" style="padding: 1rem;"></span>
</div>
</div>
</div>
</div>`,
dimmableLight: `<div class="row row-eq-height text-center" data-deviceType="dimmableLight">
<div class="col-3">
<button type="button" id="btnDOF" class="btn btn-grey darken-2 btn-sm-i" data-dismiss="popover"><i class="mdi mdi-brightness-5 mdi-light mdi-16px" aria-hidden="true"></i></button>
</div>
<div class="col-6">
<input id="brightness" />
</div>
<div class="col-3">
<button type="button" id="btnDON" class="btn btn-grey darken-2 btn-sm-i" data-dismiss="popover"><i class="mdi mdi-brightness-7 mdi-light mdi-16px" aria-hidden="true"></i></button>
</div>
</div>
<div class="row align-middle text-center">
<div class="col-6">
<button id="btnDFOF" type="button" class="btn btn-grey darken-2 btn-sm" data-dismiss="popover"><i class="fa fa-angle-double-down"></i></button>
</div>
<div class="col-6">
<button id="btnDFON" type="button" class="btn btn-grey darken-2 btn-sm" data-dismiss="popover"><i class="fa fa-angle-double-up"></i></button>
</div>
</div>`,
ecolorLight: `<div class="row row-eq-height text-center" data-deviceType="dimmableLight">
<div class="col-3">
<button type="button" id="btnDOF" class="btn btn-grey darken-2 btn-sm-i" data-dismiss="popover"><i class="mdi mdi-brightness-5 mdi-light mdi-16px" aria-hidden="true"></i></button>
</div>
<div class="col-6">
<input id="brightness" />
</div>
<div class="col-3">
<button type="button" id="btnDON" class="btn btn-grey darken-2 btn-sm-i" data-dismiss="popover"><i class="mdi mdi-brightness-7 mdi-light mdi-16px" aria-hidden="true"></i></button>
</div>
</div>
<div class="row align-middle text-center">
<div class="col-6">
<button id="btnDFOF" type="button" class="btn btn-grey darken-2 btn-sm" data-dismiss="popover"><i class="fa fa-angle-double-down"></i></button>
</div>
<div class="col-6">
<button id="btnDFON" type="button" class="btn btn-grey darken-2 btn-sm" data-dismiss="popover"><i class="fa fa-angle-double-up"></i></button>
</div>
</div>`,
colorLight: `<div class="row row-eq-height text-center" data-deviceType="dimmableLight">
<div class="col-3">
<button type="button" id="btnDOF" class="btn btn-grey darken-2 btn-sm-i" data-dismiss="popover"><i class="mdi mdi-brightness-5 mdi-light mdi-16px" aria-hidden="true"></i></button>
</div>
<div class="col-6">
<input id="brightness" />
</div>
<div class="col-3">
<button type="button" id="btnDON" class="btn btn-grey darken-2 btn-sm-i" data-dismiss="popover"><i class="mdi mdi-brightness-7 mdi-light mdi-16px" aria-hidden="true"></i></button>
</div>
</div>
<div class="row align-middle text-center">
<div class="col-6">
<button id="btnDFOF" type="button" class="btn btn-grey darken-2 btn-sm" data-dismiss="popover"><i class="fa fa-angle-double-down"></i></button>
</div>
<div class="col-6">
<button id="btnDFON" type="button" class="btn btn-grey darken-2 btn-sm" data-dismiss="popover"><i class="fa fa-angle-double-up"></i></button>
</div>
</div>`,
light: `<div class="btn-toolbar" style="justify-content: center;" role="toolbar" data-deviceType="light">
<button type="button" id="btnDOF" class="btn btn-grey darken-2" data-dismiss="popover"><i class="mdi mdi-brightness-5 mdi-light" aria-hidden="true"></i></button>
<button type="button" id="btnDON" class="btn btn-grey darken-2" data-dismiss="popover"><i class="mdi mdi-brightness-7 mdi-light" aria-hidden="true"></i></button>
</div>`,
outlet: `<div class="btn-toolbar" style="justify-content: center;" role="toolbar" data-deviceType="light">
<button type="button" id="btnDOF" class="btn btn-grey darken-2" data-dismiss="popover"><i class="mdi mdi-power-plug-off mdi-light" aria-hidden="true"></i></button>
<button type="button" id="btnDON" class="btn btn-grey darken-2" data-dismiss="popover"><i class="mdi mdi-power-plug mdi-light" aria-hidden="true"></i></button>
</div>`,
fan: `<div class="btn-toolbar" style="justify-content: center;" role="toolbar" data-deviceType="fan">
<button type="button" id="btnOff" class="btn btn-grey darken-2 btn-sm-i"><i class="mdi mdi-fan-off"></i></button>
<button type="button" id="btnLow" class="btn btn-grey darken-2 btn-sm-i"><i class="mdi mdi-signal-cellular-1"></i></button>
<button type="button" id="btnMedium" class="btn btn-grey darken-2 btn-sm-i"><i class="mdi mdi-signal-cellular-2"></i></button>
<button type="button" id="btnHigh" class="btn btn-grey darken-2 btn-sm-i"><i class="mdi mdi-fan"></i></button>
</div>`
}
});