fix(fragment): 修复 weightSnapshot key 因 Int 溢出导致主设备高亮失效的问题

将 snapshotKey 从 Int(address + hashCode*1000 易溢出)改为 String(deviceId#address),
避免不同设备的基准值互相覆盖,导致 delta 永远无法正确计算
This commit is contained in:
2026-04-20 09:08:50 +08:00
parent 193acff1e0
commit d3b69c4e46
@@ -47,8 +47,8 @@ class SeasoningConfigFragment : BaseFragment<FragmentSeasoningConfigBinding>() {
private val adapter22 by lazy { buildAdapter22() }
private val adapter18 by lazy { Seasoning18GridAdapter() }
/** 各秤地址的上一次重量快照,用于计算 delta */
private val weightSnapshot = mutableMapOf<Int, Double>()
/** 各秤的上一次重量快照,key 格式 "{deviceId}#{address}"用于计算 delta */
private val weightSnapshot = mutableMapOf<String, Double>()
/** 主线程 Handler,用于延迟清除高亮 */
private val mainHandler = Handler(Looper.getMainLooper())
/** 当前高亮清除任务,新高亮触发时取消旧任务 */
@@ -161,7 +161,7 @@ class SeasoningConfigFragment : BaseFragment<FragmentSeasoningConfigBinding>() {
viewLifecycleOwner.lifecycleScope.launch {
allScalesFlow.collect { scaleMap ->
scaleMap.forEach { (key, data) ->
val snapshotKey = data.address + data.deviceId.hashCode() * 1000
val snapshotKey = "${data.deviceId}#${data.address}"
val baseline = weightSnapshot[snapshotKey]
if (baseline == null) {
// 首次收到:记录基准
@@ -187,11 +187,12 @@ class SeasoningConfigFragment : BaseFragment<FragmentSeasoningConfigBinding>() {
} else {
// 子设备:注册本机 WeightUtil 监听,触发后通过 broadcastEvent 通知主设备
WeightUtil.addWeightListener(TAG) { address, state, weight ->
val baseline = weightSnapshot[address]
val snapshotKey = "${GlobalData.deviceId}#$address"
val baseline = weightSnapshot[snapshotKey]
Log.d(TAG, "onWeight: address=$address state=$state weight=$weight baseline=$baseline")
if (baseline == null) {
weightSnapshot[address] = weight
weightSnapshot[snapshotKey] = weight
return@addWeightListener
}
@@ -201,12 +202,12 @@ class SeasoningConfigFragment : BaseFragment<FragmentSeasoningConfigBinding>() {
when {
// 仅在秤稳定后判断增量,避免取走物品时手部冲力造成误触发
state == WeightUtil.STATE_STABLE && delta > WEIGHT_DELTA_THRESHOLD -> {
weightSnapshot[address] = weight
weightSnapshot[snapshotKey] = weight
Log.d(TAG, "onWeight: 触发高亮 address=$address delta=$delta")
triggerHighlight(GlobalData.deviceId, address, delta)
}
state == WeightUtil.STATE_STABLE -> {
weightSnapshot[address] = weight
weightSnapshot[snapshotKey] = weight
}
}
}