2025-10-06 14:21:56 +01:00
|
|
|
import { toast } from "sonner";
|
2025-09-12 13:28:14 +01:00
|
|
|
import type { SystemValues } from "../../../types/types";
|
2025-09-23 13:03:54 +01:00
|
|
|
import { CAM_BASE } from "../../../utils/config";
|
2025-09-12 13:28:14 +01:00
|
|
|
|
|
|
|
|
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-08 15:21:17 +01:00
|
|
|
|
2025-09-12 08:21:52 +01:00
|
|
|
try {
|
2025-09-23 13:03:54 +01:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config`, {
|
2025-09-12 08:21:52 +01:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
|
});
|
2025-09-08 15:21:17 +01:00
|
|
|
|
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-08 15:21:17 +01:00
|
|
|
}
|
2025-09-12 08:21:52 +01:00
|
|
|
} catch (err) {
|
2025-10-06 14:21:56 +01:00
|
|
|
if (err instanceof Error) {
|
|
|
|
|
toast.error(`Failed to save system settings: ${err.message}`);
|
|
|
|
|
console.error(err);
|
|
|
|
|
} else {
|
|
|
|
|
toast.error("An unexpected error occurred while saving.");
|
|
|
|
|
console.error("Unknown error:", err);
|
|
|
|
|
}
|
2025-09-12 08:21:52 +01:00
|
|
|
}
|
2025-09-08 15:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function handleSystemRecall() {
|
2025-09-23 13:03:54 +01:00
|
|
|
const url = `${CAM_BASE}/api/fetch-config?id=GLOBAL--Device`;
|
2025-09-08 15:21:17 +01:00
|
|
|
|
2025-09-12 08:21:52 +01:00
|
|
|
const controller = new AbortController();
|
|
|
|
|
const timeoutId = setTimeout(() => controller.abort(), 7000);
|
2025-09-08 15:21:17 +01:00
|
|
|
|
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-08 15:21:17 +01:00
|
|
|
|
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-08 15:21:17 +01:00
|
|
|
|
2025-09-12 08:21:52 +01:00
|
|
|
const data = await response.json();
|
2025-09-08 15:21:17 +01:00
|
|
|
|
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-08 15:21:17 +01:00
|
|
|
|
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-08 15:21:17 +01:00
|
|
|
|
2025-09-12 08:21:52 +01:00
|
|
|
if (!Number.isFinite(sntpInterval)) {
|
|
|
|
|
sntpInterval = 60;
|
2025-09-08 15:21:17 +01:00
|
|
|
}
|
2025-09-12 08:21:52 +01:00
|
|
|
|
|
|
|
|
return { deviceName, sntpServer, sntpInterval, timeZone };
|
|
|
|
|
} catch (err) {
|
2025-10-06 14:21:56 +01:00
|
|
|
if (err instanceof Error) {
|
|
|
|
|
toast.error(`Error: ${err.message}`);
|
|
|
|
|
} else {
|
|
|
|
|
toast.error("An unexpected error occurred");
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-12 08:21:52 +01:00
|
|
|
return null;
|
|
|
|
|
} finally {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
}
|
|
|
|
|
}
|