From 87542a09cc815c7755a2a161c787946f9f1f29d3 Mon Sep 17 00:00:00 2001 From: 639852 Date: Fri, 16 Feb 2024 12:14:50 +0400 Subject: [PATCH] feat: added shadows mocks --- mocks/es/shadow.ts | 10 ------- mocks/es/shadows.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 10 deletions(-) delete mode 100644 mocks/es/shadow.ts create mode 100644 mocks/es/shadows.ts diff --git a/mocks/es/shadow.ts b/mocks/es/shadow.ts deleted file mode 100644 index d660974..0000000 --- a/mocks/es/shadow.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { createRouterTransport } from '@connectrpc/connect' -import { ShadowService } from '../../build/es/node/node_connect'; - -export const transport = createRouterTransport(({ service }) => { - service(ShadowService, { - get() { - return { shadows: [] } - } - }) -}) diff --git a/mocks/es/shadows.ts b/mocks/es/shadows.ts new file mode 100644 index 0000000..54b649d --- /dev/null +++ b/mocks/es/shadows.ts @@ -0,0 +1,63 @@ +import { Struct, Timestamp } from '@bufbuild/protobuf' +import { createPromiseClient, createRouterTransport } from '@connectrpc/connect' +import { DevicesService, ShadowService } from 'infinimesh-proto/build/es/node/node_connect' +import { QueryRequest } from 'infinimesh-proto/build/es/node/node_pb' +import { ConnectionState, GetResponse, Shadow, State } from 'infinimesh-proto/build/es/shadow/shadow_pb' +import { transport as devicesTransport } from './devices.ts' + +export const transport = createRouterTransport(({ service }) => { + const devicesApi = createPromiseClient(DevicesService, devicesTransport) + const shadows = new Map() + + devicesApi.list(new QueryRequest()).then(({ devices }) => { + devices.forEach(({ uuid }) => { + const length = Math.floor(Math.random() * 6) + const timestamp = new Timestamp({ seconds: BigInt(Math.round(Date.now() / 1000)) }) + + shadows.set(uuid, new Shadow({ + connection: new ConnectionState({ + clientId: `${length}`, connected: true, timestamp + }), + desired: new State({ data: new Struct(), timestamp }), + reported: new State({ data: new Struct(), timestamp }), + device: uuid + })) + }) + }) + + service(ShadowService, { + get (request) { + return new GetResponse({ + shadows: request.pool.map((uuid) => shadows.get(uuid)) as Shadow[] + }) + }, + patch (request) { + shadows.set(request.device, request) + return new Shadow(request) + }, + remove (request) { + const result = shadows.get(request.device) + + shadows.delete(request.device) + return new Shadow(result) + }, + streamShadow (request) { + return { + [Symbol.asyncIterator]: () => ({ + next: async () => (request.devices.length > 0) + ? { value: new Shadow(shadows.get(request.devices.pop() as string)), done: false } + : { value: null, done: true } + }) + } + }, + streamShadowSync (request) { + return { + [Symbol.asyncIterator]: () => ({ + next: async () => (request.devices.length < 1) + ? { value: new Shadow(shadows.get(request.devices.pop() as string)), done: false } + : { value: null, done: true } + }) + } + } + }) +})