Update ws typing

This commit is contained in:
Nick Mowen 2023-12-28 09:28:09 -07:00
parent 5177c983c8
commit 6fbbb491ac
3 changed files with 37 additions and 22 deletions

View File

@ -10,18 +10,19 @@ import {
import { produce, Draft } from "immer"; import { produce, Draft } from "immer";
import useWebSocket, { ReadyState } from "react-use-websocket"; import useWebSocket, { ReadyState } from "react-use-websocket";
import { FrigateConfig } from "@/types/frigateConfig"; import { FrigateConfig } from "@/types/frigateConfig";
import { FrigateEvent } from "@/types/ws";
type ReducerState = { type ReducerState = {
[topic: string]: { [topic: string]: {
lastUpdate: number; lastUpdate: number;
payload: string; payload: any;
retain: boolean; retain: boolean;
}; };
}; };
type ReducerAction = { type ReducerAction = {
topic: string; topic: string;
payload: string; payload: any;
retain: boolean; retain: boolean;
}; };
@ -132,7 +133,7 @@ export function useWs(watchTopic: string, publishTopic: string) {
const value = state[watchTopic] || { payload: null }; const value = state[watchTopic] || { payload: null };
const send = useCallback( const send = useCallback(
(payload: string, retain = false) => { (payload: any, retain = false) => {
if (readyState === ReadyState.OPEN) { if (readyState === ReadyState.OPEN) {
sendJsonMessage({ sendJsonMessage({
topic: publishTopic || watchTopic, topic: publishTopic || watchTopic,
@ -147,7 +148,10 @@ export function useWs(watchTopic: string, publishTopic: string) {
return { value, send }; return { value, send };
} }
export function useDetectState(camera: string) { export function useDetectState(camera: string): {
payload: string;
send: (payload: string, retain?: boolean) => void;
} {
const { const {
value: { payload }, value: { payload },
send, send,
@ -155,7 +159,10 @@ export function useDetectState(camera: string) {
return { payload, send }; return { payload, send };
} }
export function useRecordingsState(camera: string) { export function useRecordingsState(camera: string): {
payload: string;
send: (payload: string, retain?: boolean) => void;
} {
const { const {
value: { payload }, value: { payload },
send, send,
@ -163,7 +170,10 @@ export function useRecordingsState(camera: string) {
return { payload, send }; return { payload, send };
} }
export function useSnapshotsState(camera: string) { export function useSnapshotsState(camera: string): {
payload: string;
send: (payload: string, retain?: boolean) => void;
} {
const { const {
value: { payload }, value: { payload },
send, send,
@ -171,7 +181,10 @@ export function useSnapshotsState(camera: string) {
return { payload, send }; return { payload, send };
} }
export function useAudioState(camera: string) { export function useAudioState(camera: string): {
payload: string;
send: (payload: string, retain?: boolean) => void;
} {
const { const {
value: { payload }, value: { payload },
send, send,
@ -179,7 +192,10 @@ export function useAudioState(camera: string) {
return { payload, send }; return { payload, send };
} }
export function usePtzCommand(camera: string) { export function usePtzCommand(camera: string): {
payload: string;
send: (payload: string, retain?: boolean) => void;
} {
const { const {
value: { payload }, value: { payload },
send, send,
@ -187,7 +203,10 @@ export function usePtzCommand(camera: string) {
return { payload, send }; return { payload, send };
} }
export function useRestart() { export function useRestart(): {
payload: string;
send: (payload: string, retain?: boolean) => void;
} {
const { const {
value: { payload }, value: { payload },
send, send,
@ -195,21 +214,21 @@ export function useRestart() {
return { payload, send }; return { payload, send };
} }
export function useFrigateEvents() { export function useFrigateEvents(): { payload: FrigateEvent } {
const { const {
value: { payload }, value: { payload },
} = useWs(`events`, ""); } = useWs(`events`, "");
return { payload }; return { payload };
} }
export function useMotionActivity(camera: string) { export function useMotionActivity(camera: string): { payload: string } {
const { const {
value: { payload }, value: { payload },
} = useWs(`${camera}/motion`, ""); } = useWs(`${camera}/motion`, "");
return { payload }; return { payload };
} }
export function useAudioActivity(camera: string) { export function useAudioActivity(camera: string): { payload: string } {
const { const {
value: { payload }, value: { payload },
} = useWs(`${camera}/audio/rms`, ""); } = useWs(`${camera}/audio/rms`, "");

View File

@ -32,14 +32,12 @@ export default function DynamicCameraImage({
return; return;
} }
const frigateEvent = event as unknown as FrigateEvent; if (event.after.camera != camera.name) {
if (frigateEvent.after.camera != camera.name) {
return; return;
} }
if (frigateEvent.type == "end") { if (event.type == "end") {
const eventIndex = activeObjects.indexOf(frigateEvent.after.id); const eventIndex = activeObjects.indexOf(event.after.id);
if (eventIndex != -1) { if (eventIndex != -1) {
const newActiveObjects = [...activeObjects]; const newActiveObjects = [...activeObjects];
@ -47,11 +45,11 @@ export default function DynamicCameraImage({
setActiveObjects(newActiveObjects); setActiveObjects(newActiveObjects);
} }
} else { } else {
if (!frigateEvent.after.stationary) { if (!event.after.stationary) {
const eventIndex = activeObjects.indexOf(frigateEvent.after.id); const eventIndex = activeObjects.indexOf(event.after.id);
if (eventIndex == -1) { if (eventIndex == -1) {
const newActiveObjects = [...activeObjects, frigateEvent.after.id]; const newActiveObjects = [...activeObjects, event.after.id];
setActiveObjects(newActiveObjects); setActiveObjects(newActiveObjects);
setKey(Date.now()); setKey(Date.now());
} }

View File

@ -10,8 +10,6 @@ import useSWR from "swr";
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig"; import { CameraConfig, FrigateConfig } from "@/types/frigateConfig";
import Heading from "@/components/ui/heading"; import Heading from "@/components/ui/heading";
import { Card } from "@/components/ui/card"; import { Card } from "@/components/ui/card";
import CameraImage from "@/components/camera/CameraImage";
import { AspectRatio } from "@/components/ui/aspect-ratio";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { AiOutlinePicture } from "react-icons/ai"; import { AiOutlinePicture } from "react-icons/ai";
import { FaWalking } from "react-icons/fa"; import { FaWalking } from "react-icons/fa";