2025-10-07 11:37:37 +01:00
|
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
|
import { CAM_BASE } from "../utils/config";
|
|
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
2025-10-07 14:00:58 +01:00
|
|
|
import type { BearerTypeFieldType, InitialValuesForm } from "../types/types";
|
2025-10-07 11:37:37 +01:00
|
|
|
|
|
|
|
|
const getDispatcherConfig = async () => {
|
|
|
|
|
const response = await fetch(`${CAM_BASE}/api/fetch-config?id=Dispatcher`);
|
|
|
|
|
if (!response.ok) throw new Error("Cannot get dispatcher configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateDispatcherConfig = async (data: BearerTypeFieldType) => {
|
|
|
|
|
const updateConfigPayload = {
|
|
|
|
|
id: "Dispatcher",
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
property: "propEnabled",
|
|
|
|
|
value: data.enabled,
|
|
|
|
|
},
|
2025-10-07 14:00:58 +01:00
|
|
|
// Todo: figure out how to add verbose conditionally
|
2025-10-07 11:37:37 +01:00
|
|
|
{
|
|
|
|
|
property: "propFormat",
|
|
|
|
|
value: data.format,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config?id=Dispatcher`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(updateConfigPayload),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) throw new Error("Cannot update dispatcher configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 14:00:58 +01:00
|
|
|
const getBackOfficeConfig = async () => {
|
2025-10-15 11:00:52 +01:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/fetch-config?id=Dispatcher-json`);
|
2025-10-07 14:00:58 +01:00
|
|
|
if (!response.ok) throw new Error("Cannot get Back Office configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateBackOfficeConfig = async (data: InitialValuesForm) => {
|
|
|
|
|
const updateConfigPayload = {
|
|
|
|
|
id: "Dispatcher-json",
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
property: "propBackofficeURL",
|
|
|
|
|
value: data.backOfficeURL,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propConnectTimeoutSeconds",
|
|
|
|
|
value: data.connectTimeoutSeconds,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propPassword",
|
|
|
|
|
value: data.password,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propReadTimeoutSeconds",
|
|
|
|
|
value: data.readTimeoutSeconds,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propUsername",
|
|
|
|
|
value: data.username,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
2025-10-15 11:00:52 +01:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config?id=Dispatcher-json`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(updateConfigPayload),
|
|
|
|
|
});
|
2025-10-07 14:00:58 +01:00
|
|
|
if (!response.ok) throw new Error("Cannot update Back Office configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 11:37:37 +01:00
|
|
|
export const useCameraOutput = () => {
|
|
|
|
|
const dispatcherQuery = useQuery({
|
|
|
|
|
queryKey: ["dispatcher"],
|
|
|
|
|
queryFn: getDispatcherConfig,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-07 14:00:58 +01:00
|
|
|
const backOfficeQuery = useQuery({
|
|
|
|
|
queryKey: ["backoffice"],
|
|
|
|
|
queryFn: getBackOfficeConfig,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-07 11:37:37 +01:00
|
|
|
const dispatcherMutation = useMutation({
|
|
|
|
|
mutationFn: updateDispatcherConfig,
|
|
|
|
|
mutationKey: ["dispatcherUpdate"],
|
|
|
|
|
onError: (error) => toast.error(error.message),
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
toast.success("Settings successfully updated");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-07 14:00:58 +01:00
|
|
|
const backOfficeMutation = useMutation({
|
|
|
|
|
mutationKey: ["backOfficeUpdate"],
|
|
|
|
|
mutationFn: updateBackOfficeConfig,
|
|
|
|
|
onError: (error) => toast.error(error.message),
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
toast.success("Settings successfully updated");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-07 11:37:37 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (dispatcherQuery.isError) toast.error(dispatcherQuery.error.message);
|
|
|
|
|
}, [dispatcherQuery?.error?.message, dispatcherQuery.isError]);
|
|
|
|
|
|
2025-10-07 14:00:58 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (backOfficeQuery.isError) toast.error(backOfficeQuery.error.message);
|
|
|
|
|
}, [backOfficeQuery?.error?.message, backOfficeQuery.isError]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dispatcherQuery,
|
|
|
|
|
dispatcherMutation,
|
|
|
|
|
backOfficeQuery,
|
|
|
|
|
backOfficeMutation,
|
|
|
|
|
};
|
2025-10-07 11:37:37 +01:00
|
|
|
};
|