2023-12-08 16:33:22 +03:00
|
|
|
import { baseUrl } from "./baseUrl";
|
2024-02-27 19:05:28 +03:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2023-12-08 16:33:22 +03:00
|
|
|
import useWebSocket, { ReadyState } from "react-use-websocket";
|
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
2024-02-27 16:37:39 +03:00
|
|
|
import { FrigateEvent, FrigateReview, ToggleableSetting } from "@/types/ws";
|
2024-02-22 05:27:02 +03:00
|
|
|
import { FrigateStats } from "@/types/stats";
|
2024-02-27 19:05:28 +03:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
import { createContainer } from "react-tracked";
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
type Update = {
|
2023-12-08 16:33:22 +03:00
|
|
|
topic: string;
|
2023-12-29 16:08:00 +03:00
|
|
|
payload: any;
|
2023-12-08 16:33:22 +03:00
|
|
|
retain: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
type WsState = {
|
|
|
|
|
[topic: string]: any;
|
2023-12-08 16:33:22 +03:00
|
|
|
};
|
|
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
type useValueReturn = [WsState, (update: Update) => void];
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
function useValue(): useValueReturn {
|
|
|
|
|
// basic config
|
|
|
|
|
const { data: config } = useSWR<FrigateConfig>("config", {
|
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
|
});
|
|
|
|
|
const wsUrl = `${baseUrl.replace(/^http/, "ws")}ws`;
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
// main state
|
|
|
|
|
const [wsState, setWsState] = useState<WsState>({});
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!config) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
const cameraStates: WsState = {};
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
Object.keys(config.cameras).forEach((camera) => {
|
|
|
|
|
const { name, record, detect, snapshots, audio } = config.cameras[camera];
|
|
|
|
|
cameraStates[`${name}/recordings/state`] = record.enabled ? "ON" : "OFF";
|
|
|
|
|
cameraStates[`${name}/detect/state`] = detect.enabled ? "ON" : "OFF";
|
|
|
|
|
cameraStates[`${name}/snapshots/state`] = snapshots.enabled
|
|
|
|
|
? "ON"
|
|
|
|
|
: "OFF";
|
|
|
|
|
cameraStates[`${name}/audio/state`] = audio.enabled ? "ON" : "OFF";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setWsState({ ...wsState, ...cameraStates });
|
|
|
|
|
}, [config]);
|
|
|
|
|
|
|
|
|
|
// ws handler
|
2023-12-08 16:33:22 +03:00
|
|
|
const { sendJsonMessage, readyState } = useWebSocket(wsUrl, {
|
|
|
|
|
onMessage: (event) => {
|
2024-02-27 19:05:28 +03:00
|
|
|
const data: Update = JSON.parse(event.data);
|
|
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
|
setWsState({ ...wsState, [data.topic]: data.payload });
|
|
|
|
|
}
|
2023-12-08 16:33:22 +03:00
|
|
|
},
|
2024-02-27 19:05:28 +03:00
|
|
|
onOpen: () => {},
|
2023-12-08 16:33:22 +03:00
|
|
|
shouldReconnect: () => true,
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
const setState = useCallback(
|
|
|
|
|
(message: Update) => {
|
|
|
|
|
if (readyState === ReadyState.OPEN) {
|
|
|
|
|
sendJsonMessage({
|
|
|
|
|
topic: message.topic,
|
|
|
|
|
payload: message.payload,
|
|
|
|
|
retain: message.retain,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[readyState, sendJsonMessage]
|
2023-12-08 16:33:22 +03:00
|
|
|
);
|
2024-02-27 19:05:28 +03:00
|
|
|
|
|
|
|
|
return [wsState, setState];
|
2023-12-08 16:33:22 +03:00
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
export const {
|
|
|
|
|
Provider: WsProvider,
|
|
|
|
|
useTrackedState: useWsState,
|
|
|
|
|
useUpdate: useWsUpdate,
|
|
|
|
|
} = createContainer(useValue, { defaultState: {}, concurrentMode: true });
|
|
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
export function useWs(watchTopic: string, publishTopic: string) {
|
2024-02-27 19:05:28 +03:00
|
|
|
const state = useWsState();
|
|
|
|
|
const sendJsonMessage = useWsUpdate();
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-02-27 19:05:28 +03:00
|
|
|
const value = { payload: state[watchTopic] || null };
|
2023-12-08 16:33:22 +03:00
|
|
|
|
|
|
|
|
const send = useCallback(
|
2023-12-29 16:08:00 +03:00
|
|
|
(payload: any, retain = false) => {
|
2024-02-27 19:05:28 +03:00
|
|
|
sendJsonMessage({
|
|
|
|
|
topic: publishTopic || watchTopic,
|
|
|
|
|
payload,
|
|
|
|
|
retain,
|
|
|
|
|
});
|
2023-12-08 16:33:22 +03:00
|
|
|
},
|
2024-02-27 19:05:28 +03:00
|
|
|
[sendJsonMessage, watchTopic, publishTopic]
|
2023-12-08 16:33:22 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { value, send };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function useDetectState(camera: string): {
|
2024-02-10 15:30:53 +03:00
|
|
|
payload: ToggleableSetting;
|
|
|
|
|
send: (payload: ToggleableSetting, retain?: boolean) => void;
|
2023-12-29 16:08:00 +03:00
|
|
|
} {
|
2023-12-08 16:33:22 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
send,
|
|
|
|
|
} = useWs(`${camera}/detect/state`, `${camera}/detect/set`);
|
|
|
|
|
return { payload, send };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function useRecordingsState(camera: string): {
|
2024-02-10 15:30:53 +03:00
|
|
|
payload: ToggleableSetting;
|
|
|
|
|
send: (payload: ToggleableSetting, retain?: boolean) => void;
|
2023-12-29 16:08:00 +03:00
|
|
|
} {
|
2023-12-08 16:33:22 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
send,
|
|
|
|
|
} = useWs(`${camera}/recordings/state`, `${camera}/recordings/set`);
|
|
|
|
|
return { payload, send };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function useSnapshotsState(camera: string): {
|
2024-02-10 15:30:53 +03:00
|
|
|
payload: ToggleableSetting;
|
|
|
|
|
send: (payload: ToggleableSetting, retain?: boolean) => void;
|
2023-12-29 16:08:00 +03:00
|
|
|
} {
|
2023-12-08 16:33:22 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
send,
|
|
|
|
|
} = useWs(`${camera}/snapshots/state`, `${camera}/snapshots/set`);
|
|
|
|
|
return { payload, send };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function useAudioState(camera: string): {
|
2024-02-10 15:30:53 +03:00
|
|
|
payload: ToggleableSetting;
|
|
|
|
|
send: (payload: ToggleableSetting, retain?: boolean) => void;
|
2023-12-29 16:08:00 +03:00
|
|
|
} {
|
2023-12-08 16:33:22 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
send,
|
|
|
|
|
} = useWs(`${camera}/audio/state`, `${camera}/audio/set`);
|
|
|
|
|
return { payload, send };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function usePtzCommand(camera: string): {
|
|
|
|
|
payload: string;
|
|
|
|
|
send: (payload: string, retain?: boolean) => void;
|
|
|
|
|
} {
|
2023-12-08 16:33:22 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
send,
|
|
|
|
|
} = useWs(`${camera}/ptz`, `${camera}/ptz`);
|
|
|
|
|
return { payload, send };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function useRestart(): {
|
|
|
|
|
payload: string;
|
|
|
|
|
send: (payload: string, retain?: boolean) => void;
|
|
|
|
|
} {
|
2023-12-08 16:33:22 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
send,
|
|
|
|
|
} = useWs("restart", "restart");
|
|
|
|
|
return { payload, send };
|
|
|
|
|
}
|
2023-12-29 16:08:00 +03:00
|
|
|
|
|
|
|
|
export function useFrigateEvents(): { payload: FrigateEvent } {
|
|
|
|
|
const {
|
|
|
|
|
value: { payload },
|
2024-02-27 16:37:39 +03:00
|
|
|
} = useWs("events", "");
|
2024-02-27 19:05:28 +03:00
|
|
|
return { payload: JSON.parse(payload) };
|
2024-02-27 16:37:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useFrigateReviews(): { payload: FrigateReview } {
|
|
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
} = useWs("reviews", "");
|
2024-02-27 19:05:28 +03:00
|
|
|
return { payload: JSON.parse(payload) };
|
2023-12-29 16:08:00 +03:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 05:27:02 +03:00
|
|
|
export function useFrigateStats(): { payload: FrigateStats } {
|
|
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
} = useWs("stats", "");
|
2024-02-27 19:05:28 +03:00
|
|
|
return { payload: JSON.parse(payload) };
|
2024-02-22 05:27:02 +03:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 16:08:00 +03:00
|
|
|
export function useMotionActivity(camera: string): { payload: string } {
|
|
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
} = useWs(`${camera}/motion`, "");
|
|
|
|
|
return { payload };
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 15:30:53 +03:00
|
|
|
export function useAudioActivity(camera: string): { payload: number } {
|
2023-12-29 16:08:00 +03:00
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
} = useWs(`${camera}/audio/rms`, "");
|
|
|
|
|
return { payload };
|
|
|
|
|
}
|