2025-08-15 09:32:33 +01:00
|
|
|
import { Formik, Field, Form } from "formik";
|
2025-10-15 11:00:52 +01:00
|
|
|
import type { CameraConfig, CameraSettingErrorValues, CameraSettingValues, ZoomInOptions } from "../../types/types";
|
2025-10-02 16:07:05 +01:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2025-09-16 14:20:38 +01:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
|
import { faEye, faEyeSlash } from "@fortawesome/free-regular-svg-icons";
|
2025-09-29 15:21:22 +01:00
|
|
|
import CardHeader from "../UI/CardHeader";
|
2025-11-05 16:30:27 +00:00
|
|
|
import { useCameraMode, useCameraZoom } from "../../hooks/useCameraZoom";
|
2025-11-04 15:29:48 +00:00
|
|
|
import { parseRTSPUrl, reverseZoomMapping, zoomMapping } from "../../utils/utils";
|
2025-08-15 09:32:33 +01:00
|
|
|
|
2025-09-16 11:07:35 +01:00
|
|
|
type CameraSettingsProps = {
|
|
|
|
|
initialData: CameraConfig;
|
2025-09-16 14:20:38 +01:00
|
|
|
updateCameraConfig: (values: CameraSettingValues) => Promise<void> | void;
|
2025-10-02 16:07:05 +01:00
|
|
|
zoomLevel?: number;
|
2025-11-04 15:29:48 +00:00
|
|
|
onZoomLevelChange?: (level: number | undefined) => void;
|
2025-10-06 14:21:56 +01:00
|
|
|
updateCameraConfigError: null | Error;
|
2025-09-16 11:07:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CameraSettingFields = ({
|
|
|
|
|
initialData,
|
|
|
|
|
updateCameraConfig,
|
2025-09-29 15:21:22 +01:00
|
|
|
zoomLevel,
|
|
|
|
|
onZoomLevelChange,
|
2025-09-16 11:07:35 +01:00
|
|
|
}: CameraSettingsProps) => {
|
2025-09-16 14:20:38 +01:00
|
|
|
const [showPwd, setShowPwd] = useState(false);
|
2025-11-05 16:30:27 +00:00
|
|
|
|
2025-10-15 11:00:52 +01:00
|
|
|
const cameraControllerSide = initialData?.id === "CameraA" ? "CameraControllerA" : "CameraControllerB";
|
2025-10-02 16:07:05 +01:00
|
|
|
const { mutation, query } = useCameraZoom({ camera: cameraControllerSide });
|
2025-11-05 16:30:27 +00:00
|
|
|
const { cameraModeQuery, cameraModeMutation } = useCameraMode({ camera: cameraControllerSide });
|
2025-11-04 13:38:06 +00:00
|
|
|
const zoomOptions = [1, 2, 4];
|
2025-11-04 15:29:48 +00:00
|
|
|
const magnification = query?.data?.propMagnification?.value;
|
|
|
|
|
const apiZoom = reverseZoomMapping(magnification);
|
2025-10-03 13:08:21 +01:00
|
|
|
const parsed = parseRTSPUrl(initialData?.propURI?.value);
|
2025-11-05 16:30:27 +00:00
|
|
|
const cameraMode = cameraModeQuery?.data?.propDayNightMode?.value;
|
2025-10-03 13:08:21 +01:00
|
|
|
|
2025-10-02 16:07:05 +01:00
|
|
|
useEffect(() => {
|
2025-10-07 09:56:24 +01:00
|
|
|
if (!query?.data) return;
|
2025-10-02 16:07:05 +01:00
|
|
|
onZoomLevelChange?.(apiZoom);
|
2025-11-10 13:44:29 +00:00
|
|
|
}, [query?.data, onZoomLevelChange, apiZoom]);
|
2025-10-02 16:07:05 +01:00
|
|
|
|
2025-09-16 14:20:38 +01:00
|
|
|
const initialValues = useMemo<CameraSettingValues>(
|
|
|
|
|
() => ({
|
2025-09-23 13:03:54 +01:00
|
|
|
friendlyName: initialData?.id ?? "",
|
|
|
|
|
cameraAddress: initialData?.propURI?.value ?? "",
|
2025-10-03 13:08:21 +01:00
|
|
|
userName: parsed?.username ?? "",
|
|
|
|
|
password: parsed?.password ?? "",
|
2025-09-16 14:20:38 +01:00
|
|
|
id: initialData?.id,
|
2025-11-10 13:44:29 +00:00
|
|
|
mode: cameraMode ?? "day",
|
2025-11-04 15:29:48 +00:00
|
|
|
zoom: apiZoom,
|
2025-09-16 14:20:38 +01:00
|
|
|
}),
|
2025-11-10 13:44:29 +00:00
|
|
|
|
|
|
|
|
[initialData?.id, initialData?.propURI?.value, parsed?.username, parsed?.password, cameraMode, apiZoom]
|
2025-09-16 14:20:38 +01:00
|
|
|
);
|
2025-08-18 12:53:30 +01:00
|
|
|
|
2025-08-15 09:32:33 +01:00
|
|
|
const validateValues = (values: CameraSettingValues) => {
|
|
|
|
|
const errors: CameraSettingErrorValues = {};
|
|
|
|
|
if (!values.friendlyName) errors.friendlyName = "Required";
|
|
|
|
|
if (!values.cameraAddress) errors.cameraAddress = "Required";
|
|
|
|
|
return errors;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (values: CameraSettingValues) => {
|
2025-09-12 08:21:52 +01:00
|
|
|
updateCameraConfig(values);
|
2025-08-15 09:32:33 +01:00
|
|
|
};
|
|
|
|
|
|
2025-10-01 10:59:10 +01:00
|
|
|
const handleRadioButtonChange = async (levelNumber: number) => {
|
2025-09-29 15:21:22 +01:00
|
|
|
if (!onZoomLevelChange || !zoomLevel) return;
|
2025-11-04 13:38:06 +00:00
|
|
|
const text = zoomMapping(levelNumber);
|
2025-10-02 16:07:05 +01:00
|
|
|
onZoomLevelChange(levelNumber);
|
2025-10-01 10:59:10 +01:00
|
|
|
|
|
|
|
|
const zoomInOptions: ZoomInOptions = {
|
|
|
|
|
camera: cameraControllerSide,
|
|
|
|
|
multiplier: levelNumber,
|
2025-11-04 13:38:06 +00:00
|
|
|
multiplierText: text,
|
2025-10-01 10:59:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mutation.mutate(zoomInOptions);
|
2025-09-29 15:21:22 +01:00
|
|
|
};
|
2025-11-10 13:44:29 +00:00
|
|
|
|
2025-10-02 16:07:05 +01:00
|
|
|
const selectedZoom = zoomLevel ?? 1;
|
2025-08-15 09:32:33 +01:00
|
|
|
return (
|
|
|
|
|
<Formik
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
validate={validateValues}
|
|
|
|
|
validateOnChange={false}
|
2025-10-02 16:07:05 +01:00
|
|
|
enableReinitialize
|
2025-08-15 09:32:33 +01:00
|
|
|
>
|
2025-11-10 13:44:29 +00:00
|
|
|
{({ errors, touched, values, setFieldValue, isSubmitting }) => (
|
|
|
|
|
<Form className="flex flex-col space-y-6 p-2 overflow-x-hidden">
|
2025-08-18 12:53:30 +01:00
|
|
|
<div className="flex flex-col space-y-2 relative">
|
2025-09-10 09:05:47 +01:00
|
|
|
<label htmlFor="friendlyName">Name</label>
|
2025-08-18 12:53:30 +01:00
|
|
|
{touched.friendlyName && errors.friendlyName && (
|
2025-10-15 11:00:52 +01:00
|
|
|
<small className="absolute right-0 top-0 text-red-500">{errors.friendlyName}</small>
|
2025-08-18 12:53:30 +01:00
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="friendlyName"
|
|
|
|
|
name="friendlyName"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg"
|
|
|
|
|
placeholder="Enter camera name"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-08-15 09:32:33 +01:00
|
|
|
|
2025-08-18 12:53:30 +01:00
|
|
|
<div className="flex flex-col space-y-2 relative">
|
|
|
|
|
<label htmlFor="cameraAddress">Camera Address</label>
|
|
|
|
|
{touched.cameraAddress && errors.cameraAddress && (
|
2025-10-15 11:00:52 +01:00
|
|
|
<small className="absolute right-0 top-0 text-red-500">{errors.cameraAddress}</small>
|
2025-08-18 12:53:30 +01:00
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="cameraAddress"
|
|
|
|
|
name="cameraAddress"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg"
|
2025-09-10 09:05:47 +01:00
|
|
|
placeholder="RTSP://..."
|
2025-08-18 12:53:30 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-col space-y-2 relative">
|
|
|
|
|
<label htmlFor="userName">User Name</label>
|
|
|
|
|
{touched.userName && errors.userName && (
|
2025-10-15 11:00:52 +01:00
|
|
|
<small className="absolute right-0 top-0 text-red-500">{errors.userName}</small>
|
2025-08-18 12:53:30 +01:00
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="userName"
|
|
|
|
|
name="userName"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg"
|
|
|
|
|
placeholder="Enter user name"
|
|
|
|
|
autoComplete="username"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-col space-y-2 relative">
|
|
|
|
|
<label htmlFor="password">Password</label>
|
|
|
|
|
{touched.password && errors.password && (
|
2025-10-15 11:00:52 +01:00
|
|
|
<small className="absolute right-0 top-0 text-red-500">{errors.password}</small>
|
2025-08-18 12:53:30 +01:00
|
|
|
)}
|
2025-09-29 15:21:22 +01:00
|
|
|
<div className="flex gap-2 items-center relative mb-4">
|
2025-09-16 14:20:38 +01:00
|
|
|
<Field
|
|
|
|
|
id="password"
|
|
|
|
|
name="password"
|
|
|
|
|
type={showPwd ? "text" : "password"}
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg w-full "
|
|
|
|
|
placeholder="Enter password"
|
|
|
|
|
/>
|
|
|
|
|
<FontAwesomeIcon
|
|
|
|
|
type="button"
|
|
|
|
|
className="absolute right-5 end-0"
|
|
|
|
|
onClick={() => setShowPwd((s) => !s)}
|
|
|
|
|
icon={showPwd ? faEyeSlash : faEye}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-09-29 15:21:22 +01:00
|
|
|
<div className="my-3">
|
|
|
|
|
<CardHeader title="Zoom settings" />
|
2025-11-04 13:38:06 +00:00
|
|
|
<div className="mx-auto grid grid-cols-3 place-items-center">
|
2025-10-02 16:07:05 +01:00
|
|
|
{zoomOptions.map((zoom) => (
|
2025-10-06 14:21:56 +01:00
|
|
|
<div key={zoom} className="my-3">
|
2025-10-02 16:07:05 +01:00
|
|
|
<Field
|
|
|
|
|
type="radio"
|
|
|
|
|
name="zoom"
|
|
|
|
|
value={zoom.toString()}
|
|
|
|
|
checked={selectedZoom === zoom}
|
|
|
|
|
className="hidden peer"
|
|
|
|
|
id={`zoom${zoom}`}
|
|
|
|
|
onChange={() => handleRadioButtonChange(zoom)}
|
|
|
|
|
/>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor={`zoom${zoom}`}
|
|
|
|
|
className="px-6 py-2 rounded-md border border-gray-300
|
|
|
|
|
peer-checked:border-2 peer-checked:border-blue-900
|
|
|
|
|
peer-checked:text-blue-600 peer-checked:bg-gray-100"
|
|
|
|
|
>
|
2025-11-04 13:38:06 +00:00
|
|
|
{zoomMapping(zoom)}
|
2025-10-02 16:07:05 +01:00
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2025-09-29 15:21:22 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-05 16:30:27 +00:00
|
|
|
<div>
|
|
|
|
|
<CardHeader title="Mode" />
|
2025-11-10 13:44:29 +00:00
|
|
|
<div
|
|
|
|
|
role="radiogroup"
|
|
|
|
|
aria-label="Camera mode"
|
|
|
|
|
className="mx-auto grid grid-cols-2 place-items-center gap-3"
|
|
|
|
|
>
|
|
|
|
|
{["day", "night"].map((el) => (
|
|
|
|
|
<div key={el} className="my-3">
|
|
|
|
|
<Field
|
|
|
|
|
type="radio"
|
|
|
|
|
name="mode"
|
|
|
|
|
value={el}
|
|
|
|
|
checked={values.mode === el}
|
|
|
|
|
id={`mode-${el}`}
|
|
|
|
|
className="peer hidden"
|
|
|
|
|
disabled={cameraModeMutation.isPending}
|
|
|
|
|
onChange={async () => {
|
|
|
|
|
setFieldValue("mode", el);
|
|
|
|
|
await cameraModeMutation.mutateAsync({ camera: cameraControllerSide, mode: el });
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor={`mode-${el}`}
|
|
|
|
|
className={`px-8 py-2 rounded-md border border-gray-300
|
|
|
|
|
peer-checked:border-2 peer-checked:border-blue-900
|
|
|
|
|
peer-checked:text-blue-600 peer-checked:bg-gray-100
|
|
|
|
|
${cameraModeMutation.isPending ? "opacity-60 cursor-not-allowed" : "cursor-pointer"}`}
|
|
|
|
|
>
|
|
|
|
|
{el === "day" ? "Day" : "Night"}
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2025-11-05 16:30:27 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-10 13:44:29 +00:00
|
|
|
<div className="mt-3">
|
|
|
|
|
{
|
|
|
|
|
<button type="submit" className="bg-green-700 text-white rounded-lg p-2 mx-auto w-full">
|
2025-11-05 16:30:27 +00:00
|
|
|
{isSubmitting ? "Saving" : "Save settings"}
|
2025-10-06 14:21:56 +01:00
|
|
|
</button>
|
2025-11-10 13:44:29 +00:00
|
|
|
}
|
|
|
|
|
</div>
|
2025-09-16 14:20:38 +01:00
|
|
|
</div>
|
2025-08-18 12:53:30 +01:00
|
|
|
</Form>
|
|
|
|
|
)}
|
2025-08-15 09:32:33 +01:00
|
|
|
</Formik>
|
|
|
|
|
);
|
2025-08-13 14:23:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CameraSettingFields;
|