Skip to content

Commit

Permalink
Remove dependency of extension from SerialPort
Browse files Browse the repository at this point in the history
  • Loading branch information
robmosca committed Dec 13, 2023
1 parent 38e20d6 commit 0b63247
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export type StorageStatus = {
slots: SlotsInfo;
};

type DeviceSpec = {
path?: string;
manufacturer?: string;
};

function checkSlotId(slotId: number) {
if (slotId < 0 || slotId > 19) {
throw new Error(`Invalid program slot index ${slotId}, valid slots: 0-19`);
Expand Down Expand Up @@ -298,4 +303,8 @@ export function removeDeviceAllListeners() {
device?.removeAllListeners();
}

export async function listDevices(): Promise<DeviceSpec[]> {
return _testing.SerialPortType.list();
}

export { _testing };
15 changes: 9 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
disconnectDevice,
getDeviceSlots,
isDeviceConnected,
listDevices,
moveProgramOnDevice,
runProgramOnDevice,
removeProgramFromDevice,
runProgramOnDevice,
stopProgramOnDevice,
uploadProgramToDevice,
} from './device';
import { existsSync } from 'fs';
import { SerialPort } from 'serialport';
import { basename } from 'path';

const toastDuration = 5000;
Expand Down Expand Up @@ -94,12 +94,15 @@ async function askDeviceName() {
}

async function askDeviceFromList(manualEntry: string) {
const serialPorts = await SerialPort.list();
const devices = await listDevices();

// using this promise in the quick-pick will cause a progress
// bar to show if there are no items.
const list = serialPorts
.filter((port) => port.manufacturer === 'LEGO System A/S')
.map((port) => port.path);
const list = devices
.filter(
(device) => device.manufacturer === 'LEGO System A/S' && device.path,
)
.map((device) => device.path!);

list.push(manualEntry);
const selected = await vscode.window.showQuickPick(list, {
Expand Down
21 changes: 17 additions & 4 deletions src/test/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
addDeviceOnChangeCallbak,
getDeviceInfo,
removeDeviceAllListeners,
listDevices,
} from '../device';
import { SerialPortMock } from 'serialport';
import * as chai from 'chai';
Expand Down Expand Up @@ -60,12 +61,11 @@ describe('Device', () => {
APIRequestStub.restore();
});

const createPortMock = (data: string, opts?: any) => {
return SerialPortMock.binding.createPort(deviceName, {
const createPortMock = (alternativeName?: string) => {
return SerialPortMock.binding.createPort(alternativeName || deviceName, {
echo: true,
record: true,
...opts,
readyData: Buffer.from(data),
readyData: Buffer.from(''),
});
};

Expand Down Expand Up @@ -464,4 +464,17 @@ describe('Device', () => {
expect(onChangeFn).to.have.been.calledOnce;
});
});

describe('listDevices', function () {
it('should list all available devices', async function () {
createPortMock('/dev/usbmodem1');
createPortMock('/dev/usbmodem2');

const devices = await listDevices();

expect(devices).to.have.lengthOf(2);
expect(devices[0].path).to.equal('/dev/usbmodem1');
expect(devices[1].path).to.equal('/dev/usbmodem2');
});
});
});

0 comments on commit 0b63247

Please sign in to comment.