-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnfc_interface.js
More file actions
118 lines (101 loc) · 3.21 KB
/
nfc_interface.js
File metadata and controls
118 lines (101 loc) · 3.21 KB
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
var nfc = require('./node_modules/nfc/index').nfc
, util = require('util')
, version = nfc.version()
, devices = nfc.scan()
, fs = require('fs')
, http = require('http')
;
var http_options = { //put it in command line argsv
host: "10.0.20.99", // simulates blockchain rpc service IP
port: 8337 // simulates blockchain rpc service port
};
if (process.argv.length == 3) http_options['host'] = process.argv[2];
var deviceID
, auth_path = './auth_card/'
, master_path = './master_card/'
, program_mode=false
, go_fwd = true
, oldTime = Date.now()
, master_uid = ''
, permissions = 0;
;
function send_http_req(action, orig_uid, perms, cb) {
var received="";
var uid=orig_uid.replace(/:/g, "");
var m_uid=master_uid.replace(/:/g, "");
http_options['path'] = '/'+action+'?uid='+uid+'&m_uid='+m_uid+'&perms='+perms;
console.log(http_options);
var req = http.get(http_options, function(res) {
res.setEncoding("utf8"); //to emit events as strings
res.on("data", function(data) {
received=data;
cb(null, received);
});
});
req.on("error", function(err, msg) {
cb(err, msg);
});
req.end();
}
function read(deviceID) {
var nfcdev = new nfc.NFC();
nfcdev.on('read', function(tag) {
var nowTime = Date.now()
, permissions =0;
uid=tag.uid;
//console.log("\n============CARD UID:", uid);
if (nowTime > oldTime + 1000) { //wait 1 second to avoid double tap
oldTime=nowTime;
send_http_req("getperms", uid, 0, function (err, result) {
permissions = result;
//console.log("perms:", permissions);
if (permissions == 127) {
master_uid=uid;
program_mode=!program_mode;
if (program_mode) {
m_uid = uid;
//Set led to yellow
console.log("Program mode enabled, Master:", uid);
}
else {
m_uid = '';
//Set led to blinking (normal mode)
console.log("Program mode disabled, Master:", uid);
}
}
if (program_mode && (uid != master_uid)){
exists = (permissions == 1);
if (exists)
send_http_req("deluser", uid, 0, function (err, result) {
console.log("UID deleted:", result);
});
else send_http_req("adduser", uid, 1, function (err, result) {
console.log("UID added:", result);
});//1= basic access permissions
}
if (!program_mode) {
if (permissions == 1) {
//Set led to green and unlock
console.log("Access granted to UID:", uid);
}
else {
console.log("Not valid for access UID:", uid);
//Set led to red
}
}
});
}//only do all of the above if there is no double tap
else {
//console.log("Ignoring consecutive tap for UID:", uid);
}
}); //end of read event
nfcdev.on('error', function(err) {
console.log(util.inspect(err, { depth: null }));
});
nfcdev.on('stopped', function() {
console.log('***** Stopped');
});
console.log(nfcdev.start(deviceID));
} //end of read(device)
deviceID=Object.keys(devices)[0];
read(deviceID);