UNPKG

2.83 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const cli = require('commander');
4const PandaLib = require('./lib');
5const Panda = PandaLib.default;
6const wait = require('./src/delay');
7const package = require('./package');
8
9cli
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
15cli
16 .command('version')
17 .description('Request the version number of the Panda unit')
18 .action(getVersion);
19cli
20 .command('health')
21 .description('Get health information from connected Panda device')
22 .action(getHealth);
23cli
24 .command('secret')
25 .description('Request the secret from the Panda device, this is also the wifi password')
26 .action(getSecret);
27cli
28 .command('wifi')
29 .description('Get wifi SID and password for the connected Panda device')
30 .action(getWifi);
31cli
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);
35cli
36 .command('safety <mode>')
37 .description('Set the current safety mode')
38 .action(setSafetyMode);
39
40if (!process.argv.slice(2).length) {
41 cli.help();
42}
43
44var safetyModes = [];
45Object.keys(PandaLib).forEach(function (key) {
46 if (key.startsWith('SAFETY_')) {
47 safetyModes.push(key.substr(7).toLowerCase());
48 }
49});
50
51cli.parse(process.argv);
52
53async 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// command implementations
70async function getVersion () {
71 var panda = await setupPanda();
72 var result = await panda.getVersion();
73 console.log(result);
74}
75async function getSecret () {
76 var panda = await setupPanda();
77 var result = await panda.getSecret();
78 console.log(result);
79}
80
81async function isGrey () {
82 var panda = await setupPanda();
83 var result = await panda.isGrey();
84 console.log(result);
85}
86
87async 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
99async 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
106async function getHealth () {
107 var panda = await setupPanda();
108 console.log(await panda.getHealth());
109}