设备接入

This commit is contained in:
mmc
2026-05-07 09:29:14 +08:00
parent 0fd80642e5
commit ed32020a0e
2 changed files with 513 additions and 1 deletions

View File

@@ -0,0 +1,511 @@
import { useState } from 'react';
import {
Card, Table, Button, Space, Form, Modal, Input, Select, InputNumber,
Tabs, Tag, Popconfirm, message, Descriptions, Divider, Empty
} from 'antd';
import {
PlusOutlined, EditOutlined, DeleteOutlined, ProductOutlined,
AppstoreOutlined, SwapOutlined, VideoCameraOutlined, AlertOutlined
} from '@ant-design/icons';
const { Option } = Select;
const { TabPane } = Tabs;
// ====================== 模拟基础数据 ======================
const productList = [
{ id: 1, name: '海康威视摄像头', code: 'hikvision', protocolType: 'onvif', manufacturer: '海康', status: 1 },
{ id: 2, name: '大华摄像头', code: 'dahua', protocolType: 'rtsp', manufacturer: '大华', status: 1 },
{ id: 3, name: '通用MQTT设备', code: 'mqtt_device', protocolType: 'mqtt', manufacturer: '通用', status: 1 },
];
const modelList = [
{ id: 10, name: '通用视频监控设备', code: 'video_camera', desc: '摄像头、云台、告警' },
{ id: 11, name: '环境监测设备', code: 'env_sensor', desc: '温湿度、烟感' },
];
// 物模型详情
const modelDetailList: Record<number, {
props: any[];
events: any[];
actions: any[];
}> = {
10: {
props: [
{ id: 1, code: 'device_online', name: '在线状态', type: 'bool', rw: 'r' },
{ id: 2, code: 'record_status', name: '录像状态', type: 'string', rw: 'r' },
{ id: 3, code: 'battery', name: '电量', type: 'int', rw: 'r' },
],
events: [
{ id: 1, code: 'alarm_perimeter', name: '周界入侵告警' },
{ id: 2, code: 'alarm_people', name: '人员闯入告警' },
],
actions: [
{ id: 1, code: 'take_snapshot', name: '截图' },
{ id: 2, code: 'set_record', name: '开启/关闭录像' },
]
}
};
// 字段映射数据
const mappingInitList = [
{ id: 1, productId: 1, standardCode: 'device_online', sourceField: 'online_flag', convertRule: 'value==1?true:false' },
{ id: 2, productId: 1, standardCode: 'record_status', sourceField: 'record_state', convertRule: 'value' },
];
const deviceList = [
{
id: 1,
name: '1#阵列区球机',
sn: 'HK20260507001',
productId: 1,
productName: '海康威视摄像头',
modelId: 10,
modelName: '通用视频监控设备',
protocolType: 'onvif',
ip: '192.168.1.65',
port: 80,
username: 'admin',
password: '123456',
onlineStatus: 1,
createTime: '2026-05-07 10:23:45'
},
];
const alarmList = [
{ id: 1, deviceId: 1, deviceName: '1#阵列区球机', level: 'warn', content: '周界入侵告警', time: '2026-05-07 10:23:41', status: 0 },
];
export default function DeviceAccessPage() {
const [activeTab, setActiveTab] = useState('device');
const [form] = Form.useForm();
const [visible, setVisible] = useState(false);
const [modalType, setModalType] = useState('');
const [editData, setEditData] = useState<any>(null);
const [protocol, setProtocol] = useState('onvif');
// 物模型详情
const [modelDetail, setModelDetail] = useState<any>({ props: [], events: [], actions: [] });
// 模型子项弹窗(属性/事件/动作)
const [itemModal, setItemModal] = useState(false);
const [itemType, setItemType] = useState<'props' | 'events' | 'actions'>('props');
const [itemForm] = Form.useForm();
// 字段映射
const [mappingList, setMappingList] = useState<any[]>(mappingInitList);
const [mappingModal, setMappingModal] = useState(false);
const [mappingForm] = Form.useForm();
// 打开主弹窗
const openModal = (type: string, record?: any) => {
setModalType(type);
setEditData(record);
form.resetFields();
if (record) {
form.setFieldsValue(record);
if (type === 'model') {
setModelDetail(modelDetailList[record.id] || { props: [], events: [], actions: [] });
}
} else {
setModelDetail({ props: [], events: [], actions: [] });
}
setVisible(true);
};
// 主表单保存
const handleSubmit = () => {
form.validateFields().then(() => {
message.success('保存成功');
setVisible(false);
});
};
// ========== 物模型 增删改 ==========
const openItemModal = (type: 'props' | 'events' | 'actions', record?: any) => {
setItemType(type);
itemForm.resetFields();
if (record) itemForm.setFieldsValue(record);
setItemModal(true);
};
const saveItem = () => {
itemForm.validateFields().then((val) => {
const list = [...modelDetail[itemType]];
if (val.id) {
const idx = list.findIndex(x => x.id === val.id);
idx >= 0 && (list[idx] = val);
} else {
list.push({ ...val, id: Date.now() });
}
setModelDetail({ ...modelDetail, [itemType]: list });
setItemModal(false);
message.success('保存成功');
});
};
const deleteItem = (type: 'props' | 'events' | 'actions', id: number) => {
const list = modelDetail[type].filter(x => x.id !== id);
setModelDetail({ ...modelDetail, [type]: list });
message.success('删除成功');
};
// ========== 字段映射 增删改 核心 ==========
const openMappingModal = (record: any, row?: any) => {
setVisible(false);
mappingForm.resetFields();
if (row) {
mappingForm.setFieldsValue(row);
} else {
mappingForm.setFieldsValue({ productId: record.id });
}
setMappingModal(true);
};
const saveMapping = () => {
mappingForm.validateFields().then((val) => {
const list = [...mappingList];
if (val.id) {
const idx = list.findIndex(item => item.id === val.id);
list[idx] = val;
} else {
list.push({ ...val, id: Date.now() });
}
setMappingList(list);
setMappingModal(false);
message.success('字段映射保存成功');
setTimeout(() => setVisible(true), 300);
});
};
const delMapping = (id: number) => {
const list = mappingList.filter(item => item.id !== id);
setMappingList(list);
message.success('删除成功');
};
// 获取当前模型标准属性下拉
const getStandardPropOptions = () => {
if (!modelDetail?.props) return [];
return modelDetail.props.map((item: any) => (
<Option key={item.code} value={item.code}>{item.name}({item.code})</Option>
));
};
// 表格列
const deviceColumns = [
{ title: '设备名称', dataIndex: 'name' },
{ title: 'SN', dataIndex: 'sn' },
{ title: '所属产品', dataIndex: 'productName' },
{ title: '物模型', dataIndex: 'modelName' },
{ title: '协议', dataIndex: 'protocolType', render: (t: string) => <Tag color={t === 'onvif' ? 'blue' : t === 'rtsp' ? 'orange' : 'purple'}>{t.toUpperCase()}</Tag> },
{ title: 'IP', dataIndex: 'ip' },
{ title: '状态', dataIndex: 'onlineStatus', render: (s: number) => <Tag color={s === 1 ? 'green' : 'red'}>{s === 1 ? '在线' : '离线'}</Tag> },
{
title: '操作', render: (r: any) => (
<Space>
<Button type="text" icon={<EditOutlined />} onClick={() => openModal('device', r)}>编辑</Button>
</Space>
)
}
];
const productColumns = [
{ title: '产品名称', dataIndex: 'name' },
{ title: '产品编码', dataIndex: 'code' },
{ title: '协议类型', dataIndex: 'protocolType', render: (t: string) => <Tag>{t.toUpperCase()}</Tag> },
{ title: '厂商', dataIndex: 'manufacturer' },
{
title: '操作', render: (r: any) => (
<Space>
<Button type="text" icon={<EditOutlined />} onClick={() => openModal('product', r)}>编辑</Button>
<Button type="text" icon={<SwapOutlined />} onClick={() => openModal('mapping', r)}>字段映射</Button>
</Space>
)
}
];
const modelColumns = [
{ title: '模型名称', dataIndex: 'name' },
{ title: '模型编码', dataIndex: 'code' },
{ title: '描述', dataIndex: 'desc' },
{ title: '操作', render: (r: any) => <Button type="text" icon={<EditOutlined />} onClick={() => openModal('model', r)}>编辑</Button> }
];
// 字段映射列表列
const mappingColumns = [
{ title: '标准模型字段', dataIndex: 'standardCode' },
{ title: '厂商原始字段', dataIndex: 'sourceField' },
{ title: '转换规则', dataIndex: 'convertRule' },
{
title: '操作', render: (r: any) => (
<Space>
<Button type="text" icon={<EditOutlined />} onClick={() => openMappingModal(editData, r)}>编辑</Button>
<Popconfirm title="确定删除该映射?" onConfirm={() => delMapping(r.id)}>
<Button type="text" danger icon={<DeleteOutlined />}>删除</Button>
</Popconfirm>
</Space>
)
}
];
const alarmColumns = [
{ title: '设备名称', dataIndex: 'deviceName' },
{ title: '告警级别', dataIndex: 'level' },
{ title: '告警内容', dataIndex: 'content' },
{ title: '告警时间', dataIndex: 'time' },
];
// 物模型子项列
const propColumns = [
{ title: '字段编码', dataIndex: 'code' },
{ title: '字段名称', dataIndex: 'name' },
{ title: '数据类型', dataIndex: 'type' },
{ title: '读写类型', dataIndex: 'rw' },
{
title: '操作', render: (r: any) => (
<Space>
<Button type="text" onClick={() => openItemModal('props', r)}>编辑</Button>
<Popconfirm title="确定删除?" onConfirm={() => deleteItem('props', r.id)}>
<Button type="text" danger>删除</Button>
</Popconfirm>
</Space>
)
}
];
const eventActionColumns = [
{ title: '编码', dataIndex: 'code' },
{ title: '名称', dataIndex: 'name' },
{
title: '操作', render: (r: any) => (
<Space>
<Button type="text" onClick={() => openItemModal(itemType, r)}>编辑</Button>
<Popconfirm title="确定删除?" onConfirm={() => deleteItem(itemType, r.id)}>
<Button type="text" danger>删除</Button>
</Popconfirm>
</Space>
)
}
];
return (
<div className="bg-[#f5f7fa] min-h-screen p-5">
<Card className="mb-4">
<Tabs activeKey={activeTab} onChange={setActiveTab}>
<TabPane tab={<span><VideoCameraOutlined />设备管理</span>} key="device" />
<TabPane tab={<span><ProductOutlined />产品管理</span>} key="product" />
<TabPane tab={<span><AppstoreOutlined />物模型管理</span>} key="model" />
<TabPane tab={<span><AlertOutlined />告警列表</span>} key="alarm" />
</Tabs>
</Card>
{/* 设备管理 */}
{activeTab === 'device' && (
<Card>
<Space className="mb-3">
<Button type="primary" icon={<PlusOutlined />} onClick={() => openModal('device')}>新增设备接入</Button>
</Space>
<Table rowKey="id" columns={deviceColumns} dataSource={deviceList} pagination={{ pageSize: 10 }} />
</Card>
)}
{/* 产品管理 */}
{activeTab === 'product' && (
<Card>
<Space className="mb-3">
<Button type="primary" icon={<PlusOutlined />} onClick={() => openModal('product')}>新增产品</Button>
</Space>
<Table rowKey="id" columns={productColumns} dataSource={productList} pagination={{ pageSize: 10 }} />
</Card>
)}
{/* 物模型管理 */}
{activeTab === 'model' && (
<Card>
<Space className="mb-3">
<Button type="primary" icon={<PlusOutlined />} onClick={() => openModal('model')}>新增物模型</Button>
</Space>
<Table rowKey="id" columns={modelColumns} dataSource={modelList} pagination={{ pageSize: 10 }} />
</Card>
)}
{/* 告警列表 */}
{activeTab === 'alarm' && (
<Card>
<Table rowKey="id" columns={alarmColumns} dataSource={alarmList} pagination={{ pageSize: 10 }} />
</Card>
)}
{/* 主弹窗:设备/产品/物模型 */}
<Modal
open={visible}
title={
modalType === 'device' ? '设备接入配置' :
modalType === 'product' ? '产品配置' :
modalType === 'model' ? '物模型配置' :
modalType === 'mapping' ? '字段映射管理' : ''
}
onCancel={() => setVisible(false)}
onOk={handleSubmit}
width={750}
destroyOnClose
>
<Form form={form} layout="vertical">
{/* 设备接入 */}
{modalType === 'device' && (
<Tabs defaultActiveKey="base">
<TabPane tab="基础信息" key="base">
<Form.Item label="设备名称" name="name" rules={[{ required: true }]}><Input /></Form.Item>
<Form.Item label="设备SN" name="sn" rules={[{ required: true }]}><Input /></Form.Item>
<Form.Item label="关联产品" name="productId" rules={[{ required: true }]}>
<Select onChange={(_, o: any) => setProtocol(o.protocolType)}>
{productList.map(p => <Option key={p.id} value={p.id}>{p.name}</Option>)}
</Select>
</Form.Item>
<Form.Item label="关联物模型" name="modelId" rules={[{ required: true }]}>
<Select>{modelList.map(m => <Option key={m.id} value={m.id}>{m.name}</Option>)}</Select>
</Form.Item>
</TabPane>
<TabPane tab="协议配置" key="protocol">
{protocol === 'onvif' && (
<>
<Form.Item label="设备IP" name="ip"><Input /></Form.Item>
<Form.Item label="端口" name="port"><InputNumber className="w-full" /></Form.Item>
<Form.Item label="登录账号" name="username"><Input /></Form.Item>
<Form.Item label="登录密码" name="password"><Input.Password /></Form.Item>
</>
)}
{protocol === 'rtsp' && (
<Form.Item label="RTSP流地址" name="rtspUrl"><Input placeholder="rtsp://xxx" /></Form.Item>
)}
</TabPane>
</Tabs>
)}
{/* 产品配置 */}
{modalType === 'product' && (
<>
<Form.Item label="产品名称" name="name" rules={[{ required: true }]}><Input /></Form.Item>
<Form.Item label="产品编码" name="code" rules={[{ required: true }]}><Input /></Form.Item>
<Form.Item label="协议类型" name="protocolType" rules={[{ required: true }]}>
<Select>
<Option value="onvif">ONVIF</Option>
<Option value="rtsp">RTSP</Option>
<Option value="mqtt">MQTT</Option>
</Select>
</Form.Item>
<Form.Item label="厂商名称" name="manufacturer"><Input /></Form.Item>
</>
)}
{/* 物模型完整编辑 */}
{modalType === 'model' && (
<>
<Form.Item label="模型名称" name="name" rules={[{ required: true }]}><Input /></Form.Item>
<Form.Item label="模型编码" name="code" rules={[{ required: true }]}><Input /></Form.Item>
<Form.Item label="模型描述" name="desc"><Input.TextArea rows={2} /></Form.Item>
<Divider orientation="left">属性列表</Divider>
<Button size="small" type="primary" icon={<PlusOutlined />} onClick={() => openItemModal('props')} className="mb-2">新增属性</Button>
<Table size="small" rowKey="id" columns={propColumns} dataSource={modelDetail.props} pagination={false} />
<Divider orientation="left">事件列表</Divider>
<Button size="small" type="primary" icon={<PlusOutlined />} onClick={() => openItemModal('events')} className="mb-2">新增事件</Button>
<Table size="small" rowKey="id" columns={eventActionColumns} dataSource={modelDetail.events} pagination={false} />
<Divider orientation="left">服务/指令</Divider>
<Button size="small" type="primary" icon={<PlusOutlined />} onClick={() => openItemModal('actions')} className="mb-2">新增指令</Button>
<Table size="small" rowKey="id" columns={eventActionColumns} dataSource={modelDetail.actions} pagination={false} />
</>
)}
</Form>
{/* ========== 字段映射列表(独立显示,不嵌套) ========== */}
{modalType === 'mapping' && (
<div className="mt-4">
<Divider orientation="left">
{editData?.name} - 字段映射配置
</Divider>
<Space className="mb-3">
<Button type="primary" size="small" icon={<PlusOutlined />} onClick={() => openMappingModal(editData)}>
新增映射
</Button>
</Space>
<Table
size="small"
rowKey="id"
columns={mappingColumns}
dataSource={mappingList.filter(item => item.productId === editData?.id)}
pagination={false}
/>
</div>
)}
</Modal>
{/* 物模型子项编辑弹窗(属性/事件/动作) */}
<Modal
open={itemModal}
title={itemType === 'props' ? '属性配置' : itemType === 'events' ? '事件配置' : '指令配置'}
onCancel={() => setItemModal(false)}
onOk={saveItem}
width={520}
>
<Form form={itemForm} layout="vertical">
<Form.Item name="id" hidden><Input /></Form.Item>
<Form.Item label="字段编码" name="code" rules={[{ required: true }]}><Input placeholder="如:device_online" /></Form.Item>
<Form.Item label="字段名称" name="name" rules={[{ required: true }]}><Input placeholder="如:设备在线状态" /></Form.Item>
{itemType === 'props' && (
<>
<Form.Item label="数据类型" name="type" rules={[{ required: true }]}>
<Select>
<Option value="bool">布尔 bool</Option>
<Option value="int">数字 int</Option>
<Option value="string">字符串 string</Option>
</Select>
</Form.Item>
<Form.Item label="读写权限" name="rw" rules={[{ required: true }]}>
<Select>
<Option value="r">只读</Option>
<Option value="rw">读写</Option>
</Select>
</Form.Item>
</>
)}
</Form>
</Modal>
{/* ========== 字段映射弹窗(完全独立,显示正常) ========== */}
<Modal
open={mappingModal}
title="字段映射配置"
onCancel={() => setMappingModal(false)}
onOk={saveMapping}
width={600}
destroyOnClose
>
<Form form={mappingForm} layout="vertical">
<Form.Item name="id" hidden><Input /></Form.Item>
<Form.Item name="productId" hidden><Input /></Form.Item>
<Form.Item label="平台标准字段" name="standardCode" rules={[{ required: true }]}>
<Select placeholder="请选择标准字段">
{getStandardPropOptions()}
</Select>
</Form.Item>
<Form.Item label="厂商原始字段" name="sourceField" rules={[{ required: true }]}>
<Input placeholder="例如:online_flag" />
</Form.Item>
<Form.Item label="转换规则" name="convertRule">
<Input placeholder="例如:value==1?true:false" />
</Form.Item>
</Form>
<div className="text-xs text-gray-500 mt-4">
说明:将设备上报的私有字段,转换为平台统一标准字段
</div>
</Modal>
</div>
);
}

View File

@@ -16,6 +16,7 @@ import MenuList from './MenuList';
import Organization from './Organization';
import StationManage from './StationManage';
import { getMenuList } from '../api/mune';
import DeviceAccessPage from './DeviceAccessPage';
const { Header, Content } = Layout;
const { Text } = Typography;
@@ -169,7 +170,7 @@ export default function SystemSettings() {
}, []);
const BasicSettingsPage = () => <div style={{ padding: 20 }}><h2>基础设置页面</h2></div>;
const NoticePage = () => <div style={{ padding: 20 }}><h2>通知策略页面</h2></div>;
const DeviceAccessPage = () => <div style={{ padding: 20 }}><h2>设备接入页面</h2></div>;
//const DeviceAccessPage = () => <div style={{ padding: 20 }}><h2>设备接入页面</h2></div>;
const DataSafetyPage = () => <div style={{ padding: 20 }}><h2>数据与安全页面</h2></div>;
const SystemMaintenancePage = () => <div style={{ padding: 20 }}><h2>系统维护页面</h2></div>;
const DefaultPage = () => <div style={{ padding: 20 }}><h2>请选择菜单</h2></div>;