-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeytag.js
90 lines (75 loc) · 2.38 KB
/
keytag.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
var mongoose = require('mongoose');
var db = mongoose.connection;
mongoose.connect('mongodb://localhost/test/');
var queryService = require('./queryServer.js');
var interfaces = require('./interfaces.js');
queryService.parseJSONQuery({'op':'comment', 'args':[]})
/*
// Legacy code
db.on('error', console.error);
db.once('open', function () {
console.log("DB Access bound");
var attendanceSchema = new mongoose.Schema({
deviceType: String,
deviceID: Number,
version: Number,
cardID: String,
unixTimestamp: Number,
humanTimestamp: String
});
Attendance = mongoose.model('Attendance', attendanceSchema);
});
//var Attendance = mongoose.model('Attendance', attendanceSchema);
mongoose.connect('mongodb://localhost/test');
function runQuery(query,lim,sorter,sel) {
var q = Attendance.find(query).sort(sorter).limit(lim);
return q.select(sel);
}
// Build and start UDP Server
// Handles incoming SwipeEvents from hardware login systems
var dgram = require("dgram");
var dserver = dgram.createSocket("udp4");
dserver.on("error", function (err) {
console.log("Server error:\n" + err.stack);
dserver.close();
});
dserver.on("message", function (msg, rinfo) {
var parsed = JSON.parse(msg);
var date = new Date(parsed.TS * 1000);
var hts = date.toLocaleDateString() + " " + date.toLocaleTimeString();
var attend = new Attendance({
deviceType: parsed.DTY,
deviceID: parsed.DID,
version: parsed.VN,
cardID: parsed.CID,
unixTimestamp: parsed.TS,
humanTimestamp: hts
});
attend.save(function (err, attend) {
if (err) return console.error(err);
console.log("I'm writing: \n " + attend + "\n to the database.");
});
});
dserver.once("listening", function () {
var address = dserver.address();
console.log("Server listening on " + address.address
+ ":" + address.port);
});
dserver.bind(8080);
var net = require('net');
var qserver = net.createServer(function(c) { //'connection' listener
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.on('data', function(msg) {
var queryReq = JSON.parse(msg);
runQuery(queryReq.query,queryReq.lim,queryReq.sorter,queryReq.sel).exec(function (err, data) {
c.write(JSON.stringify(data));
});
});
});
qserver.listen(8124, function() { //'listening' listener
console.log('server bound');
});
*/