110 lines
3.2 KiB
JavaScript
110 lines
3.2 KiB
JavaScript
// ui-utils.js - UI工具函数模块
|
|
|
|
const UIUtils = (function() {
|
|
let toast;
|
|
|
|
// 初始化UI工具
|
|
function init() {
|
|
// 初始化Toast
|
|
initToast();
|
|
|
|
// 初始化设备控制按钮
|
|
initDeviceControls();
|
|
}
|
|
|
|
// 初始化Toast通知
|
|
function initToast() {
|
|
const toastEl = document.getElementById('toast');
|
|
toast = new bootstrap.Toast(toastEl, {
|
|
autohide: true,
|
|
delay: 3000
|
|
});
|
|
}
|
|
|
|
// 显示Toast通知
|
|
function showToast(message) {
|
|
$('.toast-body').text(message);
|
|
toast.show();
|
|
}
|
|
|
|
// 初始化设备控制按钮
|
|
function initDeviceControls() {
|
|
// 设备重启按钮
|
|
$('#reboot').click(function() {
|
|
$.ajax({
|
|
url: '/api/device/reboot',
|
|
type: 'GET',
|
|
success: function(response) {
|
|
if (response.success) {
|
|
showToast('请求已发送');
|
|
} else {
|
|
showToast('请求失败:' + response.message);
|
|
}
|
|
},
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
showToast('请求失败:服务器错误');
|
|
}
|
|
});
|
|
});
|
|
|
|
// DAQ控制按钮
|
|
$('#start_daq').click(function() {
|
|
WebSocketManager.publish(WebSocketManager.topic_daq_control, {
|
|
cmd: 'start',
|
|
len: parseInt($('#daq_duration').val())
|
|
});
|
|
});
|
|
|
|
$('#stop_daq').click(function() {
|
|
WebSocketManager.publish(WebSocketManager.topic_daq_control, {
|
|
cmd: 'stop'
|
|
});
|
|
});
|
|
|
|
// 序列号设置功能
|
|
$('#daq_info').dblclick(function() {
|
|
// 获取当前序列号
|
|
const currentSn = $(this).text().trim() || '';
|
|
// 设置输入框的值
|
|
$('#snInput').val(currentSn);
|
|
// 显示模态对话框
|
|
const snModal = new bootstrap.Modal(document.getElementById('snModal'));
|
|
snModal.show();
|
|
});
|
|
|
|
// 保存序列号按钮点击事件
|
|
$('#saveSnBtn').click(function() {
|
|
// 获取输入的序列号
|
|
const newSn = $('#snInput').val().trim();
|
|
if (newSn === '') {
|
|
showToast('序列号不能为空');
|
|
return;
|
|
}
|
|
|
|
// 通过WebSocket发送设置序列号命令
|
|
WebSocketManager.publish(WebSocketManager.topic_daq_control, {
|
|
cmd: 'daq_set_sn',
|
|
sn: newSn
|
|
});
|
|
|
|
// 关闭模态对话框
|
|
const snModal = bootstrap.Modal.getInstance(document.getElementById('snModal'));
|
|
snModal.hide();
|
|
|
|
// 显示保存成功提示
|
|
showToast('序列号设置命令已发送');
|
|
});
|
|
}
|
|
|
|
// 暴露公共方法
|
|
return {
|
|
init: init,
|
|
showToast: showToast
|
|
};
|
|
})();
|
|
|
|
// 为了兼容旧代码,将showToast函数添加到全局作用域
|
|
window.showToast = function(message) {
|
|
UIUtils.showToast(message);
|
|
};
|