This commit is contained in:
zxj
2025-07-08 15:40:39 +08:00
parent d2698ed60f
commit 4501b6d5fc
132 changed files with 8998 additions and 0 deletions
@@ -0,0 +1,104 @@
package com.sw.inbound.sdk
import com.sw.inbound.utils.ThreadUtils
import com.wabon.wbintelligenthardwaresdk.api.SensorScale
import com.wabon.wbintelligenthardwaresdk.api.SensorScale.OnScaleResult
import kotlinx.coroutines.delay
import timber.log.Timber
typealias Callback = (Double) -> Unit
/**
* 称 传感器计算
*/
object SensorScaleUtils {
const val serialPort = "/dev/ttyS7"
const val baudRate = 115200
private var mSensorScale: SensorScale? = null
private var isOpened: Boolean = false
private var callback: Callback? = {}
private fun init() {
mSensorScale = SensorScale(object : OnScaleResult {
/**
* 读取重量
*/
override fun readWeight(state: Int, value: Double) {
val stateStr = when (state) {
SensorScale.STATE_STABLE -> "稳定"
SensorScale.STATE_UNSTABLE -> "不稳定"
SensorScale.STATE_OVER_WEIGHT -> "量程溢出"
else -> "未知"
}
// Timber.d("readWeight state = ${stateStr}, weight = $value")
callback?.invoke(value)
}
/**
* 读取鉴别率
*/
override fun readIdentify(rate: Int) {
Timber.e("readIdentify rate = $rate")
}
override fun fail(errCode: Int) {
Timber.e("fail code = $errCode")
}
})
}
fun startScale(autoScale: Boolean = true, callback: Callback?) {
if (isOpened) {
startContinuousRead(callback = callback)
return
}
this.callback = callback
init()
mSensorScale?.openScale(serialPort, baudRate) { open ->
isOpened = open
Timber.d("isOpened = $isOpened")
if (open) {
if (autoScale) {
ThreadUtils.launchOnIo {
delay(2000)
// 打开后需要等待后才能调用,否则会 1001 SDK未初始化
mSensorScale?.startContinuousRead()
}
}
}
}
}
fun startContinuousRead(callback: Callback?) {
this.callback = callback
Timber.d("isOpened = $isOpened")
if (!isOpened) {
return
}
mSensorScale?.startContinuousRead()
}
fun readWeight(callback: Callback?) {
this.callback = callback
Timber.d("isOpened = $isOpened")
if (!isOpened) {
return
}
mSensorScale?.readWeight()
}
fun stopContinuousRead() {
Timber.d("isOpened = $isOpened")
if (!isOpened) {
return
}
mSensorScale?.stopContinuousRead()
}
fun closeScale() {
isOpened = false
mSensorScale?.closeScale()
mSensorScale == null
}
}