Merge pull request #746 from dermotduffy/recordings-over-ws

Convert recordings to websocket.
This commit is contained in:
Dermot Duffy
2022-06-29 21:58:53 -07:00
committed by GitHub
4 changed files with 15 additions and 72 deletions
-1
View File
@@ -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",
-1
View File
@@ -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",
+15 -9
View File
@@ -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<RecordingSummary> => {
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<RecordingSegments> => {
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),
}
);
};
-61
View File
@@ -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<T>(
hass: ExtendedHomeAssistant,
schema: ZodSchema<T>,
url: string,
params?: URLSearchParams,
): Promise<T> {
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<T>(parseResult.error),
});
}
return parseResult.data;
}
interface HassStateDifference {
entity: string;
oldState?: HassEntity;