1 | # PandaJS
|
2 | Interract with your [panda OBD-II Dongle](https://shop.comma.ai/products/panda-obd-ii-dongle) from JavaScript.
|
3 |
|
4 | Supports
|
5 | * [x] Browser
|
6 | * [x] Node
|
7 | * [ ] React Native
|
8 |
|
9 | ## Installation
|
10 | `npm i --save @commaai/pandajs`
|
11 |
|
12 | or
|
13 |
|
14 | `yarn add @commaai/pandajs`
|
15 |
|
16 | ## Usage
|
17 | ```js
|
18 | import Panda from '@commaai/pandajs';
|
19 |
|
20 | // create instance
|
21 | var panda = new Panda();
|
22 |
|
23 | // register listener
|
24 | panda.onMessage((msg) => {
|
25 | console.log(msg.address, msg.busTime, msg.data, msg.bus);
|
26 | });
|
27 |
|
28 | // start reading data
|
29 | panda.start();
|
30 |
|
31 | // pause data
|
32 | panda.pause();
|
33 |
|
34 | // resume reading data
|
35 | panda.unpause();
|
36 |
|
37 | // close connects and take everything down
|
38 | panda.disconnect();
|
39 | ```
|
40 |
|
41 | ## API
|
42 |
|
43 | #### `new Panda([options])` -> `Panda`
|
44 | Creates a new Panda instance
|
45 |
|
46 | * `options` (*optional* object)
|
47 | * `selectDevice`: (*optional* `function(devices, callback)`) A user defined function for selecting which available device to use, parameters are an array of discovered devices and a callback function. The method can either return the desired device, return a promise, or use the callback function. There is no timeout for this method, so make sure it eventually does one of those three things. *This option does nothing in browser mode since webusb has it's own UI for selecting the device.*
|
48 | * `wifi`: (*optional* `boolean`) Enables wifi mode, communicates with the panda device over an already established wifi connection with it. This option will throw errors if you enable it in browser mode.
|
49 |
|
50 | ### Methods
|
51 | #### `Panda.connect()` -> `Promise: string`
|
52 | Tell the Panda instance to connect to USB. Does not start reading messages until you call `start`. On success, returns the ID of the USB device found.
|
53 |
|
54 | #### `Panda.disconnect()` -> `Promise: boolean`
|
55 | Disconnect from the USB device and stop everything. Returns true if it was running before.
|
56 |
|
57 | #### `Panda.start()` -> `Promise: string`
|
58 | Tell the Panda instance to connect to USB if it's not already and start reading messages. On success, returns the ID of the USB device found.
|
59 |
|
60 | This function is the same as running
|
61 | ```js
|
62 | panda.connect()
|
63 | .then(() => panda.unpause());
|
64 | ```
|
65 |
|
66 | #### `Panda.pause()` -> `boolean`
|
67 | Pauses reading in messages from the active connection. Returns true only if the stream was not already paused. While paused messages will queue up on the Panda device, however this queue has a limited size so pausing may result in missed messages.
|
68 |
|
69 | #### `Panda.unpause()` -> `boolean`
|
70 | Pauses reading in messages from the active connection. Returns true only if the stream was paused.
|
71 |
|
72 | #### `Panda.resume()` -> `boolean`
|
73 | Alias for `unpause`.
|
74 |
|
75 | #### `Panda.isConnected()` -> `boolean`
|
76 | Returns true if the panda instance is currecntly connected to a USB device.
|
77 |
|
78 | #### `Panda.isPaused()` -> `boolean`
|
79 | Returns true if the panda instance currently has message reading paused
|
80 |
|
81 | #### `Panda.getHealth()` -> `HealthStatus`
|
82 | Requests the current health of the Panda unit. Only available while connected, returns the health object on success or errors.
|
83 |
|
84 | * `HealthStatus`: (object)
|
85 | * `voltage`: (number)
|
86 | * `current`: (number)
|
87 | * `isStarted`: (boolean)
|
88 | * `controlsAreAllowed`: (boolean)
|
89 | * `isGasInterceptorDetector`: (boolean)
|
90 | * `isStartSignalDetected`: (boolean)
|
91 | * `isStartedAlt`: (boolean)
|
92 |
|
93 | #### `Panda.getDeviceMetadata()` -> `[string, string]`
|
94 | Requests both the serial number and secret from the Panda device. This is used internally to power `getSerialNumber` and `getSecret`.
|
95 |
|
96 | Returns an array with 2 items in, the serial number and then the secret.
|
97 |
|
98 | #### `Panda.getSerialNumber()` -> `string`
|
99 | Requests the serial number.
|
100 |
|
101 | This function is the same as running
|
102 | ```js
|
103 | panda.getDeviceMetadata()
|
104 | .then((result) => result[0])
|
105 | ```
|
106 |
|
107 | #### `Panda.getSecret()` -> `string`
|
108 | Requests the secret used for wifi among other things.
|
109 |
|
110 | This function is the same as running
|
111 | ```js
|
112 | panda.getDeviceMetadata()
|
113 | .then((result) => result[1])
|
114 | ```
|
115 |
|
116 | #### `Panda.getVersion()` -> `string`
|
117 | Requests the string version number, output will look something like `v1.0.1-11d45090-RELEASE`
|
118 |
|
119 | #### `Panda.isGrey()` -> `boolean`
|
120 | Query if the Panda device is a grey Panda or a normal Panda. Returns true if it is grey.
|
121 |
|
122 | #### `Panda.setSafetyMode(mode)` -> `void`
|
123 | Sets the safety mode on the Panda device.
|
124 |
|
125 | * `mode`: (*required* `SafetyMode`) The safety mode to enter. Must be a valid safety mode, safety modes are exposed as constants on the root of the library.
|
126 |
|
127 | ```js
|
128 | import { SAFETY_NOOUTPUT, SAFETY_HONDA, SAFETY_TOYOTA, SAFETY_HONDA_BOSCH, SAFETY_TOYOTA_NOLIMITS, SAFETY_ALLOUTPUT, SAFETY_ELM327 } from '@commaai/pandajs';
|
129 | ```
|
130 |
|
131 | ### Events
|
132 | #### `Panda.onMessage(listener)` -> `function`
|
133 | Register a handler for receiving messages. This should be done before calling `start` to avoid missing any messages. Returns an `unlisten` function that will disable the listener when called.
|
134 |
|
135 | * `listener`: (*required*, `function (messageData)`) Handler to call each time messages are received
|
136 | * `messageData`: (object)
|
137 | * `time`: (number) High precision time in which this event was received.
|
138 | * `canMessages`: (Array<CanMessage>)
|
139 | * `bus`: (number) The bus this message came from
|
140 | * `address`: (number) The bus specific address for the message
|
141 | * `busTime`: (number) The recieve time according to the bus
|
142 | * `data`: (ByteArray) Raw data for this message
|
143 |
|
144 | #### `Panda.onError(listener)` -> `function`
|
145 | Register an error handler. Returns an `unlisten` function that will disable the listener when called.
|
146 |
|
147 | * `listener`: (*required*, `function (err)`) Handler to call when errors occur
|
148 | * `err`: (object)
|
149 | * `error`: (Error) Error object incurred
|
150 | * `event`: (string) Description of where the error occured
|
151 |
|
152 | #### `Panda.onConnect(listener)` -> `function`
|
153 | Register a handler to run when successfully connecting to a Panda device. Returns an `unlisten` function that will disable the listener when called.
|
154 |
|
155 | * `listener`: (*required*, `function (usbId)`) Handler to call when connected
|
156 | * `usbId`: (string) The ID of the USB device connected to.
|
157 |
|
158 | #### `Panda.onDisconnect(listener)` -> `function`
|
159 | Register a handler to run when disconnecting from a Panda device. Returns an `unlisten` function that will disable the listener when called.
|
160 |
|
161 | * `listener`: (*required*, `function (usbId)`) Handler to call when disconnected
|
162 | * `usbId`: (string) The ID of the USB device disconnected from.
|
163 |
|
164 | ## Command Line Tools
|
165 | This package ships with 2 CLI tools. Check the `--help` on each to see all of their options.
|
166 |
|
167 | #### `dump-can`
|
168 | This is used to dump metadata about the incoming CAN messages, or output the entire messages with `-a`.
|
169 |
|
170 | Examples:
|
171 | ```bash
|
172 | $ dump-can
|
173 | Connected: ba435375436c2a6d
|
174 | { voltage: 12.123,
|
175 | current: 4.093,
|
176 | isStarted: false,
|
177 | controlsAreAllowed: false,
|
178 | isGasInterceptorDetector: false,
|
179 | isStartSignalDetected: false,
|
180 | isStartedAlt: false }
|
181 | Connect finished, waiting then reading all messages...
|
182 | Start reading...
|
183 | Message count: 1
|
184 | First message CAN count: 256
|
185 | First CAN message: { address: 420,
|
186 | busTime: 54858,
|
187 | data: <Buffer 00 66 00 02 00 00 00 0b>,
|
188 | bus: 0 }
|
189 | Message count: 1
|
190 | First message CAN count: 256
|
191 | First CAN message: { address: 1024,
|
192 | busTime: 6626,
|
193 | data: <Buffer 79 00 02 00 0a 78 00 09>,
|
194 | bus: 1 }
|
195 | ```
|
196 | ```bash
|
197 | $ dump-can -an
|
198 | {"time":7.3178732019999995,"canMessages":[{"address":1024,"busTime":53980,"data":[121,0,2,0,10,120,0,9],"bus":1},{"address":420,"busTime":54066,"data":[0,102,0,2,0,0,0,11],"bus":0},{"address":1024,"busTime":54115,"data":[121,0,2,0,10,120,0,9],"bus":1}]}
|
199 | {"time":7.318214842000001,"canMessages":[{"address":420,"busTime":54202,"data":[0,102,0,2,0,0,0,11],"bus":0},{"address":1024,"busTime":54250,"data":[121,0,2,0,10,120,0,9],"bus":1},{"address":420,"busTime":54338,"data":[0,102,0,2,0,0,0,11],"bus":0}]}
|
200 | {"time":7.31846793,"canMessages":[{"address":1024,"busTime":54385,"data":[121,0,2,0,10,120,0,9],"bus":1},{"address":420,"busTime":54474,"data":[0,102,0,2,0,0,0,11],"bus":0}]}
|
201 | ```
|
202 |
|
203 | #### `control-panda`
|
204 | This is a convenience tool wrapping each of the get and set methods exposed by this library.
|
205 |
|
206 | Examples:
|
207 | ```bash
|
208 | $ ./control-panda.js wifi
|
209 | SID: panda-2baf789dacdacb64
|
210 | Password: aGBw61RwD0
|
211 | ```
|
212 | ```bash
|
213 | $ ./control-panda.js version
|
214 | v1.0.1-11d45090-RELEASE
|
215 | ```
|
216 |
|
217 | ## Contributing
|
218 | `yarn run test`
|
219 |
|
220 | # License
|
221 | MIT @ comma.ai
|
222 |
|
223 | Read more about how to get started hacking your car with [panda](https://shop.comma.ai/products/panda-obd-ii-dongle) [here](https://medium.com/@comma_ai/a-panda-and-a-cabana-how-to-get-started-car-hacking-with-comma-ai-b5e46fae8646).
|