2025-10-02 16:07:05 +01:00
|
|
|
import {
|
|
|
|
|
useMutation,
|
|
|
|
|
useQuery,
|
|
|
|
|
type QueryFunctionContext,
|
|
|
|
|
} from "@tanstack/react-query";
|
2025-10-01 10:59:10 +01:00
|
|
|
import { CAM_BASE } from "../utils/config";
|
2025-10-02 16:07:05 +01:00
|
|
|
import type { zoomConfig, ZoomInOptions } from "../types/types";
|
2025-10-06 15:18:58 +01:00
|
|
|
import { toast } from "sonner";
|
2025-10-01 10:59:10 +01:00
|
|
|
|
|
|
|
|
async function zoomIn(options: ZoomInOptions) {
|
|
|
|
|
const response = await fetch(
|
2025-10-06 14:21:56 +01:00
|
|
|
`${CAM_BASE}/Ip${options.camera}-command?magnification=${options.multiplier}x`,
|
|
|
|
|
{
|
|
|
|
|
signal: AbortSignal.timeout(500),
|
|
|
|
|
}
|
2025-10-01 10:59:10 +01:00
|
|
|
);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot reach camera zoom endpoint");
|
|
|
|
|
}
|
2025-10-03 13:08:21 +01:00
|
|
|
|
|
|
|
|
return response.json();
|
2025-10-01 10:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 16:07:05 +01:00
|
|
|
async function fetchZoomInConfig({
|
|
|
|
|
queryKey,
|
|
|
|
|
}: QueryFunctionContext<[string, zoomConfig]>) {
|
|
|
|
|
const [, { camera }] = queryKey;
|
2025-10-06 14:21:56 +01:00
|
|
|
const response = await fetch(`${CAM_BASE}/Ip${camera}-inspect`, {
|
|
|
|
|
signal: AbortSignal.timeout(500),
|
|
|
|
|
});
|
2025-10-02 16:07:05 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot get camera zoom settings");
|
|
|
|
|
}
|
|
|
|
|
return response.text();
|
|
|
|
|
}
|
|
|
|
|
//change to string
|
|
|
|
|
export const useCameraZoom = (options: zoomConfig) => {
|
2025-10-01 10:59:10 +01:00
|
|
|
const mutation = useMutation({
|
|
|
|
|
mutationKey: ["zoomIn"],
|
|
|
|
|
mutationFn: (options: ZoomInOptions) => zoomIn(options),
|
2025-10-06 15:18:58 +01:00
|
|
|
onError: () => {
|
|
|
|
|
toast.error("Failed to update zoom settings", { id: "zoom" });
|
|
|
|
|
},
|
2025-10-01 10:59:10 +01:00
|
|
|
});
|
|
|
|
|
|
2025-10-02 16:07:05 +01:00
|
|
|
const query = useQuery({
|
|
|
|
|
queryKey: ["fetchZoomInConfig", options],
|
|
|
|
|
queryFn: fetchZoomInConfig,
|
|
|
|
|
});
|
|
|
|
|
return { mutation, query };
|
2025-10-01 10:59:10 +01:00
|
|
|
};
|