fix(recognition): 防止识别时重复触发导致多次并发处理
- 新增 isRecognizing 标记防止识别过程重复进入 - 在识别开始时设置标记,结束时释放标记 - 多处识别终止和异常处理处均释放识别锁 - 在重量回调中增加去重逻辑,避免同一重量重复触发回调 - 更新 lastCallbackWeight,确保仅在重量变化时触发回调
This commit is contained in:
@@ -178,6 +178,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
private var lastPhotoUri: Uri? = null // 最后拍照的图片
|
||||
private var debouncer = Debouncer(2000)
|
||||
private var isRecognitionFood = true
|
||||
private var isRecognizing = false
|
||||
private var isFirstOpen = true
|
||||
|
||||
private var isStartRecognize = false
|
||||
@@ -520,6 +521,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
private var startTime = 0L
|
||||
|
||||
fun recognizeFood() {
|
||||
// 防重入:识别进行中则跳过,避免重量变化触发多次并发识别
|
||||
if (isRecognizing) {
|
||||
log("recognizeFood 已在识别中,跳过重复调用")
|
||||
return
|
||||
}
|
||||
isRecognizing = true
|
||||
// 立即更新基准重量,缩小后续重量变化触发识别的窗口
|
||||
lastWeight = currentWeight
|
||||
|
||||
startTime = System.currentTimeMillis()
|
||||
// LightManager.openRedLight()
|
||||
LightManager.closeRedLight()
|
||||
@@ -545,6 +555,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
shutdownCamera()
|
||||
setupCamera()
|
||||
|
||||
// 释放识别锁,允许递归重试时重新获取
|
||||
isRecognizing = false
|
||||
binding.root.postDelayed({
|
||||
recognizeFood()
|
||||
}, 1000)
|
||||
@@ -568,6 +580,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
log("registerDataChange识别后查询接口数据:$queryData,识别数据:$recData")
|
||||
if (list.isEmpty()) {
|
||||
foodRecognizeState = false
|
||||
isRecognizing = false
|
||||
tvToSearch.let {
|
||||
it.text = "未查询到,手动搜索"
|
||||
it.visible()
|
||||
@@ -575,6 +588,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
return
|
||||
}
|
||||
foodRecognizeState = true
|
||||
isRecognizing = false
|
||||
//查询到菜品信息,同步设置lastWeight=currentWeight
|
||||
lastWeight = currentWeight
|
||||
presentation?.updateWeight(currentWeight)
|
||||
@@ -790,6 +804,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
checkedItem = null
|
||||
lastWeight = 0
|
||||
foodRecognizeState = true
|
||||
isRecognizing = false
|
||||
isSwitchFood = false
|
||||
}
|
||||
|
||||
@@ -1358,6 +1373,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
//binding.layoutRescan.visibility = View.VISIBLE
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
isRecognizing = false
|
||||
tvToSearch.let {
|
||||
it.text = "未识别到,手动搜索"
|
||||
it.visible()
|
||||
|
||||
@@ -24,6 +24,7 @@ object SensorScaleUtils {
|
||||
var isZero = false
|
||||
private var callback: Callback? = {}
|
||||
private var lastWeight: Double? = null
|
||||
private var lastCallbackWeight: Double? = null
|
||||
|
||||
private fun init() {
|
||||
mSensorScale = SensorScale(object : OnScaleResult {
|
||||
@@ -45,9 +46,13 @@ object SensorScaleUtils {
|
||||
callback?.invoke(value)
|
||||
}
|
||||
|
||||
weightCallbackList.forEach {
|
||||
val currentWeight = value * 1000
|
||||
it.invoke(currentWeight.roundToInt())
|
||||
// 去重:仅在重量变化时回调 weightCallbackList,避免同一重量反复触发
|
||||
if (lastCallbackWeight != value) {
|
||||
lastCallbackWeight = value
|
||||
weightCallbackList.forEach {
|
||||
val currentWeight = value * 1000
|
||||
it.invoke(currentWeight.roundToInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user