123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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)
- },
- })
|