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-11-04 17:04:19 +00:00
|
|
|
import type { BearerTypeFieldType, OptionalBOF2Constants, OptionalBOF2LaneIDs } 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,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propFormat",
|
|
|
|
|
value: data.format,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
2025-11-10 09:05:08 +00:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config`, {
|
2025-10-07 11:37:37 +01:00
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(updateConfigPayload),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) throw new Error("Cannot update dispatcher configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-04 10:24:06 +00:00
|
|
|
const updateBackOfficeDispatcher = async (data: OptionalBOF2Constants) => {
|
|
|
|
|
const bof2ContantsPayload = {
|
|
|
|
|
id: "Dispatcher-bof2-constants",
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
property: "propFeedIdentifier",
|
|
|
|
|
value: data?.FFID,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propSourceIdentifier",
|
|
|
|
|
value: data?.SCID,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propTimeZoneType",
|
|
|
|
|
value: data?.timestampSource,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propGpsFormat",
|
|
|
|
|
value: data?.GPSFormat,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
2025-11-10 09:05:08 +00:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config`, {
|
2025-11-04 10:24:06 +00:00
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(bof2ContantsPayload),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) throw new Error("Cannot update dispatcher configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-04 11:05:40 +00:00
|
|
|
const getBof2DispatcherData = async () => {
|
2025-11-04 17:04:19 +00:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/fetch-config?id=Dispatcher-bof2-constants`);
|
2025-11-04 11:05:40 +00:00
|
|
|
if (!response.ok) throw new Error("Cannot get BOF2 dispatcher config");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-04 17:04:19 +00:00
|
|
|
const updateBOF2LaneId = async (data: OptionalBOF2LaneIDs) => {
|
|
|
|
|
const bof2LaneIds = {
|
2025-11-10 09:05:08 +00:00
|
|
|
id: data?.laneId,
|
2025-11-04 17:04:19 +00:00
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
property: "propLaneID1",
|
|
|
|
|
value: data?.LID1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propLaneID2",
|
|
|
|
|
value: data?.LID2,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-10 09:05:08 +00:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config`, {
|
2025-11-04 17:04:19 +00:00
|
|
|
method: "post",
|
|
|
|
|
body: JSON.stringify(bof2LaneIds),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) throw new Error("cannot send to lane IDs");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBOF2LaneId = async () => {
|
2025-11-11 10:43:14 +00:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/fetch-config?id=SightingAmmendA-lane-ids`);
|
2025-11-04 17:04:19 +00:00
|
|
|
if (!response.ok) throw new Error("Canot get Lane Ids");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 11:37:37 +01:00
|
|
|
export const useCameraOutput = () => {
|
|
|
|
|
const dispatcherQuery = useQuery({
|
|
|
|
|
queryKey: ["dispatcher"],
|
|
|
|
|
queryFn: getDispatcherConfig,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const dispatcherMutation = useMutation({
|
|
|
|
|
mutationFn: updateDispatcherConfig,
|
|
|
|
|
mutationKey: ["dispatcherUpdate"],
|
|
|
|
|
onError: (error) => toast.error(error.message),
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
if (data) {
|
2025-11-04 11:05:40 +00:00
|
|
|
toast.success("Settings successfully updated", { id: "dispatchSettings" });
|
2025-10-07 11:37:37 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-04 10:24:06 +00:00
|
|
|
const backOfficeDispatcherMutation = useMutation({
|
|
|
|
|
mutationKey: ["backofficedDispatcher"],
|
|
|
|
|
mutationFn: updateBackOfficeDispatcher,
|
2025-11-04 11:05:40 +00:00
|
|
|
onSuccess: (data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
toast.success("Settings successfully updated", { id: "dispatchSettings" });
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-11-04 10:24:06 +00:00
|
|
|
});
|
|
|
|
|
|
2025-11-04 17:04:19 +00:00
|
|
|
const bof2LandMutation = useMutation({
|
|
|
|
|
mutationKey: ["updateBOF2LaneId"],
|
|
|
|
|
mutationFn: updateBOF2LaneId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const laneIdQuery = useQuery({
|
|
|
|
|
queryKey: ["getBOF2LaneId"],
|
|
|
|
|
queryFn: getBOF2LaneId,
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
return {
|
|
|
|
|
dispatcherQuery,
|
|
|
|
|
dispatcherMutation,
|
2025-11-04 10:24:06 +00:00
|
|
|
backOfficeDispatcherMutation,
|
2025-11-04 17:04:19 +00:00
|
|
|
bof2LandMutation,
|
|
|
|
|
laneIdQuery,
|
2025-10-07 14:00:58 +01:00
|
|
|
};
|
2025-10-07 11:37:37 +01:00
|
|
|
};
|
2025-11-04 11:05:40 +00:00
|
|
|
|
|
|
|
|
export const useGetDispatcherConfig = () => {
|
|
|
|
|
const bof2ConstantsQuery = useQuery({
|
|
|
|
|
queryKey: ["getBof2DispatcherData"],
|
|
|
|
|
queryFn: getBof2DispatcherData,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { bof2ConstantsQuery };
|
|
|
|
|
};
|