This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-MyQ.js
142 lines (119 loc) · 4.17 KB
/
MMM-MyQ.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
/* Magic Mirror
* Module: MMM-MyQ
*
* By parnic https://github.com/parnic/MMM-MyQ
* MIT Licensed.
*/
Module.register('MMM-MyQ', {
defaults: {
email: '',
password: '',
types: ['wifigaragedooropener'],
columns: 1,
updateInterval: 5 * 60 * 1000 // every 5 minutes
},
states: {
closed: 'closed',
open: 'open',
closing: 'closing',
opening: 'opening'
},
getStyles() {
return ['MMM-MyQ.css'];
},
start() {
Log.info(`Starting module: ${this.name}`);
this.devices = [];
this.sendSocketNotification('MYQ_CONFIG', this.config);
},
socketNotificationReceived(notification, payload) {
if (notification === 'MYQ_LOGGED_IN') {
this.actions = payload;
} else if (notification === 'MYQ_ERROR') {
const {context, err} = payload;
Log.error(`context=${context}, err=${err.message}`);
} else if (notification === 'MYQ_TOGGLE_COMPLETE') {
this.scheduleUpdate();
} else if (notification === 'MYQ_DEVICES_FOUND') {
payload.forEach((entry) => {
let existingDevice = this.getDeviceBySerial(entry.serial_number);
if (!existingDevice) {
this.devices.push(entry);
existingDevice = entry;
} else {
existingDevice.state = entry.state;
}
if (existingDevice.desiredState) {
if (existingDevice.state.door_state !== existingDevice.desiredState) {
this.scheduleUpdate();
} else {
existingDevice.desiredState = null;
}
}
});
this.updateDom();
}
},
scheduleUpdate() {
if (!this.timeout) {
this.timeout = setTimeout(() => {
this.timeout = null;
this.sendSocketNotification('MYQ_UPDATE');
}, 5000);
}
},
getDom() {
const wrapper = document.createElement('div');
let table = document.createElement('table');
wrapper.appendChild(table);
let cols = -1;
let row;
this.devices.forEach((device) => {
cols++;
if (cols % this.config.columns === 0) {
row = document.createElement('tr');
table.appendChild(row);
}
let cell = document.createElement('td');
cell.classList.add('btn');
cell.appendChild(this.getDeviceDom(device));
row.appendChild(cell);
});
return wrapper;
},
getDeviceDom(device) {
const nameLabel = document.createElement('div');
nameLabel.textContent = device.name;
const btn = document.createElement('button');
btn.appendChild(nameLabel);
if (device.desiredState) {
btn.disabled = true;
} else {
const actionLabel = document.createElement('div');
let action = this.actions.close;
actionLabel.textContent = this.translate('Opened');
if (device.state.door_state === this.states.closed) {
action = this.actions.open;
actionLabel.textContent = this.translate('Closed');
}
btn.dataset['action'] = action;
btn.appendChild(actionLabel);
}
btn.dataset['serial_number'] = device.serial_number;
btn.classList.add('control', 'light');
btn.addEventListener('click', (evt) => { this.deviceBtnAction(btn); });
return btn;
},
deviceBtnAction(btn) {
let device = this.getDeviceBySerial(btn.dataset['serial_number']);
let action = btn.dataset['action'];
if (device && action) {
device.desiredState = action === this.actions.close ? this.states.closed : this.states.open;
this.sendSocketNotification('MYQ_TOGGLE', {device, action});
this.updateDom();
}
},
getDeviceBySerial(serialNumber) {
return this.devices.find((device) => device.serial_number === serialNumber);
}
});