fix: address audit findings for Phase 1 scan
- Extract _cleanup() to properly unregister ADC listener + clear timers - ADC callback unregisters itself on first receive (prevent duplicates) - setParams failure now stops scanning and clears all state - Timeout handler unregisters ADC listener - Add BLE connection check before scan - SCAN_TIMEOUT 8s → 12s (must exceed hold_time 10s) - Pre-compute barWidth in JS instead of float math in WXML - Error only shows when scanning is false
这个提交包含在:
@@ -1,6 +1,6 @@
|
|||||||
var ble = require('../../services/ble')
|
var ble = require('../../services/ble')
|
||||||
var SCAN_BRIGHTNESS = 128
|
var SCAN_BRIGHTNESS = 128
|
||||||
var SCAN_TIMEOUT = 8000
|
var SCAN_TIMEOUT = 12000
|
||||||
|
|
||||||
Page({
|
Page({
|
||||||
data: {
|
data: {
|
||||||
@@ -32,8 +32,20 @@ Page({
|
|||||||
this.startScan()
|
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 () {
|
startScan: function () {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
|
if (!ble.isConnected()) {
|
||||||
|
self.setData({ scanning: false, error: '设备未连接,请先连接设备' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
self.setData({ scanning: true, scanProgress: 0, error: '', pdValues: [] })
|
self.setData({ scanning: true, scanProgress: 0, error: '', pdValues: [] })
|
||||||
|
|
||||||
this._progressTimer = setInterval(function () {
|
this._progressTimer = setInterval(function () {
|
||||||
@@ -44,8 +56,7 @@ Page({
|
|||||||
|
|
||||||
this._onAdc = function (data) {
|
this._onAdc = function (data) {
|
||||||
console.log('[SCAN] 收到ADC数据:', JSON.stringify(data))
|
console.log('[SCAN] 收到ADC数据:', JSON.stringify(data))
|
||||||
clearInterval(self._progressTimer)
|
self._cleanup()
|
||||||
clearTimeout(self._timeoutTimer)
|
|
||||||
self.setData({ scanning: false, scanProgress: 100 })
|
self.setData({ scanning: false, scanProgress: 100 })
|
||||||
self._handleAdcData(data)
|
self._handleAdcData(data)
|
||||||
}
|
}
|
||||||
@@ -61,13 +72,14 @@ Page({
|
|||||||
console.log('[SCAN] 扫描指令发送成功,等待ADC回传...')
|
console.log('[SCAN] 扫描指令发送成功,等待ADC回传...')
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
console.error('[SCAN] 扫描指令发送失败:', err)
|
console.error('[SCAN] 扫描指令发送失败:', err)
|
||||||
self.setData({ error: '扫描指令发送失败' })
|
self._cleanup()
|
||||||
|
self.setData({ scanning: false, scanProgress: 0, error: '扫描指令发送失败' })
|
||||||
})
|
})
|
||||||
|
|
||||||
this._timeoutTimer = setTimeout(function () {
|
this._timeoutTimer = setTimeout(function () {
|
||||||
clearInterval(self._progressTimer)
|
|
||||||
if (!self.data.scanning) return
|
if (!self.data.scanning) return
|
||||||
console.log('[SCAN] 等待ADC超时,使用默认值')
|
console.log('[SCAN] 等待ADC超时,使用默认值')
|
||||||
|
self._cleanup()
|
||||||
self.setData({ scanning: false, scanProgress: 100 })
|
self.setData({ scanning: false, scanProgress: 100 })
|
||||||
self._handleAdcData(null)
|
self._handleAdcData(null)
|
||||||
}, SCAN_TIMEOUT)
|
}, SCAN_TIMEOUT)
|
||||||
@@ -80,16 +92,17 @@ Page({
|
|||||||
if (data && data.pd && data.pd.length >= 7) {
|
if (data && data.pd && data.pd.length >= 7) {
|
||||||
this.setData({ pdRaw: data.pd, vbat: data.vbat || 0, battery: data.battery || 0 })
|
this.setData({ pdRaw: data.pd, vbat: data.vbat || 0, battery: data.battery || 0 })
|
||||||
for (var i = 0; i < 5; i++) {
|
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: '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]) })
|
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.lastScanPd = data.pd
|
||||||
getApp().globalData.lastScanVbat = data.vbat
|
getApp().globalData.lastScanVbat = data.vbat
|
||||||
} else {
|
} else {
|
||||||
for (var j = 0; j < 5; j++) {
|
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 () {
|
onUnload: function () {
|
||||||
if (this._progressTimer) clearInterval(this._progressTimer)
|
this._cleanup()
|
||||||
if (this._timeoutTimer) clearTimeout(this._timeoutTimer)
|
|
||||||
if (this._onAdc) ble.off('adc', this._onAdc)
|
|
||||||
ble.stopTreatment().catch(function () {})
|
ble.stopTreatment().catch(function () {})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
<view class="pd-region">{{item.region}}</view>
|
<view class="pd-region">{{item.region}}</view>
|
||||||
<view class="pd-value {{item.pd > 0 ? '' : 'pd-empty'}}">{{item.pdText}}</view>
|
<view class="pd-value {{item.pd > 0 ? '' : 'pd-empty'}}">{{item.pdText}}</view>
|
||||||
<view class="pd-bar-bg" wx:if="{{item.pd > 0}}">
|
<view class="pd-bar-bg" wx:if="{{item.pd > 0}}">
|
||||||
<view class="pd-bar-fill" style="width: {{item.pd > 4095 ? 100 : item.pd / 40.95}}%"></view>
|
<view class="pd-bar-fill" style="width: {{item.barWidth}}%"></view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="scan-error" wx:if="{{error}}">{{error}}</view>
|
<view class="scan-error" wx:if="{{!scanning && error}}">{{error}}</view>
|
||||||
|
|
||||||
<button class="btn-primary mt-30" wx:if="{{!scanning}}" bindtap="onNext">下一步</button>
|
<button class="btn-primary mt-30" wx:if="{{!scanning}}" bindtap="onNext">下一步</button>
|
||||||
</view>
|
</view>
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户