refactor: Break apart monolithic configuration schema (#1977)
[skip ci]
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
import { actionBaseSchema } from '../base';
|
||||
|
||||
export const advancedCameraCardCustomActionsBaseSchema = actionBaseSchema.extend({
|
||||
action: z
|
||||
.literal('fire-dom-event')
|
||||
.or(
|
||||
z
|
||||
.literal('custom:advanced-camera-card-action')
|
||||
.transform((): 'fire-dom-event' => 'fire-dom-event'),
|
||||
),
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
export const cameraSelectActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('camera_select'),
|
||||
camera: z.string().optional(),
|
||||
triggered: z.boolean().optional(),
|
||||
});
|
||||
export type CameraSelectActionConfig = z.infer<typeof cameraSelectActionConfigSchema>;
|
||||
@@ -0,0 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
import { viewDisplayModeSchema } from '../../common/display';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
export const viewDisplayModeActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('display_mode_select'),
|
||||
display_mode: viewDisplayModeSchema,
|
||||
});
|
||||
export type DisplayModeActionConfig = z.infer<typeof viewDisplayModeActionConfigSchema>;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
const GENERAL_ACTIONS = [
|
||||
'camera_ui',
|
||||
'default',
|
||||
'download',
|
||||
'expand',
|
||||
'fullscreen',
|
||||
'live_substream_off',
|
||||
'live_substream_on',
|
||||
'menu_toggle',
|
||||
'microphone_connect',
|
||||
'microphone_disconnect',
|
||||
'microphone_mute',
|
||||
'microphone_unmute',
|
||||
'mute',
|
||||
'pause',
|
||||
'play',
|
||||
'screenshot',
|
||||
'unmute',
|
||||
] as const;
|
||||
export type AdvancedCameraCardGeneralAction = (typeof GENERAL_ACTIONS)[number];
|
||||
|
||||
export const generalActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.enum(GENERAL_ACTIONS),
|
||||
});
|
||||
export type GeneralActionConfig = z.infer<typeof generalActionConfigSchema>;
|
||||
@@ -0,0 +1,16 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
// An action that can be used internally to call a callback (it is not possible
|
||||
// for the user to pass this through via the configuration).
|
||||
export const INTERNAL_CALLBACK_ACTION = '__INTERNAL_CALLBACK_ACTION__';
|
||||
export const internalCallbackActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal(INTERNAL_CALLBACK_ACTION),
|
||||
|
||||
// The callback is expected to be called with a CardController API object.
|
||||
callback: z.function().args(z.any()).returns(z.promise(z.void())),
|
||||
});
|
||||
export type InternalCallbackActionConfig = z.infer<
|
||||
typeof internalCallbackActionConfigSchema
|
||||
>;
|
||||
@@ -0,0 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
const LOG_ACTIONS_LEVELS = ['debug', 'info', 'warn', 'error'] as const;
|
||||
export type LogActionLevel = (typeof LOG_ACTIONS_LEVELS)[number];
|
||||
|
||||
export const logActionConfigSchema = advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('log'),
|
||||
message: z.string(),
|
||||
level: z.enum(LOG_ACTIONS_LEVELS).default('info'),
|
||||
});
|
||||
export type LogActionConfig = z.infer<typeof logActionConfigSchema>;
|
||||
@@ -0,0 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
export const mediaPlayerActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('media_player'),
|
||||
media_player: z.string(),
|
||||
media_player_action: z.enum(['play', 'stop']),
|
||||
});
|
||||
export type MediaPlayerActionConfig = z.infer<typeof mediaPlayerActionConfigSchema>;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
export const ptzControlsActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('ptz_controls'),
|
||||
enabled: z.boolean(),
|
||||
});
|
||||
export type PTZControlsActionConfig = z.infer<typeof ptzControlsActionConfigSchema>;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { z } from 'zod';
|
||||
import { panSchema } from '../../common/pan';
|
||||
import { zoomSchema } from '../../common/zoom';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
import { PTZ_ACTION_PHASES, PTZ_ACTIONS } from './ptz';
|
||||
|
||||
export const ptzDigitalActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('ptz_digital'),
|
||||
target_id: z.string().optional(),
|
||||
absolute: z
|
||||
.object({
|
||||
zoom: zoomSchema.optional(),
|
||||
pan: panSchema.optional(),
|
||||
})
|
||||
.optional(),
|
||||
ptz_action: z.enum(PTZ_ACTIONS).optional(),
|
||||
ptz_phase: z.enum(PTZ_ACTION_PHASES).optional(),
|
||||
});
|
||||
export type PTZDigitialActionConfig = z.infer<typeof ptzDigitalActionConfigSchema>;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
import { PTZ_ACTION_PHASES, PTZ_ACTIONS } from './ptz';
|
||||
|
||||
export const ptzMultiActionSchema = advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('ptz_multi'),
|
||||
target_id: z.string().optional(),
|
||||
|
||||
ptz_action: z.enum(PTZ_ACTIONS).optional(),
|
||||
ptz_phase: z.enum(PTZ_ACTION_PHASES).optional(),
|
||||
ptz_preset: z.string().optional(),
|
||||
});
|
||||
export type PTZMultiActionConfig = z.infer<typeof ptzMultiActionSchema>;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
const PTZ_PAN_TILT_ACTIONS = ['left', 'right', 'up', 'down'] as const;
|
||||
const PTZ_ZOOM_ACTIONS = ['zoom_in', 'zoom_out'] as const;
|
||||
const PTZ_BASE_ACTIONS = [...PTZ_PAN_TILT_ACTIONS, ...PTZ_ZOOM_ACTIONS] as const;
|
||||
export type PTZBaseAction = (typeof PTZ_BASE_ACTIONS)[number];
|
||||
|
||||
// PTZ actions as used by the PTZ control (includes a 'home' button).
|
||||
const PTZ_CONTROL_ACTIONS = [...PTZ_BASE_ACTIONS, 'home'] as const;
|
||||
export type PTZControlAction = (typeof PTZ_CONTROL_ACTIONS)[number];
|
||||
|
||||
// PTZ actions as used by the camera manager (includes generic presets).
|
||||
export const PTZ_ACTIONS = [...PTZ_BASE_ACTIONS, 'preset'] as const;
|
||||
export type PTZAction = (typeof PTZ_ACTIONS)[number];
|
||||
|
||||
export const PTZ_ACTION_PHASES = ['start', 'stop'] as const;
|
||||
export type PTZActionPhase = (typeof PTZ_ACTION_PHASES)[number];
|
||||
|
||||
export const ptzActionConfigSchema = advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('ptz'),
|
||||
camera: z.string().optional(),
|
||||
ptz_action: z.enum(PTZ_ACTIONS).optional(),
|
||||
ptz_phase: z.enum(PTZ_ACTION_PHASES).optional(),
|
||||
ptz_preset: z.string().optional(),
|
||||
});
|
||||
export type PTZActionConfig = z.infer<typeof ptzActionConfigSchema>;
|
||||
@@ -0,0 +1,16 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
const timeDeltaSchema = z.object({
|
||||
ms: z.number().optional(),
|
||||
s: z.number().optional(),
|
||||
m: z.number().optional(),
|
||||
h: z.number().optional(),
|
||||
});
|
||||
export type TimeDelta = z.infer<typeof timeDeltaSchema>;
|
||||
|
||||
export const sleepActionConfigSchema = advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('sleep'),
|
||||
duration: timeDeltaSchema.optional().default({ s: 1 }),
|
||||
});
|
||||
export type SleepActionConfig = z.infer<typeof sleepActionConfigSchema>;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
export const substreamSelectActionConfigSchema =
|
||||
advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.literal('live_substream_select'),
|
||||
camera: z.string(),
|
||||
});
|
||||
export type SubstreamSelectActionConfig = z.infer<
|
||||
typeof substreamSelectActionConfigSchema
|
||||
>;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { z } from 'zod';
|
||||
import { VIEWS_USER_SPECIFIED } from '../../common/const';
|
||||
import { advancedCameraCardCustomActionsBaseSchema } from './base';
|
||||
|
||||
export const viewActionConfigSchema = advancedCameraCardCustomActionsBaseSchema.extend({
|
||||
advanced_camera_card_action: z.enum(VIEWS_USER_SPECIFIED),
|
||||
});
|
||||
export type ViewActionConfig = z.infer<typeof viewActionConfigSchema>;
|
||||
Reference in New Issue
Block a user