From 0498e124b394d6e2592415220cba948bdadff1f2 Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Thu, 9 Apr 2026 11:59:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(weight):=20=E4=BF=AE=E5=A4=8D=E7=A7=B0?= =?UTF-8?q?=E9=87=8D=E9=80=BB=E8=BE=91=E5=92=8C=E6=94=AF=E4=BB=98=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E7=9B=B8=E5=85=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 WEIGHT_CHANGE_VALUE 从 25 修改为 20 - 添加 currentWeight 变量用于准确记录当前称重值 - 临时修改 settlementMode 为独立支付模式用于测试 - 更新日志输出以更好地跟踪称重变化 - 修复即放即取和余量计量模式下的称重处理逻辑 - 添加食物识别状态控制变量 foodRecognizeState - 在不同模式下正确设置 eatWeight 和 foodWeight 计算方式 - 优化余量计量模式下的状态重置逻辑 - 添加倒计时功能用于余量计量模式状态重置 - 修复营养计算相关的日志输出格式 --- .../sw/dualscreen/activity/MainActivity.kt | 83 ++++++++++++------- .../presentation/MainScreenPresentation.kt | 61 +++++++------- 2 files changed, 85 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt index 7df5e9e..dc997e8 100644 --- a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt +++ b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt @@ -100,7 +100,7 @@ class MainActivity : BaseActivity() { private const val TAG = "MainActivity" const val TIME_OUT = 5 * 60 * 1000L - const val WEIGHT_CHANGE_VALUE = 25 + const val WEIGHT_CHANGE_VALUE = 20 const val WEIGHT_RESET_RECOGNIZE = 10 } @@ -152,6 +152,7 @@ class MainActivity : BaseActivity() { private var canIdentify: Boolean = false private var bottomSheetDialog: CustomBottomSheetDialog? = null private var lastWeight: Int = 0 + private var currentWeight: Int = 0 private var lastPhotoUri: Uri? = null // 最后拍照的图片 private var debouncer = Debouncer(2000) private var isRecognitionFood = true @@ -200,6 +201,9 @@ class MainActivity : BaseActivity() { } //结算模式:1-独立支付,2-联合结算 settlementMode = if (deviceConfig.payType == 2) 0 else 1 + // TODO: 临时修改为独立支付模式用于测试-------------------------- + settlementMode = 1 + // TODO: 临时修改为独立支付模式用于测试-------------------------- SPUtil.getInstance().put(GlobalKey.KEY_SETTLEMENT_MODE, settlementMode) GlobalData.appId = deviceConfig.arcsoftAppId ?: "" @@ -284,8 +288,8 @@ class MainActivity : BaseActivity() { return } pauseAnalysis() - recognizeWeight = lastWeight - log("registerDataChange,副屏updateFood调用前耗时:${System.currentTimeMillis() - startTime}") + recognizeWeight = currentWeight + log("registerDataChange,副屏updateFood调用前耗时:${System.currentTimeMillis() - startTime}, recognizeWeight = ${recognizeWeight}") startTime = System.currentTimeMillis() presentation?.updateFood(foodInfo) binding.tvFoodName.text = foodInfo.foodName @@ -343,7 +347,6 @@ class MainActivity : BaseActivity() { log("registerDataChange weight 联合支付-余量计量,重量小于${WEIGHT_RESET_RECOGNIZE}g,step1FoodRecognizing") //秤上重量过小,显示识别中页面 presentation?.step1FoodRecognizing() - lastWeight = weight return } log("registerDataChange weight 联合支付-余量计量,订单完成状态:${presentation!!.jointPaymentQueryFinish}") @@ -351,6 +354,7 @@ class MainActivity : BaseActivity() { if (presentation!!.jointPaymentQueryFinish) { log("registerDataChange weight 联合支付-余量计量,updateWeight") presentation?.updateWeight(weight) + log("registerDataChange weight 重置lastWeight与weigh:${weight}一致----------------1000006") lastWeight = weight return } @@ -359,7 +363,8 @@ class MainActivity : BaseActivity() { } private fun readWeight(weight: Int) { - log("registerDataChange weight = $weight") + currentWeight = weight + log("registerDataChange weight = $weight, lastWeight = $lastWeight") binding.tvShowWeight.let { if (it.text.toString() != weight.toString()) { it.text = "$weight" @@ -381,18 +386,21 @@ class MainActivity : BaseActivity() { loadJFJQBySettlement(weight) return } - - if (weight <= WEIGHT_RESET_RECOGNIZE) { - log("registerDataChange weight 秤重量小于${WEIGHT_RESET_RECOGNIZE}g") - //检测到秤上没有东西 - checkedItem = null - } //独立支付------------------------------------------- - if (lastWeight - weight > 5) { - //及放及取,从秤上拿走超过5克 - if (presentation?.mealPickupMode == 0) { - presentation?.updateWeight(weight) - lastWeight = weight + //0-即放即取,1-余量计量 + val mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0 + //0-计费,1-不计费 + val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0) + if(mealPickupMode == 0 && chargeMode == 1) { + //即放即取+不计费模式, + if (presentation?.currentUserId.isNullOrBlank().not()) { + //已识别人脸的状态下,从秤上拿餐品后,不再读取数据,等待人脸离开提交订单 + return + } + if (weight <= WEIGHT_RESET_RECOGNIZE) { + log("registerDataChange weight 秤重量小于${WEIGHT_RESET_RECOGNIZE}g") + //检测到秤上没有东西 + presentation?.step1FoodRecognizing() return } } @@ -411,6 +419,7 @@ class MainActivity : BaseActivity() { super.registerDataChange() SensorScaleUtils.addWeightListener { value -> runOnUiThread { + if (foodRecognizeState.not()) return@runOnUiThread readWeight(value) } } @@ -426,7 +435,7 @@ class MainActivity : BaseActivity() { if (weight <= WEIGHT_RESET_RECOGNIZE) { log("registerDataChange weight recognizeByWeight---00002,秤重未超过${WEIGHT_RESET_RECOGNIZE}克") - //检测到秤上没有东西,重新启动识别菜品 && presentation?.mealPickupMode == 1 + //检测到秤上没有东西,重新启动识别菜品 isRecognitionFood = true return } @@ -439,11 +448,9 @@ class MainActivity : BaseActivity() { } debouncer.debounce { log("registerDataChange weight recognizeByWeight---00004--recognizeFood") + //保存当前重量 + currentWeight = weight recognizeFood() - if (presentation?.mealPickupMode == 0) { - log("registerDataChange weight recognizeByWeight---00004--recognizeFood,mealPickupMode == 0") - lastWeight = weight - } } } else { log("registerDataChange weight recognizeByWeight---block") @@ -457,10 +464,10 @@ class MainActivity : BaseActivity() { recognizeFood() } } - lastWeight = weight } } + private var startTime = 0L fun recognizeFood() { @@ -500,18 +507,27 @@ class MainActivity : BaseActivity() { */ private var failCount = 0 + /** + * 是否进行识别:true-识别,false-不识别,默认开启识别 + */ + private var foodRecognizeState = true + @SuppressLint("NotifyDataSetChanged") private fun updateFoodInfo(list: MutableList, scoreList: List) { val queryData = GsonUtils.toJson(list) val recData = GsonUtils.toJson(scoreList) log("registerDataChange识别后查询接口数据:$queryData,识别数据:$recData") if (list.isEmpty()) { + foodRecognizeState = false binding.tvToSearch.let { it.text = "未查询到,手动搜索" it.visible() } return } + foodRecognizeState = true + //查询到菜品信息,同步设置lastWeight=currentWeight + lastWeight=currentWeight list.forEach { foodInfo -> val scoreItem = scoreList.firstOrNull { it.name == foodInfo.foodName } val score = scoreItem?.score ?: 0.0 @@ -526,7 +542,6 @@ class MainActivity : BaseActivity() { // list // } - adapter.submitList(list) binding.recyclerview.visible() @@ -720,6 +735,8 @@ class MainActivity : BaseActivity() { binding.previewView.visible() binding.ivImg.gone() checkedItem = null + lastWeight = 0 + foodRecognizeState = true } private fun updateDateTime() { @@ -886,8 +903,13 @@ class MainActivity : BaseActivity() { } fun getEatNum(realWeight: Double?, specWeight: Double?): Int { - if (realWeight == null || realWeight <= 0.0 || specWeight == null || specWeight <= 0.0) return 0 - return (realWeight / specWeight).roundToInt() + if (realWeight == null || realWeight <= 0.0 || specWeight == null || specWeight <= 0.0) { + log("getEatNum:realWeight=$realWeight,specWeight=$specWeight") + return 0 + } + val eatNum = (realWeight / specWeight).roundToInt() + log("getEatNum:eatNum=$eatNum,realWeight=$realWeight,specWeight=$specWeight,realWeight/$specWeight=${realWeight / specWeight}}") + return eatNum; } private var foodOrderId: String = "" @@ -955,6 +977,7 @@ class MainActivity : BaseActivity() { // } private fun clickPayButton() { + log("eatWeight:recognizeWeight=$recognizeWeight, currentWeight=$currentWeight, lastWeight=$lastWeight") if (checkedItem == null) { ToastUtils.showToast("暂无识别数据,请搜索选择") return @@ -971,13 +994,13 @@ class MainActivity : BaseActivity() { //计费 if (mealPickupMode == 0) { //即放即取 - eatWeight = lastWeight - foodWeight = lastWeight + eatWeight = currentWeight + foodWeight = currentWeight } else { //余量计量 - eatWeight = recognizeWeight - lastWeight + eatWeight = recognizeWeight - currentWeight foodWeight = recognizeWeight - log("eatWeight=$eatWeight, recognizeWeight=$recognizeWeight, lastWeight=$lastWeight") + log("eatWeight=$eatWeight, recognizeWeight=$recognizeWeight, currentWeight=$currentWeight, lastWeight=$lastWeight") } } else { //不计费 @@ -987,7 +1010,7 @@ class MainActivity : BaseActivity() { recognizeWeight } else { //余量计量 - recognizeWeight - lastWeight + recognizeWeight - currentWeight } } val eatNum = getEatNum(eatWeight.toDouble(), checkedItem!!.specWeight) diff --git a/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt b/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt index 12b4a5c..767d74a 100644 --- a/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt +++ b/app/src/main/java/com/sw/dualscreen/presentation/MainScreenPresentation.kt @@ -43,6 +43,7 @@ import com.sw.dualscreen.model.response.UserEnergy import com.sw.dualscreen.model.response.UserNutrition import com.sw.dualscreen.utils.Debouncer import com.sw.dualscreen.utils.SPUtil +import com.sw.dualscreen.utils.countDownByFlow import com.sw.dualscreen.viewmodel.UserViewModel import com.sw.plate.utils.LightManager import com.sw.plate.utils.ToastUtils @@ -59,6 +60,7 @@ import com.sw.plate.utils.arcface.face.model.CompareResult import com.sw.plate.utils.arcface.face.model.FacePreviewInfo import com.sw.plate.utils.arcface.face.model.RecognizeConfiguration import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.drop import kotlinx.coroutines.launch import timber.log.Timber @@ -75,7 +77,7 @@ class MainScreenPresentation( ) : Presentation(activity, display), ViewTreeObserver.OnGlobalLayoutListener { companion object { - const val TAG = "SubScreenPresentation" + const val TAG = "MainScreenPresentation" } private lateinit var binding: PresentationMainScreenBinding @@ -144,55 +146,55 @@ class MainScreenPresentation( mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0 //0-计费,1-不计费 val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0) -// if (mealPickupMode == 1 && chargeMode == 0) { -// //余量计量+计费模式 -// if (isFirstGetWeight && lastWeight > 5) { -// recognitionWeight = lastWeight -// isFirstGetWeight = false -// } -// if (lastWeight > 5) { -// //余量计量,有重量的情况下不回到重新识别状态 -// return -// } -// isFirstGetWeight = true -// } if (currentStep == 2 && lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE && isGoStep1) { //人脸识别过程中,秤上物品拿走 isGoStep1 = false activity.runOnUiThread { - Timber.tag(TAG).e("切回step1") + Timber.tag(TAG).e("切回step1,------2000000000000") step1FoodRecognizing() } } else if (lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE) { isGoStep1 = false activity.runOnUiThread { - Timber.tag(TAG).e("切回step1") + Timber.tag(TAG).e("切回step1,------2000000000001") if (mealPickupMode == 0) { //即放即取 step1FoodRecognizing() } else { - //秤上菜品不足,请补充 + if (currentStep == 1 && currentFood == null) { + return@runOnUiThread + } + //余量计量 + if (countDownResetJob == null) { + countDownResetJob = countDownByFlow( + total = 30, + scope = activity.lifecycleScope, + onStart = {}, + onTick = { seconds -> + Timber.tag(TAG).e("余量计量模型秤重量已清空,等待${seconds}秒后继续重置识别状态") + if (lastWeight > MainActivity.WEIGHT_RESET_RECOGNIZE) { + countDownResetJob?.cancel() + } + }, + onFinish = { + step1FoodRecognizing() + countDownResetJob?.cancel() + } + ) + } } } } -// if (currentStep != 3) { -// return -// } if (currentFood == null) { Timber.tag(TAG).e("updateWeight currentFood is null") return } - //if (userNutritionData == null) { if (userNutrition == null) { Timber.tag(TAG).e("updateWeight userNutritionData is null") return } -// if (dinnerTypeInfo == null) { -// Timber.tag(TAG).e("updateWeight dinnerType is null") -// return -// } if (chargeMode == 1 || activity.settlementMode == 0) { //不计费或者联合支付模式更新营养热量数据 if (mealPickupMode == 0) { @@ -205,6 +207,8 @@ class MainScreenPresentation( } } + private var countDownResetJob: Job? = null + fun setStepChangeCallback(callback: (Int) -> Unit) { stepChangeCallback = callback } @@ -518,7 +522,7 @@ class MainScreenPresentation( * 餐品识别中 */ fun step1FoodRecognizing() { - if (currentStep == 1) { + if (currentStep == 1 && currentFood == null) { return } Timber.tag(TAG).d("main,step1,耗时:${System.currentTimeMillis() - startTime}") @@ -764,10 +768,7 @@ class MainScreenPresentation( } private fun calculateNutrition(value: Int) { - Log.d( - TAG, - "addFoodToOrder: weight = $value, recognitionWeight = $recognitionWeight foodInfo = $currentFood" - ) + Timber.tag(TAG) .d("calculateNutrition:weight=$value,recognitionWeight=$recognitionWeight,foodInfo=$currentFood") if (currentFood == null || userNutrition == null) { return } @@ -906,6 +907,7 @@ class MainScreenPresentation( */ fun updateMealPickupMode(mode: Int) { Timber.tag(TAG).d("updateMealPickupMode mode = $mode") + Timber.tag(TAG).e("切回step1,------2000000000003") mealPickupMode = mode step1FoodRecognizing() } @@ -1359,6 +1361,7 @@ class MainScreenPresentation( } } else { Timber.tag(TAG).d("drawPreviewInfo,successBlock,订单已生成,step1FoodRecognizing") + Timber.tag(TAG).e("切回step1,------2000000000002") activity.isAnalyzing = false step1FoodRecognizing() }