let sensorChart = null; let refreshInterval = null; let allSensors = []; let currentSensorData = []; // 页面加载完成后执行 document.addEventListener('DOMContentLoaded', function() { // 初始化日期选择器为今天 initializeDatePickers(); // 加载所有传感器ID loadSensors(); // 添加事件监听器 setupEventListeners(); // 设置自动刷新 setupAutoRefresh(); }); // 初始化日期选择器 function initializeDatePickers() { const now = new Date(); const today = now.toISOString().split('T')[0]; const time = now.toTimeString().split(' ')[0].substring(0, 5); // 设置默认的开始时间为当天00:00 document.getElementById('startDate').value = `${today}T00:00`; // 设置默认的结束时间为当前时间 document.getElementById('endDate').value = `${today}T${time}`; } // 设置事件监听器 function setupEventListeners() { // 查询按钮 document.getElementById('queryBtn').addEventListener('click', function() { loadData(); }); // 重置按钮 document.getElementById('resetBtn').addEventListener('click', function() { resetFilters(); }); // 传感器选择变化 document.getElementById('sensorSelect').addEventListener('change', function() { loadData(); }); // 记录数限制变化 document.getElementById('limitSelect').addEventListener('change', function() { loadData(); }); // 导出CSV按钮 document.getElementById('exportBtn').addEventListener('click', function() { exportToCSV(); }); } // 设置自动刷新 function setupAutoRefresh() { const autoRefreshCheckbox = document.getElementById('autoRefresh'); // 初始化自动刷新 if (autoRefreshCheckbox.checked) { startAutoRefresh(); } // 监听复选框变化 autoRefreshCheckbox.addEventListener('change', function() { if (this.checked) { startAutoRefresh(); } else { stopAutoRefresh(); } }); } // 开始自动刷新 function startAutoRefresh() { if (refreshInterval) { clearInterval(refreshInterval); } refreshInterval = setInterval(loadData, 10000); // 10秒刷新一次 } // 停止自动刷新 function stopAutoRefresh() { if (refreshInterval) { clearInterval(refreshInterval); refreshInterval = null; } } // 重置筛选条件 function resetFilters() { initializeDatePickers(); document.getElementById('sensorSelect').value = 'all'; document.getElementById('limitSelect').value = '100'; loadData(); } // 加载所有传感器ID function loadSensors() { fetch('/api/sensors') .then(response => { if (!response.ok) { throw new Error('获取传感器列表失败'); } return response.json(); }) .then(data => { allSensors = data; updateSensorSelect(data); // 加载数据 loadData(); }) .catch(error => { console.error('加载传感器列表出错:', error); alert('加载传感器列表出错: ' + error.message); }); } // 更新传感器选择下拉框 function updateSensorSelect(sensors) { const select = document.getElementById('sensorSelect'); // 保留"所有传感器"选项 const allOption = select.querySelector('option[value="all"]'); select.innerHTML = ''; select.appendChild(allOption); if (sensors.length === 0) { const option = document.createElement('option'); option.value = ''; option.textContent = '没有可用的传感器'; select.appendChild(option); return; } sensors.forEach(id => { const option = document.createElement('option'); option.value = id; option.textContent = `传感器 ${id}`; select.appendChild(option); }); } // 加载传感器数据 function loadData() { const sensorID = document.getElementById('sensorSelect').value; const limit = document.getElementById('limitSelect').value; const startDate = document.getElementById('startDate').value; const endDate = document.getElementById('endDate').value; let url = '/api/data?'; let params = []; // 添加查询参数 if (sensorID !== 'all') { params.push(`sensor_id=${sensorID}`); } if (limit) { params.push(`limit=${limit}`); } if (startDate) { params.push(`start_date=${encodeURIComponent(startDate)}`); } if (endDate) { params.push(`end_date=${encodeURIComponent(endDate)}`); } url += params.join('&'); // 显示加载状态 document.getElementById('queryBtn').textContent = '加载中...'; fetch(url) .then(response => { if (!response.ok) { throw new Error('获取传感器数据失败'); } return response.json(); }) .then(data => { currentSensorData = data; updateTable(data); updateChart(data); document.getElementById('queryBtn').textContent = '查询数据'; }) .catch(error => { console.error('加载数据出错:', error); alert('加载数据出错: ' + error.message); document.getElementById('queryBtn').textContent = '查询数据'; }); } // 更新数据表格 function updateTable(data) { const tableBody = document.getElementById('tableBody'); tableBody.innerHTML = ''; if (data.length === 0) { const row = document.createElement('tr'); row.innerHTML = '