fix(weight): 修复称重逻辑和支付模式相关问题

- 将 WEIGHT_CHANGE_VALUE 从 25 修改为 20
- 添加 currentWeight 变量用于准确记录当前称重值
- 临时修改 settlementMode 为独立支付模式用于测试
- 更新日志输出以更好地跟踪称重变化
- 修复即放即取和余量计量模式下的称重处理逻辑
- 添加食物识别状态控制变量 foodRecognizeState
- 在不同模式下正确设置 eatWeight 和 foodWeight 计算方式
- 优化余量计量模式下的状态重置逻辑
- 添加倒计时功能用于余量计量模式状态重置
- 修复营养计算相关的日志输出格式
This commit is contained in:
2026-04-09 11:59:25 +08:00
parent 30b2b178a9
commit 0498e124b3
2 changed files with 85 additions and 59 deletions
@@ -100,7 +100,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private const val TAG = "MainActivity" private const val TAG = "MainActivity"
const val TIME_OUT = 5 * 60 * 1000L 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 const val WEIGHT_RESET_RECOGNIZE = 10
} }
@@ -152,6 +152,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private var canIdentify: Boolean = false private var canIdentify: Boolean = false
private var bottomSheetDialog: CustomBottomSheetDialog? = null private var bottomSheetDialog: CustomBottomSheetDialog? = null
private var lastWeight: Int = 0 private var lastWeight: Int = 0
private var currentWeight: Int = 0
private var lastPhotoUri: Uri? = null // 最后拍照的图片 private var lastPhotoUri: Uri? = null // 最后拍照的图片
private var debouncer = Debouncer(2000) private var debouncer = Debouncer(2000)
private var isRecognitionFood = true private var isRecognitionFood = true
@@ -200,6 +201,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
} }
//结算模式:1-独立支付,2-联合结算 //结算模式:1-独立支付,2-联合结算
settlementMode = if (deviceConfig.payType == 2) 0 else 1 settlementMode = if (deviceConfig.payType == 2) 0 else 1
// TODO: 临时修改为独立支付模式用于测试--------------------------
settlementMode = 1
// TODO: 临时修改为独立支付模式用于测试--------------------------
SPUtil.getInstance().put(GlobalKey.KEY_SETTLEMENT_MODE, settlementMode) SPUtil.getInstance().put(GlobalKey.KEY_SETTLEMENT_MODE, settlementMode)
GlobalData.appId = deviceConfig.arcsoftAppId ?: "" GlobalData.appId = deviceConfig.arcsoftAppId ?: ""
@@ -284,8 +288,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
return return
} }
pauseAnalysis() pauseAnalysis()
recognizeWeight = lastWeight recognizeWeight = currentWeight
log("registerDataChange,副屏updateFood调用前耗时:${System.currentTimeMillis() - startTime}") log("registerDataChange,副屏updateFood调用前耗时:${System.currentTimeMillis() - startTime}, recognizeWeight = ${recognizeWeight}")
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
presentation?.updateFood(foodInfo) presentation?.updateFood(foodInfo)
binding.tvFoodName.text = foodInfo.foodName binding.tvFoodName.text = foodInfo.foodName
@@ -343,7 +347,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
log("registerDataChange weight 联合支付-余量计量,重量小于${WEIGHT_RESET_RECOGNIZE}g,step1FoodRecognizing") log("registerDataChange weight 联合支付-余量计量,重量小于${WEIGHT_RESET_RECOGNIZE}g,step1FoodRecognizing")
//秤上重量过小,显示识别中页面 //秤上重量过小,显示识别中页面
presentation?.step1FoodRecognizing() presentation?.step1FoodRecognizing()
lastWeight = weight
return return
} }
log("registerDataChange weight 联合支付-余量计量,订单完成状态:${presentation!!.jointPaymentQueryFinish}") log("registerDataChange weight 联合支付-余量计量,订单完成状态:${presentation!!.jointPaymentQueryFinish}")
@@ -351,6 +354,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (presentation!!.jointPaymentQueryFinish) { if (presentation!!.jointPaymentQueryFinish) {
log("registerDataChange weight 联合支付-余量计量,updateWeight") log("registerDataChange weight 联合支付-余量计量,updateWeight")
presentation?.updateWeight(weight) presentation?.updateWeight(weight)
log("registerDataChange weight 重置lastWeight与weigh${weight}一致----------------1000006")
lastWeight = weight lastWeight = weight
return return
} }
@@ -359,7 +363,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
} }
private fun readWeight(weight: Int) { private fun readWeight(weight: Int) {
log("registerDataChange weight = $weight") currentWeight = weight
log("registerDataChange weight = $weight lastWeight = $lastWeight")
binding.tvShowWeight.let { binding.tvShowWeight.let {
if (it.text.toString() != weight.toString()) { if (it.text.toString() != weight.toString()) {
it.text = "$weight" it.text = "$weight"
@@ -381,18 +386,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
loadJFJQBySettlement(weight) loadJFJQBySettlement(weight)
return return
} }
if (weight <= WEIGHT_RESET_RECOGNIZE) {
log("registerDataChange weight 秤重量小于${WEIGHT_RESET_RECOGNIZE}g")
//检测到秤上没有东西
checkedItem = null
}
//独立支付------------------------------------------- //独立支付-------------------------------------------
if (lastWeight - weight > 5) { //0-即放即取,1-余量计量
//及放及取,从秤上拿走超过5克 val mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
if (presentation?.mealPickupMode == 0) { //0-计费,1-不计费
presentation?.updateWeight(weight) val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
lastWeight = weight 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 return
} }
} }
@@ -411,6 +419,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
super.registerDataChange() super.registerDataChange()
SensorScaleUtils.addWeightListener { value -> SensorScaleUtils.addWeightListener { value ->
runOnUiThread { runOnUiThread {
if (foodRecognizeState.not()) return@runOnUiThread
readWeight(value) readWeight(value)
} }
} }
@@ -426,7 +435,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (weight <= WEIGHT_RESET_RECOGNIZE) { if (weight <= WEIGHT_RESET_RECOGNIZE) {
log("registerDataChange weight recognizeByWeight---00002,秤重未超过${WEIGHT_RESET_RECOGNIZE}") log("registerDataChange weight recognizeByWeight---00002,秤重未超过${WEIGHT_RESET_RECOGNIZE}")
//检测到秤上没有东西,重新启动识别菜品 && presentation?.mealPickupMode == 1 //检测到秤上没有东西,重新启动识别菜品
isRecognitionFood = true isRecognitionFood = true
return return
} }
@@ -439,11 +448,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
} }
debouncer.debounce { debouncer.debounce {
log("registerDataChange weight recognizeByWeight---00004--recognizeFood") log("registerDataChange weight recognizeByWeight---00004--recognizeFood")
//保存当前重量
currentWeight = weight
recognizeFood() recognizeFood()
if (presentation?.mealPickupMode == 0) {
log("registerDataChange weight recognizeByWeight---00004--recognizeFoodmealPickupMode == 0")
lastWeight = weight
}
} }
} else { } else {
log("registerDataChange weight recognizeByWeight---block") log("registerDataChange weight recognizeByWeight---block")
@@ -457,10 +464,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
recognizeFood() recognizeFood()
} }
} }
lastWeight = weight
} }
} }
private var startTime = 0L private var startTime = 0L
fun recognizeFood() { fun recognizeFood() {
@@ -500,18 +507,27 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
*/ */
private var failCount = 0 private var failCount = 0
/**
* 是否进行识别:true-识别,false-不识别,默认开启识别
*/
private var foodRecognizeState = true
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
private fun updateFoodInfo(list: MutableList<FoodInfo>, scoreList: List<IdNameScore>) { private fun updateFoodInfo(list: MutableList<FoodInfo>, scoreList: List<IdNameScore>) {
val queryData = GsonUtils.toJson(list) val queryData = GsonUtils.toJson(list)
val recData = GsonUtils.toJson(scoreList) val recData = GsonUtils.toJson(scoreList)
log("registerDataChange识别后查询接口数据:$queryData,识别数据:$recData") log("registerDataChange识别后查询接口数据:$queryData,识别数据:$recData")
if (list.isEmpty()) { if (list.isEmpty()) {
foodRecognizeState = false
binding.tvToSearch.let { binding.tvToSearch.let {
it.text = "未查询到,手动搜索" it.text = "未查询到,手动搜索"
it.visible() it.visible()
} }
return return
} }
foodRecognizeState = true
//查询到菜品信息,同步设置lastWeight=currentWeight
lastWeight=currentWeight
list.forEach { foodInfo -> list.forEach { foodInfo ->
val scoreItem = scoreList.firstOrNull { it.name == foodInfo.foodName } val scoreItem = scoreList.firstOrNull { it.name == foodInfo.foodName }
val score = scoreItem?.score ?: 0.0 val score = scoreItem?.score ?: 0.0
@@ -526,7 +542,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
// list // list
// } // }
adapter.submitList(list) adapter.submitList(list)
binding.recyclerview.visible() binding.recyclerview.visible()
@@ -720,6 +735,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.previewView.visible() binding.previewView.visible()
binding.ivImg.gone() binding.ivImg.gone()
checkedItem = null checkedItem = null
lastWeight = 0
foodRecognizeState = true
} }
private fun updateDateTime() { private fun updateDateTime() {
@@ -886,8 +903,13 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
} }
fun getEatNum(realWeight: Double?, specWeight: Double?): Int { fun getEatNum(realWeight: Double?, specWeight: Double?): Int {
if (realWeight == null || realWeight <= 0.0 || specWeight == null || specWeight <= 0.0) return 0 if (realWeight == null || realWeight <= 0.0 || specWeight == null || specWeight <= 0.0) {
return (realWeight / specWeight).roundToInt() 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 = "" private var foodOrderId: String = ""
@@ -955,6 +977,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
// } // }
private fun clickPayButton() { private fun clickPayButton() {
log("eatWeight:recognizeWeight=$recognizeWeight, currentWeight=$currentWeight, lastWeight=$lastWeight")
if (checkedItem == null) { if (checkedItem == null) {
ToastUtils.showToast("暂无识别数据,请搜索选择") ToastUtils.showToast("暂无识别数据,请搜索选择")
return return
@@ -971,13 +994,13 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
//计费 //计费
if (mealPickupMode == 0) { if (mealPickupMode == 0) {
//即放即取 //即放即取
eatWeight = lastWeight eatWeight = currentWeight
foodWeight = lastWeight foodWeight = currentWeight
} else { } else {
//余量计量 //余量计量
eatWeight = recognizeWeight - lastWeight eatWeight = recognizeWeight - currentWeight
foodWeight = recognizeWeight foodWeight = recognizeWeight
log("eatWeight=$eatWeight, recognizeWeight=$recognizeWeight, lastWeight=$lastWeight") log("eatWeight=$eatWeight, recognizeWeight=$recognizeWeight, currentWeight=$currentWeight, lastWeight=$lastWeight")
} }
} else { } else {
//不计费 //不计费
@@ -987,7 +1010,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
recognizeWeight recognizeWeight
} else { } else {
//余量计量 //余量计量
recognizeWeight - lastWeight recognizeWeight - currentWeight
} }
} }
val eatNum = getEatNum(eatWeight.toDouble(), checkedItem!!.specWeight) val eatNum = getEatNum(eatWeight.toDouble(), checkedItem!!.specWeight)
@@ -43,6 +43,7 @@ import com.sw.dualscreen.model.response.UserEnergy
import com.sw.dualscreen.model.response.UserNutrition import com.sw.dualscreen.model.response.UserNutrition
import com.sw.dualscreen.utils.Debouncer import com.sw.dualscreen.utils.Debouncer
import com.sw.dualscreen.utils.SPUtil import com.sw.dualscreen.utils.SPUtil
import com.sw.dualscreen.utils.countDownByFlow
import com.sw.dualscreen.viewmodel.UserViewModel import com.sw.dualscreen.viewmodel.UserViewModel
import com.sw.plate.utils.LightManager import com.sw.plate.utils.LightManager
import com.sw.plate.utils.ToastUtils 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.FacePreviewInfo
import com.sw.plate.utils.arcface.face.model.RecognizeConfiguration import com.sw.plate.utils.arcface.face.model.RecognizeConfiguration
import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
@@ -75,7 +77,7 @@ class MainScreenPresentation(
) : Presentation(activity, display), ViewTreeObserver.OnGlobalLayoutListener { ) : Presentation(activity, display), ViewTreeObserver.OnGlobalLayoutListener {
companion object { companion object {
const val TAG = "SubScreenPresentation" const val TAG = "MainScreenPresentation"
} }
private lateinit var binding: PresentationMainScreenBinding private lateinit var binding: PresentationMainScreenBinding
@@ -144,55 +146,55 @@ class MainScreenPresentation(
mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0 mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
//0-计费,1-不计费 //0-计费,1-不计费
val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0) 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) { if (currentStep == 2 && lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE && isGoStep1) {
//人脸识别过程中,秤上物品拿走 //人脸识别过程中,秤上物品拿走
isGoStep1 = false isGoStep1 = false
activity.runOnUiThread { activity.runOnUiThread {
Timber.tag(TAG).e("切回step1") Timber.tag(TAG).e("切回step1,------2000000000000")
step1FoodRecognizing() step1FoodRecognizing()
} }
} else if (lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE) { } else if (lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE) {
isGoStep1 = false isGoStep1 = false
activity.runOnUiThread { activity.runOnUiThread {
Timber.tag(TAG).e("切回step1") Timber.tag(TAG).e("切回step1,------2000000000001")
if (mealPickupMode == 0) { if (mealPickupMode == 0) {
//即放即取 //即放即取
step1FoodRecognizing() step1FoodRecognizing()
} else { } 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) { if (currentFood == null) {
Timber.tag(TAG).e("updateWeight currentFood is null") Timber.tag(TAG).e("updateWeight currentFood is null")
return return
} }
//if (userNutritionData == null) {
if (userNutrition == null) { if (userNutrition == null) {
Timber.tag(TAG).e("updateWeight userNutritionData is null") Timber.tag(TAG).e("updateWeight userNutritionData is null")
return return
} }
// if (dinnerTypeInfo == null) {
// Timber.tag(TAG).e("updateWeight dinnerType is null")
// return
// }
if (chargeMode == 1 || activity.settlementMode == 0) { if (chargeMode == 1 || activity.settlementMode == 0) {
//不计费或者联合支付模式更新营养热量数据 //不计费或者联合支付模式更新营养热量数据
if (mealPickupMode == 0) { if (mealPickupMode == 0) {
@@ -205,6 +207,8 @@ class MainScreenPresentation(
} }
} }
private var countDownResetJob: Job? = null
fun setStepChangeCallback(callback: (Int) -> Unit) { fun setStepChangeCallback(callback: (Int) -> Unit) {
stepChangeCallback = callback stepChangeCallback = callback
} }
@@ -518,7 +522,7 @@ class MainScreenPresentation(
* 餐品识别中 * 餐品识别中
*/ */
fun step1FoodRecognizing() { fun step1FoodRecognizing() {
if (currentStep == 1) { if (currentStep == 1 && currentFood == null) {
return return
} }
Timber.tag(TAG).d("main,step1,耗时:${System.currentTimeMillis() - startTime}") Timber.tag(TAG).d("main,step1,耗时:${System.currentTimeMillis() - startTime}")
@@ -764,10 +768,7 @@ class MainScreenPresentation(
} }
private fun calculateNutrition(value: Int) { private fun calculateNutrition(value: Int) {
Log.d( Timber.tag(TAG) .d("calculateNutrition:weight=$value,recognitionWeight=$recognitionWeight,foodInfo=$currentFood")
TAG,
"addFoodToOrder: weight = $value, recognitionWeight = $recognitionWeight foodInfo = $currentFood"
)
if (currentFood == null || userNutrition == null) { if (currentFood == null || userNutrition == null) {
return return
} }
@@ -906,6 +907,7 @@ class MainScreenPresentation(
*/ */
fun updateMealPickupMode(mode: Int) { fun updateMealPickupMode(mode: Int) {
Timber.tag(TAG).d("updateMealPickupMode mode = $mode") Timber.tag(TAG).d("updateMealPickupMode mode = $mode")
Timber.tag(TAG).e("切回step1,------2000000000003")
mealPickupMode = mode mealPickupMode = mode
step1FoodRecognizing() step1FoodRecognizing()
} }
@@ -1359,6 +1361,7 @@ class MainScreenPresentation(
} }
} else { } else {
Timber.tag(TAG).d("drawPreviewInfo,successBlock,订单已生成,step1FoodRecognizing") Timber.tag(TAG).d("drawPreviewInfo,successBlock,订单已生成,step1FoodRecognizing")
Timber.tag(TAG).e("切回step1,------2000000000002")
activity.isAnalyzing = false activity.isAnalyzing = false
step1FoodRecognizing() step1FoodRecognizing()
} }