This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
control-panda.js
executable file
·155 lines (137 loc) · 4.2 KB
/
control-panda.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
const cli = require('commander');
const PandaLib = require('./lib');
const Panda = PandaLib.default;
const wait = require('./src/delay');
const package = require('./package');
cli
.version(package.version)
.description('Control various settings and modes of your Panda device')
.option('-e, --no-errors', 'Don\'t print any errors')
.option('-w, --wifi', 'Connect to Panda over wifi instead of USB');
cli
.command('version')
.description('Request the version number of the Panda unit')
.action(getVersion);
cli
.command('health')
.description('Get health information from connected Panda device')
.action(getHealth);
cli
.command('secret')
.description('Request the secret from the Panda device, this is also the wifi password')
.action(getSecret);
cli
.command('wifi')
.description('Get wifi SID and password for the connected Panda device')
.action(getWifi);
cli
.command('is-white')
.description('Ask the connected Panda if it is a white Panda or not. Outputs "true" or "false"')
.action(isWhite);
cli
.command('is-grey')
.description('Ask the connected Panda if it is a grey Panda or not. Outputs "true" or "false"')
.action(isGrey);
cli
.command('is-black')
.description('Ask the connected Panda if it is a black Panda or not. Outputs "true" or "false"')
.action(isBlack);
cli
.command('has-obd')
.description('Ask the connected Panda if it has an OBD port connection or not. Outputs "true" or "false"')
.action(hasObd);
cli
.command('obd <connected>')
.description('Connect CAN bus 1 (zero-based numbering) to the OBD port (if supported and parameter is "true"')
.action(setObd);
cli
.command('safety <mode>')
.description('Set the current safety mode')
.action(setSafetyMode);
if (!process.argv.slice(2).length) {
cli.help();
}
var safetyModes = [];
Object.keys(PandaLib).forEach(function (key) {
if (key.startsWith('SAFETY_')) {
safetyModes.push(key.substr(7).toLowerCase());
}
});
cli.parse(process.argv);
async function setupPanda () {
var panda = new Panda({
wifi: cli.wifi
});
panda.onError(function (err) {
if (cli.errors) {
console.error('Error:', err);
process.exit(1);
}
});
await panda.connect();
return panda;
}
// command implementations
async function getVersion () {
var panda = await setupPanda();
var result = await panda.getVersion();
console.log(result);
}
async function getSecret () {
var panda = await setupPanda();
var result = await panda.getSecret();
console.log(result);
}
async function isWhite () {
var panda = await setupPanda();
var result = await panda.isWhite();
console.log('is white:', result);
}
async function isGrey () {
var panda = await setupPanda();
var result = await panda.isGrey();
console.log('is grey:', result);
}
async function isBlack () {
var panda = await setupPanda();
var result = await panda.isBlack();
console.log('is black:', result);
}
async function hasObd () {
var panda = await setupPanda();
var result = await panda.hasObd();
console.log('has OBD port:', result);
}
async function setObd (connected, cmd) {
if (connected !== "true" && connected !== "false" && connected !== "0" && connected !== "1") {
console.error('Connected must be true or false');
return;
}
obd = connected === "true" || connected === "1";
console.log('OBD port:', obd ? 'connected' : 'disconnected');
var panda = await setupPanda();
var result = await panda.setObd(obd);
console.log(result);
}
async function setSafetyMode (mode, cmd) {
if (safetyModes.indexOf(mode) === -1) {
console.error('Safety mode must be one of the following:', '\n\t' + safetyModes.join('\n\t'));
return;
}
var modeConst = PandaLib['SAFETY_' + mode.toUpperCase()];
console.log('Activing safety mode:', mode, '(0x' + modeConst.toString(16) + ')');
var panda = await setupPanda();
var result = await panda.setSafetyMode(modeConst);
console.log(result);
}
async function getWifi () {
var panda = await setupPanda();
var result = await panda.getDeviceMetadata();
console.log('SID: panda-' + result[0]);
console.log('Password:', result[1]);
}
async function getHealth () {
var panda = await setupPanda();
console.log(await panda.getHealth());
}