import * as plugins from './plugins.js'; export interface IShxApiClientOptions { hubUrl?: string; useSocket?: boolean; } export class SmarthomeExchangeApiClient { private hubUrl: string; public typedrouter = new plugins.typedrequest.TypedRouter(); public typedsocketClient?: plugins.typedsocket.TypedSocket; constructor(optionsArg: IShxApiClientOptions = {}) { this.hubUrl = (optionsArg.hubUrl || 'http://localhost:8080').replace(/\/$/, ''); } private get httpEndpoint() { return `${this.hubUrl}/typedrequest`; } private createWsRequest(operationArg: string) { return this.typedsocketClient?.createTypedRequest(operationArg); } private createHttpRequest(operationArg: string) { return new plugins.typedrequest.TypedRequest(this.httpEndpoint, operationArg); } private async fire( operationArg: string, payloadArg: T['request'] ): Promise { const wsRequest = this.createWsRequest(operationArg); if (wsRequest) { return wsRequest.fire(payloadArg); } return this.createHttpRequest(operationArg).fire(payloadArg); } public async start() { this.typedsocketClient = await plugins.typedsocket.TypedSocket.createClient( this.typedrouter, this.hubUrl ); } public async stop() { await this.typedsocketClient?.stop(); this.typedsocketClient = undefined; } public async listDevices(requestArg: plugins.shxInterfaces.request.IReq_ListDevices['request'] = {}) { return this.fire('listDevices', requestArg); } public async listAgents() { return this.fire('listAgents', {}); } public async listTools(requestArg: plugins.shxInterfaces.request.IReq_ListTools['request'] = {}) { return this.fire('listTools', requestArg); } public async getHomeSnapshot() { return this.fire('getHomeSnapshot', {}); } public async executeToolPlan(planArg: plugins.shxInterfaces.data.IToolPlan) { return this.fire('executeToolPlan', { plan: planArg, }); } public async listApprovals(requestArg: plugins.shxInterfaces.request.IReq_ListApprovals['request'] = {}) { return this.fire('listApprovals', requestArg); } public async submitApproval(requestArg: plugins.shxInterfaces.request.IReq_SubmitApproval['request']) { return this.fire('submitApproval', requestArg); } }