Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

pandajs cleanup #12

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build and test pandajs

on: [push, pull_request]

jobs:
build_test:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
id: yarn-cache
with:
path: node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- if: steps.yarn-cache.outputs.cache-hit != 'true'
run: |
yarn install --ignore-scripts
yarn add --ignore-scripts usb
- run: yarn build
- run: yarn test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ or
import Panda from '@commaai/pandajs';

// create instance
var panda = new Panda();
const panda = new Panda();

// register listener
panda.onMessage((msg) => {
Expand Down
45 changes: 22 additions & 23 deletions control-panda.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const cli = require('commander');
const PandaLib = require('./lib');
const Panda = PandaLib.default;
const wait = require('./src/delay');
const package = require('./package');

cli
Expand Down Expand Up @@ -57,7 +56,7 @@ if (!process.argv.slice(2).length) {
cli.help();
}

var safetyModes = [];
const safetyModes = [];
Object.keys(PandaLib).forEach(function (key) {
if (key.startsWith('SAFETY_')) {
safetyModes.push(key.substr(7).toLowerCase());
Expand All @@ -67,7 +66,7 @@ Object.keys(PandaLib).forEach(function (key) {
cli.parse(process.argv);

async function setupPanda () {
var panda = new Panda({
const panda = new Panda({
wifi: cli.wifi
});

Expand All @@ -84,37 +83,37 @@ async function setupPanda () {

// command implementations
async function getVersion () {
var panda = await setupPanda();
var result = await panda.getVersion();
const panda = await setupPanda();
const result = await panda.getVersion();
console.log(result);
}
async function getSecret () {
var panda = await setupPanda();
var result = await panda.getSecret();
const panda = await setupPanda();
const result = await panda.getSecret();
console.log(result);
}

async function isWhite () {
var panda = await setupPanda();
var result = await panda.isWhite();
const panda = await setupPanda();
const result = await panda.isWhite();
console.log('is white:', result);
}

async function isGrey () {
var panda = await setupPanda();
var result = await panda.isGrey();
const panda = await setupPanda();
const result = await panda.isGrey();
console.log('is grey:', result);
}

async function isBlack () {
var panda = await setupPanda();
var result = await panda.isBlack();
const panda = await setupPanda();
const result = await panda.isBlack();
console.log('is black:', result);
}

async function hasObd () {
var panda = await setupPanda();
var result = await panda.hasObd();
const panda = await setupPanda();
const result = await panda.hasObd();
console.log('has OBD port:', result);
}

Expand All @@ -125,8 +124,8 @@ async function setObd (connected, cmd) {
}
obd = connected === "true" || connected === "1";
console.log('OBD port:', obd ? 'connected' : 'disconnected');
var panda = await setupPanda();
var result = await panda.setObd(obd);
const panda = await setupPanda();
const result = await panda.setObd(obd);
console.log(result);
}

Expand All @@ -135,21 +134,21 @@ async function setSafetyMode (mode, cmd) {
console.error('Safety mode must be one of the following:', '\n\t' + safetyModes.join('\n\t'));
return;
}
var modeConst = PandaLib['SAFETY_' + mode.toUpperCase()];
const 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);
const panda = await setupPanda();
const result = await panda.setSafetyMode(modeConst);
console.log(result);
}

async function getWifi () {
var panda = await setupPanda();
var result = await panda.getDeviceMetadata();
const panda = await setupPanda();
const result = await panda.getDeviceMetadata();
console.log('SID: panda-' + result[0]);
console.log('Password:', result[1]);
}

async function getHealth () {
var panda = await setupPanda();
const panda = await setupPanda();
console.log(await panda.getHealth());
}
4 changes: 2 additions & 2 deletions dump-can.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cli
.option('-i, --index <i>', 'Choose a different connected panda than the first one (zero indexed)', parseInt)
.parse(process.argv);

var panda = new Panda({
const panda = new Panda({
wifi: cli.wifi,
selectDevice: (devices) => {
return devices[Math.min(devices.length, cli.index || 0)];
Expand Down Expand Up @@ -62,7 +62,7 @@ connectAndRun();
async function connectAndRun () {
await panda.connect();
if (cli.health) {
var health = await panda.getHealth();
const health = await panda.getHealth();
console.log(health);
console.log('Connect finished, waiting then reading all messages...');
await wait(1000);
Expand Down
2 changes: 1 addition & 1 deletion src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const SAFETY_HONDA_BOSCH_HARNESS = 20;
export default function Panda (options) {
options = options || {};

var device = new PandaDevice(options, navigator.usb);
const device = new PandaDevice(options, navigator.usb);
options.device = device;
return new PandaAPI(options);
}
42 changes: 21 additions & 21 deletions src/delay.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
'use strict';

class CancelError extends Error {
constructor(message) {
super(message);
this.name = 'CancelError';
}
constructor(message) {
super(message);
this.name = 'CancelError';
}
}

const createDelay = willResolve => (ms, value) => {
let timeoutId;
let internalReject;
let timeoutId;
let internalReject;

const delayPromise = new Promise((resolve, reject) => {
internalReject = reject;
const delayPromise = new Promise((resolve, reject) => {
internalReject = reject;

timeoutId = setTimeout(() => {
const settle = willResolve ? resolve : reject;
settle(value);
}, ms);
});
timeoutId = setTimeout(() => {
const settle = willResolve ? resolve : reject;
settle(value);
}, ms);
});

delayPromise.cancel = () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
internalReject(new CancelError('Delay canceled'));
}
};
delayPromise.cancel = () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
internalReject(new CancelError('Delay canceled'));
}
};

return delayPromise;
return delayPromise;
};

module.exports = createDelay(true);
Expand Down
12 changes: 5 additions & 7 deletions src/impl/browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { packCAN, unpackCAN } from 'can-message';
import { packCAN } from 'can-message';
import Event from 'weakmap-event';
import { partial } from 'ap';
import now from 'performance-now';
import wait from '../delay';

const PANDA_VENDOR_ID = 0xbbaa;
Expand Down Expand Up @@ -54,12 +53,11 @@ export default class Panda {
index: data.index
};

var result = await this.device.controlTransferIn(controlParams, length);
result = {
const result = await this.device.controlTransferIn(controlParams, length);
return {
data: Buffer.from(result.data.buffer),
status: result.status
};
return result;
}

async vendorWrite(data, length) {
Expand Down Expand Up @@ -90,8 +88,8 @@ export default class Panda {
}

async nextMessage() {
var result = null;
var attempts = 0;
let result = null;
let attempts = 0;

while (result === null) {
try {
Expand Down
26 changes: 11 additions & 15 deletions src/impl/node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import USB from 'usb';
import { packCAN, unpackCAN } from 'can-message';
import { packCAN } from 'can-message';
import Event from 'weakmap-event';
import { partial } from 'ap';
import now from 'performance-now';
import wait from '../delay';
import isPromise from 'is-promise';

Expand All @@ -26,17 +25,15 @@ export default class Panda {
}

async findDevice() {
var devices = USB.getDeviceList();

devices = devices.filter((device) => {
const devices = USB.getDeviceList().filter((device) => {
return device.deviceDescriptor.idVendor === PANDA_VENDOR_ID;
});

return this.selectDevice(devices);
}
async selectDevice(devices) {
return new Promise((resolve, reject) => {
var result = this.selectDeviceMethod(devices, resolve);
const result = this.selectDeviceMethod(devices, resolve);

if (result) {
if (isPromise(result)) {
Expand Down Expand Up @@ -141,7 +138,7 @@ export default class Panda {
endpointNumber = endpointNumber | 0x80;

return new Promise(async (resolve, reject) => {
var endpoint = null;
let endpoint = null;
this.device.interfaces.some(iface => {
const epoint = iface.endpoint(endpointNumber);

Expand All @@ -151,19 +148,19 @@ export default class Panda {
}
});
if (!endpoint) {
let err = new Error('PandaJS: nodeusb: transferIn failed to find endpoint interface ' + endpointNumber);
const err = new Error('PandaJS: nodeusb: transferIn failed to find endpoint interface ' + endpointNumber);
ErrorEvent.broadcast(this, err);
return reject(err);
}
if (endpoint.direction !== 'in') {
let err = new Error('PandaJS: nodeusb: endpoint interface is ' + endpoint.direction + ' instead of in');
const err = new Error('PandaJS: nodeusb: endpoint interface is ' + endpoint.direction + ' instead of in');
ErrorEvent.broadcast(this, err);
return reject(err);
}
var data = Buffer.from([]);
while (data.length === 0) {
let data;
do {
data = await this.endpointTransfer(endpoint, length);
}
} while (data.length === 0);
resolve(data);
});
}
Expand All @@ -180,10 +177,9 @@ export default class Panda {
}

async nextMessage() {
var result = null;
var attempts = 0;
let attempts = 0;

while (result === null) {
while (true) {
try {
return await this.transferIn(1, BUFFER_SIZE);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/impl/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const desiredDeviceString = 'it worked';
const deviceList = [false, false, desiredDeviceString, false];

test('selectDevice can return any way', async function (t) {
var panda = new PandaNode({
let panda = new PandaNode({
selectDevice: selectDeviceReturn
});
t.equals(await panda.selectDevice(deviceList), desiredDeviceString, 'works when returning directly');
Expand Down
Loading