fix:修复因数据库缺少 working_hours 列导致 ROI 配置失败的问题。

- 手动执行 SQL:ALTER TABLE rois ADD COLUMN working_hours TEXT
- 确保现有 SQLite 数据库(security_monitor.db)结构与模型定义一致
- 避免因字段缺失引发 API 或算法读取异常
This commit is contained in:
2026-01-22 16:44:26 +08:00
parent 123903950b
commit cb46d12cfa
8 changed files with 127 additions and 10 deletions

View File

@@ -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<number, CameraStatus> = {};
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 '未知';
}

View File

@@ -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 = () => {
<Form.Item name="confirm_sec" label="确认时间(秒)" rules={[{ required: true }]}>
<InputNumber min={5} style={{ width: '100%' }} />
</Form.Item>
<Divider></Divider>
<Form.List name="working_hours">
{(fields, { add, remove }) => (
<div>
{fields.map((field, index) => (
<Space key={field.key} align="baseline" style={{ display: 'flex', marginBottom: 8 }}>
<Form.Item
{...field}
label={index === 0 ? '时间段' : ''}
style={{ marginBottom: 0 }}
>
<TimePicker.RangePicker format="HH:mm" />
</Form.Item>
<Button
type="link"
danger
onClick={() => remove(field.name)}
>
</Button>
</Space>
))}
<Button type="dashed" onClick={() => add({ start: null, end: null })} block>
</Button>
</div>
)}
</Form.List>
<Form.Item style={{ fontSize: 12, color: '#999' }}>
使
</Form.Item>
</>
)}
<Form.Item name="enabled" label="启用状态" valuePropName="checked">