- added camera black board fetch and post
- region selector can save settings and painted regions and fetch on load - will add reset all
This commit is contained in:
@@ -1,9 +1,33 @@
|
||||
import { useReducer, type ReactNode } from "react";
|
||||
import { useEffect, useReducer, type ReactNode } from "react";
|
||||
import { CameraFeedContext } from "../context/CameraFeedContext";
|
||||
import { initialState, reducer } from "../reducers/cameraFeedReducer";
|
||||
import { useBlackBoard } from "../../hooks/useBlackBoard";
|
||||
import type { CameraFeedState } from "../../types/types";
|
||||
|
||||
export const CameraFeedProvider = ({ children }: { children: ReactNode }) => {
|
||||
const { blackboardMutation } = useBlackBoard();
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchBlackBoardData = async () => {
|
||||
const result = await blackboardMutation.mutateAsync({
|
||||
operation: "VIEW",
|
||||
path: "cameraFeed",
|
||||
});
|
||||
if (!result?.result || typeof result.result === "string") return;
|
||||
const cameraFeedData: CameraFeedState = result.result;
|
||||
const recontructedState = {
|
||||
...cameraFeedData,
|
||||
paintedCells: {
|
||||
A: new Map(cameraFeedData.paintedCells.A),
|
||||
B: new Map(cameraFeedData.paintedCells.B),
|
||||
C: new Map(cameraFeedData.paintedCells.C),
|
||||
},
|
||||
};
|
||||
dispatch({ type: "SET_CAMERA_FEED_DATA", cameraState: recontructedState });
|
||||
};
|
||||
fetchBlackBoardData();
|
||||
}, []);
|
||||
|
||||
return <CameraFeedContext.Provider value={{ state, dispatch }}>{children}</CameraFeedContext.Provider>;
|
||||
};
|
||||
|
||||
@@ -98,6 +98,14 @@ export function reducer(state: CameraFeedState, action: CameraFeedAction) {
|
||||
[state.cameraFeedID]: new Map<string, PaintedCell>(),
|
||||
},
|
||||
};
|
||||
case "SET_CAMERA_FEED_DATA":
|
||||
return {
|
||||
...action.cameraState,
|
||||
};
|
||||
case "RESET_CAMERA_FEED":
|
||||
return {
|
||||
...initialState,
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
|
||||
@@ -2,6 +2,8 @@ import type { ColourData, PaintedCell, Region } from "../../../../types/types";
|
||||
import ColourPicker from "./ColourPicker";
|
||||
import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext";
|
||||
import { useColourDectection } from "../../hooks/useColourDetection";
|
||||
import { useBlackBoard } from "../../../../hooks/useBlackBoard";
|
||||
import { toast } from "sonner";
|
||||
|
||||
type RegionSelectorProps = {
|
||||
regions: Region[];
|
||||
@@ -13,6 +15,7 @@ type RegionSelectorProps = {
|
||||
const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: RegionSelectorProps) => {
|
||||
const { colourMutation } = useColourDectection();
|
||||
const { state, dispatch } = useCameraFeedContext();
|
||||
const { blackboardMutation } = useBlackBoard();
|
||||
const paintedCells = state.paintedCells[cameraFeedID];
|
||||
|
||||
const handleChange = (e: { target: { value: string } }) => {
|
||||
@@ -58,6 +61,10 @@ const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: Re
|
||||
});
|
||||
};
|
||||
|
||||
const handleResetAll = () => {
|
||||
dispatch({ type: "RESET_CAMERA_FEED" });
|
||||
};
|
||||
|
||||
const handleSaveclick = () => {
|
||||
const regions: ColourData[] = [];
|
||||
const test = Array.from(paintedCells.entries());
|
||||
@@ -103,12 +110,24 @@ const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: Re
|
||||
}
|
||||
|
||||
colourMutation.mutate({ cameraFeedID, regions: regions });
|
||||
|
||||
// Convert Map to plain object for blackboard
|
||||
const serializableState = {
|
||||
...state,
|
||||
paintedCells: {
|
||||
A: Array.from(state.paintedCells.A.entries()),
|
||||
B: Array.from(state.paintedCells.B.entries()),
|
||||
C: Array.from(state.paintedCells.C.entries()),
|
||||
},
|
||||
};
|
||||
blackboardMutation.mutate({ operation: "INSERT", path: `cameraFeed`, value: serializableState });
|
||||
toast.success("Region data saved successfully!");
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4 max-h-[50%]">
|
||||
<div className="flex flex-col md:flex-row gap-3">
|
||||
<div className="p-2 border border-gray-600 rounded-lg flex flex-col h-50 w-full">
|
||||
<div className="p-2 border border-gray-600 rounded-lg flex flex-col h-[10%] w-full">
|
||||
<h2 className="text-2xl mb-2">Tools</h2>
|
||||
<div className="flex flex-col">
|
||||
<label
|
||||
@@ -208,6 +227,12 @@ const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: Re
|
||||
>
|
||||
Reset Region
|
||||
</button>
|
||||
<button
|
||||
onClick={handleResetAll}
|
||||
className="mt-2 px-4 py-2 border border-red-600 rounded-md text-white bg-red-600 w-full md:w-full hover:bg-red-700 hover:cursor-pointer"
|
||||
>
|
||||
Reset All
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -16,7 +16,7 @@ const gap = 0;
|
||||
const VideoFeedGridPainter = () => {
|
||||
const { state } = useCameraFeedContext();
|
||||
const cameraFeedID = state.cameraFeedID;
|
||||
const paintedCells = state.paintedCells[cameraFeedID];
|
||||
const paintedCells = state?.paintedCells?.[cameraFeedID];
|
||||
const regions = state.regionsByCamera[cameraFeedID];
|
||||
const selectedRegionIndex = state.selectedRegionIndex;
|
||||
const mode = state.modeByCamera[cameraFeedID];
|
||||
@@ -135,7 +135,8 @@ const VideoFeedGridPainter = () => {
|
||||
<Shape
|
||||
sceneFunc={(ctx, shape) => {
|
||||
const cells = paintedCells;
|
||||
cells.forEach((cell, key) => {
|
||||
if (!cells || cells.size === 0 || !paintLayerRef.current) return;
|
||||
cells?.forEach((cell, key) => {
|
||||
const [rowStr, colStr] = key.split("-");
|
||||
const row = Number(rowStr);
|
||||
const col = Number(colStr);
|
||||
|
||||
35
src/hooks/useBlackBoard.ts
Normal file
35
src/hooks/useBlackBoard.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { useQuery, useMutation } from "@tanstack/react-query";
|
||||
import { CAMBASE } from "../utils/config";
|
||||
import type { BlackBoardOptions } from "../types/types";
|
||||
|
||||
const fetchBlackBoardData = async () => {
|
||||
const response = await fetch(`${CAMBASE}/api/blackboard`);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch blackboard data");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
const viewBlackBoardData = async (options: BlackBoardOptions) => {
|
||||
const response = await fetch(`${CAMBASE}/api/blackboard`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(options),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to view blackboard data");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const useBlackBoard = () => {
|
||||
const blackboardQuery = useQuery({
|
||||
queryKey: ["blackboardData"],
|
||||
queryFn: fetchBlackBoardData,
|
||||
});
|
||||
|
||||
const blackboardMutation = useMutation({
|
||||
mutationKey: ["viewBlackBoardData"],
|
||||
mutationFn: (options: BlackBoardOptions) => viewBlackBoardData(options),
|
||||
});
|
||||
return { blackboardQuery, blackboardMutation };
|
||||
};
|
||||
@@ -170,6 +170,13 @@ export type CameraFeedAction =
|
||||
| {
|
||||
type: "RESET_PAINTED_CELLS";
|
||||
payload: { cameraFeedID: "A" | "B" | "C"; paintedCells: Map<string, PaintedCell> };
|
||||
}
|
||||
| {
|
||||
type: "SET_CAMERA_FEED_DATA";
|
||||
cameraState: CameraFeedState;
|
||||
}
|
||||
| {
|
||||
type: "RESET_CAMERA_FEED";
|
||||
};
|
||||
|
||||
export type DecodeReading = {
|
||||
@@ -214,3 +221,9 @@ export type CustomFieldConfig = {
|
||||
label: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
export type BlackBoardOptions = {
|
||||
operation?: string;
|
||||
path?: string;
|
||||
value?: object | string | number | (string | number)[] | null;
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ const Card = ({ children, className }: CardProps) => {
|
||||
return (
|
||||
<div
|
||||
className={clsx(
|
||||
"bg-[#253445] rounded-lg mt-4 shadow-2xl overflow-x-hidden md:row-span-1 px-2 border border-gray-600 ",
|
||||
"bg-[#253445] rounded-lg mt-4 shadow-2xl overflow-x-hidden md:row-span-1 px-2 border border-gray-600 ",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user