Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
busticated committed Feb 11, 2020
1 parent e364dea commit 5e9d958
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/cli/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ module.exports = ({ commandProcessor, root }) => {

commandProcessor.createCommand(variable, 'get', 'Retrieve a value from your device', {
params: '[device] [variableName]',
options: timeOption,
options: Object.assign({}, timeOption, {
'product': {
description: 'product id or slug'
}
}),
handler: (args) => {
const VariableCommand = require('../cmd/variable');
return new VariableCommand().getValue(args);
Expand Down
1 change: 1 addition & 0 deletions src/cli/variable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('Variable Command-Line Interface', () => {
'',
'Options:',
' --time Show the time when the variable was received [boolean]',
' --product product id or slug [string]',
'',
'Examples:',
' particle variable get basement temperature Read the temperature variable from the device basement',
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ module.exports = class ParticleApi {
return this._wrap(this.api.compileCode({ files, platformId, targetVersion, auth: this.accessToken }));
}

getVariable(deviceId, name, product){
return this._wrap(
this.api.getVariable({
name,
deviceId,
product,
auth: this.accessToken
})
);
}

downloadFirmwareBinary(binaryId, downloadPath){
return new Promise((resolve, reject) => {
const req = this.api.downloadFirmwareBinary({ binaryId, auth: this.accessToken });
Expand Down
44 changes: 43 additions & 1 deletion src/cmd/variable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const os = require('os');
const VError = require('verror');
const moment = require('moment');
const has = require('lodash/has');
Expand All @@ -6,8 +7,10 @@ const find = require('lodash/find');
const filter = require('lodash/filter');
const prompt = require('inquirer').prompt;
const settings = require('../../settings');
const { errors: { usageError } } = require('../app/command-processor');
const spinnerMixin = require('../lib/spinner-mixin');
const LegacyApiClient = require('../lib/api-client');
const ParticleAPI = require('./api');
const UI = require('../lib/ui');

const { normalizedApiError } = LegacyApiClient;
Expand All @@ -34,7 +37,32 @@ module.exports = class VariableCommand {
});
}

getValue({ time, params: { device, variableName } }){
getValue({ time, product, params: { device, variableName } }){
if (product){
if (!device){
throw usageError(
'`device` parameter is required when `--product` flag is set'
);
}

if (!variableName){
throw usageError(
`\`variableName\` parameter is required when \`--product\` flag is set. To view available variables, run: particle product device list ${product}`
);
}

const msg = `Fetching variable ${variableName} from device ${device} in product ${product}`;
const fetchVar = createAPI().getVariable(device, variableName, product);
return this.showBusySpinnerUntilResolved(msg, fetchVar)
.then(res => {
this.ui.stdout.write(`${res.result}${os.EOL}`);
})
.catch(error => {
const message = `Error fetching variable: \`${variableName}\``;
throw createAPIErrorResult({ error, message });
});
}

return Promise.resolve()
.then(() => {
if (!device && !variableName){
Expand Down Expand Up @@ -233,3 +261,17 @@ module.exports = class VariableCommand {
}
};


// UTILS //////////////////////////////////////////////////////////////////////
function createAPI(){
return new ParticleAPI(settings.apiUrl, {
accessToken: settings.access_token
});
}

function createAPIErrorResult({ error: e, message, json }){
const error = new VError(normalizedApiError(e), message);
error.asJSON = json;
return error;
}

12 changes: 11 additions & 1 deletion test/e2e/variable.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const cli = require('../lib/cli');
const {
DEVICE_ID,
DEVICE_NAME,
DEVICE_PLATFORM_NAME
DEVICE_PLATFORM_NAME,
PRODUCT_01_DEVICE_01_ID
} = require('../lib/env');


Expand Down Expand Up @@ -116,6 +117,15 @@ describe('Variable Commands [@device]', () => {
expect(exitCode).to.equal(0);
});

it.skip('Gets a variable from a product device by name', async () => {
const args = ['get', PRODUCT_01_DEVICE_01_ID, 'version'];
const { stdout, stderr, exitCode } = await cli.run(args);

expect(stdout).to.equal('42');
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});

it('Monitors a variable', async () => {
const args = ['variable', 'monitor', DEVICE_ID, 'version', '--delay', 1000];
const subprocess = cli.run(args);
Expand Down

0 comments on commit 5e9d958

Please sign in to comment.