83 lines
2.4 KiB
TypeScript
83 lines
2.4 KiB
TypeScript
import { PTZAction, PTZBaseAction } from '../../config/ptz';
|
|
import { ActionPhase, ActionType, CameraConfig } from '../../config/types';
|
|
import { PTZCapabilities, PTZMovementType } from '../../types';
|
|
|
|
export const getConfiguredPTZAction = (
|
|
cameraConfig: CameraConfig,
|
|
action: PTZAction,
|
|
options?: {
|
|
phase?: ActionPhase;
|
|
preset?: string;
|
|
},
|
|
): ActionType | ActionType[] | null => {
|
|
if (action === 'preset') {
|
|
return (options?.preset ? cameraConfig.ptz.presets?.[options.preset] : null) ?? null;
|
|
}
|
|
|
|
if (options?.phase) {
|
|
return cameraConfig.ptz[`actions_${action}_${options.phase}`] ?? null;
|
|
}
|
|
|
|
return cameraConfig.ptz[`actions_${action}`] ?? null;
|
|
};
|
|
|
|
const hasConfiguredPTZAction = (
|
|
cameraConfig: CameraConfig,
|
|
action: PTZBaseAction,
|
|
options?: {
|
|
phase?: ActionPhase;
|
|
preset?: string;
|
|
},
|
|
): boolean => {
|
|
return !!getConfiguredPTZAction(cameraConfig, action, options);
|
|
};
|
|
|
|
export const getConfiguredPTZMovementType = (
|
|
cameraConfig: CameraConfig,
|
|
action: PTZBaseAction,
|
|
): PTZMovementType[] | null => {
|
|
const continuous =
|
|
hasConfiguredPTZAction(cameraConfig, action, { phase: 'start' }) &&
|
|
hasConfiguredPTZAction(cameraConfig, action, { phase: 'stop' });
|
|
const relative = hasConfiguredPTZAction(cameraConfig, action);
|
|
|
|
return continuous || relative
|
|
? [
|
|
...(continuous ? ['continuous' as const] : []),
|
|
...(relative ? ['relative' as const] : []),
|
|
]
|
|
: null;
|
|
};
|
|
|
|
export const getPTZCapabilitiesFromCameraConfig = (
|
|
cameraConfig: CameraConfig,
|
|
): PTZCapabilities | null => {
|
|
const left = getConfiguredPTZMovementType(cameraConfig, 'left');
|
|
const right = getConfiguredPTZMovementType(cameraConfig, 'right');
|
|
const up = getConfiguredPTZMovementType(cameraConfig, 'up');
|
|
const down = getConfiguredPTZMovementType(cameraConfig, 'down');
|
|
const zoomIn = getConfiguredPTZMovementType(cameraConfig, 'zoom_in');
|
|
const zoomOut = getConfiguredPTZMovementType(cameraConfig, 'zoom_out');
|
|
const presets = cameraConfig.ptz.presets
|
|
? Object.keys(cameraConfig.ptz.presets)
|
|
: undefined;
|
|
|
|
return left?.length ||
|
|
right?.length ||
|
|
up?.length ||
|
|
down?.length ||
|
|
zoomIn?.length ||
|
|
zoomOut?.length ||
|
|
presets?.length
|
|
? {
|
|
left: left ?? undefined,
|
|
right: right ?? undefined,
|
|
up: up ?? undefined,
|
|
down: down ?? undefined,
|
|
zoomIn: zoomIn ?? undefined,
|
|
zoomOut: zoomOut ?? undefined,
|
|
presets: presets,
|
|
}
|
|
: null;
|
|
};
|