From cf5f099dc19c0007072d80cd601e3837dbf32fcc Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Wed, 29 Jun 2022 21:42:16 -0700 Subject: [PATCH] Convert recordings to websocket. --- src/localize/languages/en.json | 1 - src/localize/languages/pt-BR.json | 1 - src/utils/frigate.ts | 24 +++++++----- src/utils/ha/index.ts | 61 ------------------------------- 4 files changed, 15 insertions(+), 72 deletions(-) diff --git a/src/localize/languages/en.json b/src/localize/languages/en.json index 72a82ca5..75cb97d8 100644 --- a/src/localize/languages/en.json +++ b/src/localize/languages/en.json @@ -332,7 +332,6 @@ "reconnecting": "Reconnecting", "timeline_no_cameras": "No Frigate cameras to show in timeline", "troubleshooting": "Check troubleshooting", - "undecodable_response": "Could not decode response from Home Assistant for request", "unknown": "Unknown error", "upgrade_available": "An automated card configuration upgrade is available, please visit the visual card editor", "webrtc_card_reported_error": "WebRTC Card reported an error", diff --git a/src/localize/languages/pt-BR.json b/src/localize/languages/pt-BR.json index ee76f868..32abb5da 100644 --- a/src/localize/languages/pt-BR.json +++ b/src/localize/languages/pt-BR.json @@ -334,7 +334,6 @@ "reconnecting": "Reconectando", "timeline_no_cameras": "Nenhuma câmera do Frigate para mostrar na linha do tempo", "troubleshooting": "Verifique a solução de problemas", - "undecodable_response": "Não foi possível decodificar a resposta do Home Assistant para solicitação", "unknown": "Erro desconhecido", "upgrade_available": "Uma atualização automatizada da configuração do cartão está disponível, visite o editor visual do cartão", "webrtc_card_reported_error": "O cartão WebRTC relatou um erro", diff --git a/src/utils/frigate.ts b/src/utils/frigate.ts index ebb226a2..ef18c18f 100644 --- a/src/utils/frigate.ts +++ b/src/utils/frigate.ts @@ -2,7 +2,7 @@ import { HomeAssistant } from 'custom-card-helpers'; import { z } from 'zod'; import { localize } from '../localize/localize'; import { CameraConfig, ExtendedHomeAssistant, FrigateCardError } from '../types'; -import { homeAssistantHTTPRequest, homeAssistantWSRequest } from './ha'; +import { homeAssistantWSRequest } from './ha'; export const FRIGATE_ICON_SVG_PATH = 'm 4.8759466,22.743573 c 0.0866,0.69274 0.811811,1.16359 0.37885,1.27183 ' + @@ -70,10 +70,14 @@ export const getRecordingsSummary = async ( client_id: string, camera_name: string, ): Promise => { - return await homeAssistantHTTPRequest( + return await homeAssistantWSRequest( hass, recordingSummarySchema, - `/api/frigate/${client_id}/${camera_name}/recordings/summary`, + { + type: "frigate/recordings/summary", + instance_id: client_id, + camera: camera_name + } ); }; @@ -93,14 +97,16 @@ export const getRecordingSegments = async ( before: Date, after: Date, ): Promise => { - return await homeAssistantHTTPRequest( + return await homeAssistantWSRequest( hass, recordingSegmentsSchema, - `/api/frigate/${client_id}/${camera_name}/recordings`, - new URLSearchParams({ - before: String(before.getTime() / 1000), - after: String(after.getTime() / 1000), - }), + { + type: "frigate/recordings/get", + instance_id: client_id, + camera: camera_name, + before: Math.floor(before.getTime() / 1000), + after: Math.ceil(after.getTime() / 1000), + } ); }; diff --git a/src/utils/ha/index.ts b/src/utils/ha/index.ts index 8f07606c..963a014a 100644 --- a/src/utils/ha/index.ts +++ b/src/utils/ha/index.ts @@ -83,67 +83,6 @@ export async function homeAssistantSignPath( return hass.hassUrl(response.path); } -/** - * Make a HomeAssistant HTTP request. May throw. - * @param hass The HomeAssistant object to send the request with. - * @param schema The expected Zod schema of the response. - * @param request The request to make. - * @returns The parsed valid response or null on malformed. - */ -export async function homeAssistantHTTPRequest( - hass: ExtendedHomeAssistant, - schema: ZodSchema, - url: string, - params?: URLSearchParams, -): Promise { - let signResponse: string | null | undefined; - try { - signResponse = await homeAssistantSignPath(hass, url); - } catch (e) {} - - if (!signResponse) { - throw new FrigateCardError(localize('error.failed_sign'), { - url: url.toString(), - }); - } - - const signedURL = new URL(signResponse); - - if (params) { - for (const [key, value] of params.entries()) { - signedURL.searchParams.append(key, value); - } - } - - const response = await fetch(signedURL.toString()); - if (!response.ok) { - throw new FrigateCardError(localize('error.failed_response'), { - url: signedURL.toString(), - status: response.status, - statusText: response.statusText, - }); - } - - let raw_json; - try { - raw_json = await response.json(); - } catch (e) { - throw new FrigateCardError(localize('error.undecodable_response'), { - url: signedURL.toString(), - }); - } - - const parseResult = schema.safeParse(raw_json); - if (!parseResult.success) { - throw new FrigateCardError(localize('error.invalid_response'), { - url: signedURL.toString(), - raw_json: raw_json, - invalid_keys: getParseErrorKeys(parseResult.error), - }); - } - return parseResult.data; -} - interface HassStateDifference { entity: string; oldState?: HassEntity;