-
Notifications
You must be signed in to change notification settings - Fork 19
/
init.js
84 lines (64 loc) · 1.59 KB
/
init.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
var argv = require('minimist')(process.argv.slice(2), {
string: ['id', 'basebuffer']
});
var socketcan = require('socketcan');
//set vcan0 as default
argv.i = argv.i || "vcan0";
var cmdOptions = [{
opt: "-i",
desc: "can interface (default vcan0)"
}];
module.exports.cmdOptions = cmdOptions;
module.exports.printOptions = function(exit) {
console.log("Command Options: ");
cmdOptions.forEach(function(opt) {
console.log(opt.opt + ": " + opt.desc);
});
if (exit) {
process.exit();
}
};
//get IDs for modules that may need them. parse as base 16 and provide as array
if (Array.isArray(argv.id)) {
argv.id.forEach(function(id, ind, arr) {
arr[ind] = parseInt(id, 16);
});
} else {
argv.id = [parseInt(argv.id, 16)];
}
module.exports.argv = argv;
module.exports.decToHex = function(d, padlength) {
padlength = padlength || 3;
var hex = Number(d).toString(16).toUpperCase();
while (hex.length < padlength) {
hex = "0" + hex;
}
return hex;
};
var msgCount = 0;
// console.error("creating channel on " + argv.i, typeof(argv.i));
var can = socketcan.createRawChannel(argv.i);
module.exports.onMessage = function(cb) {
can.addListener("onMessage", cb);
};
can.addListener("onMessage", function() {
msgCount += 1;
});
can.start();
module.exports.send = function(msg){
can.send(msg);
};
var closecallbacks = [];
module.exports.onClose = function(cb) {
closecallbacks.push(cb);
};
process.on('SIGINT', function() {
can.stop();
if (closecallbacks.length === 0) {
console.error("total messages: ", msgCount);
}
closecallbacks.forEach(function(cb) {
cb(msgCount);
});
process.exit();
});