2025-05-14 14:30:38 +08:00
|
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
2025-06-11 10:05:26 +08:00
|
|
|
|
const { initializeMonitor, cleanupMonitor } = require('../utils/xnCoreService');
|
2025-05-14 14:30:38 +08:00
|
|
|
|
|
|
|
|
|
// 存储监控服务的状态
|
|
|
|
|
let monitorStatus = {
|
|
|
|
|
isInitialized: false,
|
|
|
|
|
domainId: null,
|
2025-06-24 16:07:07 +08:00
|
|
|
|
confID: null,
|
|
|
|
|
forceGen: 0,
|
2025-06-09 11:01:30 +08:00
|
|
|
|
lastError: null
|
2025-05-14 14:30:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 初始化监控服务
|
|
|
|
|
router.post('/initialize', async (req, res) => {
|
|
|
|
|
try {
|
2025-06-24 16:07:07 +08:00
|
|
|
|
const { domainId, confID, forceGen = 0 } = req.body;
|
2025-05-14 14:30:38 +08:00
|
|
|
|
|
2025-06-24 16:07:07 +08:00
|
|
|
|
if (!domainId || confID === undefined) {
|
|
|
|
|
return res.status(400).json({ error: '缺少必要的参数: domainId 和 confID' });
|
2025-05-14 14:30:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 16:07:07 +08:00
|
|
|
|
// 如果已经初始化,检查域ID和配置ID是否匹配
|
2025-05-14 14:30:38 +08:00
|
|
|
|
if (monitorStatus.isInitialized) {
|
2025-06-24 16:07:07 +08:00
|
|
|
|
if (monitorStatus.domainId !== domainId || monitorStatus.confID !== confID) {
|
|
|
|
|
return res.status(400).json({ error: 'DDS域ID或配置ID不匹配' });
|
2025-05-15 10:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
return res.json({
|
2025-06-09 11:01:30 +08:00
|
|
|
|
message: '监控服务已初始化',
|
2025-05-15 10:56:49 +08:00
|
|
|
|
status: monitorStatus
|
|
|
|
|
});
|
2025-05-14 14:30:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 10:56:49 +08:00
|
|
|
|
// 首次初始化
|
2025-06-24 16:07:07 +08:00
|
|
|
|
const result = initializeMonitor(domainId, confID, forceGen);
|
2025-05-14 14:30:38 +08:00
|
|
|
|
if (result && result.includes('失败')) {
|
|
|
|
|
monitorStatus.lastError = result;
|
|
|
|
|
return res.status(500).json({ error: result });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
monitorStatus.isInitialized = true;
|
|
|
|
|
monitorStatus.domainId = domainId;
|
2025-06-24 16:07:07 +08:00
|
|
|
|
monitorStatus.confID = confID;
|
|
|
|
|
monitorStatus.forceGen = forceGen;
|
2025-05-14 14:30:38 +08:00
|
|
|
|
monitorStatus.lastError = null;
|
|
|
|
|
|
|
|
|
|
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-06-09 11:01:30 +08:00
|
|
|
|
// 清理资源
|
|
|
|
|
await cleanupMonitor();
|
|
|
|
|
monitorStatus = {
|
|
|
|
|
isInitialized: false,
|
|
|
|
|
domainId: null,
|
2025-06-24 16:07:07 +08:00
|
|
|
|
confID: null,
|
|
|
|
|
forceGen: 0,
|
2025-06-09 11:01:30 +08:00
|
|
|
|
lastError: null
|
|
|
|
|
};
|
2025-05-14 14:30:38 +08:00
|
|
|
|
|
|
|
|
|
res.json({
|
2025-06-09 11:01:30 +08:00
|
|
|
|
message: '监控服务注销成功',
|
2025-05-14 14:30:38 +08:00
|
|
|
|
status: monitorStatus
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2025-06-09 11:01:30 +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;
|