Commit fd7760d1 authored by chuxiaoshan's avatar chuxiaoshan

优化0到负数临界情况发送与判断。

parent 98d58d49
...@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME ...@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
...@@ -30,7 +30,7 @@ android { ...@@ -30,7 +30,7 @@ android {
} }
} }
group 'com.qmai.android.zqtoolkit' group 'com.qmai.android.zqtoolkit'
version '1.3.9.6' version '1.3.9.7'
gradlePublish { gradlePublish {
......
...@@ -72,14 +72,33 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction { ...@@ -72,14 +72,33 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction {
} }
} }
private fun String?.toDoubleOrZero(): Double {
return try {
this?.toDouble() ?: 0.0
} catch (e: Exception) {
0.0
}
}
private var lastLivingWeight: LivingWeight? = null
/**
* 判断是否需要立即发送
*/
private fun shouldEmitOnce(weight: LivingWeight): Boolean {
val lastNum = lastLivingWeight?.weight.toDoubleOrZero()
val num = weight.weight.toDoubleOrZero()
return (lastNum >= 0 && num < 0.0) || (lastNum <= 0 && num > 0)
}
override fun updateWeight(weight: LivingWeight) { override fun updateWeight(weight: LivingWeight) {
GlobalScope.launch { GlobalScope.launch {
//稳定状态且读数正常 //稳定状态且读数正常
if (weight.isStable && !weight.isReadErr) { if (weight.isStable && !weight.isReadErr && !shouldEmitOnce(weight)) {
readErrCache.clear() readErrCache.clear()
when { when {
stableWeight.size == STABLE_COUNT -> { stableWeight.size == STABLE_COUNT -> {
var single = stableWeight.distinctBy { it.weight } val single = stableWeight.distinctBy { it.weight }
if (single.size == 1) { if (single.size == 1) {
//给业务的unit要小写 //给业务的unit要小写
if (weight.unit.isNotEmpty()) if (weight.unit.isNotEmpty())
...@@ -90,9 +109,8 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction { ...@@ -90,9 +109,8 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction {
} }
stableWeight.clear() stableWeight.clear()
} }
stableWeight.size > STABLE_COUNT -> {
stableWeight.clear() stableWeight.size > STABLE_COUNT -> stableWeight.clear()
}
else -> stableWeight.add(weight) else -> stableWeight.add(weight)
} }
} else { } else {
...@@ -104,9 +122,11 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction { ...@@ -104,9 +122,11 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction {
disconnect() disconnect()
connectCache.clear() connectCache.clear()
} }
else -> readErrCache.add(true) else -> readErrCache.add(true)
} }
} }
lastLivingWeight = weight
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment