From cb46d12cfa363610d245721eeefe314f0c84e28c Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Thu, 22 Jan 2026 16:44:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=9B=A0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=BC=BA=E5=B0=91=20working=5Fhours=20?= =?UTF-8?q?=E5=88=97=E5=AF=BC=E8=87=B4=20ROI=20=E9=85=8D=E7=BD=AE=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 手动执行 SQL:ALTER TABLE rois ADD COLUMN working_hours TEXT - 确保现有 SQLite 数据库(security_monitor.db)结构与模型定义一致 - 避免因字段缺失引发 API 或算法读取异常 --- api/roi.py | 26 +++++++++-- db/crud.py | 6 +++ db/models.py | 1 + frontend/src/pages/CameraManagement.tsx | 44 ++++++++++++++++--- frontend/src/pages/ROIEditor.tsx | 56 +++++++++++++++++++++++- inference/pipeline.py | 1 + inference/rules/algorithms.py | 3 +- security_monitor.db | Bin 24576 -> 32768 bytes 8 files changed, 127 insertions(+), 10 deletions(-) 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 a12356b6e1cdea6b907d915cbe8974f6bf709fa2..8c51b6cc5d5eb58f8aa3b56ffb10bfc38b86acbe 100644 GIT binary patch literal 32768 zcmeHQ+ixS+eI`ZSuijlt%385^9A~|&MjoB(oH;~;L~&$yo4q75T{LhFmXWwCW+hS~ zDW_YsXt{NION;^qiUI|Kyrf0@l%Qx@pg~^JKcH!gzV&t8Jocd}3ZxJH&dhMg84k6i z*4`q;A+VA-XD;9G``ymvcV^%C!p&N{>fCKKk1K75=bp*s^SMtuPA-@KPA-?5#lP<7 z68@O%e!=g2-`_+2#68^i{(EWeI5#!7oczhc#UZEL!qDFOo7GCYdQe7M z`Yg0kx^Zhaar@JggEQLqj~b^3ecIV_PO9~TTK!(R^`O2VOZ3^@*LL2#S#rphV~&^a zR%#H~JkvbyjndAo&qpml+k8;oZ=BZKv9#%{->EiRhTE>a^tFGr(Y$Y-e%Lr|hMF@3 ze}3=At)0CuI-lG9qI11=5NP7HJA1n~UcYUQU5|}y)7jg7ZFg_?_N%*ZIN?zkC$5;^ z{GIFhne~m0{O?{1j5%tIeoZ7yI=*nIRVNHHZMDsI>!f_zJQ_5-vkc|#NvnKRJFfL; z>o?hxW@EqFYL%qn2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