Files
Mav-Mobile-UI/src/components/SettingForms/System/SettingSaveRecall.tsx

87 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-09-12 13:28:14 +01:00
import type { SystemValues } from "../../../types/types";
export async function handleSystemSave(values: SystemValues) {
2025-09-12 08:21:52 +01:00
const payload = {
// Build JSON
id: "GLOBAL--Device",
fields: [
2025-09-12 13:28:14 +01:00
{ property: "propDeviceName", value: values.deviceName },
{ property: "propSNTPServer", value: values.sntpServer },
{
property: "propSNTPIntervalMinutes",
value: Number(values.sntpInterval),
},
{ property: "propLocalTimeZone", value: values.timeZone },
2025-09-12 08:21:52 +01:00
],
};
2025-09-12 08:21:52 +01:00
try {
const response = await fetch("http://192.168.75.11/api/update-config", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(payload),
});
2025-09-12 08:21:52 +01:00
if (!response.ok) {
const text = await response.text().catch(() => "");
throw new Error(
`HTTP ${response.status} ${response.statusText}${
text ? ` - ${text}` : ""
}`
);
}
2025-09-12 08:21:52 +01:00
} catch (err) {
console.error(err);
}
}
export async function handleSystemRecall() {
2025-09-12 08:21:52 +01:00
const url = "http://192.168.75.11/api/fetch-config?id=GLOBAL--Device";
2025-09-12 08:21:52 +01:00
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 7000);
2025-09-12 08:21:52 +01:00
try {
const response = await fetch(url, {
method: "GET",
headers: { Accept: "application/json" },
signal: controller.signal,
});
2025-09-12 08:21:52 +01:00
if (!response.ok) {
const text = await response.text().catch(() => "");
throw new Error(
`HTTP ${response.status} ${response.statusText}${
text ? ` - ${text}` : ""
}`
);
}
2025-09-12 08:21:52 +01:00
const data = await response.json();
2025-09-12 08:21:52 +01:00
const deviceName = data?.propDeviceName?.value ?? null;
const sntpServer = data?.propSNTPServer?.value ?? null;
const timeZone = data?.propLocalTimeZone?.value ?? null;
2025-09-12 08:21:52 +01:00
const sntpIntervalRaw = data?.propSNTPIntervalMinutes?.value;
let sntpInterval =
typeof sntpIntervalRaw === "number"
? sntpIntervalRaw
: Number.parseInt(String(sntpIntervalRaw).trim(), 10);
2025-09-12 08:21:52 +01:00
if (!Number.isFinite(sntpInterval)) {
sntpInterval = 60;
}
2025-09-12 08:21:52 +01:00
return { deviceName, sntpServer, sntpInterval, timeZone };
} catch (err) {
console.error(err);
return null;
} finally {
clearTimeout(timeoutId);
}
}