frigate/web/src/components/settings/MotionTuner.tsx

321 lines
9.9 KiB
TypeScript
Raw Normal View History

2024-04-05 15:25:23 +03:00
import Heading from "@/components/ui/heading";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
2024-04-10 17:16:46 +03:00
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "../ui/alert-dialog";
2024-04-05 15:25:23 +03:00
import { FrigateConfig } from "@/types/frigateConfig";
import useSWR from "swr";
2024-04-10 17:16:46 +03:00
import axios from "axios";
2024-04-05 15:25:23 +03:00
import ActivityIndicator from "@/components/indicators/activity-indicator";
2024-04-08 04:57:15 +03:00
import AutoUpdatingCameraImage from "@/components/camera/AutoUpdatingCameraImage";
2024-04-10 17:16:46 +03:00
import { useCallback, useEffect, useMemo, useState } from "react";
2024-04-05 15:25:23 +03:00
import { Slider } from "@/components/ui/slider";
import { Label } from "@/components/ui/label";
2024-04-10 17:16:46 +03:00
import {
useImproveContrast,
useMotionContourArea,
useMotionThreshold,
} from "@/api/ws";
2024-04-09 17:27:51 +03:00
import { Skeleton } from "../ui/skeleton";
2024-04-10 17:16:46 +03:00
import { Button } from "../ui/button";
import { Switch } from "../ui/switch";
import { Toaster } from "@/components/ui/sonner";
import { toast } from "sonner";
type MotionSettings = {
threshold?: number;
contour_area?: number;
improve_contrast?: boolean;
};
2024-04-05 15:25:23 +03:00
export default function MotionTuner() {
2024-04-10 17:16:46 +03:00
const { data: config, mutate: updateConfig } =
useSWR<FrigateConfig>("config");
const [changedValue, setChangedValue] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false);
2024-04-05 15:25:23 +03:00
const cameras = useMemo(() => {
if (!config) {
return [];
}
return Object.values(config.cameras)
.filter((conf) => conf.ui.dashboard && conf.enabled)
.sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);
}, [config]);
2024-04-10 17:16:46 +03:00
const [selectedCamera, setSelectedCamera] = useState(cameras[0]?.name);
const [nextSelectedCamera, setNextSelectedCamera] = useState("");
const { send: sendMotionThreshold } = useMotionThreshold(selectedCamera);
const { send: sendMotionContourArea } = useMotionContourArea(selectedCamera);
const { send: sendImproveContrast } = useImproveContrast(selectedCamera);
const [motionSettings, setMotionSettings] = useState<MotionSettings>({
threshold: undefined,
contour_area: undefined,
improve_contrast: undefined,
});
2024-04-05 15:25:23 +03:00
const cameraConfig = useMemo(() => {
if (config && selectedCamera) {
return config.cameras[selectedCamera];
}
}, [config, selectedCamera]);
2024-04-10 17:16:46 +03:00
useEffect(() => {
if (cameraConfig) {
setMotionSettings({
threshold: cameraConfig.motion.threshold,
contour_area: cameraConfig.motion.contour_area,
improve_contrast: cameraConfig.motion.improve_contrast,
});
}
}, [cameraConfig]);
2024-04-05 15:25:23 +03:00
2024-04-10 17:16:46 +03:00
useEffect(() => {
if (cameraConfig) {
const { threshold, contour_area, improve_contrast } = motionSettings;
2024-04-05 15:25:23 +03:00
2024-04-10 17:16:46 +03:00
if (
threshold !== undefined &&
cameraConfig.motion.threshold !== threshold
) {
2024-04-05 15:25:23 +03:00
sendMotionThreshold(threshold);
}
2024-04-10 17:16:46 +03:00
if (
contour_area !== undefined &&
cameraConfig.motion.contour_area !== contour_area
) {
sendMotionContourArea(contour_area);
}
if (
improve_contrast !== undefined &&
cameraConfig.motion.improve_contrast !== improve_contrast
) {
sendImproveContrast(improve_contrast ? "ON" : "OFF");
}
}
}, [
cameraConfig,
motionSettings,
sendMotionThreshold,
sendMotionContourArea,
sendImproveContrast,
]);
const handleMotionConfigChange = (newConfig: Partial<MotionSettings>) => {
setMotionSettings((prevConfig) => ({ ...prevConfig, ...newConfig }));
setChangedValue(true);
};
const saveToConfig = useCallback(async () => {
setIsLoading(true);
axios
.put(
`config/set?cameras.${selectedCamera}.motion.threshold=${motionSettings.threshold}&cameras.${selectedCamera}.motion.contour_area=${motionSettings.contour_area}&cameras.${selectedCamera}.motion.improve_contrast=${motionSettings.improve_contrast}`,
{ requires_restart: 0 },
)
.then((res) => {
if (res.status === 200) {
toast.success("Motion settings saved.", { position: "top-center" });
setChangedValue(false);
updateConfig();
} else {
toast.error(`Failed to save config changes: ${res.statusText}`, {
position: "top-center",
});
}
})
.catch((error) => {
toast.error(
`Failed to save config changes: ${error.response.data.message}`,
{ position: "top-center" },
);
})
.finally(() => {
setIsLoading(false);
});
}, [
updateConfig,
motionSettings.threshold,
motionSettings.contour_area,
motionSettings.improve_contrast,
selectedCamera,
]);
const handleSelectedCameraChange = useCallback(
(camera: string) => {
if (changedValue) {
setNextSelectedCamera(camera);
setConfirmationDialogOpen(true);
} else {
setSelectedCamera(camera);
setNextSelectedCamera("");
}
2024-04-05 15:25:23 +03:00
},
2024-04-10 17:16:46 +03:00
[setSelectedCamera, changedValue],
2024-04-05 15:25:23 +03:00
);
2024-04-10 17:16:46 +03:00
const handleDialog = useCallback(
(save: boolean) => {
if (save) {
saveToConfig();
2024-04-05 15:25:23 +03:00
}
2024-04-10 17:16:46 +03:00
setSelectedCamera(nextSelectedCamera);
setNextSelectedCamera("");
setConfirmationDialogOpen(false);
setChangedValue(false);
2024-04-05 15:25:23 +03:00
},
2024-04-10 17:16:46 +03:00
[saveToConfig, setSelectedCamera, nextSelectedCamera],
2024-04-05 15:25:23 +03:00
);
if (!cameraConfig && !selectedCamera) {
return <ActivityIndicator />;
}
return (
<>
<Heading as="h2">Motion Detection Tuner</Heading>
2024-04-10 17:16:46 +03:00
<Toaster />
2024-04-05 15:25:23 +03:00
<div className="flex items-center space-x-2 mt-5">
2024-04-10 17:16:46 +03:00
<Select
value={selectedCamera}
onValueChange={handleSelectedCameraChange}
>
2024-04-05 15:25:23 +03:00
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Camera" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>Choose a camera</SelectLabel>
{cameras.map((camera) => (
<SelectItem
key={camera.name}
2024-04-10 17:16:46 +03:00
value={camera.name}
2024-04-05 15:25:23 +03:00
className="capitalize"
>
{camera.name}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
</div>
2024-04-09 17:27:51 +03:00
{cameraConfig ? (
<div className="flex flex-col justify-start">
<AutoUpdatingCameraImage
camera={cameraConfig.name}
searchParams={new URLSearchParams([["motion", "1"]])}
className="w-[50%]"
2024-04-05 15:25:23 +03:00
/>
2024-04-09 17:27:51 +03:00
<div className="flex flex-row justify-evenly w-full">
<div className="flex flex-row mb-5">
<Slider
id="motion-threshold"
className="w-[300px]"
2024-04-10 17:16:46 +03:00
disabled={motionSettings.threshold === undefined}
value={[motionSettings.threshold ?? 0]}
2024-04-09 17:27:51 +03:00
min={10}
max={80}
step={1}
2024-04-10 17:16:46 +03:00
onValueChange={(value) => {
handleMotionConfigChange({ threshold: value[0] });
}}
2024-04-09 17:27:51 +03:00
/>
<Label htmlFor="motion-threshold" className="px-2">
2024-04-10 17:16:46 +03:00
Threshold: {motionSettings.threshold}
2024-04-09 17:27:51 +03:00
</Label>
</div>
<div className="flex flex-row">
<Slider
id="motion-contour-area"
className="w-[300px]"
2024-04-10 17:16:46 +03:00
disabled={motionSettings.contour_area === undefined}
value={[motionSettings.contour_area ?? 0]}
2024-04-09 17:27:51 +03:00
min={10}
max={200}
step={5}
2024-04-10 17:16:46 +03:00
onValueChange={(value) => {
handleMotionConfigChange({ contour_area: value[0] });
}}
2024-04-09 17:27:51 +03:00
/>
<Label htmlFor="motion-contour-area" className="px-2">
2024-04-10 17:16:46 +03:00
Contour Area: {motionSettings.contour_area}
2024-04-09 17:27:51 +03:00
</Label>
</div>
2024-04-10 17:16:46 +03:00
<div className="flex flex-row">
<Switch
id="improve-contrast"
disabled={motionSettings.improve_contrast === undefined}
checked={motionSettings.improve_contrast === true}
onCheckedChange={(isChecked) => {
handleMotionConfigChange({ improve_contrast: isChecked });
}}
/>
<Label htmlFor="improve-contrast">Improve Contrast</Label>
</div>
<div className="flex">
<Button
size="sm"
variant={isLoading ? "ghost" : "select"}
disabled={!changedValue || isLoading}
onClick={saveToConfig}
>
{isLoading ? "Saving..." : "Save to Config"}
</Button>
</div>
2024-04-09 17:27:51 +03:00
</div>
2024-04-10 17:16:46 +03:00
{confirmationDialogOpen && (
<AlertDialog
open={confirmationDialogOpen}
onOpenChange={() => setConfirmationDialogOpen(false)}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
You have unsaved changes on this camera.
</AlertDialogTitle>
<AlertDialogDescription>
Do you want to save your changes before continuing?
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => handleDialog(false)}>
Cancel
</AlertDialogCancel>
<AlertDialogAction onClick={() => handleDialog(true)}>
Save
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)}
2024-04-05 15:25:23 +03:00
</div>
2024-04-09 17:27:51 +03:00
) : (
<Skeleton className="size-full rounded-2xl" />
)}
2024-04-05 15:25:23 +03:00
</>
);
}