diff --git a/packages/data-sdk/src/Fleet.ts b/packages/data-sdk/src/Fleet.ts index e77880005..097827768 100644 --- a/packages/data-sdk/src/Fleet.ts +++ b/packages/data-sdk/src/Fleet.ts @@ -216,22 +216,32 @@ export class Fleet { if (!Array.isArray(streamNameOrStreamNames)) { streamNames = [streamNameOrStreamNames]; } - const data = await fetch(`${FORMANT_API_URL}/v1/queries/queries`, { - method: "POST", - body: JSON.stringify({ + const query = JSON.parse( + JSON.stringify({ deviceIds, - end: end.toISOString(), names: streamNames, - start: start.toISOString(), + start, + end, tags, - }), - headers: { - "Content-Type": "application/json", - Authorization: "Bearer " + Authentication.token, - }, - }); - const telemetry = await data.json(); - return telemetry.items; + }) + ); + let nextToken = null; + let results: any = { items: [] }; + do { + if (nextToken) query.next = nextToken; + const data = await fetch(`${FORMANT_API_URL}/v1/queries/queries`, { + method: "POST", + body: JSON.stringify(query), + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + Authentication.token, + }, + }); + const telemetry = await data.json(); + nextToken = telemetry.next; + results.items = [...results.items, ...telemetry.items]; + } while (nextToken !== undefined); + return results.items; } static async getFileUrl(uuid: string) { diff --git a/packages/ui-sdk/src/hooks/index.ts b/packages/ui-sdk/src/hooks/index.ts index 131ae81ed..58155703b 100644 --- a/packages/ui-sdk/src/hooks/index.ts +++ b/packages/ui-sdk/src/hooks/index.ts @@ -2,3 +2,5 @@ export { default as useDevice } from "./useDevice"; export { default as useQueryTelemetry } from "./useQueryTelemetry"; export { default as useQueryTelemetryByDays } from "./useQueryTelemetryByDays"; export { default as useLatestTelemetry } from "./useLatestTelemetry"; +export { default as useModuleDataListener } from "./useModuleDataListener"; + diff --git a/packages/ui-sdk/src/hooks/useModuleDataListener.ts b/packages/ui-sdk/src/hooks/useModuleDataListener.ts new file mode 100644 index 000000000..8c7dce717 --- /dev/null +++ b/packages/ui-sdk/src/hooks/useModuleDataListener.ts @@ -0,0 +1,64 @@ +import { useEffect, useState } from "react"; +import { App, ModuleData } from "@formant/data-sdk"; + +interface data { + agentId: string; + deviceId: string; + name: string; + tags: { [key: string]: string }; + points: [number, any][]; + type: string; +} + +interface Stream { + loading: boolean; + tooMuchData: boolean; + data: data[]; + type: string; +} + +type Streams = { [key: string]: any }; + +//Intended to be use for custom modules within Formant + +const useModuleDataListener = () => { + const [streams, setStreams] = useState({}); + useEffect(() => { + App.addModuleDataListener(_cleanData); + }, []); + + const _cleanData = (moduleData: ModuleData) => { + let streams: any = Object.values(moduleData.streams); + //check is is there any stream coming in + if (streams.length === 0) { + throw new Error("No streams."); + } + setStreams( + streams.reduce((_: any, stream: Stream) => { + if (stream === undefined) { + throw new Error("No stream."); + } + if (stream.loading) { + return undefined; + } + if (stream.tooMuchData) { + throw new Error("Too much data."); + } + if (stream.data.length === 0) { + throw new Error("No data."); + } + const latestPoint = stream.data[0].points.at(-1); + + if (!latestPoint) { + throw new Error("No datapoints."); + } + + return { ..._, [stream.data[0].name]: latestPoint[1] }; + }, {}) + ); + }; + //Returns an object with stream name as key and last know value + return streams; +}; + +export default useModuleDataListener;