Skip to content

Commit

Permalink
Add api to get device id
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jul 12, 2024
1 parent 9b55d11 commit 6477f58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ class Device extends DeviceBase {
return r.serial;
}

/**
* Get the device's id.
*
* Supported platforms:
* - Gen 3 (since Device OS 0.9.0)
* - Gen 2 (since Device OS 1.5.0)
*
* @param {Object} [options] Options.
* @param {Number} [options.timeout] Timeout (milliseconds).
* @return {Promise<String>}
*/
async getDeviceId({ timeout = globalOptions.requestTimeout } = {}) {
const r = await this.sendProtobufRequest(
'GetDeviceIdRequest',
null,
{ timeout }
);
return r.id;
}

/**
* Perform the system reset.
*
Expand Down
8 changes: 8 additions & 0 deletions src/device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('Device', () => {
expect(result).to.eql(exampleSerialNumber);
});

it('provides getDeviceId()', async () => {
const exampleDeviceId = '0123456789abcdef';
sinon.stub(device, 'sendProtobufRequest').resolves({ id: exampleDeviceId });
const result = await device.getDeviceId();
expect(device.sendProtobufRequest).to.have.property('callCount', 1);
expect(result).to.eql(exampleDeviceId);
});

it('provides enterListeningMode()', async () => {
sinon.stub(device, 'sendProtobufRequest');
device.sendProtobufRequest.onCall(0).resolves({});
Expand Down
5 changes: 5 additions & 0 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ const { definitions: proto } = require('@particle/device-os-protobuf');

// Mapping of request types to Protobuf messages
const Request = {
GET_DEVICE_ID: {
id: 20,
request: proto.GetDeviceIdRequest,
reply: proto.GetDeviceIdReply
},
GET_SERIAL_NUMBER: {
id: 21,
request: proto.GetSerialNumberRequest,
Expand Down

0 comments on commit 6477f58

Please sign in to comment.