Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/ui-sdk/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 useStream } from "./useStream";
export { default as useStreamInTimeRange } from "./useStreamInTimeRange";
42 changes: 42 additions & 0 deletions packages/ui-sdk/src/hooks/useStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Authentication, Fleet } from "@formant/data-sdk";
import * as React from "react";

interface Stream {
currentValue: any;
currentValueTime: string;
deviceId: string;
id: string;
organizationId: string;
streamName: string;
streamType: string;
tags?: any;
}

const useStream = (streamName: string, deviceName: string) => {
const [stream, setStream] = React.useState();

React.useEffect(() => {
getStream();
}, [stream]);

const getStream = async () => {
try {
if (await Authentication.waitTilAuthenticated()) {
const _devices = await Fleet.getDevices();
const _device = _devices.filter((_) => _.name === deviceName)[0];
const _streams = await _device.getLatestTelemetry();
const _stream = _streams.filter(
(_: Stream) => _.streamName === streamName
);

setStream(_stream[0].currentValue);
}
} catch (err) {
throw new Error(err as string);
}
};

return stream;
};

export default useStream;
59 changes: 59 additions & 0 deletions packages/ui-sdk/src/hooks/useStreamInTimeRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Authentication, Fleet } from "@formant/data-sdk";
import * as React from "react";

type Point = [number, any];

interface TelemetryStream {
agentId: string;
deviceId: string;
name: string;
points: Point[];
tags: any;
type: string;
}

const useStreamInTimeRange = (
streamName: string,
deviceName: string,
range?: number
) => {
//Range will be in seconds
const [stream, setStream] = React.useState<Point[]>();

React.useEffect(() => {
getStream();
}, [stream]);

const timeout = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

const getStream = async () => {
await timeout(1000);
try {
if (await Authentication.waitTilAuthenticated()) {
const _devices = await Fleet.getDevices();
const _device = _devices.filter((_) => _.name === deviceName)[0];
const start = getStartISODate(range);
const end = new Date();
const _telemetry = await _device.getTelemetry(streamName, start, end);
setStream(_telemetry[0].points);
}
} catch (err) {
throw new Error(err as string);
}
};

return stream;
};

const getStartISODate = (range: number | undefined) => {
if (range === undefined) range = 1;
let minuteInMilli = 60000;
minuteInMilli = minuteInMilli * range;
let startDate = Date.now();
startDate = startDate - minuteInMilli;
return new Date(startDate);
};

export default useStreamInTimeRange;