-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (42 loc) · 1.17 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
const axios = require('axios')
const TokenReader = require('./token_reader.js')
const allowed = [ '0003908287' ];
function log(text) {
console.log(new Date().toISOString() + " - " + text);
}
function openDoor() {
setTimeout(function() {
sendRequest('ON');
setTimeout(function() {
sendRequest('OFF');
}, 5000);
},
3000);
}
function sendRequest(data) {
axios
.post('http://raspberrypi:8080/rest/items/GHoma_Switch_d3590c', data, {
headers: { 'Content-Type': 'text-plain' }
})
.then(res => {
log('Sent request ' + data + ', received statusCode: ' + res.status)
})
.catch(error => {
console.error(error)
})
}
const name = '/dev/input/by-id/usb-HXGCoLtd_HIDKeys-event-kbd'
const reader = new TokenReader(name);
reader.onTokenEvent(function(chunk) {
if (allowed.includes(chunk)) {
log("Grant access to " + chunk);
openDoor();
} else {
log("Forbid access to " + chunk);
}
});
function intervalFunc() {
console.log('Waiting for input!');
}
setInterval(intervalFunc, 3600000);
console.log("Init complete");