Add WebRTC upgrade functionality.

This commit is contained in:
Dermot Duffy
2022-01-14 21:31:16 -08:00
parent 8305258a36
commit b616d2270f
2 changed files with 26 additions and 23 deletions
+24 -23
View File
@@ -7,6 +7,8 @@ import {
CONF_CAMERAS_ARRAY_CLIENT_ID, CONF_CAMERAS_ARRAY_CLIENT_ID,
CONF_CAMERAS_ARRAY_LABEL, CONF_CAMERAS_ARRAY_LABEL,
CONF_CAMERAS_ARRAY_URL, CONF_CAMERAS_ARRAY_URL,
CONF_CAMERAS_ARRAY_WEBRTC_ENTITY,
CONF_CAMERAS_ARRAY_WEBRTC_URL,
CONF_CAMERAS_ARRAY_ZONE, CONF_CAMERAS_ARRAY_ZONE,
CONF_EVENT_VIEWER_AUTOPLAY_CLIP, CONF_EVENT_VIEWER_AUTOPLAY_CLIP,
CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_SIZE, CONF_EVENT_VIEWER_CONTROLS_NEXT_PREVIOUS_SIZE,
@@ -31,7 +33,7 @@ import { RawFrigateCardConfig, RawFrigateCardConfigArray } from './types';
export const setConfigValue = ( export const setConfigValue = (
obj: RawFrigateCardConfig, obj: RawFrigateCardConfig,
keys: string | (string|number)[], keys: string | (string | number)[],
value: unknown, value: unknown,
): void => { ): void => {
dset(obj, keys, value); dset(obj, keys, value);
@@ -45,11 +47,11 @@ export const setConfigValue = (
*/ */
export const getConfigValue = ( export const getConfigValue = (
obj: RawFrigateCardConfig, obj: RawFrigateCardConfig,
keys: string | (string|number)[], keys: string | (string | number)[],
def?: unknown, def?: unknown,
): unknown => { ): unknown => {
// Need to manually split the key apart to use delve array accesses by number. // Need to manually split the key apart to use delve array accesses by number.
if (typeof(keys) === 'string') { if (typeof keys === 'string') {
keys = keys.split('.'); keys = keys.split('.');
} }
return delve(obj, keys, def); return delve(obj, keys, def);
@@ -149,7 +151,7 @@ export const moveConfigValue = (
obj: RawFrigateCardConfig, obj: RawFrigateCardConfig,
oldPath: string, oldPath: string,
newPath: string, newPath: string,
transform?: (valueIn: unknown) => (unknown), transform?: (valueIn: unknown) => unknown,
): boolean => { ): boolean => {
let value = getConfigValue(obj, oldPath); let value = getConfigValue(obj, oldPath);
if (transform) { if (transform) {
@@ -183,7 +185,7 @@ export const getArrayConfigPath = (path: string, index: number): string => {
const upgradeMoveTo = function ( const upgradeMoveTo = function (
oldPath: string, oldPath: string,
newPath: string, newPath: string,
transform?: (valueIn: unknown) => (unknown), transform?: (valueIn: unknown) => unknown,
): (obj: RawFrigateCardConfig) => boolean { ): (obj: RawFrigateCardConfig) => boolean {
return function (obj: RawFrigateCardConfig): boolean { return function (obj: RawFrigateCardConfig): boolean {
return moveConfigValue(obj, oldPath, newPath, transform); return moveConfigValue(obj, oldPath, newPath, transform);
@@ -195,37 +197,36 @@ const upgradeMoveTo = function (
* @param key A string key. * @param key A string key.
* @returns A safe key. * @returns A safe key.
*/ */
const upgradeToMultipleCameras = (): ((obj: RawFrigateCardConfig) => boolean) => { const upgradeToMultipleCameras = (): ((obj: RawFrigateCardConfig) => boolean) => {
return function (obj: RawFrigateCardConfig): boolean { return function (obj: RawFrigateCardConfig): boolean {
let modified = false; let modified = false;
const cameras = getConfigValue(obj, CONF_CAMERAS) as RawFrigateCardConfigArray;
let cameras = getConfigValue(obj, CONF_CAMERAS) as RawFrigateCardConfigArray; // Only do an upgrade if the cameras section does not exist.
if (!Array.isArray(cameras)) { if (cameras !== undefined) {
// Note: This will replace `cameras` if it already exists and isn't an return false;
// array.
cameras = []
} }
const imports = { const imports = {
'camera_entity': CONF_CAMERAS_ARRAY_CAMERA_ENTITY, camera_entity: CONF_CAMERAS_ARRAY_CAMERA_ENTITY,
'frigate.camera_name': CONF_CAMERAS_ARRAY_CAMERA_NAME, 'frigate.camera_name': CONF_CAMERAS_ARRAY_CAMERA_NAME,
'frigate.client_id': CONF_CAMERAS_ARRAY_CLIENT_ID, 'frigate.client_id': CONF_CAMERAS_ARRAY_CLIENT_ID,
'frigate.label': CONF_CAMERAS_ARRAY_LABEL, 'frigate.label': CONF_CAMERAS_ARRAY_LABEL,
'frigate.url': CONF_CAMERAS_ARRAY_URL, 'frigate.url': CONF_CAMERAS_ARRAY_URL,
'frigate.zone': CONF_CAMERAS_ARRAY_ZONE, 'frigate.zone': CONF_CAMERAS_ARRAY_ZONE,
} 'live.webrtc.entity': CONF_CAMERAS_ARRAY_WEBRTC_ENTITY,
const cameraIndex = cameras.length; 'live.webrtc.url': CONF_CAMERAS_ARRAY_WEBRTC_URL,
};
Object.keys(imports).forEach((key) => { Object.keys(imports).forEach((key) => {
const oldValue = getConfigValue(obj, key); modified = moveConfigValue(
if (oldValue !== undefined) { obj,
deleteConfigValue(obj, key) key,
setConfigValue(obj, getArrayConfigPath(imports[key], cameraIndex), oldValue); getArrayConfigPath(imports[key], 0),
modified = true; ) || modified;
} });
})
return modified; return modified;
} };
} };
const UPGRADES = [ const UPGRADES = [
// v1.2.1 -> v2.0.0 // v1.2.1 -> v2.0.0
+2
View File
@@ -12,6 +12,8 @@ export const CONF_CAMERAS_ARRAY_ZONE = 'cameras.#.zone';
export const CONF_CAMERAS_ARRAY_ID = 'cameras.#.id'; export const CONF_CAMERAS_ARRAY_ID = 'cameras.#.id';
export const CONF_CAMERAS_ARRAY_TITLE = 'cameras.#.title'; export const CONF_CAMERAS_ARRAY_TITLE = 'cameras.#.title';
export const CONF_CAMERAS_ARRAY_ICON = 'cameras.#.icon'; export const CONF_CAMERAS_ARRAY_ICON = 'cameras.#.icon';
export const CONF_CAMERAS_ARRAY_WEBRTC_ENTITY = 'cameras.#.webrtc.entity';
export const CONF_CAMERAS_ARRAY_WEBRTC_URL = 'cameras.#.webrtc.url';
export const CONF_VIEW_DEFAULT = 'view.default'; export const CONF_VIEW_DEFAULT = 'view.default';
export const CONF_VIEW_TIMEOUT = 'view.timeout'; export const CONF_VIEW_TIMEOUT = 'view.timeout';