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, '帧')
|
||||
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()
|
||||
|
||||
@@ -2,48 +2,40 @@
|
||||
<view class="page-header page-header-pink" style="padding-top: {{statusBarHeight + 24}}px;">
|
||||
<view class="nav-back" bindtap="onBack">‹ 返回</view>
|
||||
<view class="page-header-title">智能检测</view>
|
||||
<view class="page-header-subtitle">{{state === 'ready' ? '准备就绪' : state === 'scanning' ? '检测中 (' + frameCount + '帧)...' : '检测完成'}}</view>
|
||||
<view class="page-header-subtitle">{{phase === 'ready' ? '准备就绪' : phase === 'waiting_fit' ? '等待佩戴贴合...' : phase === 'collecting' ? '采集数据中 (' + frameCount + '帧)...' : '检测完成,正在启动...'}}</view>
|
||||
</view>
|
||||
|
||||
<view class="page-content">
|
||||
<!-- 待开始 -->
|
||||
<view wx:if="{{state === 'ready'}}">
|
||||
<view wx:if="{{phase === 'ready'}}">
|
||||
<view class="scan-hint">设备已佩戴,点击下方按钮开始智能检测</view>
|
||||
<view class="scan-hint sub-hint">检测过程中请保持设备贴合</view>
|
||||
<view class="scan-hint sub-hint">检测过程约需 2 分钟,请保持设备贴合</view>
|
||||
<button class="btn-primary mt-30" bindtap="onStartScan">开始检测</button>
|
||||
</view>
|
||||
|
||||
<!-- 检测中 -->
|
||||
<view wx:if="{{state === 'scanning'}}">
|
||||
<!-- 等待贴合 -->
|
||||
<view wx:if="{{phase === 'waiting_fit'}}">
|
||||
<view class="scan-container">
|
||||
<view class="face-outline">
|
||||
<view class="scan-line"></view>
|
||||
</view>
|
||||
<view class="scan-progress-bar">
|
||||
<view class="scan-progress-fill" style="width: {{scanProgress}}%"></view>
|
||||
</view>
|
||||
<view class="scan-status">正在采集数据,已收到 {{frameCount}} 帧...</view>
|
||||
</view>
|
||||
<view class="scan-status">正在检测面膜贴合状态,请确保设备正确佩戴...</view>
|
||||
</view>
|
||||
|
||||
<!-- 检测完成 -->
|
||||
<view wx:if="{{state === 'done'}}">
|
||||
<view class="scan-result">
|
||||
<view class="result-title">PD 平均值 (共 {{frameCount}} 帧)</view>
|
||||
<view class="pd-list">
|
||||
<view class="pd-item" wx:for="{{pdAvg}}" wx:key="*this">
|
||||
<view class="pd-region">PD{{index + 1}}</view>
|
||||
<view class="pd-value">{{item}}</view>
|
||||
<view class="pd-bar-bg" wx:if="{{item > 0}}">
|
||||
<view class="pd-bar-fill" style="width: {{item > 4095 ? 100 : item / 40.95}}%"></view>
|
||||
<!-- 采集中 -->
|
||||
<view wx:if="{{phase === 'collecting'}}">
|
||||
<view class="scan-container">
|
||||
<view class="face-outline">
|
||||
<view class="scan-line"></view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="scan-status">已贴合,正在采集光谱数据... 已收到 {{frameCount}} 帧</view>
|
||||
</view>
|
||||
<view class="vbat-info" wx:if="{{vbat > 0}}">
|
||||
电池电压: {{vbat}}mV ({{battery}}%)
|
||||
</view>
|
||||
</view>
|
||||
<button class="btn-primary mt-30" bindtap="onNext">开始使用</button>
|
||||
|
||||
<!-- 完成,自动跳转 -->
|
||||
<view wx:if="{{phase === 'done'}}">
|
||||
<view class="scan-status">检测完成(共 {{frameCount}} 帧),正在启动治疗...</view>
|
||||
</view>
|
||||
|
||||
<view class="scan-error" wx:if="{{error}}">{{error}}</view>
|
||||
|
||||
在新工单中引用
屏蔽一个用户