forked from sbrinkmann/rc522-rfid
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
61 lines (51 loc) · 1.6 KB
/
main.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
var spawn = require('child_process').spawn;
var readline = require('readline');
var registeredCallback = null;
var child = null;
module.exports = exports = function(givenCallback){
registeredCallback = givenCallback;
};
var mainProcessShutdown = false;
var initChildProcess = function()
{
child = spawn("node", [__dirname + "/" + "rc522_output.js"]);
var linereader = readline.createInterface(child.stdout, child.stdin);
linereader.on('line', function (rfidTagSerialNumber) {
if(registeredCallback instanceof Function)
{
registeredCallback(rfidTagSerialNumber);
}
});
child.on('close', function(code) {
if(!mainProcessShutdown)
{
initChildProcess();
}
});
};
// SIGTERM AND SIGINT will trigger the exit event.
process.once("SIGTERM", function () {
process.exit(0);
});
process.once("SIGINT", function () {
process.exit(0);
});
// And the exit event shuts down the child.
process.once("exit", function () {
mainProcessShutdown = true;
child.kill();
});
// This is a somewhat ugly approach, but it has the advantage of working
// in conjunction with most of what third parties might choose to do with
// uncaughtException listeners, while preserving whatever the exception is.
process.once("uncaughtException", function (error) {
// If this was the last of the listeners, then shut down the child and rethrow.
// Our assumption here is that any other code listening for an uncaught
// exception is going to do the sensible thing and call process.exit().
if (process.listeners("uncaughtException").length === 0) {
mainProcessShutdown = true;
child.kill();
throw error;
}
});
initChildProcess();