324 lines
8.1 KiB
JavaScript
324 lines
8.1 KiB
JavaScript
const app = getApp();
|
||
const ppScale = app.globalData.ppScale;
|
||
|
||
Page({
|
||
data: {
|
||
progress: {
|
||
index: 0,
|
||
data: ['配置蓝牙', '初始化用户信息', '配备网络', '完成']
|
||
},
|
||
|
||
progressNext: false,
|
||
device: null
|
||
},
|
||
|
||
onLoad() {
|
||
// 一次性数据初始化,订阅逻辑在 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,
|
||
|
||
onShow() {
|
||
console.log("configureDevice onShow");
|
||
const elapsed = Date.now() - (this._stopTime || 0);
|
||
const delay = Math.max(0, 500 - elapsed);
|
||
setTimeout(() => {
|
||
// 注册 app 级别的全局 bus 订阅
|
||
app.registerBusListeners();
|
||
|
||
// 页面级 bus 订阅(标志位防重)
|
||
this._subscribePageBus();
|
||
|
||
// devicesList 每次 onShow 都需要重新订阅(重连场景)
|
||
if (this.data.device) {
|
||
wx.showLoading({ title: "正在重新连接...", mask: true });
|
||
ppScale.plugin.bus.subscribe("devicesList", (res) => {
|
||
let fIndex = res.findIndex(item => item.deviceId === this.data.device.deviceId);
|
||
if (fIndex >= 0) {
|
||
ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
|
||
ppScale.plugin.Blue.createBLEConnection(res[fIndex]);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 通过完整链路重新初始化蓝牙(权限检查 → 打开适配器 → start)
|
||
this.checkBluetoothPermissionAndInit();
|
||
}, delay);
|
||
},
|
||
|
||
// 选择了某个设备
|
||
deviceChange(e) {
|
||
let status = e.detail.status;
|
||
if (status) {
|
||
wx.showLoading({
|
||
title: "蓝牙配对中...",
|
||
mask: true
|
||
});
|
||
this.setData({
|
||
progressNext: true
|
||
})
|
||
let device = e.detail.device;
|
||
this.connectedDevice_(device);
|
||
}
|
||
},
|
||
|
||
connectedDevice(e) {
|
||
app.resetReconnectCount();
|
||
wx.showLoading({
|
||
title: "正在尝试连接...",
|
||
mask: true
|
||
});
|
||
this.setData({
|
||
progressNext: false
|
||
})
|
||
let device = e.detail.device;
|
||
// 先断开旧连接再重新连接
|
||
ppScale.plugin.Blue.disconnect((disres) => {
|
||
this.connectedDevice_(device);
|
||
});
|
||
},
|
||
|
||
connectedDevice_(device) {
|
||
if (device) {
|
||
this.setData({
|
||
device: device
|
||
})
|
||
ppScale.plugin.Blue.createBLEConnection(device);
|
||
}
|
||
},
|
||
|
||
setDeviceUserList(e) {
|
||
let status = e.detail.status;
|
||
if (status) {
|
||
this.setProgress();
|
||
}
|
||
},
|
||
|
||
setDeviceConfig(e) {
|
||
let status = e.detail.status;
|
||
if (status) {
|
||
this.setProgress();
|
||
}
|
||
},
|
||
|
||
setConfigSuccessful() {
|
||
this._stopKeepAlive();
|
||
ppScale.plugin.Blue.stop();
|
||
wx.switchTab({
|
||
url: "/pages/home/home"
|
||
})
|
||
},
|
||
|
||
setProgress() {
|
||
const newIndex = this.data.progress.index + 1;
|
||
this.setData({ "progress.index": newIndex });
|
||
// 进入步骤2(配网)时,等待组件渲染完成后由父页面显式触发 WiFi 列表获取
|
||
if (newIndex === 2) {
|
||
setTimeout(() => {
|
||
const comp = this.selectComponent('#configureDevice3');
|
||
if (comp) comp.initWifi();
|
||
}, 100);
|
||
}
|
||
// 进入步骤3(OTA升级)时,等待组件渲染完成后由父页面显式触发升级
|
||
if (newIndex === 3) {
|
||
setTimeout(() => {
|
||
const comp = this.selectComponent('#configureDevice4');
|
||
if (comp) comp.startOtaUpgrade();
|
||
}, 100);
|
||
}
|
||
},
|
||
|
||
// 连接完成 + mac 获取完成,双条件满足后才进入下一步
|
||
_connectReady: false,
|
||
_macReady: false,
|
||
_tryProgress() {
|
||
if (this._connectReady && this._macReady && this.data.progressNext) {
|
||
this._connectReady = false;
|
||
this._macReady = false;
|
||
this.setData({ progressNext: false });
|
||
this.setProgress();
|
||
}
|
||
},
|
||
|
||
// 启动保活定时器,每10秒发送一次保活指令
|
||
_keepAliveTimer: null,
|
||
_startKeepAlive() {
|
||
this._stopKeepAlive();
|
||
this._keepAliveTimer = setInterval(() => {
|
||
if (ppScale.activeProtocol) {
|
||
ppScale.activeProtocol.sendKeepAliveCode();
|
||
}
|
||
}, 15000);
|
||
},
|
||
|
||
// 清除保活定时器
|
||
_stopKeepAlive() {
|
||
if (this._keepAliveTimer) {
|
||
clearInterval(this._keepAliveTimer);
|
||
this._keepAliveTimer = null;
|
||
}
|
||
},
|
||
|
||
checkBluetoothPermissionAndInit() {
|
||
wx.getSetting({
|
||
success: (res) => {
|
||
if (res.authSetting['scope.bluetooth']) {
|
||
this.openBluetoothAdapter();
|
||
} else {
|
||
wx.authorize({
|
||
scope: 'scope.bluetooth',
|
||
success: () => {
|
||
this.openBluetoothAdapter();
|
||
},
|
||
fail: () => {
|
||
wx.hideLoading();
|
||
wx.showModal({
|
||
title: '提示',
|
||
content: '蓝牙权限被拒绝,无法连接设备。是否前往设置开启?',
|
||
confirmText: '去开启',
|
||
cancelText: '不开启',
|
||
success: (modalRes) => {
|
||
if (modalRes.confirm) {
|
||
wx.openSetting();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
},
|
||
fail: () => {
|
||
wx.hideLoading();
|
||
}
|
||
});
|
||
},
|
||
|
||
openBluetoothAdapter() {
|
||
wx.openBluetoothAdapter({
|
||
success: () => {
|
||
// 蓝牙适配器打开成功,设置设备配置后启动扫描
|
||
let setting = ppScale.device.setting;
|
||
ppScale.plugin.Blue.setDeviceSetting(setting);
|
||
let deviceNames = setting.map(item => item.deviceName);
|
||
ppScale.plugin.Blue.start(deviceNames, false);
|
||
},
|
||
fail: (err) => {
|
||
wx.hideLoading();
|
||
if (err.errCode === 10001) {
|
||
wx.showModal({
|
||
title: "提示",
|
||
content: "请确保手机蓝牙已开启。",
|
||
showCancel: false
|
||
});
|
||
}
|
||
}
|
||
});
|
||
},
|
||
|
||
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: ""
|
||
});
|
||
}
|
||
}
|
||
app.resetReconnectCount();
|
||
app.setGlobalData('ppScale.device.connectState', null);
|
||
this._connectReady = false;
|
||
this._macReady = false;
|
||
this._stopTime = Date.now();
|
||
ppScale.plugin.Blue.stop();
|
||
},
|
||
|
||
onUnload() {
|
||
console.log("configureDevice onUnload");
|
||
this._stopKeepAlive();
|
||
this._unsubscribePageBus();
|
||
app.unregisterBusListeners();
|
||
app.resetReconnectCount();
|
||
app.setGlobalData('ppScale.device.connectState', null);
|
||
this._connectReady = false;
|
||
this._macReady = false;
|
||
this._stopTime = Date.now();
|
||
ppScale.plugin.Blue.stop();
|
||
}
|
||
}) |