-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSDK-6838: add provisioning wrappers (#266)
- Loading branch information
1 parent
4b91d72
commit 7620980
Showing
5 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @vitest-environment happy-dom | ||
|
||
import { beforeEach, expect, it, vi } from 'vitest'; | ||
import { ProvisioningServiceClient } from '../gen/provisioning/v1/provisioning_pb_service'; | ||
import { RobotClient } from '../robot'; | ||
import { ProvisioningClient } from './client'; | ||
|
||
let provisioning: ProvisioningClient; | ||
|
||
const testProvisioningInfo = { | ||
fragmentId: 'id', | ||
model: 'model', | ||
manufacturer: 'manufacturer', | ||
}; | ||
const testNetworkInfo = { | ||
type: 'type', | ||
ssid: 'ssid', | ||
security: 'security', | ||
signal: 999, | ||
connected: 'true', | ||
lastError: 'last error', | ||
}; | ||
const testSmartMachineStatus = { | ||
provisioningInfo: testProvisioningInfo, | ||
hasSmartMachineCredentials: true, | ||
isOnline: true, | ||
latestConnectionAttempt: testNetworkInfo, | ||
errorsList: ['error', 'err'], | ||
}; | ||
|
||
beforeEach(() => { | ||
RobotClient.prototype.createServiceClient = vi | ||
.fn() | ||
.mockImplementation( | ||
() => new ProvisioningServiceClient('test-provisioning') | ||
); | ||
|
||
ProvisioningServiceClient.prototype.getSmartMachineStatus = vi | ||
.fn() | ||
.mockImplementation((_req, _md, cb) => { | ||
cb(null, { | ||
toObject: () => testSmartMachineStatus, | ||
}); | ||
}); | ||
|
||
ProvisioningServiceClient.prototype.getNetworkList = vi | ||
.fn() | ||
.mockImplementation((_req, _md, cb) => { | ||
cb(null, { | ||
toObject: () => ({ networksList: [testNetworkInfo] }), | ||
}); | ||
}); | ||
|
||
provisioning = new ProvisioningClient(new RobotClient('host')); | ||
}); | ||
|
||
it('getSmartMachineStatus', async () => { | ||
await expect(provisioning.getSmartMachineStatus()).resolves.toStrictEqual( | ||
testSmartMachineStatus | ||
); | ||
}); | ||
|
||
it('getNetworkList', async () => { | ||
await expect(provisioning.getNetworkList()).resolves.toStrictEqual([ | ||
testNetworkInfo, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { type Options } from '../types'; | ||
import pb from '../gen/provisioning/v1/provisioning_pb'; | ||
import { ProvisioningServiceClient } from '../gen/provisioning/v1/provisioning_pb_service'; | ||
import { RobotClient } from '../robot'; | ||
import type { Provisioning } from './provisioning'; | ||
import { promisify } from '../utils'; | ||
import { type CloudConfig, encodeCloudConfig } from './types'; | ||
|
||
export class ProvisioningClient implements Provisioning { | ||
private client: ProvisioningServiceClient; | ||
private readonly options: Options; | ||
|
||
constructor(client: RobotClient, options: Options = {}) { | ||
this.client = client.createServiceClient(ProvisioningServiceClient); | ||
this.options = options; | ||
} | ||
|
||
private get service() { | ||
return this.client; | ||
} | ||
|
||
async getSmartMachineStatus() { | ||
const { service } = this; | ||
const request = new pb.GetSmartMachineStatusRequest(); | ||
this.options.requestLogger?.(request); | ||
|
||
const response = await promisify< | ||
pb.GetSmartMachineStatusRequest, | ||
pb.GetSmartMachineStatusResponse | ||
>(service.getSmartMachineStatus.bind(service), request); | ||
return response.toObject(); | ||
} | ||
|
||
async setNetworkCredentials(type: string, ssid: string, psk: string) { | ||
const { service } = this; | ||
const request = new pb.SetNetworkCredentialsRequest(); | ||
request.setType(type); | ||
request.setSsid(ssid); | ||
request.setPsk(psk); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
await promisify< | ||
pb.SetNetworkCredentialsRequest, | ||
pb.SetNetworkCredentialsResponse | ||
>(service.setNetworkCredentials.bind(service), request); | ||
} | ||
|
||
async setSmartMachineCredentials(cloud?: CloudConfig) { | ||
const { service } = this; | ||
const request = new pb.SetSmartMachineCredentialsRequest(); | ||
if (cloud) { | ||
request.setCloud(encodeCloudConfig(cloud)); | ||
} | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
await promisify< | ||
pb.SetSmartMachineCredentialsRequest, | ||
pb.SetSmartMachineCredentialsResponse | ||
>(service.setSmartMachineCredentials.bind(service), request); | ||
} | ||
|
||
async getNetworkList() { | ||
const { service } = this; | ||
const request = new pb.GetNetworkListRequest(); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const response = await promisify< | ||
pb.GetNetworkListRequest, | ||
pb.GetNetworkListResponse | ||
>(service.getNetworkList.bind(service), request); | ||
return response.toObject().networksList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { CloudConfig, NetworkInfo, SmartMachineStatus } from './types'; | ||
|
||
export interface Provisioning { | ||
/** Get the status of the Smart Machine */ | ||
getSmartMachineStatus: () => Promise<SmartMachineStatus>; | ||
|
||
/** | ||
* Set the network credentials of the Smart Machine, so it can connect to the | ||
* internet. | ||
* | ||
* @param type - The type of network. | ||
* @param ssid - The SSID of the network. | ||
* @param psk - The network's passkey. | ||
*/ | ||
setNetworkCredentials: ( | ||
type: string, | ||
ssid: string, | ||
psk: string | ||
) => Promise<void>; | ||
|
||
/** | ||
* Set the Viam credentials of the smart machine credentials, so it connect to | ||
* the Cloud. | ||
* | ||
* @param cloud - The configuration of the Cloud. | ||
*/ | ||
setSmartMachineCredentials: (cloud?: CloudConfig) => Promise<void>; | ||
|
||
/** Get the networks that are visible to the Smart Machine. */ | ||
getNetworkList: () => Promise<NetworkInfo[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pb from '../gen/provisioning/v1/provisioning_pb'; | ||
|
||
export type SmartMachineStatus = pb.GetSmartMachineStatusResponse.AsObject; | ||
export type NetworkInfo = pb.NetworkInfo.AsObject; | ||
export type CloudConfig = pb.CloudConfig.AsObject; | ||
|
||
export const encodeCloudConfig = ( | ||
obj: pb.CloudConfig.AsObject | ||
): pb.CloudConfig => { | ||
const result = new pb.CloudConfig(); | ||
result.setId(obj.id); | ||
result.setSecret(obj.secret); | ||
result.setAppAddress(obj.appAddress); | ||
return result; | ||
}; |