import { useQuery, useMutation } from "@tanstack/react-query"; import { CAMBASE } from "../../../utils/config"; import type { CameraZoomConfig } from "../../../types/types"; import type { CameraID } from "../../../app/config/cameraConfig"; 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(); }; export const useCameraZoom = (cameraFeedID: CameraID) => { const cameraZoomQuery = useQuery({ queryKey: ["cameraZoom", cameraFeedID], queryFn: () => fetchZoomLevel(cameraFeedID), }); const cameraZoomMutation = useMutation({ mutationKey: ["postCameraZoom"], mutationFn: (zoomConfig: CameraZoomConfig) => postZoomLevel(zoomConfig), }); return { cameraZoomQuery, cameraZoomMutation }; };