From 515153f134f69fa3cccd0caa80a90ff7e04ec59e Mon Sep 17 00:00:00 2001 From: Guoguo Date: Mon, 29 Jun 2026 05:36:12 -0700 Subject: [PATCH] feat: auto-scan 3-phase state machine with proper PD collection timing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- miniprogram/pages/auto-scan/auto-scan.js | 105 +++++++++++++-------- miniprogram/pages/auto-scan/auto-scan.wxml | 42 ++++----- 2 files changed, 83 insertions(+), 64 deletions(-) diff --git a/miniprogram/pages/auto-scan/auto-scan.js b/miniprogram/pages/auto-scan/auto-scan.js index d0deea6..be3da1b 100644 --- a/miniprogram/pages/auto-scan/auto-scan.js +++ b/miniprogram/pages/auto-scan/auto-scan.js @@ -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() diff --git a/miniprogram/pages/auto-scan/auto-scan.wxml b/miniprogram/pages/auto-scan/auto-scan.wxml index 867dedd..0356801 100644 --- a/miniprogram/pages/auto-scan/auto-scan.wxml +++ b/miniprogram/pages/auto-scan/auto-scan.wxml @@ -2,48 +2,40 @@ ‹ 返回 智能检测 - {{state === 'ready' ? '准备就绪' : state === 'scanning' ? '检测中 (' + frameCount + '帧)...' : '检测完成'}} + {{phase === 'ready' ? '准备就绪' : phase === 'waiting_fit' ? '等待佩戴贴合...' : phase === 'collecting' ? '采集数据中 (' + frameCount + '帧)...' : '检测完成,正在启动...'}} - + 设备已佩戴,点击下方按钮开始智能检测 - 检测过程中请保持设备贴合 + 检测过程约需 2 分钟,请保持设备贴合 - - + + - - - - 正在采集数据,已收到 {{frameCount}} 帧... + 正在检测面膜贴合状态,请确保设备正确佩戴... - - - - PD 平均值 (共 {{frameCount}} 帧) - - - PD{{index + 1}} - {{item}} - - - - - - - 电池电压: {{vbat}}mV ({{battery}}%) + + + + + - + 已贴合,正在采集光谱数据... 已收到 {{frameCount}} 帧 + + + + + 检测完成(共 {{frameCount}} 帧),正在启动治疗... {{error}}