-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
140 lines (123 loc) · 3.08 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require("express");
const { Gpio } = require("onoff");
const PushOver = require("pushover-notifications");
const Lock1 = new Gpio(17, "out");
const Lock2 = new Gpio(27, "out");
let app = express();
Lock1.writeSync(1);
Lock2.writeSync(1);
require("dotenv").config();
const {
HID_VENDOR,
HID_PRODUCT,
AIRTABLE_BASE_ID,
AIRTABLE_API_KEY
} = process.env;
const Airtable = require("airtable-node");
const { KeyboardLines } = require("node-hid-stream");
const HID = require("node-hid");
var lines = new KeyboardLines({ vendorId: HID_VENDOR, productId: HID_PRODUCT });
const ACCESS_CODES = {
"3D_Printing": "receQ2eIPil4SUYhg"
};
try {
const airtable = new Airtable({ apiKey: AIRTABLE_API_KEY })
.base(AIRTABLE_BASE_ID)
.table("Members");
let lastCache = new Date();
let membersCache = [];
loadCache(true);
} catch (e) {
console.log(e);
}
lines.on("data", function(data) {
//notify({access: false, data});
isValidCometCard(data);
loadCache();
});
function loadCache(force = false) {
try{
if (!force && (new Date().getTime() - lastCache.getTime()) / 1000 < 300) {
return;
}
airtable
.list({
view: "lockbot",
maxRecords: 200
})
.then(({ records }) => {
membersCache = records
.map(element => {
const { fields } = element;
return {
id: fields.ID,
ccid: fields.CCID ? fields.CCID : null,
access: fields.Access ? fields.Access : null
};
})
.filter(element => {
return element.access !== null && element.ccid !== null;
});
//console.log(membersCache)
});
}catch(e){
console.log(e)
}
}
function notify(access, number) {
const p = new PushOver({
user: process.env["PUSHOVER_GROUP"],
token: process.env["PUSHOVER_API_KEY"]
});
let msg = {
message: `At ${new Date()}, a comet card swipe ${
access ? "succeded" : "failed"
} ${number}`, // required
title: `3D Printer Lock System ${access ? "opened" : "failed to open"}`,
sound: "magic",
priority: 1
};
p.send(msg, function(err, result) {
if (err) {
throw err;
}
console.log(result);
});
}
function isValidCometCard(number) {
let results;
if ((results = number.match(/;([0-9]{0,})=(20[0-9]{0,})\?\+[0-9]{0,}\?/))) {
let numbers = results.slice(1);
let found = membersCache.find(
o => o.ccid == numbers[0] || o.ccid == numbers[1]
);
if (found && found.access.find(x => x == ACCESS_CODES["3D_Printing"])) {
//notify(true, JSON.stringify(found));
openLock();
} else {
loadCache();
//notify(false, numbers.toString())
console.log("No Access");
}
console.log(found);
} else {
console.log("nope");
}
}
function openLock() {
console.log("Opening Lock");
Lock1.write(0);
Lock2.write(0);
setTimeout(function() {
closeLock();
}, 3000);
}
function closeLock() {
Lock1.write(1);
Lock2.write(1);
}
app.post("/unlock", (req, res) => {
openLock();
res.send("Success");
});
app.listen(80);