fix: address audit findings — fitting trigger, race guards, state conflicts

wear-check:
- Send weak pulse light (brightness=26) to trigger FFE4 fitting report
- Timeout degrades to pass (backward compat with 17-byte firmware)
- Sync _resolved flag prevents double-resolve

treating:
- Only treat run_state=0x00 as completion after seeing run_state=0x02
  (prevents false completion on startup)
- Sync _finishing flag replaces async data.completed for race prevention
- onStatus preserves paused state during fitting loss (|| fittingLost)
- Remove dead _fittingTimer reference from onUnload

auto-scan:
- Sync _completed flag in _scanComplete prevents double-build
这个提交包含在:
Guoguo
2026-06-22 06:28:25 -07:00
父节点 c5a9e89bd9
当前提交 4c674e5c23
修改 3 个文件,包含 31 行新增8 行删除
+3
查看文件
@@ -43,6 +43,7 @@ Page({
startScan: function () {
var self = this
self._scanResults = []
self._completed = false
if (!ble.isConnected()) {
self.setData({ scanning: false, error: '设备未连接,请先连接设备' })
@@ -110,6 +111,8 @@ Page({
},
_scanComplete: function () {
if (this._completed) return
this._completed = true
this._cleanup()
this.setData({ scanning: false, scanProgress: 100 })
this._buildDisplay()
+11 -6
查看文件
@@ -84,7 +84,6 @@ Page({
if (this._onFitting) ble.off('fitting', this._onFitting)
if (this._onRunState) ble.off('run_state', this._onRunState)
if (this._localTimer) clearInterval(this._localTimer)
if (this._fittingTimer) clearTimeout(this._fittingTimer)
},
onFitting: function (data) {
@@ -99,10 +98,15 @@ Page({
},
onRunState: function (data) {
if (this.data.completed) return
if (this._finishing) return
console.log('[TREAT] run_state:', data.run_state)
if (data.run_state === 0xFF) {
if (data.run_state === 0x02) {
this._seenTreating = true
}
if (data.run_state === 0xFF && !this._finishing) {
this._finishing = true
this.setData({ deviceFault: true })
var self = this
wx.showModal({
@@ -114,7 +118,7 @@ Page({
return
}
if (data.run_state === 0x00 && !this.data.paused && !this.data.fittingLost) {
if (data.run_state === 0x00 && this._seenTreating && !this.data.fittingLost) {
this.finishAsComplete()
}
},
@@ -170,7 +174,7 @@ Page({
this.setData({
battery: status.battery || this.data.battery,
temperature: status.temperature || this.data.temperature,
paused: status.mode_state === 0x03
paused: status.mode_state === 0x03 || this.data.fittingLost
})
this.syncCommands()
},
@@ -239,7 +243,8 @@ Page({
},
finishAsComplete: function () {
if (this.data.completed) return
if (this._finishing) return
this._finishing = true
var elapsed = Math.min(this.data.duration, Date.now() - this.data.startedAt)
this.syncTreatmentEnd(elapsed)
this.onComplete({
+17 -2
查看文件
@@ -42,6 +42,7 @@ Page({
checkWearing: function () {
var self = this
self._cleanup()
self._resolved = false
self.setData({ checking: true, result: null, error: '' })
if (!ble.isConnected()) {
@@ -57,18 +58,32 @@ Page({
ble.on('battery', self._onBattery)
self._onFitting = function (data) {
if (self._resolved) return
console.log('[WEAR] 贴合状态:', data.fitting)
if (data.fitting === 1) {
self._resolved = true
self._cleanup()
self.setData({ checking: false, result: 'ok' })
}
}
ble.on('fitting', self._onFitting)
ble.setParams({
region_mask: 0x1F,
wavelength: 2,
brightness: 26,
hold_time: 0,
control: 0x01
}).then(function () {
console.log('[WEAR] 微弱光贴合检测指令已发送')
}).catch(function () {})
self._fittingTimer = setTimeout(function () {
if (self.data.result) return
if (self._resolved) return
self._resolved = true
self._cleanup()
self.setData({ checking: false, result: 'fail', error: '未检测到贴合,请确认设备已正确佩戴' })
console.log('[WEAR] 超时未收到 fitting 事件,降级为通过(兼容旧固件)')
self.setData({ checking: false, result: 'ok' })
}, FITTING_TIMEOUT)
},