2025-05-14 14:30:38 +08:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2025-05-15 09:15:01 +08:00
|
|
|
const { initializeMonitor, cleanupMonitor } = require('../utils/systemMonitor');
|
2025-05-14 14:30:38 +08:00
|
|
|
|
|
|
|
// 存储监控服务的状态
|
|
|
|
let monitorStatus = {
|
|
|
|
isInitialized: false,
|
|
|
|
domainId: null,
|
2025-05-15 10:56:49 +08:00
|
|
|
lastError: null,
|
|
|
|
activeMonitors: new Set() // 添加活跃监控器集合
|
2025-05-14 14:30:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 初始化监控服务
|
|
|
|
router.post('/initialize', async (req, res) => {
|
|
|
|
try {
|
2025-05-15 10:56:49 +08:00
|
|
|
const { domainId, monitorId } = req.body;
|
2025-05-14 14:30:38 +08:00
|
|
|
|
2025-05-15 10:56:49 +08:00
|
|
|
if (!domainId || !monitorId) {
|
|
|
|
return res.status(400).json({ error: '缺少必要的参数' });
|
2025-05-14 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2025-05-15 10:56:49 +08:00
|
|
|
// 如果已经初始化,检查是否是新的监控器
|
2025-05-14 14:30:38 +08:00
|
|
|
if (monitorStatus.isInitialized) {
|
2025-05-15 10:56:49 +08:00
|
|
|
if (monitorStatus.domainId !== domainId) {
|
|
|
|
return res.status(400).json({ error: 'DDS域ID不匹配' });
|
|
|
|
}
|
|
|
|
monitorStatus.activeMonitors.add(monitorId);
|
|
|
|
return res.json({
|
|
|
|
message: '监控器已注册',
|
|
|
|
status: monitorStatus
|
|
|
|
});
|
2025-05-14 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2025-05-15 10:56:49 +08:00
|
|
|
// 首次初始化
|
2025-05-14 14:30:38 +08:00
|
|
|
const result = initializeMonitor(domainId);
|
|
|
|
if (result && result.includes('失败')) {
|
|
|
|
monitorStatus.lastError = result;
|
|
|
|
return res.status(500).json({ error: result });
|
|
|
|
}
|
|
|
|
|
|
|
|
monitorStatus.isInitialized = true;
|
|
|
|
monitorStatus.domainId = domainId;
|
|
|
|
monitorStatus.lastError = null;
|
2025-05-15 10:56:49 +08:00
|
|
|
monitorStatus.activeMonitors.add(monitorId);
|
2025-05-14 14:30:38 +08:00
|
|
|
|
|
|
|
res.json({
|
|
|
|
message: '监控服务初始化成功',
|
|
|
|
status: monitorStatus
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('初始化监控服务失败:', error);
|
|
|
|
monitorStatus.lastError = error.message;
|
|
|
|
res.status(500).json({ error: '初始化监控服务失败', message: error.message });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-05-15 10:56:49 +08:00
|
|
|
// 注销监控器
|
|
|
|
router.post('/unregister', async (req, res) => {
|
2025-05-14 14:30:38 +08:00
|
|
|
try {
|
2025-05-15 10:56:49 +08:00
|
|
|
const { monitorId } = req.body;
|
|
|
|
|
|
|
|
if (!monitorId) {
|
|
|
|
return res.status(400).json({ error: '缺少必要的monitorId参数' });
|
2025-05-14 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2025-05-15 10:56:49 +08:00
|
|
|
monitorStatus.activeMonitors.delete(monitorId);
|
|
|
|
|
|
|
|
// 如果没有活跃的监控器了,清理资源
|
|
|
|
if (monitorStatus.activeMonitors.size === 0) {
|
|
|
|
cleanupMonitor();
|
|
|
|
monitorStatus = {
|
|
|
|
isInitialized: false,
|
|
|
|
domainId: null,
|
|
|
|
lastError: null,
|
|
|
|
activeMonitors: new Set()
|
|
|
|
};
|
|
|
|
}
|
2025-05-14 14:30:38 +08:00
|
|
|
|
|
|
|
res.json({
|
2025-05-15 10:56:49 +08:00
|
|
|
message: '监控器注销成功',
|
2025-05-14 14:30:38 +08:00
|
|
|
status: monitorStatus
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2025-05-15 10:56:49 +08:00
|
|
|
console.error('注销监控器失败:', error);
|
|
|
|
res.status(500).json({ error: '注销监控器失败', message: error.message });
|
2025-05-14 14:30:38 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 获取监控服务状态
|
|
|
|
router.get('/status', (req, res) => {
|
|
|
|
res.json(monitorStatus);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|