diff --git a/go.mod b/go.mod index fd9dc2c..b8acbb2 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( connectrpc.com/connect v1.14.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe - google.golang.org/grpc v1.61.0 + google.golang.org/grpc v1.61.1 google.golang.org/protobuf v1.32.0 ) diff --git a/go.sum b/go.sum index 4128e55..72721c1 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe h1: google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA= google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe h1:bQnxqljG/wqi4NTXu2+DJ3n7APcEA882QZ1JvhQAq9o= google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s= -google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= -google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= +google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY= +google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= diff --git a/mocks/es/devices.ts b/mocks/es/devices.ts index 29bc20b..d2a0d11 100644 --- a/mocks/es/devices.ts +++ b/mocks/es/devices.ts @@ -13,10 +13,10 @@ export const transport = createRouterTransport(({ service }) => { const nodes = new Map() function changeDevice(key: string, uuid: string, value: any) { - const device = devices.get(uuid) ?? new Device({ uuid }) + const device: any = devices.get(uuid) ?? new Device({ uuid }) for (const devices of devicesByNs.values()) { - const device = devices.find(({ uuid: id }) => id === uuid) + const device: any = devices.find(({ uuid: id }) => id === uuid) if (device) { device[key] = value diff --git a/mocks/es/namespaces.ts b/mocks/es/namespaces.ts index c6d468a..bef1144 100644 --- a/mocks/es/namespaces.ts +++ b/mocks/es/namespaces.ts @@ -12,8 +12,8 @@ export const transport = createRouterTransport(({ service }) => { const namespaces = new Map() const accountsList = new Map() - const accountsByNs = new Map() - const nodes = new Map() + const accountsByNs: any = new Map() + const nodes: any = new Map() const role = Math.floor(Math.random() * 2 + 1) const level = Math.floor(Math.random() * 4 + 1) @@ -88,7 +88,7 @@ export const transport = createRouterTransport(({ service }) => { }, join(request) { const account = accountsByNs.get(request.namespace)?.find( - ({ uuid }) => uuid === request.account + ({ uuid }: any) => uuid === request.account ) if (account?.access) { 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 } + }) + } + } + }) +}) diff --git a/mocks/es/timeseries.ts b/mocks/es/timeseries.ts index fb81e1e..7f6d161 100644 --- a/mocks/es/timeseries.ts +++ b/mocks/es/timeseries.ts @@ -12,16 +12,16 @@ export const transport = createRouterTransport(({ service }) => { const devicesApi = createPromiseClient(DevicesService, devicesTransport) const metrics = new Map>() - function getField () { + function getField() { return [50, 40, 30, 20, 10].map((num) => { const ts = BigInt(Date.now() - num * 3600 * 1000) const value = Value.fromJson(Math.floor(Math.random() * 101)) - return new DataPoint({ ts, value }) + return new DataPoint({ ts, value: +value }) }) } - function getMetrics (uuid: string) { + function getMetrics(uuid: string) { const metricsList: Metric[] = [] metrics.get(uuid)?.forEach((value, name) => { @@ -54,26 +54,26 @@ export const transport = createRouterTransport(({ service }) => { }) service(TimeseriesService, { - read (request) { + read(request: any) { const fieldsInfo: MetricInfo[] = [] if (!metrics.has(request.device)) { metrics.set(request.device, new Map()) } - request.fields.forEach((field) => { + request.fields?.forEach((field: any) => { const fields = metrics.get(request.device) const dataPoints = fields?.get(field)?.filter(({ ts }) => ts >= request.from && (request.to) ? ts <= request.to : true ) - - fieldsInfo.push(new MetricInfo({ field, dataPoints })) + const dto = { field, dataPoints } + fieldsInfo.push(new MetricInfo(dto)) }) - return new ReadResponse({ fieldsInfo }) + return new ReadResponse({ metricsInfo: fieldsInfo }) }, - async stat (request) { + async stat(request) { await new Promise((resolve) => setTimeout(resolve, 300)) const deviceMetrics: DeviceMetric[] = [] @@ -99,7 +99,7 @@ export const transport = createRouterTransport(({ service }) => { return new StatResponse({ deviceMetrics }) }, - write (request) { + write(request: any) { const dataPoint = request.dataPoint ?? new DataPoint() const metric = new Map([[request.field, [dataPoint]]]) const value = metrics.get(request.device) @@ -114,7 +114,7 @@ export const transport = createRouterTransport(({ service }) => { return new WriteResponse({ result: true, ts: BigInt(Date.now()) }) }, - writeBulk (request) { + writeBulk(request: any) { const dataPoint = request.dataPoint ?? new DataPoint() const metric = new Map([[request.field, [dataPoint]]]) const value = metrics.get(request.device) @@ -129,7 +129,7 @@ export const transport = createRouterTransport(({ service }) => { return new WriteBulkResponse({ result: true }) }, - async flush (request) { + async flush(request) { if (request.namespace) { const { devices } = await devicesApi.list(new QueryRequest({ namespace: request.namespace