Merge pull request '- added camera black board fetch and post' (#16) from feature/blackboard into develop

Reviewed-on: #16
This commit is contained in:
2025-12-08 11:03:47 +00:00
7 changed files with 111 additions and 5 deletions

View File

@@ -1,9 +1,33 @@
import { useReducer, type ReactNode } from "react"; import { useEffect, useReducer, type ReactNode } from "react";
import { CameraFeedContext } from "../context/CameraFeedContext"; import { CameraFeedContext } from "../context/CameraFeedContext";
import { initialState, reducer } from "../reducers/cameraFeedReducer"; import { initialState, reducer } from "../reducers/cameraFeedReducer";
import { useBlackBoard } from "../../hooks/useBlackBoard";
import type { CameraFeedState } from "../../types/types";
export const CameraFeedProvider = ({ children }: { children: ReactNode }) => { export const CameraFeedProvider = ({ children }: { children: ReactNode }) => {
const { blackboardMutation } = useBlackBoard();
const [state, dispatch] = useReducer(reducer, initialState); 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>; return <CameraFeedContext.Provider value={{ state, dispatch }}>{children}</CameraFeedContext.Provider>;
}; };

View File

@@ -98,6 +98,14 @@ export function reducer(state: CameraFeedState, action: CameraFeedAction) {
[state.cameraFeedID]: new Map<string, PaintedCell>(), [state.cameraFeedID]: new Map<string, PaintedCell>(),
}, },
}; };
case "SET_CAMERA_FEED_DATA":
return {
...action.cameraState,
};
case "RESET_CAMERA_FEED":
return {
...initialState,
};
default: default:
return state; return state;

View File

@@ -2,6 +2,8 @@ import type { ColourData, PaintedCell, Region } from "../../../../types/types";
import ColourPicker from "./ColourPicker"; import ColourPicker from "./ColourPicker";
import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext"; import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext";
import { useColourDectection } from "../../hooks/useColourDetection"; import { useColourDectection } from "../../hooks/useColourDetection";
import { useBlackBoard } from "../../../../hooks/useBlackBoard";
import { toast } from "sonner";
type RegionSelectorProps = { type RegionSelectorProps = {
regions: Region[]; regions: Region[];
@@ -13,6 +15,7 @@ type RegionSelectorProps = {
const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: RegionSelectorProps) => { const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: RegionSelectorProps) => {
const { colourMutation } = useColourDectection(); const { colourMutation } = useColourDectection();
const { state, dispatch } = useCameraFeedContext(); const { state, dispatch } = useCameraFeedContext();
const { blackboardMutation } = useBlackBoard();
const paintedCells = state.paintedCells[cameraFeedID]; const paintedCells = state.paintedCells[cameraFeedID];
const handleChange = (e: { target: { value: string } }) => { 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 handleSaveclick = () => {
const regions: ColourData[] = []; const regions: ColourData[] = [];
const test = Array.from(paintedCells.entries()); const test = Array.from(paintedCells.entries());
@@ -103,12 +110,24 @@ const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: Re
} }
colourMutation.mutate({ cameraFeedID, regions: regions }); 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 ( return (
<div className="flex flex-col gap-4 max-h-[50%]"> <div className="flex flex-col gap-4 max-h-[50%]">
<div className="flex flex-col md:flex-row gap-3"> <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> <h2 className="text-2xl mb-2">Tools</h2>
<div className="flex flex-col"> <div className="flex flex-col">
<label <label
@@ -208,6 +227,12 @@ const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID }: Re
> >
Reset Region Reset Region
</button> </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> </div>
</div> </div>

View File

@@ -16,7 +16,7 @@ const gap = 0;
const VideoFeedGridPainter = () => { const VideoFeedGridPainter = () => {
const { state } = useCameraFeedContext(); const { state } = useCameraFeedContext();
const cameraFeedID = state.cameraFeedID; const cameraFeedID = state.cameraFeedID;
const paintedCells = state.paintedCells[cameraFeedID]; const paintedCells = state?.paintedCells?.[cameraFeedID];
const regions = state.regionsByCamera[cameraFeedID]; const regions = state.regionsByCamera[cameraFeedID];
const selectedRegionIndex = state.selectedRegionIndex; const selectedRegionIndex = state.selectedRegionIndex;
const mode = state.modeByCamera[cameraFeedID]; const mode = state.modeByCamera[cameraFeedID];
@@ -135,7 +135,8 @@ const VideoFeedGridPainter = () => {
<Shape <Shape
sceneFunc={(ctx, shape) => { sceneFunc={(ctx, shape) => {
const cells = paintedCells; 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 [rowStr, colStr] = key.split("-");
const row = Number(rowStr); const row = Number(rowStr);
const col = Number(colStr); const col = Number(colStr);

View 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 };
};

View File

@@ -170,6 +170,13 @@ export type CameraFeedAction =
| { | {
type: "RESET_PAINTED_CELLS"; type: "RESET_PAINTED_CELLS";
payload: { cameraFeedID: "A" | "B" | "C"; paintedCells: Map<string, PaintedCell> }; payload: { cameraFeedID: "A" | "B" | "C"; paintedCells: Map<string, PaintedCell> };
}
| {
type: "SET_CAMERA_FEED_DATA";
cameraState: CameraFeedState;
}
| {
type: "RESET_CAMERA_FEED";
}; };
export type DecodeReading = { export type DecodeReading = {
@@ -214,3 +221,9 @@ export type CustomFieldConfig = {
label: string; label: string;
value: string; value: string;
}; };
export type BlackBoardOptions = {
operation?: string;
path?: string;
value?: object | string | number | (string | number)[] | null;
};

View File

@@ -10,7 +10,7 @@ const Card = ({ children, className }: CardProps) => {
return ( return (
<div <div
className={clsx( 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, className,
)} )}
> >