Skip to content

Commit

Permalink
RSDK-6838: add provisioning wrappers (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
purplenicole730 authored Mar 28, 2024
1 parent 4b91d72 commit 7620980
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ update-buf: $(node_modules)
.PHONY: build-buf
build-buf: $(node_modules) clean-buf
$(buf) generate $$(./scripts/get-buf-lock-version.js buf.build/googleapis/googleapis)
$(buf) generate $$(./scripts/get-buf-lock-version.js buf.build/viamrobotics/api) --path common,component,robot,service,app
$(buf) generate $$(./scripts/get-buf-lock-version.js buf.build/viamrobotics/api) --path common,component,robot,service,app,provisioning
$(buf) generate $$(./scripts/get-buf-lock-version.js buf.build/erdaniels/gostream)
$(buf) generate $$(./scripts/get-buf-lock-version.js buf.build/viamrobotics/goutils)

Expand Down
67 changes: 67 additions & 0 deletions src/provisioning/client.test.ts
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,
]);
});
76 changes: 76 additions & 0 deletions src/provisioning/client.ts
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;
}
}
31 changes: 31 additions & 0 deletions src/provisioning/provisioning.ts
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[]>;
}
15 changes: 15 additions & 0 deletions src/provisioning/types.ts
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;
};

0 comments on commit 7620980

Please sign in to comment.