class SimulationMonitor extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.monitorId = `simulation_monitor_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; this.monitorStatus = { isMonitoring: false, lastError: null }; this.systemInfo = null; this.threadInfo = null; this.statusCheckInterval = null; this.chart = null; this.chartInitialized = false; this.domainId = '10'; // 默认值 this.isActive = false; this.initializing = false; } connectedCallback() { this.render(); this.isActive = true; // 设置初始状态为激活 // 等待组件完全加载后初始化 setTimeout(() => { this.initializeComponent(); // 初始化完成后再启动定时器,给服务器一些准备时间 setTimeout(() => { this.startStatusCheck(); }, 1000); // 延迟1秒启动定时器 }, 100); } // 重新激活组件 reactivate() { if (this.isActive) return; // 如果已经激活,直接返回 this.isActive = true; // 强制重新初始化 this.chartInitialized = false; this.chart = null; this.initializeComponent(); } // 修改 startStatusCheck 方法 startStatusCheck() { if (!this.isActive) return; // 清除可能存在的旧定时器 this.stopStatusCheck(); // 设置定时器,每秒执行一次 this.statusCheckInterval = setInterval(() => { if (this.isActive) { this.checkMonitorStatus(); } else { // 如果组件不再激活,清除定时器 this.stopStatusCheck(); } }, 1000); } // 修改 checkMonitorStatus 方法 async checkMonitorStatus() { try { // 获取监控状态 const statusResponse = await fetch('/api/system-monitor/status'); const statusData = await statusResponse.json(); this.monitorStatus = statusData; // 只有在监控状态为true时才获取详细信息 if (this.monitorStatus.isMonitoring) { // 使用 Promise.all 并行获取系统信息和线程信息 const [systemResponse, threadResponse] = await Promise.all([ fetch('/api/system-monitor/system-info'), fetch('/api/system-monitor/thread-info') ]); if (systemResponse.ok) { const systemData = await systemResponse.json(); this.systemInfo = systemData.data; } if (threadResponse.ok) { const threadData = await threadResponse.json(); this.threadInfo = threadData.data; // 确保图表存在并更新 if (!this.chart) { this.initChart(); } } // 只在成功获取数据后更新UI this.updateUI(); } else { // 如果监控已停止,尝试启动监控 this.startMonitoring(); } } catch (error) { console.error('获取监控状态失败:', error); this.monitorStatus.lastError = error.message; this.updateUI(); } } // 修改 stopStatusCheck 方法 stopStatusCheck() { if (this.statusCheckInterval) { clearInterval(this.statusCheckInterval); this.statusCheckInterval = null; } } // 修改 startMonitoring 方法 async startMonitoring() { const savedSelection = localStorage.getItem('xnsim-selection'); const selection = savedSelection ? JSON.parse(savedSelection) : {}; const confID = selection.configurationId; // 如果已经在监控中,直接返回 if (this.monitorStatus.isMonitoring) { console.log('监控已经在运行中'); return; } try { // 首先检查DDS监控状态 const ddsStatusResponse = await fetch('/api/dds-monitor/status'); const ddsStatusData = await ddsStatusResponse.json(); // 如果DDS监控未初始化,直接返回,等待下次定时器运行时再检查 if (!ddsStatusData.isInitialized) { //console.log('DDS监控未初始化,等待下次检查'); return; } // 启动系统监控 const response = await fetch('/api/system-monitor/start', { method: 'POST' }); const data = await response.json(); if (response.ok) { this.monitorStatus = data.status; this.updateUI(); // 只有在没有运行中的状态检查时才启动新的检查 if (!this.statusCheckInterval) { this.startStatusCheck(); } } else { console.error('启动监控失败:', data.error); } } catch (error) { console.error('启动监控失败:', error); } } disconnectedCallback() { this.isActive = false; this.stopStatusCheck(); // 自动停止监控 this.stopMonitoring(); // 注意:不再在这里注销 DDS 监控,因为其他组件可能正在使用它 } async initializeComponent() { if (this.initializing) return; // 防止重复初始化 this.initializing = true; try { // 确保图表被正确初始化 if (this.chart) { this.chart.destroy(); this.chart = null; } // 初始化图表 this.initChart(); // 检查当前监控状态 const statusResponse = await fetch('/api/system-monitor/status'); const statusData = await statusResponse.json(); this.monitorStatus = statusData; // 如果监控未运行,尝试启动监控 if (!this.monitorStatus.isMonitoring) { this.startMonitoring(); } this.chartInitialized = true; } catch (error) { console.error('初始化组件失败:', error); this.monitorStatus.lastError = error.message; this.updateUI(); } finally { this.initializing = false; } } getStatusDisplay(status) { switch (status) { case 0: return { text: '未运行', color: '#999999' }; case 1: return { text: '运行中', color: '#4CAF50' }; case 2: return { text: '暂停中', color: '#FF9800' }; case 3: return { text: '错误', color: '#f44336' }; default: return { text: '未知状态', color: '#f44336' }; } } getCoreStatusDisplay(status) { switch (status) { case 0: return { text: '未加载', color: '#999999' }; case 1: return { text: '初始化完成', color: '#FFC107' }; case 2: return { text: '正常', color: '#4CAF50' }; case 3: return { text: '异常', color: '#f44336' }; default: return { text: '未知状态', color: '#f44336' }; } } getThreadStatusDisplay(status) { switch (status) { case 0: return { text: '未运行', color: '#999999' }; case 1: return { text: '运行中', color: '#4CAF50' }; case 2: return { text: '暂停中', color: '#FF9800' }; case 3: return { text: '错误', color: '#f44336' }; default: return { text: '未知状态', color: '#f44336' }; } } updateUI() { const statusIndicator = this.shadowRoot.querySelector('#statusIndicator'); const statusText = this.shadowRoot.querySelector('#statusText'); const engineInfo = this.shadowRoot.querySelector('#engine-info'); const coreStatus = this.shadowRoot.querySelector('#core-status'); const threadTableBody = this.shadowRoot.querySelector('#thread-table-body'); // 更新状态显示 if (statusIndicator && statusText) { if (this.monitorStatus.isMonitoring) { statusIndicator.classList.add('active'); statusIndicator.classList.remove('error'); statusText.textContent = '监控中'; } else if (this.monitorStatus.lastError) { statusIndicator.classList.remove('active'); statusIndicator.classList.add('error'); statusText.textContent = `监控错误: ${this.monitorStatus.lastError}`; } else { statusIndicator.classList.remove('active', 'error'); statusText.textContent = '未监控'; } } // 更新引擎信息 const engineInfoItems = engineInfo.querySelectorAll('.status-item'); engineInfoItems.forEach(item => { const label = item.querySelector('.status-label').textContent; const valueElement = item.querySelector('.status-value'); switch(label) { case '引擎名称': valueElement.textContent = this.systemInfo?.engineInfo?.name || 'XNSim'; valueElement.style.color = '#666'; break; case '引擎ID': valueElement.textContent = this.systemInfo?.engineInfo?.id || '未知'; valueElement.style.color = '#666'; break; case '引擎状态': const statusInfo = this.getStatusDisplay(this.systemInfo?.engineInfo?.status || 0); valueElement.textContent = statusInfo.text; valueElement.style.color = statusInfo.color; break; case '引擎亲和性': valueElement.textContent = this.systemInfo?.engineInfo?.affinity || '未知'; valueElement.style.color = '#666'; break; case '线程数': valueElement.textContent = this.systemInfo?.engineInfo?.threadCount || '未知'; valueElement.style.color = '#666'; break; } }); // 更新核心状态 const coreStatusItems = coreStatus.querySelectorAll('.status-item'); coreStatusItems.forEach(item => { const label = item.querySelector('.status-label').textContent; const valueElement = item.querySelector('.status-value'); const key = this.getCoreStatusKey(label); if (key) { const status = this.systemInfo?.coreStatus?.[key] || 0; const statusInfo = this.getCoreStatusDisplay(status); valueElement.textContent = statusInfo.text; valueElement.style.color = statusInfo.color; } }); // 更新线程表格 if (this.threadInfo && Array.isArray(this.threadInfo)) { threadTableBody.innerHTML = this.threadInfo.map(thread => { const statusInfo = this.getThreadStatusDisplay(thread.status); return `
线程名称 | 线程ID | 状态 | 优先级 | 运行次数 | 设定频率(Hz) | 平均频率(Hz) | 最大频率(Hz) | 最小频率(Hz) | 设定周期(ms) | 平均周期(ms) | 最大周期(ms) | 最小周期(ms) |
---|