/
index.js
64 lines (56 loc) · 1.14 KB
/
index.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
/*//===
Switch Controller Library
*///===
var async = require('async'),
gpio = require('gpio'),
events = require('events'),
translation = {
1 : 26,
2 : 6,
3 : 22,
4 : 4
}
class switchModule{
constructor(){
this.switches = {}
this.events = new events();
this.settings = {}
var switchArray = []
for(var x in translation)
switchArray.push({
number : x,
pin : translation[x]
})
async.eachSeries(switchArray, (switchObj, nextSwitch) => {
this.settings[switchObj.number] = false;
this.switches[switchObj.number] = gpio.export(switchObj.pin, {
direction: 'out',
interval: 200,
ready: function() {
this.set();
nextSwitch();
}
});
}, () => {
this.events.emit('ready', {});
})
}
set(sw, setting){
if(!(sw in this.switches))
throw new Error(`Invalid Switch ${sw}`);
if(!setting)
this.switches[sw].set()
else
this.switches[sw].set(0)
this.settings[sw] = !!setting;
this.events.emit('change', {
switch : sw,
setting : !setting,
state : this.settings
})
}
get(sw){
return sw in this.settings ? this.settings[sw] : false;
}
}
module.exports = switchModule;