功能优化
This commit is contained in:
@@ -10,73 +10,25 @@ import com.wabon.wbintelligenthardwaresdk.api.SensorScale
|
||||
object WeightUtil {
|
||||
|
||||
var isConnected = false
|
||||
var weightFuncMap: MutableMap<String, ((address: Int, state: Int, weight: Int) -> Unit)?>? = null
|
||||
|
||||
fun init() {
|
||||
Weigher.init()
|
||||
|
||||
// Weigher.setListener(object : WeightListenerImpl() {
|
||||
// override fun onInit(connect: Boolean) {
|
||||
// Log.d("WeightUtil", "connect=$connect")
|
||||
// }
|
||||
//
|
||||
// override fun onZero() {
|
||||
// Log.d("WeightUtil", "zero ok")
|
||||
// }
|
||||
//
|
||||
// override fun onTare() {
|
||||
// Log.d("WeightUtil", "去皮置零操作成功")
|
||||
// }
|
||||
//
|
||||
// override fun onSetIdentify() {
|
||||
// Log.d("WeightUtil", "鉴别率设置操作成功")
|
||||
// }
|
||||
//
|
||||
// override fun onGetWeight(address: Int, state: Int, weight: Int) {
|
||||
// runOnUiThread {
|
||||
// val stateStr = when (state) {
|
||||
// SensorScale.STATE_STABLE -> "稳定"
|
||||
// SensorScale.STATE_UNSTABLE -> "不稳定"
|
||||
// SensorScale.STATE_OVER_WEIGHT -> "量程溢出"
|
||||
// else -> ""
|
||||
// }
|
||||
// val result1 = String.format("$stateStr 重量:%s kg", weight / 1000f)
|
||||
// Log.d("WeightUtil", "ttlReturn, address:$address,$result1")
|
||||
//// weightArray.put(address, weight)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// override fun onReadIdentify(rate: Int) {
|
||||
// Log.d("WeightUtil", "读取鉴别率成功:rate=$rate")
|
||||
// }
|
||||
//
|
||||
// override fun onFail(errCode: Int) {
|
||||
// val str = when (errCode) {
|
||||
// Weigher.ERR_001 -> "电子称未初始化"
|
||||
// Weigher.ERR_002 -> "开机零位异常"
|
||||
// Weigher.ERR_003 -> "传感器故障"
|
||||
// Weigher.ERR_004 -> "鉴别率超出范围"
|
||||
// Weigher.ERR_PCB_NOT_SUPPORT -> "主板不支持, " + Build.MODEL
|
||||
// else -> ""
|
||||
// }
|
||||
// Log.d("WeightUtil", str)
|
||||
// }
|
||||
//
|
||||
// override fun onFastFilter() {
|
||||
// Log.d("WeightUtil", "快速滤波设置成功")
|
||||
// }
|
||||
//
|
||||
// override fun onReadParam() {
|
||||
// Log.d("WeightUtil", "prepare ok")
|
||||
// }
|
||||
//
|
||||
// override fun onRangCal() {
|
||||
// Log.d("WeightUtil", "rang cal ok")
|
||||
// }
|
||||
//
|
||||
// override fun onSetParam() {
|
||||
// Log.d("WeightUtil", "onSetParam")
|
||||
// }
|
||||
// })
|
||||
Weigher.setListener(object : WeightListenerImpl() {
|
||||
override fun onInit(connect: Boolean) {
|
||||
super.onInit(connect)
|
||||
isConnected = connect
|
||||
}
|
||||
|
||||
override fun onGetWeight(address: Int, state: Int, weight: Int) {
|
||||
super.onGetWeight(address, state, weight)
|
||||
weightFuncMap?.forEach { (key, value) ->
|
||||
value?.invoke(address, state, weight)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun getWeight() {
|
||||
@@ -98,51 +50,46 @@ object WeightUtil {
|
||||
}
|
||||
|
||||
fun addWeightListener(
|
||||
onConnect: (state: Boolean) -> Unit = {},
|
||||
zero: () -> Unit = {},
|
||||
weightKey:String,
|
||||
getWeight: ((address: Int, state: Int, weight: Int) -> Unit) = { _, _, _ -> }
|
||||
) {
|
||||
Weigher.setListener(object : WeightListenerImpl() {
|
||||
override fun onInit(connect: Boolean) {
|
||||
super.onInit(connect)
|
||||
onConnect(connect)
|
||||
}
|
||||
|
||||
override fun onZero() {
|
||||
zero()
|
||||
}
|
||||
|
||||
override fun onGetWeight(address: Int, state: Int, weight: Int) {
|
||||
getWeight(address, state, weight)
|
||||
}
|
||||
})
|
||||
if (weightFuncMap == null) {
|
||||
weightFuncMap = mutableMapOf()
|
||||
}
|
||||
weightFuncMap?.put(weightKey, getWeight)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
open class WeightListenerImpl : Weigher.Listener {
|
||||
override fun onInit(connect: Boolean) {
|
||||
|
||||
Log.d("WeightUtil", "connect=$connect")
|
||||
}
|
||||
|
||||
override fun onZero() {
|
||||
|
||||
Log.d("WeightUtil", "zero ok")
|
||||
}
|
||||
|
||||
override fun onTare() {
|
||||
|
||||
Log.d("WeightUtil", "去皮置零操作成功")
|
||||
}
|
||||
|
||||
override fun onSetIdentify() {
|
||||
|
||||
Log.d("WeightUtil", "鉴别率设置操作成功")
|
||||
}
|
||||
|
||||
override fun onGetWeight(address: Int, state: Int, weight: Int) {
|
||||
|
||||
val stateStr = when (state) {
|
||||
SensorScale.STATE_STABLE -> "稳定"
|
||||
SensorScale.STATE_UNSTABLE -> "不稳定"
|
||||
SensorScale.STATE_OVER_WEIGHT -> "量程溢出"
|
||||
else -> ""
|
||||
}
|
||||
val result1 = String.format("$stateStr 重量:%s kg", weight / 1000f)
|
||||
Log.d("WeightUtil", "ttlReturn, address:$address,$result1")
|
||||
}
|
||||
|
||||
override fun onReadIdentify(rate: Int) {
|
||||
|
||||
Log.d("WeightUtil", "读取鉴别率成功:rate=$rate")
|
||||
}
|
||||
|
||||
override fun onFail(errCode: Int) {
|
||||
@@ -158,19 +105,20 @@ open class WeightListenerImpl : Weigher.Listener {
|
||||
}
|
||||
|
||||
override fun onFastFilter() {
|
||||
|
||||
Log.d("WeightUtil", "快速滤波设置成功")
|
||||
}
|
||||
|
||||
override fun onReadParam() {
|
||||
|
||||
Log.d("WeightUtil", "prepare ok")
|
||||
}
|
||||
|
||||
override fun onRangCal() {
|
||||
|
||||
Log.d("WeightUtil", "rang cal ok")
|
||||
}
|
||||
|
||||
override fun onSetParam() {
|
||||
|
||||
Log.d("WeightUtil", "onSetParam")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user