Skip to content

Commit

Permalink
Cloning filtering from particle list command to the particle usb list…
Browse files Browse the repository at this point in the history
… command. Test are required.
  • Loading branch information
JamesHagerman committed Apr 23, 2020
1 parent 2da65b6 commit 8adf5c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cli/usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = ({ commandProcessor, root }) => {
const usb = commandProcessor.createCategory(root, 'usb', 'Control USB devices');

commandProcessor.createCommand(usb, 'list', 'List the devices connected to the host computer', {
params: '[filter]',
options: {
'exclude-dfu': {
description: 'Do not list devices which are in DFU mode',
Expand Down
34 changes: 32 additions & 2 deletions src/cmd/usb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { spin } = require('../app/ui');
const { asyncMapSeries } = require('../lib/utilities');
const { asyncMapSeries, knownPlatforms } = require('../lib/utilities');
const { getDevice, formatDeviceInfo } = require('./device-util');
const { getUsbDevices, openUsbDevice, openUsbDeviceById } = require('./usb-util');
const { systemSupportsUdev, udevRulesInstalled, installUdevRules } = require('./udev');
Expand All @@ -16,6 +16,30 @@ module.exports = class UsbCommand {
list(args) {
const idsOnly = args['ids-only'];
const excludeDfu = args['exclude-dfu'];
const filter = args.params.filter;

let filterFunc = null;
if (filter){
const platforms = knownPlatforms();
if (filter === 'online') {
filterFunc = (d) => {
return d.connected;
};
} else if (filter === 'offline') {
filterFunc = (d) => {
return !d.connected;
};
} else if (Object.keys(platforms).indexOf(filter) >= 0) {
filterFunc = (d) => {
return d.platform_id === platforms[filter];
};
} else {
filterFunc = (d) => {
return d.id === filter || d.name === filter;
};
}
}

// Enumerate USB devices
return getUsbDevices({ dfuMode: !excludeDfu })
.then(usbDevices => {
Expand Down Expand Up @@ -59,7 +83,9 @@ module.exports = class UsbCommand {
return {
id: usbDevice.id,
name: (device && device.name) ? device.name : '',
type: `${type.join(', ')}`
type: `${type.join(', ')}`,
platform_id: device.platform_id,
connected: device.connected
};
})
.finally(() => usbDevice.close());
Expand All @@ -73,6 +99,10 @@ module.exports = class UsbCommand {
console.log('No devices found.');
} else {
devices = devices.sort((a, b) => a.name.localeCompare(b.name)); // Sort devices by name

if (filter) {
devices = devices.filter(filterFunc);
}
devices.forEach(device => {
console.log(formatDeviceInfo(device));
});
Expand Down

0 comments on commit 8adf5c0

Please sign in to comment.