Files
bodyWeight/components/configureDevice_4/configureDevice_4.js
T
17792275749 de8fa1fca8 [AI Generated]: refactor(*): 保活定时器与连接清理逻辑收口至 app.js,修复 null 安全与注释错误
- 新增 app.startKeepAlive/stopKeepAlive/cleanupConnection,bus 监听自动控制保活与断联清理
- Blue.stop() 收入 cleanupConnection,页面 onHide/onUnload 统一调用
- 修复 replaceNetwork.js seviceList 可能为 undefined 导致崩溃
- 修复 configureDevice_2.js device.mac 为 null 时崩溃
- 修复 replaceNetwork_1.js activeProtocol 无 null 检查及 wifiList 容错
- 修正 configureDevice_4.js 超时注释(3分钟→30秒)
2026-04-08 14:56:48 +08:00

133 lines
4.5 KiB
JavaScript

const app = getApp();
Component({
data: {
otaProgress: 0,
otaStatus: '', // 'upgrading' | 'success' | 'failed' | ''
otaMessage: ''
},
lifetimes: {
attached() {
// OTA 升级由父页面在 activeProtocol 就绪后显式调用,此处不自动触发
},
detached() {
// 组件销毁时清除超时定时器,防止泄漏
this._clearOtaTimer();
}
},
methods: {
// 完成按钮回调(成功或失败后展示)
bindOk() {
this.triggerEvent('deviceEvent', { status: true });
},
// 清除 OTA 超时定时器
_clearOtaTimer() {
if (this._otaTimer) {
clearTimeout(this._otaTimer);
this._otaTimer = null;
}
},
// 开始 OTA 升级
startOtaUpgrade() {
const activeProtocol = app.globalData.ppScale.activeProtocol;
if (!activeProtocol) {
wx.showToast({ title: '设备未连接,请重新连接', icon: 'none' });
return;
}
wx.showLoading({ title: '正在升级...', mask: true });
// 重置完成标志位,防止超时与回调重复触发
this._otaCompleted = false;
this.setData({
otaProgress: 0,
otaStatus: 'upgrading',
otaMessage: '升级初始化中...'
});
// 启动超时保护,30 秒内未完成则视为失败
this._clearOtaTimer();
this._otaTimer = setTimeout(() => {
console.log('OTA 升级超时');
this.handleOtaFailed('升级超时,设备即将重启');
}, 30000);
// 回调参数:数字(指令状态码)或对象 { progress, isFailed }
activeProtocol.codeOtaUpdate((res, status) => {
console.log('OTA 升级 status 回调 :', status);
console.log('OTA 升级 res 回调:', res);
// // 已完成(成功或失败)则忽略后续回调
// if (this._otaCompleted) return;
//
// // SDK 返回数字时:1 表示指令已下发、设备开始通过 WiFi 升级并重启
// if (typeof res === 'number') {
// if (res === 1) {
// this._clearOtaTimer();
// this.handleOtaSuccess();
// } else {
// this._clearOtaTimer();
// this.handleOtaFailed(`OTA 指令失败,错误码:${res}`);
// }
// return;
// }
//
// // SDK 返回对象时:{ progress, isFailed }
// if (res && res.progress !== undefined) {
// this.setData({
// otaProgress: res.progress,
// otaMessage: `升级进度: ${res.progress}%`
// });
// }
//
// if (res && res.isFailed === false) {
// this._clearOtaTimer();
// this.handleOtaSuccess();
// } else if (res && res.isFailed === true) {
// this._clearOtaTimer();
// this.handleOtaFailed();
// }
});
},
// OTA 升级成功处理
handleOtaSuccess() {
if (this._otaCompleted) return;
this._otaCompleted = true;
wx.hideLoading();
this.setData({
otaProgress: 100,
otaStatus: 'success',
otaMessage: '升级指令已下发,设备正在重启...'
});
wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 });
setTimeout(() => {
this.bindOk();
}, 2000);
},
// OTA 升级失败处理
handleOtaFailed(msg) {
if (this._otaCompleted) return;
this._otaCompleted = true;
wx.hideLoading();
const message = msg || '升级失败,设备即将重启';
this.setData({
otaProgress: 0,
otaStatus: 'failed',
otaMessage: message
});
wx.showToast({ title: message, icon: 'none', duration: 2000 });
setTimeout(() => {
this.bindOk();
}, 2000);
}
}
});