[AI Generated]: fix(*): 修复蓝牙连接全链路问题,包含 bus 重复订阅、activeProtocol 空指针、stop 竞速、重连计数器跨页面共享、OTA 超时保护、硬超时改事件驱动、保活定时器泄漏及回调错误处理
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
const app = getApp();
|
||||
|
||||
// OTA 超时时间:3 分钟
|
||||
const OTA_TIMEOUT_MS = 3 * 60 * 1000;
|
||||
|
||||
Component({
|
||||
data: {
|
||||
otaProgress: 0,
|
||||
@@ -9,8 +12,11 @@ Component({
|
||||
|
||||
lifetimes: {
|
||||
attached() {
|
||||
// 组件挂载后自动触发 OTA 升级
|
||||
this.startOtaUpgrade();
|
||||
// OTA 升级由父页面在 activeProtocol 就绪后显式调用,此处不自动触发
|
||||
},
|
||||
detached() {
|
||||
// 组件销毁时清除超时定时器,防止泄漏
|
||||
this._clearOtaTimer();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -20,6 +26,14 @@ Component({
|
||||
this.triggerEvent('deviceEvent', { status: true });
|
||||
},
|
||||
|
||||
// 清除 OTA 超时定时器
|
||||
_clearOtaTimer() {
|
||||
if (this._otaTimer) {
|
||||
clearTimeout(this._otaTimer);
|
||||
this._otaTimer = null;
|
||||
}
|
||||
},
|
||||
|
||||
// 开始 OTA 升级
|
||||
startOtaUpgrade() {
|
||||
const activeProtocol = app.globalData.ppScale.activeProtocol;
|
||||
@@ -37,6 +51,13 @@ Component({
|
||||
otaMessage: '升级初始化中...'
|
||||
});
|
||||
|
||||
// 启动超时保护,3 分钟内未完成则视为失败
|
||||
this._clearOtaTimer();
|
||||
this._otaTimer = setTimeout(() => {
|
||||
console.log('OTA 升级超时');
|
||||
this.handleOtaFailed('升级超时,设备即将重启');
|
||||
}, OTA_TIMEOUT_MS);
|
||||
|
||||
// 回调参数为 { progress, isFailed }
|
||||
activeProtocol.codeOtaUpdate((res) => {
|
||||
console.log('OTA 升级回调:', res);
|
||||
@@ -49,8 +70,10 @@ Component({
|
||||
}
|
||||
|
||||
if (res && res.isFailed === false) {
|
||||
this._clearOtaTimer();
|
||||
this.handleOtaSuccess();
|
||||
} else if (res && res.isFailed === true) {
|
||||
this._clearOtaTimer();
|
||||
this.handleOtaFailed();
|
||||
}
|
||||
});
|
||||
@@ -66,21 +89,22 @@ Component({
|
||||
});
|
||||
wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 });
|
||||
setTimeout(() => {
|
||||
this.triggerEvent('deviceEvent', { status: true });
|
||||
this.bindOk();
|
||||
}, 2000);
|
||||
},
|
||||
|
||||
// OTA 升级失败处理
|
||||
handleOtaFailed() {
|
||||
handleOtaFailed(msg) {
|
||||
wx.hideLoading();
|
||||
const message = msg || '升级失败,设备即将重启';
|
||||
this.setData({
|
||||
otaProgress: 0,
|
||||
otaStatus: 'failed',
|
||||
otaMessage: '升级失败,设备即将重启'
|
||||
otaMessage: message
|
||||
});
|
||||
wx.showToast({ title: '升级失败,设备即将重启', icon: 'none', duration: 2000 });
|
||||
wx.showToast({ title: message, icon: 'none', duration: 2000 });
|
||||
setTimeout(() => {
|
||||
this.triggerEvent('deviceEvent', { status: true });
|
||||
this.bindOk();
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user