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

@@ -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">