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