fix(home): 解决格口数据更新和权重计算问题

- 将超时时间从5分钟调整为1分钟
- 优化格口数据匹配逻辑,提高查找效率
- 根据物品类型分别处理重量数据存储
- 添加JSON序列化支持用于调试日志
- 修复itemType字段类型不一致问题
- 优化重量显示计算逻辑并简化条件判断
- 修复代码格式化问题
This commit is contained in:
mazengfei
2026-06-04 11:19:25 +08:00
parent 4b8e5f84e6
commit ac17fca325
4 changed files with 24 additions and 20 deletions
@@ -38,6 +38,7 @@ import com.shuwei.intelligent.shelves.serial.ScaleManager
import com.shuwei.intelligent.shelves.utils.GridLayoutTool
import com.shuwei.intelligent.shelves.utils.IntervalExecutor
import com.shuwei.intelligent.shelves.utils.ext.gone
import com.shuwei.intelligent.shelves.utils.ext.toJsonString
import com.shuwei.intelligent.shelves.utils.ext.toast
import com.shuwei.intelligent.shelves.utils.ext.visible
import kotlinx.coroutines.Job
@@ -83,17 +84,21 @@ class HomeV2Activity : BaseActivity() {
.onHeartbeat { _, data ->
data.weights.forEach { weight ->
val shelfNo = weight.index
if (shelfNo in 1..list.size) {
// if (shelfNo in 1..list.size) {
// 用 slotNo 末尾数字与串口编号匹配
list.firstOrNull {
val pos = list.indexOfFirst {
it.slotNo?.filter { c -> c.isDigit() }?.toIntOrNull() == shelfNo
}?.let { model ->
val pos = list.indexOf(model)
}
val model = list.getOrNull(pos)?:return@forEach
if (model.itemType == 1) {
model.actualQty = (weight.grams / 1000.0).toBigDecimal()
} else {
model.totalWeightG = weight.grams.toBigDecimal()
}
shelfAdapter.notifyItemChanged(pos)
log("getWeightInfo: slotNo=${model.slotNo},realWeight=${weight.grams}")
log("getWeightInfo: slotNo=${model.slotNo},realWeight=${weight.grams}, item:${model.toJsonString()}")
EventBus.getDefault().post(SendWeightEvent(shelfNo, weight.grams))
}
}
// }
}
}
.onTempReport { _ ->
@@ -166,7 +171,9 @@ class HomeV2Activity : BaseActivity() {
private fun showProgress() {
Loading.show(this)
}
private var cabinetInitResult: CabinetInitResult? = null
@SuppressLint("NotifyDataSetChanged")
private fun updateUI(data: RespData<*>) {
binding.include?.root?.gone()
@@ -36,11 +36,8 @@ class ShelfV2Adapter(list: MutableList<ShelfModelV2>) :
else binding.ivStaleFood.gone()
// 重量:itemType=2 用 totalWeightG(g)itemType=1 用 actualQty×1000(g)
val weightG: Double = when (item.itemType.toInt()) {
2 -> item.totalWeightG?.toDouble() ?: 0.0
1 -> (item.actualQty?.toDouble() ?: 0.0) * 1000.0
else -> 0.0
}
val weightG = if (item.itemType == 1) (item.actualQty?.toDouble() ?: 0.0) * 1000.0
else item.totalWeightG?.toDouble() ?: 0.0
binding.tvFoodWeight.run {
setTextColor(getColor(R.color.food_weight_orange))
text = if (abs(weightG) < 1000) "${weightG.toInt()}"
@@ -112,7 +112,7 @@ open class BaseActivity : AppCompatActivity() {
}
// 5分钟
private val TIME_OUT: Long = (5 * 60 * 1000).toLong()
private val TIME_OUT: Long = (1 * 60 * 1000).toLong()
private val mHandler by lazy { Handler(Looper.getMainLooper()) }
private val mTimeoutRunnable = Runnable {
// 用户长时间无操作时回调,子类可覆写实现具体逻辑(如关灯)
@@ -9,7 +9,7 @@ import java.math.BigDecimal
data class ShelfModelV2(
var id: Long = 0,
/** 类型:1-食材净菜 / 2-餐品净菜包 */
var itemType: Short = 0,
var itemType: Int = 0,
/** 格口编号 */
var slotNo: String? = "",
/** 格口所在柜子编号 */