Skip to content

Commit

Permalink
Use platform helper
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Feb 2, 2024
1 parent 124a3ca commit 069f7bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
26 changes: 9 additions & 17 deletions src/cmd/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const spinnerMixin = require('../lib/spinner-mixin');
const { ensureError } = require('../lib/utilities');
const FlashCommand = require('./flash');
const usbUtils = require('./usb-util');
const deviceConstants = require('@particle/device-constants');
const { platformForId } = require('../lib/platform');
const { FirmwareModuleDisplayNames } = require('../lib/require-optional')('particle-usb');

// TODO: DRY this up somehow
Expand Down Expand Up @@ -258,8 +258,7 @@ module.exports = class SerialCommand extends CLICommandBase {

// If the device is a cellular device, obtain imei and iccid
try {
const platform = Object.values(deviceConstants).find(p => p.id === device.platformId);
const features = (platform && platform.features) || [];
const features = platformForId(device.platformId).features;
if (features.includes('cellular')) {
const cellularMetrics = await device.getCellularInfo();
cellularImei = cellularMetrics.imei;
Expand Down Expand Up @@ -317,16 +316,14 @@ module.exports = class SerialCommand extends CLICommandBase {
// We expect either one Wifi interface or one Ethernet interface
// Find it and return the hw address value from that interface
for (const iface of networkIfaceListreply) {
if (macAddress) {
break;
}
const index = iface.index;
const type = iface.type;

if (type === 'WIFI' || type === 'ETHERNET') {
const networkIfaceReply = await device.getNetworkInterface({ index, timeout: 2000 });
macAddress = networkIfaceReply.hwAddress;
currIfaceName = type;
break;
}
}

Expand All @@ -335,7 +332,7 @@ module.exports = class SerialCommand extends CLICommandBase {
this.ui.stdout.write(`Your device MAC address is ${chalk.bold.cyan(macAddress)}${os.EOL}`);
this.ui.stdout.write(`Interface is ${_.capitalize(currIfaceName)}${os.EOL}`);
} else {
this.ui.stdout.write(`Your device MAC address is ${chalk.bold.cyan('00:00:00:00:00:00')}${os.EOL}`);
this.ui.stdout.write(`Your device does not have a MAC address${os.EOL}`);
}
} catch (err) {
throw new VError(ensureError(err), 'Could not get MAC address');
Expand Down Expand Up @@ -365,15 +362,9 @@ module.exports = class SerialCommand extends CLICommandBase {
throw new VError(ensureError(err), 'Could not inspect device');
}

let platformName = null;
let platformId = null;
try {
platformName = deviceFromSerialPort.specs.productName;
platformId = deviceFromSerialPort.specs.productId;
} catch (err) {
// ignore error and move on to get other fields
}
this.ui.stdout.write(`Platform : ${platformId} - ${chalk.bold.cyan(platformName)}${os.EOL}${os.EOL}`);
const platform = platformForId(device.platformId);
this.ui.stdout.write(`Device : ${chalk.bold.cyan(deviceId)}${os.EOL}`);
this.ui.stdout.write(`Platform : ${platform.id} - ${chalk.bold.cyan(platform.name)}${os.EOL}${os.EOL}`);

try {
await this._getModuleInfo(device);
Expand All @@ -394,6 +385,7 @@ module.exports = class SerialCommand extends CLICommandBase {
*/
async _getModuleInfo(device) {
const modules = await device.getFirmwareModuleInfo({ timeout: 5000 });

if (modules && modules.length > 0) {
this.ui.stdout.write(chalk.underline(`Modules${os.EOL}`));
for (const m of modules) {
Expand All @@ -402,7 +394,7 @@ module.exports = class SerialCommand extends CLICommandBase {
this.ui.stdout.write(` Size: ${m.size/1000} kB${m.maxSize ? ` / MaxSize: ${m.maxSize/1000} kB` : ''}${os.EOL}`);

if (m.type === 'USER_PART' && m.hash) {
this.ui.stdout.write(` UUID:${m.hash}${os.EOL}`);
this.ui.stdout.write(` UUID: ${m.hash}${os.EOL}`);
}

const errors = m.validityErrors;
Expand Down
13 changes: 4 additions & 9 deletions src/cmd/serial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const MockSerial = require('../../test/__mocks__/serial.mock');
const { expect, sinon } = require('../../test/setup');
const SerialCommand = require('./serial');
const usbUtils = require('./usb-util');
const deviceConstants = require('@particle/device-constants');
const { PlatformId } = require('../lib/platform');

describe('Serial Command', () => {
let serial;
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('Serial Command', () => {
isOpen: true,
close: sinon.stub(),
firmwareVersion: fwVer,
platformId: deviceConstants.boron.id,
platformId: PlatformId.BORON,
getCellularInfo: sinon.stub().resolves({ iccid, imei }),
};
deviceStub.resolves(device);
Expand All @@ -62,7 +62,7 @@ describe('Serial Command', () => {
const device = {
isOpen: true,
close: sinon.stub(),
platformId: deviceConstants.p2.id,
platformId: PlatformId.P2,
firmwareVersion: fwVer
};
deviceStub.resolves(device);
Expand All @@ -84,15 +84,13 @@ describe('Serial Command', () => {
it('inspects a Particle device', async () => {
const fwVer = '5.6.0';
const wifiDeviceFromSerialPort = {
'specs': {
'name': 'p2'
},
deviceId: '1234456789abcdef'
};

const device = {
isOpen: true,
close: sinon.stub(),
platformId: PlatformId.P2,
firmwareVersion: fwVer
};
deviceStub.resolves(device);
Expand Down Expand Up @@ -131,9 +129,6 @@ describe('Serial Command', () => {
it ('returns mac address of a device', async () => {
const fwVer = '5.6.0';
const wifiDeviceFromSerialPort = {
'specs': {
'name': 'p2'
},
deviceId: '1234456789abcdef'
};
const device = {
Expand Down

0 comments on commit 069f7bf

Please sign in to comment.