2024-02-10 15:30:53 +03:00
|
|
|
import {
|
|
|
|
|
useAudioActivity,
|
|
|
|
|
useFrigateEvents,
|
|
|
|
|
useMotionActivity,
|
|
|
|
|
} from "@/api/ws";
|
|
|
|
|
import { CameraConfig } from "@/types/frigateConfig";
|
2024-03-23 21:21:11 +03:00
|
|
|
import { MotionData, ReviewSegment } from "@/types/review";
|
|
|
|
|
import { TimeRange } from "@/types/timeline";
|
2024-02-10 15:30:53 +03:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
|
|
|
|
|
|
type useCameraActivityReturn = {
|
|
|
|
|
activeTracking: boolean;
|
|
|
|
|
activeMotion: boolean;
|
|
|
|
|
activeAudio: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-23 21:21:11 +03:00
|
|
|
export function useCameraActivity(
|
2024-02-29 01:23:56 +03:00
|
|
|
camera: CameraConfig,
|
2024-02-10 15:30:53 +03:00
|
|
|
): useCameraActivityReturn {
|
|
|
|
|
const [activeObjects, setActiveObjects] = useState<string[]>([]);
|
|
|
|
|
const hasActiveObjects = useMemo(
|
|
|
|
|
() => activeObjects.length > 0,
|
2024-02-29 01:23:56 +03:00
|
|
|
[activeObjects],
|
2024-02-10 15:30:53 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { payload: detectingMotion } = useMotionActivity(camera.name);
|
|
|
|
|
const { payload: event } = useFrigateEvents();
|
|
|
|
|
const { payload: audioRms } = useAudioActivity(camera.name);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!event) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.after.camera != camera.name) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const eventIndex = activeObjects.indexOf(event.after.id);
|
|
|
|
|
|
|
|
|
|
if (event.type == "end") {
|
|
|
|
|
if (eventIndex != -1) {
|
|
|
|
|
const newActiveObjects = [...activeObjects];
|
|
|
|
|
newActiveObjects.splice(eventIndex, 1);
|
|
|
|
|
setActiveObjects(newActiveObjects);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (eventIndex == -1) {
|
|
|
|
|
// add unknown event to list if not stationary
|
|
|
|
|
if (!event.after.stationary) {
|
|
|
|
|
const newActiveObjects = [...activeObjects, event.after.id];
|
|
|
|
|
setActiveObjects(newActiveObjects);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// remove known event from list if it has become stationary
|
|
|
|
|
if (event.after.stationary) {
|
|
|
|
|
activeObjects.splice(eventIndex, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 01:23:56 +03:00
|
|
|
}, [camera, event, activeObjects]);
|
2024-02-10 15:30:53 +03:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
activeTracking: hasActiveObjects,
|
|
|
|
|
activeMotion: detectingMotion == "ON",
|
|
|
|
|
activeAudio: camera.audio.enabled_in_config
|
|
|
|
|
? audioRms >= camera.audio.min_volume
|
|
|
|
|
: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-03-23 21:21:11 +03:00
|
|
|
|
|
|
|
|
export function useCameraMotionTimestamps(
|
|
|
|
|
timeRange: TimeRange,
|
|
|
|
|
motionOnly: boolean,
|
|
|
|
|
events: ReviewSegment[],
|
|
|
|
|
motion: MotionData[],
|
|
|
|
|
) {
|
|
|
|
|
const timestamps = useMemo(() => {
|
|
|
|
|
const seekableTimestamps = [];
|
|
|
|
|
for (let i = timeRange.after; i <= timeRange.before; i += 0.5) {
|
|
|
|
|
if (!motionOnly) {
|
|
|
|
|
seekableTimestamps.push(i);
|
|
|
|
|
} else {
|
|
|
|
|
if (
|
|
|
|
|
events.find((seg) => seg.start_time <= i && seg.end_time >= i) !=
|
|
|
|
|
undefined
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const relevantMotion = motion.find(
|
|
|
|
|
(mot) => mot.start_time <= i && mot.start_time + 15 >= i,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!relevantMotion || relevantMotion.motion == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seekableTimestamps.push(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return seekableTimestamps;
|
|
|
|
|
}, [timeRange, motionOnly, events, motion]);
|
|
|
|
|
|
|
|
|
|
return timestamps;
|
|
|
|
|
}
|