feat: 端口管理页面添加网卡功能和服务器背板可视化

- 在端口管理页面添加网卡管理功能,与3D可视化页面同步
- 新增 ServerBackplanePanel 组件,按真实服务器背板布局展示网卡和端口
- 支持板载网卡、管理口、PCIe扩展插槽的可视化展示
- 添加网卡-端口层级展示,点击网卡可查看端口详情
- 更新 VirtualDeviceList 组件,集成服务器背板视图
This commit is contained in:
zhang1106
2026-01-30 17:53:05 +08:00
parent 5e3ac98bac
commit bc2f17890d
8 changed files with 2149 additions and 97 deletions
+212 -19
View File
@@ -3,6 +3,29 @@ const router = express.Router();
const { Op } = require('sequelize');
const Cable = require('../models/Cable');
const Device = require('../models/Device');
const DevicePort = require('../models/DevicePort');
// 辅助函数:更新端口状态
async function updatePortStatus(deviceId, portName, status) {
try {
await DevicePort.update(
{ status },
{ where: { deviceId, portName } }
);
} catch (error) {
console.error(`更新端口状态失败: ${deviceId}:${portName} -> ${status}`, error);
}
}
// 辅助函数:将端口状态设为occupied
async function occupyPort(deviceId, portName) {
await updatePortStatus(deviceId, portName, 'occupied');
}
// 辅助函数:将端口状态恢复为free
async function freePort(deviceId, portName) {
await updatePortStatus(deviceId, portName, 'free');
}
router.get('/', async (req, res) => {
try {
@@ -136,33 +159,146 @@ router.get('/rack/:rackId', async (req, res) => {
}
});
router.post('/', async (req, res) => {
// 检查接线冲突
router.post('/check-conflict', async (req, res) => {
try {
const { cableId, sourceDeviceId, sourcePort, targetDeviceId, targetPort, cableType, cableLength, status, description } = req.body;
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort, excludeCableId } = req.body;
if (!sourceDeviceId || !sourcePort || !targetDeviceId || !targetPort) {
return res.status(400).json({ error: '缺少必填字段' });
}
if (sourceDeviceId === targetDeviceId) {
return res.status(400).json({ error: '源设备和目标设备不能相同' });
}
const existingCable = await Cable.findOne({
const conflicts = [];
// 检查源端口冲突
const sourceConflict = await Cable.findOne({
where: {
[Op.or]: [
{ sourceDeviceId, sourcePort },
{ targetDeviceId, targetPort }
]
}
{ targetDeviceId: sourceDeviceId, targetPort: sourcePort }
],
...(excludeCableId && { cableId: { [Op.ne]: excludeCableId } })
},
include: [
{
model: Device,
as: 'sourceDevice',
attributes: ['deviceId', 'name', 'type']
},
{
model: Device,
as: 'targetDevice',
attributes: ['deviceId', 'name', 'type']
}
]
});
if (existingCable) {
return res.status(400).json({ error: '端口已被占用' });
if (sourceConflict) {
conflicts.push({
type: 'source',
port: sourcePort,
deviceId: sourceDeviceId,
existingCable: sourceConflict
});
}
// 检查目标端口冲突
const targetConflict = await Cable.findOne({
where: {
[Op.or]: [
{ sourceDeviceId: targetDeviceId, sourcePort: targetPort },
{ targetDeviceId, targetPort }
],
...(excludeCableId && { cableId: { [Op.ne]: excludeCableId } })
},
include: [
{
model: Device,
as: 'sourceDevice',
attributes: ['deviceId', 'name', 'type']
},
{
model: Device,
as: 'targetDevice',
attributes: ['deviceId', 'name', 'type']
}
]
});
if (targetConflict) {
conflicts.push({
type: 'target',
port: targetPort,
deviceId: targetDeviceId,
existingCable: targetConflict
});
}
res.json({
hasConflict: conflicts.length > 0,
conflicts
});
} catch (error) {
console.error('检查接线冲突失败:', error);
res.status(500).json({ error: error.message });
}
});
router.post('/', async (req, res) => {
try {
const { cableId, sourceDeviceId, sourcePort, targetDeviceId, targetPort, cableType, cableLength, status, description, force } = req.body;
if (!sourceDeviceId || !sourcePort || !targetDeviceId || !targetPort) {
return res.status(400).json({ error: '缺少必填字段' });
}
if (sourceDeviceId === targetDeviceId) {
return res.status(400).json({ error: '源设备和目标设备不能相同' });
}
// 如果不是强制模式,检查冲突
if (!force) {
const existingCable = await Cable.findOne({
where: {
[Op.or]: [
{ sourceDeviceId, sourcePort },
{ targetDeviceId, targetPort }
]
}
});
if (existingCable) {
return res.status(409).json({
error: '端口已被占用',
conflict: true,
existingCable
});
}
}
// 如果是强制模式,先断开原有连接
if (force) {
const existingCables = await Cable.findAll({
where: {
[Op.or]: [
{ sourceDeviceId, sourcePort },
{ targetDeviceId: sourceDeviceId, targetPort: sourcePort },
{ sourceDeviceId: targetDeviceId, sourcePort: targetPort },
{ targetDeviceId, targetPort }
]
}
});
for (const cable of existingCables) {
await cable.destroy();
// 释放原有端口
await freePort(cable.sourceDeviceId, cable.sourcePort);
await freePort(cable.targetDeviceId, cable.targetPort);
}
}
const autoCableId = cableId || `CABLE-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
const cable = await Cable.create({
cableId: autoCableId,
sourceDeviceId,
@@ -174,7 +310,7 @@ router.post('/', async (req, res) => {
status: status || 'normal',
description
});
const createdCable = await Cable.findByPk(cable.cableId, {
include: [
{
@@ -189,7 +325,11 @@ router.post('/', async (req, res) => {
}
]
});
// 自动将源端口和目标端口状态设为occupied
await occupyPort(sourceDeviceId, sourcePort);
await occupyPort(targetDeviceId, targetPort);
res.status(201).json(createdCable);
} catch (error) {
console.error('创建接线失败:', error);
@@ -250,6 +390,10 @@ router.post('/batch', async (req, res) => {
description: cableData.description
});
// 自动将源端口和目标端口状态设为occupied
await occupyPort(cableData.sourceDeviceId, cableData.sourcePort);
await occupyPort(cableData.targetDeviceId, cableData.targetPort);
results.success++;
} catch (error) {
results.failed++;
@@ -270,6 +414,15 @@ router.post('/batch', async (req, res) => {
router.put('/:cableId', async (req, res) => {
try {
// 获取更新前的接线信息
const oldCable = await Cable.findByPk(req.params.cableId);
if (!oldCable) {
return res.status(404).json({ error: '接线不存在' });
}
const { sourceDeviceId: oldSourceDeviceId, sourcePort: oldSourcePort, targetDeviceId: oldTargetDeviceId, targetPort: oldTargetPort } = oldCable;
const [updated] = await Cable.update(req.body, {
where: { cableId: req.params.cableId }
});
@@ -289,6 +442,22 @@ router.put('/:cableId', async (req, res) => {
}
]
});
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = cable;
// 同步更新端口状态
// 源端口变更:释放旧端口,占用新端口
if (oldSourceDeviceId !== sourceDeviceId || oldSourcePort !== sourcePort) {
await freePort(oldSourceDeviceId, oldSourcePort);
await occupyPort(sourceDeviceId, sourcePort);
}
// 目标端口变更:释放旧端口,占用新端口
if (oldTargetDeviceId !== targetDeviceId || oldTargetPort !== targetPort) {
await freePort(oldTargetDeviceId, oldTargetPort);
await occupyPort(targetDeviceId, targetPort);
}
res.json(cable);
} else {
res.status(404).json({ error: '接线不存在' });
@@ -301,11 +470,24 @@ router.put('/:cableId', async (req, res) => {
router.delete('/:cableId', async (req, res) => {
try {
// 先获取接线信息,用于后续恢复端口状态
const cable = await Cable.findByPk(req.params.cableId);
if (!cable) {
return res.status(404).json({ error: '接线不存在' });
}
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = cable;
const deleted = await Cable.destroy({
where: { cableId: req.params.cableId }
});
if (deleted) {
// 自动将源端口和目标端口状态恢复为free
await freePort(sourceDeviceId, sourcePort);
await freePort(targetDeviceId, targetPort);
res.status(204).json();
} else {
res.status(404).json({ error: '接线不存在' });
@@ -324,10 +506,21 @@ router.delete('/batch', async (req, res) => {
return res.status(400).json({ error: '请提供有效的接线ID列表' });
}
// 先获取所有要删除的接线信息,用于后续恢复端口状态
const cables = await Cable.findAll({
where: { cableId: { [Op.in]: cableIds } }
});
const deletedCount = await Cable.destroy({
where: { cableId: { [Op.in]: cableIds } }
});
// 自动将所有相关端口状态恢复为free
for (const cable of cables) {
await freePort(cable.sourceDeviceId, cable.sourcePort);
await freePort(cable.targetDeviceId, cable.targetPort);
}
res.json({
message: `批量删除成功,已删除 ${deletedCount} 条接线`,
deletedCount