1 | #!/usr/bin/env node
|
2 |
|
3 | const cli = require('commander');
|
4 | const Panda = require('./lib').default;
|
5 | const wait = require('./src/delay');
|
6 | const package = require('./package');
|
7 |
|
8 | cli
|
9 | .version(package.version)
|
10 | .description('Dump data from connected Panda over either USB or wifi. Uses pandajs under the hood.')
|
11 | .option('-w, --wifi', 'Connect to Panda over wifi instead of USB')
|
12 | .option('-a, --all', 'Print every message instead of just summaries (VERY spammy, one message per line JSON encoded)')
|
13 | .option('-n, --no-health', 'Don\'t print startup/connection messages (useful with --all and output redirection)')
|
14 | .option('-e, --no-errors', 'Don\'t print errors either')
|
15 | .option('-i, --index <i>', 'Choose a different connected panda than the first one (zero indexed)', parseInt)
|
16 | .parse(process.argv);
|
17 |
|
18 | var panda = new Panda({
|
19 | wifi: cli.wifi,
|
20 | selectDevice: (devices) => {
|
21 | return devices[Math.min(devices.length, cli.index || 0)];
|
22 | }
|
23 | });
|
24 |
|
25 | panda.onMessage(function (msg) {
|
26 | if (cli.all) {
|
27 | msg.forEach((m) => console.log(JSON.stringify(m, function(k, v) {
|
28 | if (!k) {
|
29 | return v;
|
30 | }
|
31 | if (k === 'data' && v.type === 'Buffer') {
|
32 | return v.data;
|
33 |
|
34 | }
|
35 | return v;
|
36 | })));
|
37 | } else {
|
38 | console.log('Message count:', msg.length);
|
39 | console.log('First message CAN count:', msg[0].canMessages.length);
|
40 | console.log('First CAN message:', msg[0].canMessages[0]);
|
41 | }
|
42 | });
|
43 |
|
44 | panda.onError(function (err) {
|
45 | if (cli.errors) {
|
46 | console.error('Error:', err);
|
47 | process.exit(1);
|
48 | }
|
49 | });
|
50 |
|
51 | if (cli.health) {
|
52 | panda.onConnect(function (data) {
|
53 | console.log('Connected:', data);
|
54 | });
|
55 | panda.onDisconnect(function (data) {
|
56 | console.log('Disconnected:', data);
|
57 | });
|
58 | }
|
59 |
|
60 | connectAndRun();
|
61 |
|
62 | async function connectAndRun () {
|
63 | await panda.connect();
|
64 | if (cli.health) {
|
65 | var health = await panda.getHealth();
|
66 | console.log(health);
|
67 | console.log('Connect finished, waiting then reading all messages...');
|
68 | await wait(1000);
|
69 | console.log('Start reading...');
|
70 | }
|
71 | panda.unpause();
|
72 | }
|