diff --git a/api/roi.py b/api/roi.py index 589cffb..f5eff0d 100644 --- a/api/roi.py +++ b/api/roi.py @@ -29,9 +29,10 @@ class CreateROIRequest(BaseModel): rule_type: str direction: Optional[str] = None stay_time: Optional[int] = None - threshold_sec: int = 360 - confirm_sec: int = 30 - return_sec: int = 5 + threshold_sec: int = 300 + confirm_sec: int = 10 + return_sec: int = 30 + working_hours: Optional[List[dict]] = None class UpdateROIRequest(BaseModel): @@ -45,6 +46,7 @@ class UpdateROIRequest(BaseModel): threshold_sec: Optional[int] = None confirm_sec: Optional[int] = None return_sec: Optional[int] = None + working_hours: Optional[List[dict]] = None def _invalidate_roi_cache(camera_id: int): @@ -70,6 +72,7 @@ def list_rois(camera_id: int, db: Session = Depends(get_db)): "threshold_sec": roi.threshold_sec, "confirm_sec": roi.confirm_sec, "return_sec": roi.return_sec, + "working_hours": json.loads(roi.working_hours) if roi.working_hours else None, } for roi in roi_configs ] @@ -93,6 +96,7 @@ def get_roi(camera_id: int, roi_id: int, db: Session = Depends(get_db)): "threshold_sec": roi.threshold_sec, "confirm_sec": roi.confirm_sec, "return_sec": roi.return_sec, + "working_hours": json.loads(roi.working_hours) if roi.working_hours else None, } @@ -102,6 +106,10 @@ def add_roi( request: CreateROIRequest, db: Session = Depends(get_db), ): + import json + + working_hours_json = json.dumps(request.working_hours) if request.working_hours else None + roi = create_roi( db, camera_id=camera_id, @@ -115,6 +123,7 @@ def add_roi( threshold_sec=request.threshold_sec, confirm_sec=request.confirm_sec, return_sec=request.return_sec, + working_hours=working_hours_json, ) _invalidate_roi_cache(camera_id) @@ -126,7 +135,13 @@ def add_roi( "type": roi.roi_type, "points": request.points, "rule": roi.rule_type, + "direction": roi.direction, + "stay_time": roi.stay_time, "enabled": roi.enabled, + "threshold_sec": roi.threshold_sec, + "confirm_sec": roi.confirm_sec, + "return_sec": roi.return_sec, + "working_hours": request.working_hours, } @@ -137,6 +152,9 @@ def modify_roi( request: UpdateROIRequest, db: Session = Depends(get_db), ): + import json + working_hours_json = json.dumps(request.working_hours) if request.working_hours else None + roi = update_roi( db, roi_id=roi_id, @@ -149,6 +167,7 @@ def modify_roi( threshold_sec=request.threshold_sec, confirm_sec=request.confirm_sec, return_sec=request.return_sec, + working_hours=working_hours_json, ) if not roi: raise HTTPException(status_code=404, detail="ROI不存在") @@ -163,6 +182,7 @@ def modify_roi( "points": json.loads(roi.points), "rule": roi.rule_type, "enabled": roi.enabled, + "working_hours": json.loads(roi.working_hours) if roi.working_hours else None, } diff --git a/db/crud.py b/db/crud.py index 7ff9cae..5638379 100644 --- a/db/crud.py +++ b/db/crud.py @@ -139,6 +139,7 @@ def create_roi( threshold_sec: int = 300, confirm_sec: int = 10, return_sec: int = 30, + working_hours: Optional[str] = None, ) -> ROI: import json @@ -154,6 +155,7 @@ def create_roi( threshold_sec=threshold_sec, confirm_sec=confirm_sec, return_sec=return_sec, + working_hours=working_hours, ) db.add(roi) db.commit() @@ -173,6 +175,7 @@ def update_roi( threshold_sec: Optional[int] = None, confirm_sec: Optional[int] = None, return_sec: Optional[int] = None, + working_hours: Optional[str] = None, ) -> Optional[ROI]: import json @@ -198,6 +201,8 @@ def update_roi( roi.confirm_sec = confirm_sec if return_sec is not None: roi.return_sec = return_sec + if working_hours is not None: + roi.working_hours = working_hours db.commit() db.refresh(roi) @@ -232,6 +237,7 @@ def get_roi_points(db: Session, camera_id: int) -> List[dict]: "threshold_sec": roi.threshold_sec, "confirm_sec": roi.confirm_sec, "return_sec": roi.return_sec, + "working_hours": json.loads(roi.working_hours) if roi.working_hours else None, } for roi in rois ] diff --git a/db/models.py b/db/models.py index 2734629..f4630fc 100644 --- a/db/models.py +++ b/db/models.py @@ -93,6 +93,7 @@ class ROI(Base): threshold_sec: Mapped[int] = mapped_column(Integer, default=300) confirm_sec: Mapped[int] = mapped_column(Integer, default=10) return_sec: Mapped[int] = mapped_column(Integer, default=30) + working_hours: Mapped[Optional[str]] = mapped_column(Text, nullable=True) pending_sync: Mapped[bool] = mapped_column(Boolean, default=False) sync_version: Mapped[int] = mapped_column(Integer, default=0) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) diff --git a/frontend/src/pages/CameraManagement.tsx b/frontend/src/pages/CameraManagement.tsx index 65d7ddf..84d79b3 100644 --- a/frontend/src/pages/CameraManagement.tsx +++ b/frontend/src/pages/CameraManagement.tsx @@ -38,14 +38,48 @@ const CameraManagement: React.FC = () => { const fetchCameras = async () => { setLoading(true); try { - const [camerasRes, statusRes] = await Promise.all([ + const [camerasRes, statusRes, pipelineRes] = await Promise.all([ axios.get('/api/cameras?enabled_only=false'), + axios.get('/api/camera/status/all'), axios.get('/api/pipeline/status') ]); setCameras(camerasRes.data); - setCameraStatus(statusRes.data.cameras || {}); + + const statusMap: Record = {}; + + for (const cam of camerasRes.data) { + const camId = cam.id; + + let status: CameraStatus = { + is_running: false, + fps: 0, + error_message: null, + last_check_time: null, + }; + + const pipelineStatus = pipelineRes.data.cameras?.[String(camId)]; + if (pipelineStatus) { + status.is_running = pipelineStatus.is_running || false; + status.fps = pipelineStatus.fps || 0; + status.last_check_time = pipelineStatus.last_check_time; + } + + const dbStatus = statusRes.data.find((s: any) => s.camera_id === camId); + if (dbStatus) { + if (!status.is_running) { + status.is_running = dbStatus.is_running || false; + } + status.fps = dbStatus.fps || status.fps; + status.error_message = dbStatus.error_message; + status.last_check_time = status.last_check_time || dbStatus.last_check_time; + } + + statusMap[camId] = status; + } + + setCameraStatus(statusMap); } catch (err) { - message.error('获取摄像头列表失败'); + console.error('获取摄像头状态失败', err); } finally { setLoading(false); } @@ -59,8 +93,8 @@ const CameraManagement: React.FC = () => { const extractIP = (url: string): string => { try { - const match = url.match(/:\/\/([^:]+):?(\d+)?\//); - return match ? match[1] : '未知'; + const ipMatch = url.match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/); + return ipMatch ? ipMatch[1] : '未知'; } catch { return '未知'; } diff --git a/frontend/src/pages/ROIEditor.tsx b/frontend/src/pages/ROIEditor.tsx index 190cb9d..5213d45 100644 --- a/frontend/src/pages/ROIEditor.tsx +++ b/frontend/src/pages/ROIEditor.tsx @@ -1,7 +1,14 @@ import React, { useEffect, useState, useRef } from 'react'; -import { Card, Button, Space, Select, message, Drawer, Form, Input, InputNumber, Switch } from 'antd'; +import { Card, Button, Space, Select, message, Drawer, Form, Input, InputNumber, Switch, TimePicker, Divider } from 'antd'; import { Stage, Layer, Rect, Line, Circle, Text as KonvaText } from 'react-konva'; +import { RangePickerProps } from 'antd/es/date-picker'; import axios from 'axios'; +import dayjs from 'dayjs'; + +interface WorkingHours { + start: number[]; + end: number[]; +} interface ROI { id: number; @@ -13,6 +20,7 @@ interface ROI { threshold_sec: number; confirm_sec: number; return_sec: number; + working_hours: WorkingHours[] | null; } interface Camera { @@ -95,12 +103,18 @@ const ROIEditor: React.FC = () => { const handleSaveROI = async (values: any) => { if (!selectedCamera || !selectedROI) return; try { + const workingHours = values.working_hours?.map((item: any) => ({ + start: [item.start.hour(), item.start.minute()], + end: [item.end.hour(), item.end.minute()], + })); + await axios.put(`/api/camera/${selectedCamera}/roi/${selectedROI.id}`, { name: values.name, roi_type: values.roi_type, rule_type: values.rule_type, threshold_sec: values.threshold_sec, confirm_sec: values.confirm_sec, + working_hours: workingHours, enabled: values.enabled, }); message.success('保存成功'); @@ -150,6 +164,7 @@ const ROIEditor: React.FC = () => { threshold_sec: 60, confirm_sec: 5, return_sec: 5, + working_hours: [], }) .then(() => { message.success('ROI添加成功'); @@ -211,6 +226,10 @@ const ROIEditor: React.FC = () => { threshold_sec: roi.threshold_sec, confirm_sec: roi.confirm_sec, enabled: roi.enabled, + working_hours: roi.working_hours?.map((wh: WorkingHours) => ({ + start: dayjs().hour(wh.start[0]).minute(wh.start[1]), + end: dayjs().hour(wh.end[0]).minute(wh.end[1]), + })), }); setDrawerVisible(true); }} @@ -368,6 +387,10 @@ const ROIEditor: React.FC = () => { threshold_sec: roi.threshold_sec, confirm_sec: roi.confirm_sec, enabled: roi.enabled, + working_hours: roi.working_hours?.map((wh: WorkingHours) => ({ + start: dayjs().hour(wh.start[0]).minute(wh.start[1]), + end: dayjs().hour(wh.end[0]).minute(wh.end[1]), + })), }); setDrawerVisible(true); }} @@ -434,6 +457,37 @@ const ROIEditor: React.FC = () => { + 工作时间配置(可选) + + {(fields, { add, remove }) => ( +
+ {fields.map((field, index) => ( + + + + + + + ))} + +
+ )} +
+ + 不配置工作时间则使用系统全局设置 + )} diff --git a/inference/pipeline.py b/inference/pipeline.py index b4046de..a1e6d00 100644 --- a/inference/pipeline.py +++ b/inference/pipeline.py @@ -190,6 +190,7 @@ class InferencePipeline: "threshold_sec": roi_config.get("threshold_sec", 300), "confirm_sec": roi_config.get("confirm_sec", 10), "return_sec": roi_config.get("return_sec", 30), + "working_hours": roi_config.get("working_hours"), }, ) diff --git a/inference/rules/algorithms.py b/inference/rules/algorithms.py index c59c70e..46b574d 100644 --- a/inference/rules/algorithms.py +++ b/inference/rules/algorithms.py @@ -293,11 +293,12 @@ class AlgorithmManager: algo_params.update(params) if algorithm_type == "leave_post": + roi_working_hours = algo_params.get("working_hours") or self.working_hours self.algorithms[roi_id]["leave_post"] = LeavePostAlgorithm( threshold_sec=algo_params.get("threshold_sec", 300), confirm_sec=algo_params.get("confirm_sec", 10), return_sec=algo_params.get("return_sec", 30), - working_hours=self.working_hours, + working_hours=roi_working_hours, ) elif algorithm_type == "intrusion": self.algorithms[roi_id]["intrusion"] = IntrusionAlgorithm( diff --git a/security_monitor.db b/security_monitor.db index a12356b..8c51b6c 100644 Binary files a/security_monitor.db and b/security_monitor.db differ