2025-10-02 22:53:38 +01:00
|
|
|
import { useQuery, useMutation } from "@tanstack/react-query";
|
|
|
|
|
import { CAM_BASE } from "../utils/config";
|
2025-10-03 10:19:49 +01:00
|
|
|
import type { ModemConfig, WifiConfig } from "../types/types";
|
2025-10-02 22:53:38 +01:00
|
|
|
|
|
|
|
|
const getWiFiSettings = async () => {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${CAM_BASE}/api/fetch-config?id=ModemAndWifiManager-wifi`
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot fetch Wifi settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateWifiSettings = async (wifiConfig: WifiConfig) => {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${CAM_BASE}/api/update-config?id=ModemAndWifiManager-wifi`,
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify(wifiConfig),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot update wifi settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-03 10:19:49 +01:00
|
|
|
const getModemSettings = async () => {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${CAM_BASE}/api/fetch-config?id=ModemAndWifiManager-modem`
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot fetch modem settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateModemSettings = async (modemConfig: ModemConfig) => {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${CAM_BASE}/api/update-config?id=ModemAndWifiManager-modem`,
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify(modemConfig),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("cannot update modem settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-02 22:53:38 +01:00
|
|
|
export const useWifiAndModem = () => {
|
|
|
|
|
const wifiQuery = useQuery({
|
|
|
|
|
queryKey: ["getWifiSettings"],
|
|
|
|
|
queryFn: getWiFiSettings,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const wifiMutation = useMutation({
|
|
|
|
|
mutationKey: ["updateWifiSettings"],
|
|
|
|
|
mutationFn: (wifiConfig: WifiConfig) => updateWifiSettings(wifiConfig),
|
2025-10-03 10:19:49 +01:00
|
|
|
onError: (error) => console.log(error),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const modemQuery = useQuery({
|
|
|
|
|
queryKey: ["getModemSettings"],
|
|
|
|
|
queryFn: getModemSettings,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const modemMutation = useMutation({
|
|
|
|
|
mutationKey: ["updateModemSettings"],
|
|
|
|
|
mutationFn: (modemConfig: ModemConfig) => updateModemSettings(modemConfig),
|
2025-10-02 22:53:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
wifiQuery,
|
|
|
|
|
wifiMutation,
|
2025-10-03 10:19:49 +01:00
|
|
|
modemQuery,
|
|
|
|
|
modemMutation,
|
2025-10-02 22:53:38 +01:00
|
|
|
};
|
|
|
|
|
};
|