优化
This commit is contained in:
@@ -9,8 +9,74 @@ import com.wabon.wbintelligenthardwaresdk.api.SensorScale
|
||||
|
||||
object WeightUtil {
|
||||
|
||||
var isConnected = false
|
||||
|
||||
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")
|
||||
// }
|
||||
// })
|
||||
}
|
||||
|
||||
fun getWeight() {
|
||||
@@ -25,73 +91,6 @@ object WeightUtil {
|
||||
Weigher.tareTwo(address)
|
||||
}
|
||||
|
||||
fun connect(initCallback: (connect: Boolean) -> Unit) {
|
||||
Weigher.setListener(object : WeightListenerImpl() {
|
||||
override fun onInit(connect: Boolean) {
|
||||
initCallback(connect)
|
||||
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")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun runOnUiThread(action: () -> Unit) {
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
action()
|
||||
@@ -99,10 +98,16 @@ object WeightUtil {
|
||||
}
|
||||
|
||||
fun addWeightListener(
|
||||
zero: (() -> Unit) = {},
|
||||
onConnect: (state: Boolean) -> Unit = {},
|
||||
zero: () -> Unit = {},
|
||||
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()
|
||||
}
|
||||
@@ -141,7 +146,15 @@ open class WeightListenerImpl : Weigher.Listener {
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user