358 lines
12 KiB
JavaScript
358 lines
12 KiB
JavaScript
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');
|
|
rows.forEach(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;
|
|
}
|
|
|
|
serialNumberInput.disabled = true;
|
|
button.style.display = 'none';
|
|
|
|
const newRow = table.insertRow(table.rows.length);
|
|
const rowIndex = table.rows.length - 1;
|
|
|
|
const cell1 = newRow.insertCell(0);
|
|
const cell2 = newRow.insertCell(1);
|
|
const cell3 = newRow.insertCell(2);
|
|
|
|
cell1.innerHTML = '<input type="text" class="form-control serial-number" placeholder="输入设备序列号">';
|
|
cell2.innerHTML = `
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" id="result-${rowIndex}" ${allTestsPassed ? 'checked' : 'disabled'}>
|
|
<label class="form-check-label" for="result-${rowIndex}">
|
|
<span class="pending" ${allTestsPassed ? 'style="display: none;"' : ''}>待测试</span>
|
|
<span class="pass" ${allTestsPassed ? '' : 'style="display: none;"'}>合格</span>
|
|
<span class="fail" style="display: none;">不合格</span>
|
|
</label>
|
|
</div>`;
|
|
cell3.innerHTML = '<button class="btn btn-primary add-row-btn" onclick="addNewRow(this)">增加下一条</button>';
|
|
|
|
updateTestResult();
|
|
}
|
|
|
|
function updateResultStyle(row) {
|
|
const checkbox = row.cells[1].querySelector('.form-check-input');
|
|
const serialNumberInput = row.cells[0].querySelector('.serial-number');
|
|
const label = checkbox.nextElementSibling;
|
|
const pendingSpan = label.querySelector('.pending');
|
|
const passSpan = label.querySelector('.pass');
|
|
const failSpan = label.querySelector('.fail');
|
|
|
|
checkbox.addEventListener('change', function() {
|
|
if (this.checked) {
|
|
row.classList.remove('fail-row');
|
|
row.classList.add('pass-row');
|
|
pendingSpan.style.display = 'none';
|
|
passSpan.style.display = 'inline';
|
|
failSpan.style.display = 'none';
|
|
} else {
|
|
row.classList.remove('pass-row');
|
|
row.classList.add('fail-row');
|
|
pendingSpan.style.display = 'none';
|
|
passSpan.style.display = 'none';
|
|
failSpan.style.display = 'inline';
|
|
}
|
|
});
|
|
}
|
|
|
|
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);
|
|
});
|