feat: auto-scan 3-phase state machine with proper PD collection timing
- Phase 1 (waiting_fit): after sending control=0x01, wait for fitting=1 - Phase 2 (collecting): once fitting=1 AND run_state=1, start PD accumulation - Phase 3 (done): run_state 1→0, calculate averages, auto-send treatment - Check start-collecting in BOTH fitting and run_state handlers - fitting=0 + run_state 1→0 = fitting failure, prompt retry - Timeout 3 minutes (was 30s), auto-start treatment on completion
这个提交包含在:
@@ -1,12 +1,11 @@
|
||||
var ble = require('../../services/ble')
|
||||
var SCAN_TIMEOUT = 30000
|
||||
var SCAN_TIMEOUT = 180000
|
||||
|
||||
Page({
|
||||
data: {
|
||||
statusBarHeight: 44,
|
||||
regions: 0x1F,
|
||||
state: 'ready',
|
||||
scanProgress: 0,
|
||||
phase: 'ready',
|
||||
frameCount: 0,
|
||||
pdAvg: [],
|
||||
vbat: 0,
|
||||
@@ -31,9 +30,9 @@ Page({
|
||||
},
|
||||
|
||||
_cleanup: function () {
|
||||
if (this._progressTimer) { clearInterval(this._progressTimer); this._progressTimer = null }
|
||||
if (this._timeoutTimer) { clearTimeout(this._timeoutTimer); this._timeoutTimer = null }
|
||||
if (this._onAdc) { ble.off('adc', this._onAdc); this._onAdc = null }
|
||||
if (this._onFitting) { ble.off('fitting', this._onFitting); this._onFitting = null }
|
||||
if (this._onRunState) { ble.off('run_state', this._onRunState); this._onRunState = null }
|
||||
},
|
||||
|
||||
@@ -48,16 +47,55 @@ Page({
|
||||
self._pdSum = [0, 0, 0, 0, 0, 0, 0]
|
||||
self._frameCount = 0
|
||||
self._completed = false
|
||||
self._seenScanning = false
|
||||
self.setData({ state: 'scanning', scanProgress: 0, frameCount: 0, pdAvg: [], error: '' })
|
||||
self._collecting = false
|
||||
self._lastFitting = -1
|
||||
self._lastRunState = -1
|
||||
self.setData({ phase: 'waiting_fit', frameCount: 0, pdAvg: [], error: '' })
|
||||
|
||||
self._progressTimer = setInterval(function () {
|
||||
var p = self.data.scanProgress
|
||||
if (p < 90) self.setData({ scanProgress: p + 1 })
|
||||
}, 200)
|
||||
self._onFitting = function (data) {
|
||||
if (self._completed) return
|
||||
if (data.fitting !== self._lastFitting) {
|
||||
console.log('[SCAN] fitting:', data.fitting)
|
||||
self._lastFitting = data.fitting
|
||||
}
|
||||
if (data.fitting === 1 && self._lastRunState === 1 && !self._collecting) {
|
||||
console.log('[SCAN] 贴合确认,开始采集PD数据')
|
||||
self._collecting = true
|
||||
self.setData({ phase: 'collecting' })
|
||||
}
|
||||
}
|
||||
ble.on('fitting', self._onFitting)
|
||||
|
||||
self._onRunState = function (data) {
|
||||
if (self._completed) return
|
||||
if (data.run_state !== self._lastRunState) {
|
||||
console.log('[SCAN] run_state:', data.run_state)
|
||||
self._lastRunState = data.run_state
|
||||
}
|
||||
|
||||
if (self._lastFitting === 1 && data.run_state === 1 && !self._collecting) {
|
||||
console.log('[SCAN] 贴合确认,开始采集PD数据')
|
||||
self._collecting = true
|
||||
self.setData({ phase: 'collecting' })
|
||||
}
|
||||
|
||||
if (self._lastFitting === 0 && data.run_state === 0 && self.data.phase === 'waiting_fit') {
|
||||
console.log('[SCAN] 未贴合,检测失败')
|
||||
self._completed = true
|
||||
self._cleanup()
|
||||
self.setData({ phase: 'ready', error: '面膜未佩戴好,请重新佩戴后再试' })
|
||||
return
|
||||
}
|
||||
|
||||
if (data.run_state === 0 && self._collecting) {
|
||||
console.log('[SCAN] 扫描完成,共', self._frameCount, '帧')
|
||||
self._scanComplete()
|
||||
}
|
||||
}
|
||||
ble.on('run_state', self._onRunState)
|
||||
|
||||
self._onAdc = function (data) {
|
||||
if (self._completed) return
|
||||
if (self._completed || !self._collecting) return
|
||||
if (!data || !data.pd || data.pd.length < 7) return
|
||||
|
||||
self._frameCount++
|
||||
@@ -72,23 +110,6 @@ Page({
|
||||
}
|
||||
ble.on('adc', self._onAdc)
|
||||
|
||||
self._lastRunState = -1
|
||||
self._onRunState = function (data) {
|
||||
if (self._completed) return
|
||||
if (data.run_state !== self._lastRunState) {
|
||||
console.log('[SCAN] run_state:', data.run_state)
|
||||
self._lastRunState = data.run_state
|
||||
}
|
||||
if (data.run_state === 0x01) {
|
||||
self._seenScanning = true
|
||||
}
|
||||
if (data.run_state === 0x00 && self._seenScanning) {
|
||||
console.log('[SCAN] 检测完成,收到', self._frameCount, '帧')
|
||||
self._scanComplete()
|
||||
}
|
||||
}
|
||||
ble.on('run_state', self._onRunState)
|
||||
|
||||
console.log('[SCAN] 发送检测命令 control=0x01')
|
||||
ble.setParams({
|
||||
region_mask: self.data.regions,
|
||||
@@ -97,17 +118,23 @@ Page({
|
||||
hold_time: 0,
|
||||
control: 0x01
|
||||
}).then(function () {
|
||||
console.log('[SCAN] 检测命令发送成功')
|
||||
console.log('[SCAN] 检测命令发送成功,等待贴合...')
|
||||
}).catch(function (err) {
|
||||
console.error('[SCAN] 检测命令发送失败:', err)
|
||||
self._cleanup()
|
||||
self.setData({ state: 'ready', error: '检测命令发送失败' })
|
||||
self.setData({ phase: 'ready', error: '检测命令发送失败' })
|
||||
})
|
||||
|
||||
self._timeoutTimer = setTimeout(function () {
|
||||
if (self._completed) return
|
||||
console.log('[SCAN] 超时,已收到', self._frameCount, '帧')
|
||||
self._scanComplete()
|
||||
console.log('[SCAN] 超时(3分钟),已收到', self._frameCount, '帧')
|
||||
if (self._frameCount > 0) {
|
||||
self._scanComplete()
|
||||
} else {
|
||||
self._completed = true
|
||||
self._cleanup()
|
||||
self.setData({ phase: 'ready', error: '检测超时,未收到数据,请重试' })
|
||||
}
|
||||
}, SCAN_TIMEOUT)
|
||||
},
|
||||
|
||||
@@ -122,19 +149,20 @@ Page({
|
||||
avg.push(Math.round(this._pdSum[i] / count))
|
||||
}
|
||||
|
||||
this.setData({ state: 'done', scanProgress: 100, pdAvg: avg })
|
||||
this.setData({ phase: 'done', pdAvg: avg, frameCount: count })
|
||||
getApp().globalData.lastScanPdAvg = avg
|
||||
console.log('[SCAN] PD平均值:', avg, '帧数:', count)
|
||||
|
||||
this._startTreatment()
|
||||
},
|
||||
|
||||
onNext: function () {
|
||||
_startTreatment: function () {
|
||||
var self = this
|
||||
var mask = self.data.regions || 0x1F
|
||||
var avg = self.data.pdAvg
|
||||
|
||||
var params = self._calculateTreatParams(mask, avg)
|
||||
|
||||
console.log('[SCAN] 发送治疗命令:', JSON.stringify(params))
|
||||
console.log('[SCAN] 自动发送治疗命令:', JSON.stringify(params))
|
||||
ble.setParams(params).then(function () {
|
||||
return ble.startTreatment(mask)
|
||||
}).then(function () {
|
||||
@@ -143,14 +171,13 @@ Page({
|
||||
'&wavelength=2&duration=600000&mode=1'
|
||||
})
|
||||
}).catch(function (err) {
|
||||
wx.showToast({ title: err.error_msg || '启动失败', icon: 'none' })
|
||||
self.setData({ error: err.error_msg || '启动治疗失败' })
|
||||
})
|
||||
},
|
||||
|
||||
_calculateTreatParams: function (mask, pdAvg) {
|
||||
// TODO: 接入完整诊断算法(NR 归一化吸收率 → 匹配光谱+强度)
|
||||
// 需要 PD→区域映射确认后实现
|
||||
// 当前使用统一参数:红光、亮度 200、10 分钟
|
||||
return {
|
||||
region_mask: mask,
|
||||
wavelength: 2,
|
||||
@@ -162,7 +189,7 @@ Page({
|
||||
},
|
||||
|
||||
onUnload: function () {
|
||||
if (this.data.state === 'scanning' && !this._completed) {
|
||||
if (!this._completed && (this.data.phase === 'waiting_fit' || this.data.phase === 'collecting')) {
|
||||
ble.stopTreatment().catch(function () {})
|
||||
}
|
||||
this._cleanup()
|
||||
|
||||
在新工单中引用
屏蔽一个用户