2025-12-09 08:47:21 +00:00
|
|
|
import { useQuery, useMutation } from "@tanstack/react-query";
|
|
|
|
|
import { CAMBASE } from "../../../utils/config";
|
|
|
|
|
import type { CameraZoomConfig } from "../../../types/types";
|
2025-12-17 14:19:23 +00:00
|
|
|
import type { CameraID } from "../../../app/config/cameraConfig";
|
2025-12-09 08:47:21 +00:00
|
|
|
|
|
|
|
|
const fetchZoomLevel = async (cameraFeedID: string) => {
|
|
|
|
|
const response = await fetch(`${CAMBASE}/api/fetch-config?id=Camera${cameraFeedID}-onvif-controller`);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Network response was not ok");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const postZoomLevel = async (zoomConfig: CameraZoomConfig) => {
|
|
|
|
|
const fields = [
|
|
|
|
|
{ property: "propPhysCurrent", value: zoomConfig.zoomLevel },
|
|
|
|
|
{ property: "propCameraHost", value: "192.168.0.101" },
|
|
|
|
|
{ property: "propCameraPort", value: 80 },
|
|
|
|
|
{ property: "propCameraUsername", value: "administrator" },
|
|
|
|
|
{ property: "propCameraPassword", value: "MAV12345" },
|
|
|
|
|
];
|
|
|
|
|
const zoomPayload = {
|
|
|
|
|
id: `Camera${zoomConfig.cameraFeedID}-onvif-controller`,
|
|
|
|
|
fields,
|
|
|
|
|
};
|
|
|
|
|
const response = await fetch(`${CAMBASE}/api/update-config`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(zoomPayload),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Network response was not ok");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-17 14:19:23 +00:00
|
|
|
export const useCameraZoom = (cameraFeedID: CameraID) => {
|
2025-12-09 08:47:21 +00:00
|
|
|
const cameraZoomQuery = useQuery({
|
|
|
|
|
queryKey: ["cameraZoom", cameraFeedID],
|
|
|
|
|
queryFn: () => fetchZoomLevel(cameraFeedID),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const cameraZoomMutation = useMutation({
|
|
|
|
|
mutationKey: ["postCameraZoom"],
|
|
|
|
|
mutationFn: (zoomConfig: CameraZoomConfig) => postZoomLevel(zoomConfig),
|
|
|
|
|
});
|
|
|
|
|
return { cameraZoomQuery, cameraZoomMutation };
|
|
|
|
|
};
|