[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秒)
This commit is contained in:
@@ -151,6 +151,42 @@ App({
|
||||
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
|
||||
},
|
||||
|
||||
// 保活定时器引用
|
||||
_keepAliveTimer: null,
|
||||
|
||||
/**
|
||||
* 启动保活定时器,每 15 秒发送一次保活指令
|
||||
* 内部已做去重处理,重复调用安全
|
||||
*/
|
||||
startKeepAlive() {
|
||||
this.stopKeepAlive();
|
||||
this._keepAliveTimer = setInterval(() => {
|
||||
if (this.globalData.ppScale.activeProtocol) {
|
||||
this.globalData.ppScale.activeProtocol.sendKeepAliveCode();
|
||||
}
|
||||
}, 15000);
|
||||
},
|
||||
|
||||
/**
|
||||
* 清除保活定时器
|
||||
*/
|
||||
stopKeepAlive() {
|
||||
if (this._keepAliveTimer) {
|
||||
clearInterval(this._keepAliveTimer);
|
||||
this._keepAliveTimer = null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面离开时的统一清理:停止保活 + 重置重连计数 + connectState 置空 + 停止蓝牙
|
||||
*/
|
||||
cleanupConnection() {
|
||||
this.stopKeepAlive();
|
||||
this.resetReconnectCount();
|
||||
this.setGlobalData('ppScale.device.connectState', null);
|
||||
this.globalData.ppScale.plugin.Blue.stop();
|
||||
},
|
||||
|
||||
/**
|
||||
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect)
|
||||
* 依赖 Blue.stop() 清空订阅,onShow 时直接调用,无需标志位
|
||||
@@ -164,8 +200,10 @@ App({
|
||||
this.setGlobalData('ppScale.device.connectState', res);
|
||||
if (res == plugin.BLUE_STATE.CONNECTSUCCESS) {
|
||||
this.globalData.ppScale.reconnect.count = 0;
|
||||
this.startKeepAlive();
|
||||
}
|
||||
if (res == plugin.BLUE_STATE.CONNECTFAILED) {
|
||||
this.stopKeepAlive();
|
||||
this.reconnectDevice();
|
||||
}
|
||||
wx.hideLoading();
|
||||
@@ -179,6 +217,7 @@ App({
|
||||
// 设备自动断开监听
|
||||
plugin.bus.subscribe('deviceWillDisconnect', (res) => {
|
||||
console.log("app.js ===> deviceWillDisconnect", res);
|
||||
this.stopKeepAlive();
|
||||
this.setGlobalData('ppScale.device.connectState', plugin.BLUE_STATE.CONNECTFAILED);
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user