35 lines
1 KiB
TypeScript
35 lines
1 KiB
TypeScript
import { api, API_BASE_URL } from './client';
|
|
|
|
export interface RawTelemetryPacket {
|
|
id: string;
|
|
timestamp: number; // unix seconds
|
|
lat: number;
|
|
lon: number;
|
|
alt: number;
|
|
payload: Record<string, unknown>;
|
|
raw_data: Record<string, unknown>;
|
|
}
|
|
|
|
/** Derives a WebSocket URL from the configured API base URL. */
|
|
export function buildWsUrl(satelliteId: string): string {
|
|
let base = API_BASE_URL;
|
|
if (!base.startsWith('http')) {
|
|
base = `${window.location.protocol}//${window.location.host}${base}`;
|
|
}
|
|
const url = new URL(base);
|
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
return `${url.origin}${url.pathname}/ws/satellite/${satelliteId}/telemetry/`;
|
|
}
|
|
|
|
export const telemetryApi = {
|
|
fetchHistory: async (
|
|
satelliteId: string,
|
|
params?: { from?: number; till?: number },
|
|
): Promise<RawTelemetryPacket[]> => {
|
|
const res = await api.get<RawTelemetryPacket[] | { results: RawTelemetryPacket[] }>(
|
|
`/${satelliteId}/telemetry/`,
|
|
{ query: params },
|
|
);
|
|
return Array.isArray(res) ? res : res.results;
|
|
},
|
|
};
|