-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·110 lines (87 loc) · 2.59 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
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
#!/usr/bin/env node
var door = require("./door_control.js")
var users = require("./users.js")
var log = require("./log.js")
// the two main parts of our interface, one handles input via http, and the other
// (cli) takes input from the command line
const http = require("./interface/http_interface.js")
const cli = require("./interface/cli_interface.js")
// build the event handler that our interfaces will use
const events = require('events')
const interface = new events()
const { to } = require('utils')
events.prototype.next = function(event){
return new Promise(function(resolve, reject){
interface.once(event, function(args){
resolve(args)
})
})
}
//-------------------------------------
// Handle Input Events
//-------------------------------------
interface.on('open_door', function(){
door.open()
})
// fetch a list of all the users and pass it back
// to the interface so it can display the output
interface.on('list_users', async function(callback){
const result = await users.all()
callback(result)
})
interface.on('begin_server', function(port){
http.listen(interface, port)
})
interface.on('scan', async function(id){
log.info(`scan: ${id}`)
const { studio_door } = await users.get_permissions(id)
if(studio_door) {
log.info('Success, opening studio door')
door.open()
}
})
interface.on('register', async function(data, callback){
console.log(data)
const [err, res] = await to(users.register(data))
if(err) {
callback(err)
} else {
callback("SUCCESS")
}
})
cli.listen(interface)
// // Comment out to disable force_open functionality
// server.on("force_open", () => door.open())
// server.on("scan", function(id){
// users.lookup(id)
// .then(function(row){
// console.log(row)
// if(row[0].accessgroup == "dj" || row[0].accessgroup == "admin") door.open()
// })
// .catch(err => console.log(err))
// })
// server.on("register", data => users.register(data))
// server.get("/users/get", function(req, res){
// users.lookup(req.query.id)
// .then(rows => res.send(rows))
// .catch(err => res.send(err))
// })
// // register a new user
// server.post("/register", function(req, res){
// if(! server.authenticate(req.headers)) {
// res.send("failure, password incorrect")
// return
// }
// console.log(req.body)
// var data = {
// firstname: req.body.firstname,
// lastname: req.body.lastname,
// middlename: req.body.middlename,
// communitygroup: req.body.communitygroup,
// accessgroup: req.body.accessgroup,
// id: req.body.id
// }
// users.register(data)
// .then(() => res.send("success"))
// .catch(err => res.send(err))
// })