diff --git a/miniprogram/pages/auto-scan/auto-scan.js b/miniprogram/pages/auto-scan/auto-scan.js index ea824c4..d38dc41 100644 --- a/miniprogram/pages/auto-scan/auto-scan.js +++ b/miniprogram/pages/auto-scan/auto-scan.js @@ -1,6 +1,6 @@ var ble = require('../../services/ble') var SCAN_BRIGHTNESS = 128 -var SCAN_TIMEOUT = 8000 +var SCAN_TIMEOUT = 12000 Page({ data: { @@ -32,8 +32,20 @@ Page({ this.startScan() }, + _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 } + }, + startScan: function () { var self = this + + if (!ble.isConnected()) { + self.setData({ scanning: false, error: '设备未连接,请先连接设备' }) + return + } + self.setData({ scanning: true, scanProgress: 0, error: '', pdValues: [] }) this._progressTimer = setInterval(function () { @@ -44,8 +56,7 @@ Page({ this._onAdc = function (data) { console.log('[SCAN] 收到ADC数据:', JSON.stringify(data)) - clearInterval(self._progressTimer) - clearTimeout(self._timeoutTimer) + self._cleanup() self.setData({ scanning: false, scanProgress: 100 }) self._handleAdcData(data) } @@ -61,13 +72,14 @@ Page({ console.log('[SCAN] 扫描指令发送成功,等待ADC回传...') }).catch(function (err) { console.error('[SCAN] 扫描指令发送失败:', err) - self.setData({ error: '扫描指令发送失败' }) + self._cleanup() + self.setData({ scanning: false, scanProgress: 0, error: '扫描指令发送失败' }) }) this._timeoutTimer = setTimeout(function () { - clearInterval(self._progressTimer) if (!self.data.scanning) return console.log('[SCAN] 等待ADC超时,使用默认值') + self._cleanup() self.setData({ scanning: false, scanProgress: 100 }) self._handleAdcData(null) }, SCAN_TIMEOUT) @@ -80,16 +92,17 @@ Page({ if (data && data.pd && data.pd.length >= 7) { this.setData({ pdRaw: data.pd, vbat: data.vbat || 0, battery: data.battery || 0 }) for (var i = 0; i < 5; i++) { - pdValues.push({ region: regionNames[i], pd: data.pd[i], pdText: String(data.pd[i]) }) + var barW = Math.min(100, Math.round(data.pd[i] / 40.95)) + pdValues.push({ region: regionNames[i], pd: data.pd[i], pdText: String(data.pd[i]), barWidth: barW }) } - pdValues.push({ region: 'PD6', pd: data.pd[5], pdText: String(data.pd[5]) }) - pdValues.push({ region: 'PD7', pd: data.pd[6], pdText: String(data.pd[6]) }) + pdValues.push({ region: 'PD6', pd: data.pd[5], pdText: String(data.pd[5]), barWidth: Math.min(100, Math.round(data.pd[5] / 40.95)) }) + pdValues.push({ region: 'PD7', pd: data.pd[6], pdText: String(data.pd[6]), barWidth: Math.min(100, Math.round(data.pd[6] / 40.95)) }) getApp().globalData.lastScanPd = data.pd getApp().globalData.lastScanVbat = data.vbat } else { for (var j = 0; j < 5; j++) { - pdValues.push({ region: regionNames[j], pd: 0, pdText: '未采集' }) + pdValues.push({ region: regionNames[j], pd: 0, pdText: '未采集', barWidth: 0 }) } } @@ -97,9 +110,7 @@ Page({ }, onUnload: function () { - if (this._progressTimer) clearInterval(this._progressTimer) - if (this._timeoutTimer) clearTimeout(this._timeoutTimer) - if (this._onAdc) ble.off('adc', this._onAdc) + this._cleanup() ble.stopTreatment().catch(function () {}) }, diff --git a/miniprogram/pages/auto-scan/auto-scan.wxml b/miniprogram/pages/auto-scan/auto-scan.wxml index 0b3bbfc..5a3937e 100644 --- a/miniprogram/pages/auto-scan/auto-scan.wxml +++ b/miniprogram/pages/auto-scan/auto-scan.wxml @@ -23,7 +23,7 @@ {{item.region}} {{item.pdText}} - + @@ -32,7 +32,7 @@ - {{error}} + {{error}}