2026-04-28 10:57:17 +08:00
|
|
|
const logger = require('../utils/logger').module('CablesRoute');
|
2026-01-23 08:55:16 +08:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
const { Op } = require('sequelize');
|
|
|
|
|
const Cable = require('../models/Cable');
|
|
|
|
|
const Device = require('../models/Device');
|
2026-01-30 17:53:05 +08:00
|
|
|
const DevicePort = require('../models/DevicePort');
|
|
|
|
|
|
2026-04-07 17:05:19 +08:00
|
|
|
const PORT_COMPATIBILITY = {
|
|
|
|
|
RJ45: {
|
|
|
|
|
compatibleWith: ['RJ45'],
|
|
|
|
|
cableTypes: ['ethernet', 'copper'],
|
|
|
|
|
description: '电口',
|
|
|
|
|
},
|
|
|
|
|
SFP: {
|
|
|
|
|
compatibleWith: ['SFP', 'SFP+', 'SFP28', 'QSFP', 'QSFP28'],
|
|
|
|
|
cableTypes: ['fiber'],
|
|
|
|
|
description: '光口',
|
|
|
|
|
},
|
|
|
|
|
'SFP+': {
|
|
|
|
|
compatibleWith: ['SFP', 'SFP+', 'SFP28', 'QSFP28'],
|
|
|
|
|
cableTypes: ['fiber'],
|
|
|
|
|
description: '万兆光口',
|
|
|
|
|
},
|
|
|
|
|
SFP28: {
|
|
|
|
|
compatibleWith: ['SFP28', 'SFP+', 'QSFP28'],
|
|
|
|
|
cableTypes: ['fiber'],
|
|
|
|
|
description: '25G光口',
|
|
|
|
|
},
|
|
|
|
|
QSFP: {
|
|
|
|
|
compatibleWith: ['QSFP', 'QSFP28'],
|
|
|
|
|
cableTypes: ['fiber'],
|
|
|
|
|
description: '40G光口',
|
|
|
|
|
},
|
|
|
|
|
QSFP28: {
|
|
|
|
|
compatibleWith: ['QSFP28', 'QSFP'],
|
|
|
|
|
cableTypes: ['fiber'],
|
|
|
|
|
description: '100G光口',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SPEED_COMPATIBILITY = {
|
|
|
|
|
'100M': { compatibleSpeeds: ['100M', '1G'], warningThreshold: null },
|
|
|
|
|
'1G': { compatibleSpeeds: ['100M', '1G', '10G'], warningThreshold: '10G' },
|
|
|
|
|
'10G': { compatibleSpeeds: ['1G', '10G', '25G'], warningThreshold: '25G' },
|
|
|
|
|
'25G': { compatibleSpeeds: ['10G', '25G', '40G'], warningThreshold: '40G' },
|
|
|
|
|
'40G': { compatibleSpeeds: ['25G', '40G', '100G'], warningThreshold: '100G' },
|
|
|
|
|
'100G': { compatibleSpeeds: ['40G', '100G'], warningThreshold: null },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function checkPortCompatibility(sourcePort, targetPort) {
|
|
|
|
|
const incompatibilityReasons = [];
|
|
|
|
|
|
|
|
|
|
const sourcePortType = sourcePort.portType;
|
|
|
|
|
const targetPortType = targetPort.portType;
|
|
|
|
|
|
|
|
|
|
const sourceCompat = PORT_COMPATIBILITY[sourcePortType];
|
|
|
|
|
const targetCompat = PORT_COMPATIBILITY[targetPortType];
|
|
|
|
|
|
|
|
|
|
if (!sourceCompat || !targetCompat) {
|
|
|
|
|
incompatibilityReasons.push({
|
|
|
|
|
type: 'unknown',
|
|
|
|
|
message: `未知端口类型: 源端口=${sourcePortType}, 目标端口=${targetPortType}`,
|
|
|
|
|
severity: 'error',
|
|
|
|
|
});
|
|
|
|
|
return { compatible: false, reasons: incompatibilityReasons };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sourceCanConnect = sourceCompat.compatibleWith.includes(targetPortType);
|
|
|
|
|
const targetCanConnect = targetCompat.compatibleWith.includes(sourcePortType);
|
|
|
|
|
|
|
|
|
|
if (!sourceCanConnect || !targetCanConnect) {
|
|
|
|
|
incompatibilityReasons.push({
|
|
|
|
|
type: 'portType',
|
|
|
|
|
message: `端口类型不兼容: 源端口(${sourcePortType}-${sourceCompat.description})无法连接到目标端口(${targetPortType}-${targetCompat.description})`,
|
|
|
|
|
severity: 'error',
|
|
|
|
|
details: {
|
|
|
|
|
sourcePortType,
|
|
|
|
|
targetPortType,
|
|
|
|
|
sourceDescription: sourceCompat.description,
|
|
|
|
|
targetDescription: targetCompat.description,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sourceSpeedCompat = SPEED_COMPATIBILITY[sourcePort.portSpeed];
|
|
|
|
|
const targetSpeedCompat = SPEED_COMPATIBILITY[targetPort.portSpeed];
|
|
|
|
|
|
|
|
|
|
if (sourceSpeedCompat && targetSpeedCompat) {
|
|
|
|
|
const sourceCanSupportTarget = sourceSpeedCompat.compatibleSpeeds.includes(targetPort.portSpeed);
|
|
|
|
|
const targetCanSupportSource = targetSpeedCompat.compatibleSpeeds.includes(sourcePort.portSpeed);
|
|
|
|
|
|
|
|
|
|
if (!sourceCanSupportTarget || !targetCanSupportSource) {
|
|
|
|
|
if (sourcePort.portSpeed !== targetPort.portSpeed) {
|
|
|
|
|
incompatibilityReasons.push({
|
|
|
|
|
type: 'speed',
|
|
|
|
|
message: `端口速率不匹配: 源端口(${sourcePort.portSpeed})与目标端口(${targetPort.portSpeed})速率不一致,可能影响连接质量`,
|
|
|
|
|
severity: 'warning',
|
|
|
|
|
details: {
|
|
|
|
|
sourceSpeed: sourcePort.portSpeed,
|
|
|
|
|
targetSpeed: targetPort.portSpeed,
|
|
|
|
|
sourceWarning: sourceSpeedCompat.warningThreshold,
|
|
|
|
|
targetWarning: targetSpeedCompat.warningThreshold,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
compatible: incompatibilityReasons.filter(r => r.severity === 'error').length === 0,
|
|
|
|
|
reasons: incompatibilityReasons,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function validatePortCompatibility(sourceDeviceId, sourcePortName, targetDeviceId, targetPortName) {
|
|
|
|
|
try {
|
|
|
|
|
const [sourcePorts, targetPorts] = await Promise.all([
|
|
|
|
|
DevicePort.findAll({ where: { deviceId: sourceDeviceId, portName: sourcePortName } }),
|
|
|
|
|
DevicePort.findAll({ where: { deviceId: targetDeviceId, portName: targetPortName } }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const sourcePort = sourcePorts[0];
|
|
|
|
|
const targetPort = targetPorts[0];
|
|
|
|
|
|
|
|
|
|
if (!sourcePort) {
|
|
|
|
|
return {
|
|
|
|
|
compatible: false,
|
|
|
|
|
reasons: [{
|
|
|
|
|
type: 'notFound',
|
|
|
|
|
message: `源设备 ${sourceDeviceId} 的端口 ${sourcePortName} 不存在`,
|
|
|
|
|
severity: 'error',
|
|
|
|
|
}],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!targetPort) {
|
|
|
|
|
return {
|
|
|
|
|
compatible: false,
|
|
|
|
|
reasons: [{
|
|
|
|
|
type: 'notFound',
|
|
|
|
|
message: `目标设备 ${targetDeviceId} 的端口 ${targetPortName} 不存在`,
|
|
|
|
|
severity: 'error',
|
|
|
|
|
}],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkPortCompatibility(sourcePort, targetPort);
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('验证端口兼容性时出错', { error: error.message, stack: error.stack });
|
2026-04-07 17:05:19 +08:00
|
|
|
return {
|
|
|
|
|
compatible: false,
|
|
|
|
|
reasons: [{
|
|
|
|
|
type: 'error',
|
|
|
|
|
message: `验证端口兼容性时出错: ${error.message}`,
|
|
|
|
|
severity: 'error',
|
|
|
|
|
}],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 辅助函数:更新端口状态
|
|
|
|
|
async function updatePortStatus(deviceId, portName, status) {
|
|
|
|
|
try {
|
2026-03-27 19:12:16 +08:00
|
|
|
await DevicePort.update({ status }, { where: { deviceId, portName } });
|
2026-01-30 17:53:05 +08:00
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('更新端口状态失败: ${deviceId}:${portName} -> ${status}', { error: error.message, stack: error.stack });
|
2026-01-30 17:53:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 辅助函数:将端口状态设为occupied
|
|
|
|
|
async function occupyPort(deviceId, portName) {
|
|
|
|
|
await updatePortStatus(deviceId, portName, 'occupied');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 辅助函数:将端口状态恢复为free
|
|
|
|
|
async function freePort(deviceId, portName) {
|
|
|
|
|
await updatePortStatus(deviceId, portName, 'free');
|
|
|
|
|
}
|
2026-01-23 08:55:16 +08:00
|
|
|
|
|
|
|
|
router.get('/', async (req, res) => {
|
|
|
|
|
try {
|
2026-03-27 19:12:16 +08:00
|
|
|
const {
|
|
|
|
|
sourceDeviceId,
|
|
|
|
|
targetDeviceId,
|
|
|
|
|
status,
|
|
|
|
|
cableType,
|
|
|
|
|
page = 1,
|
|
|
|
|
pageSize = 10,
|
|
|
|
|
} = req.query;
|
2026-01-23 08:55:16 +08:00
|
|
|
const offset = (page - 1) * pageSize;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const where = {};
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (sourceDeviceId) {
|
|
|
|
|
where.sourceDeviceId = sourceDeviceId;
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (targetDeviceId) {
|
|
|
|
|
where.targetDeviceId = targetDeviceId;
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (status && status !== 'all') {
|
|
|
|
|
where.status = status;
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (cableType && cableType !== 'all') {
|
|
|
|
|
where.cableType = cableType;
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const { count, rows } = await Cable.findAndCountAll({
|
|
|
|
|
where,
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
2026-01-23 08:55:16 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
|
|
|
|
},
|
2026-01-23 08:55:16 +08:00
|
|
|
],
|
|
|
|
|
offset,
|
|
|
|
|
limit: parseInt(pageSize),
|
2026-03-27 19:12:16 +08:00
|
|
|
order: [['createdAt', 'DESC']],
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.json({
|
|
|
|
|
total: count,
|
|
|
|
|
cables: rows,
|
|
|
|
|
page: parseInt(page),
|
2026-03-27 19:12:16 +08:00
|
|
|
pageSize: parseInt(pageSize),
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('获取接线列表失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get('/device/:deviceId', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { deviceId } = req.params;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const cables = await Cable.findAll({
|
|
|
|
|
where: {
|
2026-03-27 19:12:16 +08:00
|
|
|
[Op.or]: [{ sourceDeviceId: deviceId }, { targetDeviceId: deviceId }],
|
2026-01-23 08:55:16 +08:00
|
|
|
},
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
2026-01-23 08:55:16 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.json(cables);
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('获取设备接线失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-26 17:21:20 +08:00
|
|
|
// 获取指定机柜内所有设备的接线
|
|
|
|
|
router.get('/rack/:rackId', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { rackId } = req.params;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-26 17:21:20 +08:00
|
|
|
// 1. 找出该机柜下的所有设备ID
|
|
|
|
|
const devices = await Device.findAll({
|
|
|
|
|
where: { rackId: rackId },
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId'],
|
2026-01-26 17:21:20 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-26 17:21:20 +08:00
|
|
|
const deviceIds = devices.map(d => d.deviceId);
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-26 17:21:20 +08:00
|
|
|
if (deviceIds.length === 0) {
|
|
|
|
|
return res.json([]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. 查找这些设备参与的所有接线
|
|
|
|
|
const cables = await Cable.findAll({
|
|
|
|
|
where: {
|
|
|
|
|
[Op.or]: [
|
|
|
|
|
{ sourceDeviceId: { [Op.in]: deviceIds } },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ targetDeviceId: { [Op.in]: deviceIds } },
|
|
|
|
|
],
|
2026-01-26 17:21:20 +08:00
|
|
|
},
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
2026-01-26 17:21:20 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-26 17:21:20 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-26 17:21:20 +08:00
|
|
|
res.json(cables);
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('获取机柜接线失败', { error: error.message, stack: error.stack });
|
2026-01-26 17:21:20 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 检查接线冲突
|
|
|
|
|
router.post('/check-conflict', async (req, res) => {
|
2026-01-23 08:55:16 +08:00
|
|
|
try {
|
2026-01-30 17:53:05 +08:00
|
|
|
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort, excludeCableId } = req.body;
|
|
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (!sourceDeviceId || !sourcePort || !targetDeviceId || !targetPort) {
|
|
|
|
|
return res.status(400).json({ error: '缺少必填字段' });
|
|
|
|
|
}
|
2026-01-30 17:53:05 +08:00
|
|
|
|
|
|
|
|
const conflicts = [];
|
|
|
|
|
|
|
|
|
|
// 检查源端口冲突
|
|
|
|
|
const sourceConflict = await Cable.findOne({
|
2026-01-23 08:55:16 +08:00
|
|
|
where: {
|
|
|
|
|
[Op.or]: [
|
|
|
|
|
{ sourceDeviceId, sourcePort },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ targetDeviceId: sourceDeviceId, targetPort: sourcePort },
|
2026-01-30 17:53:05 +08:00
|
|
|
],
|
2026-03-27 19:12:16 +08:00
|
|
|
...(excludeCableId && { cableId: { [Op.ne]: excludeCableId } }),
|
2026-01-30 17:53:05 +08:00
|
|
|
},
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type'],
|
2026-01-30 17:53:05 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-01-30 17:53:05 +08:00
|
|
|
|
|
|
|
|
if (sourceConflict) {
|
|
|
|
|
conflicts.push({
|
|
|
|
|
type: 'source',
|
|
|
|
|
port: sourcePort,
|
|
|
|
|
deviceId: sourceDeviceId,
|
2026-03-27 19:12:16 +08:00
|
|
|
existingCable: sourceConflict,
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
2026-01-23 08:55:16 +08:00
|
|
|
}
|
2026-01-30 17:53:05 +08:00
|
|
|
|
|
|
|
|
// 检查目标端口冲突
|
|
|
|
|
const targetConflict = await Cable.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
[Op.or]: [
|
|
|
|
|
{ sourceDeviceId: targetDeviceId, sourcePort: targetPort },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ targetDeviceId, targetPort },
|
2026-01-30 17:53:05 +08:00
|
|
|
],
|
2026-03-27 19:12:16 +08:00
|
|
|
...(excludeCableId && { cableId: { [Op.ne]: excludeCableId } }),
|
2026-01-30 17:53:05 +08:00
|
|
|
},
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type'],
|
2026-01-30 17:53:05 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (targetConflict) {
|
|
|
|
|
conflicts.push({
|
|
|
|
|
type: 'target',
|
|
|
|
|
port: targetPort,
|
|
|
|
|
deviceId: targetDeviceId,
|
2026-03-27 19:12:16 +08:00
|
|
|
existingCable: targetConflict,
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
hasConflict: conflicts.length > 0,
|
2026-03-27 19:12:16 +08:00
|
|
|
conflicts,
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('检查接线冲突失败', { error: error.message, stack: error.stack });
|
2026-01-30 17:53:05 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-07 17:05:19 +08:00
|
|
|
// 检查端口兼容性
|
|
|
|
|
router.post('/check-compatibility', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!sourceDeviceId || !sourcePort || !targetDeviceId || !targetPort) {
|
|
|
|
|
return res.status(400).json({ error: '缺少必填字段' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const compatibilityResult = await validatePortCompatibility(sourceDeviceId, sourcePort, targetDeviceId, targetPort);
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
compatible: compatibilityResult.compatible,
|
|
|
|
|
...compatibilityResult,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('检查端口兼容性失败', { error: error.message, stack: error.stack });
|
2026-04-07 17:05:19 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
router.post('/', async (req, res) => {
|
|
|
|
|
try {
|
2026-03-27 19:12:16 +08:00
|
|
|
const {
|
|
|
|
|
cableId,
|
|
|
|
|
sourceDeviceId,
|
|
|
|
|
sourcePort,
|
|
|
|
|
targetDeviceId,
|
|
|
|
|
targetPort,
|
|
|
|
|
cableType,
|
|
|
|
|
cableLength,
|
|
|
|
|
status,
|
|
|
|
|
description,
|
2026-03-31 14:34:34 +08:00
|
|
|
cableLabel,
|
|
|
|
|
cableColor,
|
|
|
|
|
installedBy,
|
|
|
|
|
installedAt,
|
|
|
|
|
lastTestedAt,
|
2026-03-27 19:12:16 +08:00
|
|
|
force,
|
|
|
|
|
} = req.body;
|
2026-01-30 17:53:05 +08:00
|
|
|
|
|
|
|
|
if (!sourceDeviceId || !sourcePort || !targetDeviceId || !targetPort) {
|
|
|
|
|
return res.status(400).json({ error: '缺少必填字段' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sourceDeviceId === targetDeviceId) {
|
|
|
|
|
return res.status(400).json({ error: '源设备和目标设备不能相同' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 17:05:19 +08:00
|
|
|
// 端口兼容性验证
|
|
|
|
|
const compatibilityResult = await validatePortCompatibility(sourceDeviceId, sourcePort, targetDeviceId, targetPort);
|
|
|
|
|
if (!compatibilityResult.compatible) {
|
|
|
|
|
const errorReasons = compatibilityResult.reasons.filter(r => r.severity === 'error');
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error: '端口不兼容',
|
|
|
|
|
incompatibility: true,
|
|
|
|
|
compatibilityResult,
|
|
|
|
|
errors: errorReasons.map(r => r.message),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 07:27:19 +00:00
|
|
|
// 如果不是强制模式,检查冲突(包括反向端口分配)
|
2026-01-30 17:53:05 +08:00
|
|
|
if (!force) {
|
|
|
|
|
const existingCable = await Cable.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
[Op.or]: [
|
|
|
|
|
{ sourceDeviceId, sourcePort },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ targetDeviceId, targetPort },
|
2026-04-01 07:27:19 +00:00
|
|
|
{ sourceDeviceId: targetDeviceId, sourcePort: targetPort },
|
|
|
|
|
{ targetDeviceId: sourceDeviceId, targetPort: sourcePort },
|
2026-03-27 19:12:16 +08:00
|
|
|
],
|
|
|
|
|
},
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingCable) {
|
|
|
|
|
return res.status(409).json({
|
|
|
|
|
error: '端口已被占用',
|
|
|
|
|
conflict: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
existingCable,
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是强制模式,先断开原有连接
|
|
|
|
|
if (force) {
|
|
|
|
|
const existingCables = await Cable.findAll({
|
|
|
|
|
where: {
|
|
|
|
|
[Op.or]: [
|
|
|
|
|
{ sourceDeviceId, sourcePort },
|
|
|
|
|
{ targetDeviceId: sourceDeviceId, targetPort: sourcePort },
|
|
|
|
|
{ sourceDeviceId: targetDeviceId, sourcePort: targetPort },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ targetDeviceId, targetPort },
|
|
|
|
|
],
|
|
|
|
|
},
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const cable of existingCables) {
|
|
|
|
|
await cable.destroy();
|
|
|
|
|
// 释放原有端口
|
|
|
|
|
await freePort(cable.sourceDeviceId, cable.sourcePort);
|
|
|
|
|
await freePort(cable.targetDeviceId, cable.targetPort);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const autoCableId = cableId || `CABLE-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
|
2026-01-30 17:53:05 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const cable = await Cable.create({
|
|
|
|
|
cableId: autoCableId,
|
|
|
|
|
sourceDeviceId,
|
|
|
|
|
sourcePort,
|
|
|
|
|
targetDeviceId,
|
|
|
|
|
targetPort,
|
|
|
|
|
cableType: cableType || 'ethernet',
|
|
|
|
|
cableLength,
|
|
|
|
|
status: status || 'normal',
|
2026-03-27 19:12:16 +08:00
|
|
|
description,
|
2026-03-31 14:34:34 +08:00
|
|
|
cableLabel: cableLabel || null,
|
|
|
|
|
cableColor: cableColor || null,
|
|
|
|
|
installedBy: installedBy || null,
|
|
|
|
|
installedAt: installedAt || null,
|
|
|
|
|
lastTestedAt: lastTestedAt || null,
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-01-30 17:53:05 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const createdCable = await Cable.findByPk(cable.cableId, {
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
2026-01-23 08:55:16 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-01-30 17:53:05 +08:00
|
|
|
|
|
|
|
|
// 自动将源端口和目标端口状态设为occupied
|
|
|
|
|
await occupyPort(sourceDeviceId, sourcePort);
|
|
|
|
|
await occupyPort(targetDeviceId, targetPort);
|
|
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(201).json(createdCable);
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('创建接线失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/batch', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { cables } = req.body;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (!cables || !Array.isArray(cables) || cables.length === 0) {
|
|
|
|
|
return res.status(400).json({ error: '请提供有效的接线数据' });
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const results = {
|
|
|
|
|
total: cables.length,
|
|
|
|
|
success: 0,
|
|
|
|
|
failed: 0,
|
2026-03-27 19:12:16 +08:00
|
|
|
errors: [],
|
2026-01-23 08:55:16 +08:00
|
|
|
};
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
for (let i = 0; i < cables.length; i++) {
|
|
|
|
|
const cableData = cables[i];
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
try {
|
2026-03-27 19:12:16 +08:00
|
|
|
if (
|
|
|
|
|
!cableData.cableId ||
|
|
|
|
|
!cableData.sourceDeviceId ||
|
|
|
|
|
!cableData.sourcePort ||
|
|
|
|
|
!cableData.targetDeviceId ||
|
|
|
|
|
!cableData.targetPort
|
|
|
|
|
) {
|
2026-01-23 08:55:16 +08:00
|
|
|
throw new Error('缺少必填字段');
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (cableData.sourceDeviceId === cableData.targetDeviceId) {
|
|
|
|
|
throw new Error('源设备和目标设备不能相同');
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const existingCable = await Cable.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
[Op.or]: [
|
|
|
|
|
{ sourceDeviceId: cableData.sourceDeviceId, sourcePort: cableData.sourcePort },
|
2026-03-27 19:12:16 +08:00
|
|
|
{ targetDeviceId: cableData.targetDeviceId, targetPort: cableData.targetPort },
|
|
|
|
|
],
|
|
|
|
|
},
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (existingCable) {
|
|
|
|
|
throw new Error('端口已被占用');
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
await Cable.create({
|
|
|
|
|
cableId: cableData.cableId,
|
|
|
|
|
sourceDeviceId: cableData.sourceDeviceId,
|
|
|
|
|
sourcePort: cableData.sourcePort,
|
|
|
|
|
targetDeviceId: cableData.targetDeviceId,
|
|
|
|
|
targetPort: cableData.targetPort,
|
|
|
|
|
cableType: cableData.cableType || 'ethernet',
|
|
|
|
|
cableLength: cableData.cableLength,
|
|
|
|
|
status: cableData.status || 'normal',
|
2026-03-27 19:12:16 +08:00
|
|
|
description: cableData.description,
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 自动将源端口和目标端口状态设为occupied
|
|
|
|
|
await occupyPort(cableData.sourceDeviceId, cableData.sourcePort);
|
|
|
|
|
await occupyPort(cableData.targetDeviceId, cableData.targetPort);
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
results.success++;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
results.failed++;
|
|
|
|
|
results.errors.push({
|
|
|
|
|
index: i + 1,
|
|
|
|
|
cableId: cableData.cableId,
|
2026-03-27 19:12:16 +08:00
|
|
|
error: error.message,
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.json(results);
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('批量创建接线失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.put('/:cableId', async (req, res) => {
|
|
|
|
|
try {
|
2026-01-30 17:53:05 +08:00
|
|
|
// 获取更新前的接线信息
|
|
|
|
|
const oldCable = await Cable.findByPk(req.params.cableId);
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
if (!oldCable) {
|
|
|
|
|
return res.status(404).json({ error: '接线不存在' });
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
sourceDeviceId: oldSourceDeviceId,
|
|
|
|
|
sourcePort: oldSourcePort,
|
|
|
|
|
targetDeviceId: oldTargetDeviceId,
|
|
|
|
|
targetPort: oldTargetPort,
|
|
|
|
|
} = oldCable;
|
|
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const [updated] = await Cable.update(req.body, {
|
2026-03-27 19:12:16 +08:00
|
|
|
where: { cableId: req.params.cableId },
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (updated) {
|
|
|
|
|
const cable = await Cable.findByPk(req.params.cableId, {
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
2026-01-23 08:55:16 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = cable;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 同步更新端口状态
|
|
|
|
|
// 源端口变更:释放旧端口,占用新端口
|
|
|
|
|
if (oldSourceDeviceId !== sourceDeviceId || oldSourcePort !== sourcePort) {
|
|
|
|
|
await freePort(oldSourceDeviceId, oldSourcePort);
|
|
|
|
|
await occupyPort(sourceDeviceId, sourcePort);
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 目标端口变更:释放旧端口,占用新端口
|
|
|
|
|
if (oldTargetDeviceId !== targetDeviceId || oldTargetPort !== targetPort) {
|
|
|
|
|
await freePort(oldTargetDeviceId, oldTargetPort);
|
|
|
|
|
await occupyPort(targetDeviceId, targetPort);
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.json(cable);
|
|
|
|
|
} else {
|
|
|
|
|
res.status(404).json({ error: '接线不存在' });
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('更新接线失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-01 10:14:46 +08:00
|
|
|
// 批量删除接线
|
2026-01-23 08:55:16 +08:00
|
|
|
router.delete('/batch', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { cableIds } = req.body;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (!cableIds || !Array.isArray(cableIds) || cableIds.length === 0) {
|
|
|
|
|
return res.status(400).json({ error: '请提供有效的接线ID列表' });
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 先获取所有要删除的接线信息,用于后续恢复端口状态
|
|
|
|
|
const cables = await Cable.findAll({
|
2026-03-27 19:12:16 +08:00
|
|
|
where: { cableId: { [Op.in]: cableIds } },
|
2026-01-30 17:53:05 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
const deletedCount = await Cable.destroy({
|
2026-03-27 19:12:16 +08:00
|
|
|
where: { cableId: { [Op.in]: cableIds } },
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-30 17:53:05 +08:00
|
|
|
// 自动将所有相关端口状态恢复为free
|
|
|
|
|
for (const cable of cables) {
|
|
|
|
|
await freePort(cable.sourceDeviceId, cable.sourcePort);
|
|
|
|
|
await freePort(cable.targetDeviceId, cable.targetPort);
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.json({
|
|
|
|
|
message: `批量删除成功,已删除 ${deletedCount} 条接线`,
|
2026-03-27 19:12:16 +08:00
|
|
|
deletedCount,
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('批量删除接线失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-01 10:14:46 +08:00
|
|
|
// 删除单个接线
|
|
|
|
|
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: '接线不存在' });
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('删除接线失败', { error: error.message, stack: error.stack });
|
2026-04-01 10:14:46 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
router.get('/:cableId', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const cable = await Cable.findByPk(req.params.cableId, {
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'sourceDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
2026-01-23 08:55:16 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: Device,
|
|
|
|
|
as: 'targetDevice',
|
2026-03-27 19:12:16 +08:00
|
|
|
attributes: ['deviceId', 'name', 'type', 'rackId'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-01-23 08:55:16 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
if (!cable) {
|
|
|
|
|
return res.status(404).json({ error: '接线不存在' });
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-01-23 08:55:16 +08:00
|
|
|
res.json(cable);
|
|
|
|
|
} catch (error) {
|
2026-04-28 10:57:17 +08:00
|
|
|
logger.error('获取接线详情失败', { error: error.message, stack: error.stack });
|
2026-01-23 08:55:16 +08:00
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|