console.log('main.js 文件开始执行'); let lightHistory = { common: false, alarm: false, up: false, down: false, emergency_stop: false }; let allTestsPassed = false; let currentDeviceType = '5'; // 默认为五孔设备 function updateLights() { fetch('/get_lights_status') .then(response => response.json()) .then(data => { for (let light in data) { let element5 = document.getElementById(light + '-5'); let element4 = document.getElementById(light + '-4'); if (element5) { updateLightStatus(element5, data[light]); } if (element4) { updateLightStatus(element4, data[light]); } // 更新灯光历史和检查列表 if (data[light] && !lightHistory[light]) { lightHistory[light] = true; updateCheckList(light); } } checkAllTestsPassed(); }); } function updateLightStatus(element, isOn) { if (isOn) { element.classList.add('on'); element.classList.remove('off'); } else { element.classList.add('off'); element.classList.remove('on'); } } function updateCheckList(light) { const listItem = document.querySelector(`#statusCheckList${currentDeviceType} li[data-light="${light}"]`); if (listItem) { const checkMark = listItem.querySelector('.badge'); checkMark.style.display = 'inline-block'; } } function checkAllTestsPassed() { allTestsPassed = Array.from(document.querySelectorAll(`#statusCheckList${currentDeviceType} .badge`)) .every(badge => badge.style.display === 'inline-block'); // 更新测试结果 updateTestResult(); } function resetCheckList() { lightHistory = { common: false, alarm: false, up: false, down: false, emergency_stop: false }; allTestsPassed = false; const deviceType = document.getElementById('deviceType').value; const checkList = document.getElementById(`statusCheckList${deviceType}`); if (checkList) { const badges = checkList.querySelectorAll('.badge'); badges.forEach(badge => { badge.style.display = 'none'; }); } updateTestResult(); } function updateTestResult() { const rows = document.querySelectorAll('#resultTable tbody tr'); const lastRow = rows[rows.length - 1]; // 只更新最后一行的状态 updateRowTestResult(lastRow); } function updateRowTestResult(row) { const checkbox = row.querySelector('.form-check-input'); const label = row.querySelector('.form-check-label'); const pendingSpan = label.querySelector('.pending'); const passSpan = label.querySelector('.pass'); const failSpan = label.querySelector('.fail'); if (allTestsPassed) { checkbox.disabled = false; checkbox.checked = true; pendingSpan.style.display = 'none'; passSpan.style.display = 'inline'; failSpan.style.display = 'none'; row.classList.remove('pending-row'); row.classList.add('pass-row'); row.classList.remove('fail-row'); } else { checkbox.disabled = true; checkbox.checked = false; pendingSpan.style.display = 'inline'; passSpan.style.display = 'none'; failSpan.style.display = 'none'; row.classList.add('pending-row'); row.classList.remove('pass-row'); row.classList.remove('fail-row'); } } function addNewRow(button) { const table = document.getElementById('resultTable').getElementsByTagName('tbody')[0]; const currentRow = button.closest('tr'); const serialNumberInput = currentRow.querySelector('.serial-number'); if (serialNumberInput.value.trim() === '') { alert('请输入设备序列号'); return; } // 固定当前行状态 finalizeRowStatus(currentRow); // 创建新行 const newRow = table.insertRow(); const rowIndex = table.rows.length - 1; const cell1 = newRow.insertCell(0); const cell2 = newRow.insertCell(1); const cell3 = newRow.insertCell(2); cell1.innerHTML = ''; cell2.innerHTML = `
`; cell3.innerHTML = ''; // 重置表单和状态 resetForm(); // 聚焦到新的设备序列号输入框 const newSerialInput = newRow.querySelector('.serial-number'); newSerialInput.focus(); // 清空app.py的信号数据 clearSignalData(); } function finalizeRowStatus(row) { const serialNumberInput = row.querySelector('.serial-number'); const addButton = row.querySelector('.add-row-btn'); const checkbox = row.querySelector('.form-check-input'); serialNumberInput.disabled = true; addButton.style.display = 'none'; checkbox.disabled = true; } function resetForm() { // 重置测试结果为待测试状态 resetTestResult(); // 重置检测状态记录 resetDetectionStatus(); // 重置灯光状态 resetLightStatus(); } function resetTestResult() { allTestsPassed = false; updateTestResult(); } function resetDetectionStatus() { lightHistory = { common: false, alarm: false, up: false, down: false, emergency_stop: false }; const deviceType = document.getElementById('deviceType').value; const checkList = document.getElementById(`statusCheckList${deviceType}`); if (checkList) { const badges = checkList.querySelectorAll('.badge'); badges.forEach(badge => { badge.style.display = 'none'; }); } } function resetLightStatus() { const lights = document.querySelectorAll('.light'); lights.forEach(light => { light.classList.remove('on'); light.classList.add('off'); }); } function clearSignalData() { fetch('/clear_signal_data', { method: 'POST', }).then(response => { if (!response.ok) { console.error('清空信号数据失败'); } }); } function exportToExcel() { const table = document.getElementById('resultTable'); const deviceType = document.getElementById('deviceType').value; const tester = document.getElementById('tester').value; const orderNumber = document.getElementById('orderNumber').value; const testDate = document.getElementById('testDate').value; const data = []; // 添加测试信息 data.push(['设备型号', deviceType === '5' ? '五孔' : '四孔']); data.push(['检测人员', tester]); data.push(['订单编号', orderNumber]); data.push(['测试日期', testDate]); data.push([]); // 空行 const headers = ['设备序列号', '测试结果']; data.push(headers); for (let i = 1; i < table.rows.length; i++) { const row = table.rows[i]; const serialNumber = row.cells[0].querySelector('input').value; const testResult = row.cells[1].querySelector('input').checked ? '合格' : '不合格'; data.push([serialNumber, testResult]); } const wb = XLSX.utils.book_new(); const ws = XLSX.utils.aoa_to_sheet(data); const colWidth = [{ wch: 20 }, { wch: 20 }]; ws['!cols'] = colWidth; XLSX.utils.book_append_sheet(wb, ws, "测试结果"); XLSX.writeFile(wb, `测试结果_${orderNumber}_${testDate}.xlsx`); } function startTest() { disableInfoInputs(); const deviceTypeSelect = document.getElementById('deviceType'); if (deviceTypeSelect) { deviceTypeSelect.disabled = false; } const firstSerialInput = document.getElementById('firstSerialNumber'); if (firstSerialInput) { firstSerialInput.focus(); firstSerialInput.scrollIntoView({ behavior: 'smooth', block: 'center' }); } } function disableInfoInputs() { const infoInputs = document.querySelectorAll('.info-input:not(#deviceType)'); infoInputs.forEach(input => { input.disabled = true; }); const startTestBtn = document.getElementById('startTestBtn'); if (startTestBtn) { startTestBtn.disabled = true; } } function setupDeviceTypeListener() { const deviceTypeSelect = document.getElementById('deviceType'); if (deviceTypeSelect) { deviceTypeSelect.addEventListener('change', function() { currentDeviceType = this.value; updateDeviceTypeDisplay(); }); } } function updateDeviceTypeDisplay() { const deviceType = document.getElementById('deviceType').value; const fiveHoleLights = document.getElementById('fiveHoleLights'); const fourHoleLights = document.getElementById('fourHoleLights'); const statusCheckList5 = document.getElementById('statusCheckList5'); const statusCheckList4 = document.getElementById('statusCheckList4'); if (deviceType === '5') { fiveHoleLights.style.display = 'flex'; fourHoleLights.style.display = 'none'; statusCheckList5.style.display = 'block'; statusCheckList4.style.display = 'none'; } else if (deviceType === '4') { fiveHoleLights.style.display = 'none'; fourHoleLights.style.display = 'flex'; statusCheckList5.style.display = 'none'; statusCheckList4.style.display = 'block'; } else { fiveHoleLights.style.display = 'none'; fourHoleLights.style.display = 'none'; statusCheckList5.style.display = 'none'; statusCheckList4.style.display = 'none'; } resetCheckList(); } function setupPortSelection() { const selectPortBtn = document.getElementById('selectPortBtn'); const confirmPortBtn = document.getElementById('confirmPortBtn'); const portSelect = document.getElementById('portSelect'); selectPortBtn.addEventListener('click', async () => { try { const response = await fetch('/get_ports'); const ports = await response.json(); portSelect.innerHTML = ''; if (ports.length === 0) { const option = document.createElement('option'); option.textContent = '没有找到可用的串口'; portSelect.appendChild(option); confirmPortBtn.disabled = true; } else { ports.forEach(port => { const option = document.createElement('option'); option.value = port.device; option.textContent = `${port.device} - ${port.description}`; portSelect.appendChild(option); }); confirmPortBtn.disabled = false; } const portModal = new bootstrap.Modal(document.getElementById('portModal')); portModal.show(); } catch (error) { console.error('获取串口列表失败:', error); alert('获取串口列表失败,请重试。'); } }); confirmPortBtn.addEventListener('click', async () => { const selectedPort = portSelect.value; if (selectedPort) { try { const response = await fetch('/set_port', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ port: selectedPort }), }); const result = await response.json(); if (result.success) { alert('串口设置成功'); bootstrap.Modal.getInstance(document.getElementById('portModal')).hide(); } else { alert('串口设置失败: ' + result.message); } } catch (error) { console.error('设置串口失败:', error); alert('设置串口失败,请重试。'); } } else { alert('请选择一个串口'); } }); } document.addEventListener('DOMContentLoaded', function() { setupDeviceTypeListener(); setupPortSelection(); const startTestBtn = document.getElementById('startTestBtn'); if (startTestBtn) { startTestBtn.addEventListener('click', function(event) { event.preventDefault(); startTest(); }); } // 初始化设备类型显示 updateDeviceTypeDisplay(); // 每100毫秒更新一次灯光状态 setInterval(updateLights, 100); });