forked from sterling/gd-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarage-door-ctl.js
72 lines (58 loc) · 1.31 KB
/
garage-door-ctl.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
'use strict';
let co = require('co');
let Events = require('events');
let doorStates = {
0: 'closed',
1: 'open',
2: 'transitioning'
};
class GarageDoor extends Events {
constructor() {
super();
this.comms = null;
this._currentState = null;
}
/**
* setup communications module
* @param {NRFSecure} comms
*/
* connect(comms) {
console.log('connecting...');
this.comms = comms;
yield this.comms.setup();
console.log('connected');
}
* monitor() {
while (true) {
yield new Promise(resolve => {
setTimeout(() => {
resolve();
}, 2000);
});
try {
this.updateDoorState(yield this._getDoorState());
} catch (e) {}
}
}
* openDoor() {
yield this.comms.send(new Buffer([0x02]));
}
* closeDoor() {
yield this.comms.send(new Buffer([0x02]));
}
getDoorStatus() {
let status = doorStates[this._currentState];
return status ? status : 'unknown';
}
updateDoorState(newState) {
if (this._currentState !== null && this._currentState != newState) {
}
this._currentState = newState;
console.log(this.getDoorStatus());
}
* _getDoorState() {
let response = yield this.comms.request(new Buffer([0x01]));
return response.readUIntLE(0, 1);
}
}
module.exports = GarageDoor;