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
/
Copy pathnode_helper.js
79 lines (68 loc) · 2.23 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-MyQ
*
* By parnic https://github.com/parnic/MMM-MyQ
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
module.exports = NodeHelper.create({
_customActions: {
open: 'open',
close: 'close'
},
start() {
console.log(`Starting module helper: ${this.name}`);
},
resetUpdates() {
if (this.intervalId) {
clearInterval(this.intervalId);
}
this.intervalId = setInterval(() => {
this.getData();
}, this.config.updateInterval);
},
async socketNotificationReceived(notification, payload) {
if (notification === 'MYQ_CONFIG') {
if (!this.config) {
this.config = payload;
const MyQ = await import('@hjdhjd/myq');
this.account = new MyQ.myQApi(this.config.email, this.config.password, null, 'east');
}
this.sendSocketNotification('MYQ_LOGGED_IN', this._customActions);
this.getData();
} else if (this.config) {
if (notification === 'MYQ_TOGGLE') {
const {device, action} = payload;
this.execute(device, action);
} else if (notification === 'MYQ_UPDATE') {
this.getData();
}
}
},
getData() {
this.resetUpdates();
this.refreshDevices();
},
async refreshDevices() {
let result = await this.account.refreshDevices();
if (!result) {
this.handleError('refreshDevices returned false');
return;
}
let devices = this.account.devices.filter((device) => this.config.types.includes(device.device_type));
this.sendSocketNotification('MYQ_DEVICES_FOUND', devices);
},
async execute(device, action) {
let result = await this.account.execute(device, action);
if (result) {
this.sendSocketNotification('MYQ_TOGGLE_COMPLETE', result);
} else {
this.handleError('execute');
}
},
handleError(context) {
let err = `${context} failed`;
this.sendSocketNotification('MYQ_ERROR', {context, err});
console.error(`${this.name} ${context} error: ${err}`);
}
});