const { BtHelper } = require("../../devices/bt_helper"); import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event'; import EventManager from '../../utils/event_bus' // pages/OTA/ota.js Page({ /** * 页面的初始数据 */ data: { navbarData: { showCapsule: 1, // title: '固件信息', }, device: {}, otaData: {}, btHelper: null, progress: 0, // 进度条初始值 otaStatus: 0, buttonTips: "固件更新", _chunks: [], progressPercent: 0, progress: 0, progressTextLeft: "0%", _timer: null, }, setOtaStatus(status) { switch (status) { case 0: this.setData({ otaStatus: 0, buttonTips: "固件更新" }); break; case 1: this.setData({ otaStatus: 1, buttonTips: "下载升级包" }); break; case 2: this.setData({ otaStatus: 2, buttonTips: "开始更新" }); break; case 3: this.setData({ otaStatus: 3, buttonTips: "更新中..." }); this.startProgress(); break; case 4: this.setData({ otaStatus: 4, buttonTips: "更新失败,再试一次" }); break; default: break; } }, startOtaTap() { let connect = getApp().globalData.device.connect ?? false if (!connect) { wx.showToast({ title: '设备已经掉线了', }) return; } let hasNewVersion = this.data.otaData.hasNewVersion ?? false if (!hasNewVersion) { wx.showToast({ title: '没有可升级的固件', }) return; } let _this = this wx.showModal({ title: '有新固件可升级', content: _this.otaData.content ?? '提升体验,操作更流畅', cancelText: '以后再说', confirmText: '升级', complete: (res) => { if (res.cancel) { } if (res.confirm) { _this.downloadFile(_this.otaData.url) } } }) // const btHelper = BtHelper.getInstance(); // btHelper.ota(this.data.otaData.file_url) }, downloadFile(url) { urlPath = updateData.url ?? ""; content = updateData.content ?? "暂无更新"; var list = urlPath.split("/"); // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA'); const fs = wx.getFileSystemManager(); // 构建本地文件路径 const localFilePath = list[list.length - 1]; let _this = this; // 判断文件是否存在 fs.access(localFilePath, (err) => { if (err) { // 文件不存在,开始下载 wx.downloadFile({ url: url, // 下载资源的URL success: (res) => { if (res.statusCode === 200) { // 下载成功,保存文件路径 const filePath = res.tempFilePath; // 将下载的文件移动到本地路径 fs.rename({ oldPath: filePath, newPath: localFilePath, success: () => { // this.setData({ // localFilePath: localFilePath // }); // 调用 OTA 更新方法 // const btHelper = BtHelper.getInstance(); // btHelper.ota(localFilePath); wx.showToast({ title: '文件下载成功', }); _this.readFile(localFilePath); }, fail: (err) => { wx.showToast({ title: '文件移动失败', }); console.error('文件移动失败:', err); } }); } else { wx.showToast({ title: '文件下载失败', }); } }, fail: (err) => { wx.showToast({ title: '文件下载失败', }); console.error('下载文件失败:', err); } }); } else { _this.readFile(localFilePath); wx.showToast({ title: '文件已存在,跳过下载', }); } }); }, readFile(localFilePath) { if (!localFilePath) { wx.showToast({ title: '下载文件为空', }); return; } this.setOtaStatus(2); const fs = wx.getFileSystemManager(); let _this = this; fs.readFile({ filePath: localFilePath, encoding: 'base64', // 可以选择 'utf8' 或 'base64' success: (res) => { // const btHelper = BtHelper.getInstance(); // btHelper.ota(localFilePath); // console.log('文件内容:', res.data); _this.startSend(); }, fail: (err) => { wx.showToast({ title: 'OTA文件下载失败', }); console.error('文件读取失败:', err); } }); }, startSend() { const chunkSize = 20; // 每次发送的数据块大小 const chunks = []; for (let i = 0; i < data.length; i += chunkSize) { chunks.push(data.slice(i, i + chunkSize)); } this.data._chunks = chunks; const btHelper = BtHelper.getInstance(); btHelper.otaCmd(1) }, sendOtaData() { const btHelper = BtHelper.getInstance(); this.data._chunks.forEach(element => { btHelper.otaData(element) }); this.startProgress() }, endEnd() { const btHelper = BtHelper.getInstance(); btHelper.otaCmd(0) }, updateProgress(newProgress) { this.setData({ progress: newProgress, }); }, // 示例:模拟进度更新 simulateProgressUpdate() { let currentProgress = 0; const interval = setInterval(() => { if (currentProgress >= 100) { clearInterval(interval); wx.showToast({ title: '升级完成', }); } else { currentProgress += 10; this.updateProgress(currentProgress); } }, 500); }, startProgress: function () { const _this = this; let progress = 0; // 定时器,每100毫秒执行一次 _this._timer = setInterval(function () { if (progress >= 100) { clearInterval(_this._timer); // 停止定时器 _this.setData({ progress: 0, progressPercent: 0, showProgress: false, }); _this.data.otaData.hasNewVersion = false; _this.setOtaStatus(0) wx.showToast({ title: '数据上传成功', }) } else { progress += 1; // 每次增加1% let ptl = (progress / 2) + "%" _this.setData({ progress: progress, progressPercent: progress, progressTextLeft:ptl // 文字位置跟随进度 }); } }, 100); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let param = JSON.parse(options.param) console.log(param) let otaData = param.otaData let device = param.device device.clientType = device.clientType ?? device.ProdModel this.setData({ otaData: otaData, device: device, isShowOta: device.clientType === 'MW-S2(BLE)' }) let _this = this; EventManager.addNotification(CmdEvent.eventName, function (event) { let name = event.name; console.log("OTA页:", event) switch (name) { case EnumCmdEvent.otaCmd: let otaCmd = event.otaCmd; let kind = event.kind; if (otaCmd === 1 && kind == 1) { // 开始发送 _this.sendOtaData() } else if (otaCmd === 0 && kind == 1) { // 发送结束 _this.endEnd() } break; } }, this) }, })