Convert recordings to websocket.
This commit is contained in:
@@ -332,7 +332,6 @@
|
|||||||
"reconnecting": "Reconnecting",
|
"reconnecting": "Reconnecting",
|
||||||
"timeline_no_cameras": "No Frigate cameras to show in timeline",
|
"timeline_no_cameras": "No Frigate cameras to show in timeline",
|
||||||
"troubleshooting": "Check troubleshooting",
|
"troubleshooting": "Check troubleshooting",
|
||||||
"undecodable_response": "Could not decode response from Home Assistant for request",
|
|
||||||
"unknown": "Unknown error",
|
"unknown": "Unknown error",
|
||||||
"upgrade_available": "An automated card configuration upgrade is available, please visit the visual card editor",
|
"upgrade_available": "An automated card configuration upgrade is available, please visit the visual card editor",
|
||||||
"webrtc_card_reported_error": "WebRTC Card reported an error",
|
"webrtc_card_reported_error": "WebRTC Card reported an error",
|
||||||
|
|||||||
@@ -334,7 +334,6 @@
|
|||||||
"reconnecting": "Reconectando",
|
"reconnecting": "Reconectando",
|
||||||
"timeline_no_cameras": "Nenhuma câmera do Frigate para mostrar na linha do tempo",
|
"timeline_no_cameras": "Nenhuma câmera do Frigate para mostrar na linha do tempo",
|
||||||
"troubleshooting": "Verifique a solução de problemas",
|
"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",
|
"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",
|
"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",
|
"webrtc_card_reported_error": "O cartão WebRTC relatou um erro",
|
||||||
|
|||||||
+15
-9
@@ -2,7 +2,7 @@ import { HomeAssistant } from 'custom-card-helpers';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { localize } from '../localize/localize';
|
import { localize } from '../localize/localize';
|
||||||
import { CameraConfig, ExtendedHomeAssistant, FrigateCardError } from '../types';
|
import { CameraConfig, ExtendedHomeAssistant, FrigateCardError } from '../types';
|
||||||
import { homeAssistantHTTPRequest, homeAssistantWSRequest } from './ha';
|
import { homeAssistantWSRequest } from './ha';
|
||||||
|
|
||||||
export const FRIGATE_ICON_SVG_PATH =
|
export const FRIGATE_ICON_SVG_PATH =
|
||||||
'm 4.8759466,22.743573 c 0.0866,0.69274 0.811811,1.16359 0.37885,1.27183 ' +
|
'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,
|
client_id: string,
|
||||||
camera_name: string,
|
camera_name: string,
|
||||||
): Promise<RecordingSummary> => {
|
): Promise<RecordingSummary> => {
|
||||||
return await homeAssistantHTTPRequest(
|
return await homeAssistantWSRequest(
|
||||||
hass,
|
hass,
|
||||||
recordingSummarySchema,
|
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,
|
before: Date,
|
||||||
after: Date,
|
after: Date,
|
||||||
): Promise<RecordingSegments> => {
|
): Promise<RecordingSegments> => {
|
||||||
return await homeAssistantHTTPRequest(
|
return await homeAssistantWSRequest(
|
||||||
hass,
|
hass,
|
||||||
recordingSegmentsSchema,
|
recordingSegmentsSchema,
|
||||||
`/api/frigate/${client_id}/${camera_name}/recordings`,
|
{
|
||||||
new URLSearchParams({
|
type: "frigate/recordings/get",
|
||||||
before: String(before.getTime() / 1000),
|
instance_id: client_id,
|
||||||
after: String(after.getTime() / 1000),
|
camera: camera_name,
|
||||||
}),
|
before: Math.floor(before.getTime() / 1000),
|
||||||
|
after: Math.ceil(after.getTime() / 1000),
|
||||||
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -83,67 +83,6 @@ export async function homeAssistantSignPath(
|
|||||||
return hass.hassUrl(response.path);
|
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 {
|
interface HassStateDifference {
|
||||||
entity: string;
|
entity: string;
|
||||||
oldState?: HassEntity;
|
oldState?: HassEntity;
|
||||||
|
|||||||
Reference in New Issue
Block a user