262 lines
9.2 KiB
JavaScript
262 lines
9.2 KiB
JavaScript
// firmware-upgrade.js - 固件升级模块
|
|
|
|
const FirmwareUpgrade = (function() {
|
|
const CHUNK_SIZE = 4096; // 4KB 分段大小
|
|
let uploadInProgress = false;
|
|
let uploadOffset = 0;
|
|
let uploadFile = null;
|
|
let fileChecksum = '';
|
|
|
|
// 初始化固件升级功能
|
|
function init() {
|
|
// 文件选择事件
|
|
$('#firmwareFile').on('change', handleFileSelect);
|
|
|
|
// 开始上传按钮点击事件
|
|
$('#startUpload').on('click', handleStartUpload);
|
|
|
|
// 取消上传按钮点击事件
|
|
$('#cancelUpload').on('click', handleCancelUpload);
|
|
|
|
// 执行升级按钮点击事件
|
|
$('#upgradeFirmware').on('click', handleUpgradeFirmware);
|
|
}
|
|
|
|
// 处理文件选择
|
|
function handleFileSelect(e) {
|
|
const file = e.target.files[0];
|
|
if (!file) return;
|
|
|
|
// 验证文件类型
|
|
if (!file.name.endsWith('.bin')) {
|
|
showToast('请选择.bin格式的固件文件');
|
|
$(this).val('');
|
|
return;
|
|
}
|
|
|
|
// 显示文件信息
|
|
$('#fileName').text(file.name);
|
|
$('#fileSize').text(formatFileSize(file.size));
|
|
|
|
// 计算校验和
|
|
calculateFileChecksum(file);
|
|
|
|
// 重置状态
|
|
uploadOffset = 0;
|
|
uploadFile = file;
|
|
$('#uploadProgress').css('width', '0%').text('0%').attr('aria-valuenow', 0);
|
|
$('#startUpload').prop('disabled', true);
|
|
$('#upgradeFirmware').prop('disabled', true);
|
|
}
|
|
|
|
// 计算文件校验和
|
|
function calculateFileChecksum(file) {
|
|
$('#fileChecksum').text('计算中...');
|
|
|
|
const reader = new FileReader();
|
|
reader.onload = function(e) {
|
|
const arrayBuffer = e.target.result;
|
|
const checksum = calculateChecksum(arrayBuffer);
|
|
fileChecksum = checksum;
|
|
$('#fileChecksum').text(checksum);
|
|
|
|
// 启用上传按钮
|
|
$('#startUpload').prop('disabled', false);
|
|
};
|
|
reader.onerror = function() {
|
|
$('#fileChecksum').text('计算失败');
|
|
showToast('文件校验和计算失败');
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
}
|
|
|
|
// 计算校验和的函数
|
|
function calculateChecksum(arrayBuffer) {
|
|
let checksum = 0;
|
|
const dataView = new DataView(arrayBuffer);
|
|
|
|
// 对每个字节进行累加,32位
|
|
for (let i = 0; i < dataView.byteLength; i++) {
|
|
checksum = (checksum + dataView.getUint8(i)) & 0xFFFFFFFF;
|
|
}
|
|
|
|
// 转换为十六进制字符串,确保8位
|
|
return checksum.toString(16).padStart(8, '0');
|
|
}
|
|
|
|
// 格式化文件大小
|
|
function formatFileSize(bytes) {
|
|
if (bytes < 1024) return bytes + ' B';
|
|
else if (bytes < 1048576) return (bytes / 1024).toFixed(2) + ' KB';
|
|
else return (bytes / 1048576).toFixed(2) + ' MB';
|
|
}
|
|
|
|
// 处理开始上传
|
|
function handleStartUpload() {
|
|
if (!uploadFile) return;
|
|
|
|
$('#startUpload').prop('disabled', true);
|
|
$('#cancelUpload').prop('disabled', false);
|
|
uploadInProgress = true;
|
|
|
|
// 清空日志
|
|
$('#upgradeLog').text('开始上传固件...\n');
|
|
|
|
// 开始分段上传
|
|
uploadOffset = 0;
|
|
uploadNextChunk();
|
|
}
|
|
|
|
// 处理取消上传
|
|
function handleCancelUpload() {
|
|
uploadInProgress = false;
|
|
$(this).prop('disabled', true);
|
|
|
|
// 发送取消请求
|
|
$.ajax({
|
|
url: '/api/firmware/cancel',
|
|
type: 'POST',
|
|
success: function() {
|
|
showToast('固件上传已取消');
|
|
$('#upgradeLog').append('上传已取消\n');
|
|
},
|
|
error: function() {
|
|
showToast('取消上传失败');
|
|
}
|
|
});
|
|
}
|
|
|
|
// 处理执行升级
|
|
function handleUpgradeFirmware() {
|
|
$(this).prop('disabled', true);
|
|
$('#upgradeLog').append('开始执行固件升级...\n');
|
|
|
|
$.ajax({
|
|
url: '/api/firmware/upgrade',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
success: function(response) {
|
|
if (response.success) {
|
|
$('#upgradeLog').append('固件升级成功!\n');
|
|
$('#upgradeLog').append('设备将在1秒后重启...\n');
|
|
showToast('固件升级成功,设备将在1秒后重启');
|
|
} else {
|
|
$('#upgradeLog').append('升级失败:' + response.message + '\n');
|
|
showToast('固件升级失败:' + response.message);
|
|
}
|
|
},
|
|
error: function(xhr) {
|
|
let message = '升级请求失败';
|
|
if (xhr.responseJSON && xhr.responseJSON.message) {
|
|
message = xhr.responseJSON.message;
|
|
}
|
|
$('#upgradeLog').append('升级失败:' + message + '\n');
|
|
showToast('固件升级失败:' + message);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 上传下一个文件块
|
|
function uploadNextChunk() {
|
|
if (!uploadInProgress || !uploadFile) return;
|
|
|
|
// 计算当前块的开始和结束位置
|
|
const start = uploadOffset;
|
|
const end = Math.min(start + CHUNK_SIZE, uploadFile.size);
|
|
const chunk = uploadFile.slice(start, end);
|
|
|
|
// 读取块内容并转换为Base64
|
|
const reader = new FileReader();
|
|
reader.onload = function(e) {
|
|
const chunkData = e.target.result;
|
|
const base64Data = btoa(chunkData);
|
|
|
|
// 发送上传请求
|
|
$.ajax({
|
|
url: '/api/firmware/upload',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
content: base64Data,
|
|
offset: start,
|
|
final: end >= uploadFile.size
|
|
}),
|
|
success: function(response) {
|
|
if (response.success) {
|
|
uploadOffset = response.offset;
|
|
|
|
// 更新进度
|
|
const progress = Math.min(Math.round((uploadOffset / uploadFile.size) * 100), 100);
|
|
$('#uploadProgress').css('width', progress + '%').text(progress + '%').attr('aria-valuenow', progress);
|
|
|
|
// 检查是否完成
|
|
if (response.final || uploadOffset >= uploadFile.size) {
|
|
$('#upgradeLog').append('固件上传完成!\n');
|
|
$('#upgradeLog').append('正在验证固件...\n');
|
|
|
|
// 验证固件
|
|
verifyFirmware();
|
|
} else {
|
|
// 继续上传下一块
|
|
setTimeout(uploadNextChunk, 100); // 添加小延迟,避免请求过于频繁
|
|
}
|
|
} else {
|
|
throw new Error(response.message || '上传失败');
|
|
}
|
|
},
|
|
error: function(xhr) {
|
|
let message = '上传失败';
|
|
if (xhr.responseJSON && xhr.responseJSON.message) {
|
|
message = xhr.responseJSON.message;
|
|
}
|
|
$('#upgradeLog').append('上传失败:' + message + '\n');
|
|
showToast('固件上传失败:' + message);
|
|
uploadInProgress = false;
|
|
$('#cancelUpload').prop('disabled', true);
|
|
}
|
|
});
|
|
};
|
|
|
|
reader.readAsBinaryString(chunk);
|
|
}
|
|
|
|
// 验证固件
|
|
function verifyFirmware() {
|
|
$.ajax({
|
|
url: '/api/firmware/verify',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
checksum: fileChecksum,
|
|
size: uploadFile.size
|
|
}),
|
|
success: function(response) {
|
|
if (response.success) {
|
|
$('#upgradeLog').append('固件验证成功!\n');
|
|
$('#upgradeLog').append('可以点击"执行升级"按钮进行升级\n');
|
|
$('#upgradeFirmware').prop('disabled', false);
|
|
$('#cancelUpload').prop('disabled', true);
|
|
showToast('固件上传完成并通过验证');
|
|
} else {
|
|
throw new Error(response.message || '验证失败');
|
|
}
|
|
},
|
|
error: function(xhr) {
|
|
let message = '验证失败';
|
|
if (xhr.responseJSON && xhr.responseJSON.message) {
|
|
message = xhr.responseJSON.message;
|
|
}
|
|
$('#upgradeLog').append('验证失败:' + message + '\n');
|
|
showToast('固件验证失败:' + message);
|
|
$('#cancelUpload').prop('disabled', true);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 暴露公共方法
|
|
return {
|
|
init: init
|
|
};
|
|
})();
|