1 | #!/usr/bin/env node
|
2 |
|
3 | const cli = require('commander');
|
4 | const PandaLib = require('./lib');
|
5 | const Panda = PandaLib.default;
|
6 | const wait = require('./src/delay');
|
7 | const package = require('./package');
|
8 |
|
9 | cli
|
10 | .version(package.version)
|
11 | .description('Control various settings and modes of your Panda device')
|
12 | .option('-e, --no-errors', 'Don\'t print any errors')
|
13 | .option('-w, --wifi', 'Connect to Panda over wifi instead of USB');
|
14 |
|
15 | cli
|
16 | .command('version')
|
17 | .description('Request the version number of the Panda unit')
|
18 | .action(getVersion);
|
19 | cli
|
20 | .command('health')
|
21 | .description('Get health information from connected Panda device')
|
22 | .action(getHealth);
|
23 | cli
|
24 | .command('secret')
|
25 | .description('Request the secret from the Panda device, this is also the wifi password')
|
26 | .action(getSecret);
|
27 | cli
|
28 | .command('wifi')
|
29 | .description('Get wifi SID and password for the connected Panda device')
|
30 | .action(getWifi);
|
31 | cli
|
32 | .command('is-grey')
|
33 | .description('Ask the connected Panda if it is a grey Panda or not. Outputs "true" or "false"')
|
34 | .action(isGrey);
|
35 | cli
|
36 | .command('safety <mode>')
|
37 | .description('Set the current safety mode')
|
38 | .action(setSafetyMode);
|
39 |
|
40 | if (!process.argv.slice(2).length) {
|
41 | cli.help();
|
42 | }
|
43 |
|
44 | var safetyModes = [];
|
45 | Object.keys(PandaLib).forEach(function (key) {
|
46 | if (key.startsWith('SAFETY_')) {
|
47 | safetyModes.push(key.substr(7).toLowerCase());
|
48 | }
|
49 | });
|
50 |
|
51 | cli.parse(process.argv);
|
52 |
|
53 | async function setupPanda () {
|
54 | var panda = new Panda({
|
55 | wifi: cli.wifi
|
56 | });
|
57 |
|
58 | panda.onError(function (err) {
|
59 | if (cli.errors) {
|
60 | console.error('Error:', err);
|
61 | process.exit(1);
|
62 | }
|
63 | });
|
64 |
|
65 | await panda.connect();
|
66 | return panda;
|
67 | }
|
68 |
|
69 |
|
70 | async function getVersion () {
|
71 | var panda = await setupPanda();
|
72 | var result = await panda.getVersion();
|
73 | console.log(result);
|
74 | }
|
75 | async function getSecret () {
|
76 | var panda = await setupPanda();
|
77 | var result = await panda.getSecret();
|
78 | console.log(result);
|
79 | }
|
80 |
|
81 | async function isGrey () {
|
82 | var panda = await setupPanda();
|
83 | var result = await panda.isGrey();
|
84 | console.log(result);
|
85 | }
|
86 |
|
87 | async function setSafetyMode (mode, cmd) {
|
88 | if (safetyModes.indexOf(mode) === -1) {
|
89 | console.error('Safety mode must be one of the following:', '\n\t' + safetyModes.join('\n\t'));
|
90 | return;
|
91 | }
|
92 | var modeConst = PandaLib['SAFETY_' + mode.toUpperCase()];
|
93 | console.log('Activing safety mode:', mode, '(0x' + modeConst.toString(16) + ')');
|
94 | var panda = await setupPanda();
|
95 | var result = await panda.setSafetyMode(modeConst);
|
96 | console.log(result);
|
97 | }
|
98 |
|
99 | async function getWifi () {
|
100 | var panda = await setupPanda();
|
101 | var result = await panda.getDeviceMetadata();
|
102 | console.log('SID: panda-' + result[0]);
|
103 | console.log('Password:', result[1]);
|
104 | }
|
105 |
|
106 | async function getHealth () {
|
107 | var panda = await setupPanda();
|
108 | console.log(await panda.getHealth());
|
109 | }
|