[AI Generated]: refactor(*): 对齐 SDK demo 模式,移除 bus 标志位,依赖 Blue.stop() 清空订阅,onShow 直接重新 subscribe
This commit is contained in:
@@ -16,74 +16,6 @@ Page({
|
||||
// 一次性数据初始化,订阅逻辑在 onShow 中注册
|
||||
},
|
||||
|
||||
// 标志位:防止页面级 bus 重复订阅(SDK bus 不支持 unsubscribe)
|
||||
_pageBusSubscribed: false,
|
||||
|
||||
// 注册页面级 bus 订阅(通过标志位防重)
|
||||
_subscribePageBus() {
|
||||
if (this._pageBusSubscribed) return;
|
||||
this._pageBusSubscribed = true;
|
||||
|
||||
ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
||||
console.log("===》devicesModel", res);
|
||||
ppScale.device.mac = res.deviceMac;
|
||||
this._macReady = true;
|
||||
this._tryProgress();
|
||||
});
|
||||
|
||||
ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('===》deviceConnect', res);
|
||||
|
||||
ppScale.plugin.ScaleAction.startDataProgress(true);
|
||||
ppScale.activeProtocol = ppScale.plugin.ScaleAction.getActiveProtocol();
|
||||
|
||||
let currentStep = this.data.progress.index;
|
||||
|
||||
// 步骤 1/2/3:最小化后重连,只需恢复连接状态(跳过绑定检查)
|
||||
if (currentStep >= 1) {
|
||||
ppScale.activeProtocol.codeUpdateMTU((mtuRes) => {
|
||||
console.log("===》重连 codeUpdateMTU", mtuRes);
|
||||
|
||||
ppScale.device.name = this.data.device.name;
|
||||
ppScale.device.connection = this.data.device;
|
||||
|
||||
wx.hideLoading();
|
||||
this._startKeepAlive();
|
||||
|
||||
// 配网步骤:重新获取 WiFi 列表
|
||||
if (currentStep === 2) {
|
||||
const comp = this.selectComponent('#configureDevice3');
|
||||
if (comp) comp.reInitWifi();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤 0:首次连接设备
|
||||
ppScale.activeProtocol.codeUpdateMTU((mtuRes) => {
|
||||
console.log("===》codeUpdateMTU", mtuRes);
|
||||
|
||||
ppScale.activeProtocol.codeSyncTime((codeSyncTime) => {
|
||||
console.log("===》codeSyncTime", codeSyncTime);
|
||||
|
||||
wx.hideLoading();
|
||||
this._startKeepAlive();
|
||||
ppScale.device.name = this.data.device.name;
|
||||
ppScale.device.connection = this.data.device;
|
||||
const nameArr = this.data.device.name.split("-");
|
||||
ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
|
||||
this._connectReady = true;
|
||||
this._tryProgress();
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// 重置页面级 bus 订阅标志(页面离开时调用,下次 onShow 重新订阅)
|
||||
_unsubscribePageBus() {
|
||||
this._pageBusSubscribed = false;
|
||||
},
|
||||
|
||||
// 记录 stop() 调用时间,用于 onShow 动态计算安全延迟
|
||||
_stopTime: 0,
|
||||
|
||||
@@ -92,13 +24,58 @@ Page({
|
||||
const elapsed = Date.now() - (this._stopTime || 0);
|
||||
const delay = Math.max(0, 500 - elapsed);
|
||||
setTimeout(() => {
|
||||
// 注册 app 级别的全局 bus 订阅
|
||||
// 注册 app 级别的全局 bus 订阅(stop() 已清空,直接重新订阅)
|
||||
app.registerBusListeners();
|
||||
|
||||
// 页面级 bus 订阅(标志位防重)
|
||||
this._subscribePageBus();
|
||||
// 页面级 bus 订阅
|
||||
ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
||||
console.log("===》devicesModel", res);
|
||||
ppScale.device.mac = res.deviceMac;
|
||||
this._macReady = true;
|
||||
this._tryProgress();
|
||||
});
|
||||
|
||||
// devicesList 每次 onShow 都需要重新订阅(重连场景)
|
||||
ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('===》deviceConnect', res);
|
||||
ppScale.plugin.ScaleAction.startDataProgress(true);
|
||||
ppScale.activeProtocol = ppScale.plugin.ScaleAction.getActiveProtocol();
|
||||
|
||||
let currentStep = this.data.progress.index;
|
||||
|
||||
// 步骤 1/2/3:最小化后重连,只需恢复连接状态
|
||||
if (currentStep >= 1) {
|
||||
ppScale.activeProtocol.codeUpdateMTU((mtuRes) => {
|
||||
console.log("===》重连 codeUpdateMTU", mtuRes);
|
||||
ppScale.device.name = this.data.device.name;
|
||||
ppScale.device.connection = this.data.device;
|
||||
wx.hideLoading();
|
||||
this._startKeepAlive();
|
||||
if (currentStep === 2) {
|
||||
const comp = this.selectComponent('#configureDevice3');
|
||||
if (comp) comp.reInitWifi();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤 0:首次连接设备
|
||||
ppScale.activeProtocol.codeUpdateMTU((mtuRes) => {
|
||||
console.log("===》codeUpdateMTU", mtuRes);
|
||||
ppScale.activeProtocol.codeSyncTime((codeSyncTime) => {
|
||||
console.log("===》codeSyncTime", codeSyncTime);
|
||||
wx.hideLoading();
|
||||
this._startKeepAlive();
|
||||
ppScale.device.name = this.data.device.name;
|
||||
ppScale.device.connection = this.data.device;
|
||||
const nameArr = this.data.device.name.split("-");
|
||||
ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
|
||||
this._connectReady = true;
|
||||
this._tryProgress();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 如果之前已选择过设备,自动扫描并重连
|
||||
if (this.data.device) {
|
||||
wx.showLoading({ title: "正在重新连接...", mask: true });
|
||||
ppScale.plugin.bus.subscribe("devicesList", (res) => {
|
||||
@@ -287,18 +264,11 @@ Page({
|
||||
onHide() {
|
||||
console.log("configureDevice onHide");
|
||||
this._stopKeepAlive();
|
||||
this._unsubscribePageBus();
|
||||
app.unregisterBusListeners();
|
||||
// 配网步骤:立即清空 WiFi 列表(stop 后数据已失效)
|
||||
if (this.data.progress.index === 2) {
|
||||
const comp = this.selectComponent('#configureDevice3');
|
||||
if (comp) {
|
||||
comp.setData({
|
||||
progress: 0,
|
||||
wifiList: [],
|
||||
selectWifi: null,
|
||||
selectWifiPWD: ""
|
||||
});
|
||||
comp.setData({ progress: 0, wifiList: [], selectWifi: null, selectWifiPWD: "" });
|
||||
}
|
||||
}
|
||||
app.resetReconnectCount();
|
||||
@@ -312,8 +282,6 @@ Page({
|
||||
onUnload() {
|
||||
console.log("configureDevice onUnload");
|
||||
this._stopKeepAlive();
|
||||
this._unsubscribePageBus();
|
||||
app.unregisterBusListeners();
|
||||
app.resetReconnectCount();
|
||||
app.setGlobalData('ppScale.device.connectState', null);
|
||||
this._connectReady = false;
|
||||
|
||||
Reference in New Issue
Block a user