Commit 1ec9ee45 authored by tongzifang's avatar tongzifang

fix leak

parent d399515e
...@@ -30,7 +30,7 @@ android { ...@@ -30,7 +30,7 @@ android {
} }
} }
group 'com.qmai.android.zqtoolkit' group 'com.qmai.android.zqtoolkit'
version '1.0.6.20' version '1.0.7'
gradlePublish { gradlePublish {
......
...@@ -9,6 +9,7 @@ import kotlinx.coroutines.* ...@@ -9,6 +9,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlin.coroutines.EmptyCoroutineContext
/** /**
* tongzi * tongzi
...@@ -21,8 +22,8 @@ object ScaleHub : ScaleBaseAction { ...@@ -21,8 +22,8 @@ object ScaleHub : ScaleBaseAction {
val connectState: SharedFlow<ConnectState> = _connectState val connectState: SharedFlow<ConnectState> = _connectState
private val _livingWeight = MutableSharedFlow<LivingWeight>() private val _livingWeight = MutableSharedFlow<LivingWeight>()
val livingWeight: SharedFlow<LivingWeight> = _livingWeight val livingWeight: SharedFlow<LivingWeight> = _livingWeight
private var connectJob: Job? = null private var connectJob: CoroutineScope? = null
private var weightJob: Job? = null private var weightJob: CoroutineScope? = null
private val scaleKit by lazy { private val scaleKit by lazy {
when { when {
ScaleChecker.isSUNMI() -> { ScaleChecker.isSUNMI() -> {
...@@ -36,7 +37,14 @@ object ScaleHub : ScaleBaseAction { ...@@ -36,7 +37,14 @@ object ScaleHub : ScaleBaseAction {
override fun connect(cxt: Context?, port: String?, buad: String?) { override fun connect(cxt: Context?, port: String?, buad: String?) {
// var trueCxt = cxt?.applicationContext // var trueCxt = cxt?.applicationContext
connectJob = GlobalScope.launch(context = Dispatchers.IO) { //称只给连一个端口,其他端口用其他hub
if (isScaleSuccess()) {
return
}
if (connectJob == null) {
connectJob = CoroutineScope(EmptyCoroutineContext)
}
connectJob?.launch(context = Dispatchers.IO) {
scaleKit.connect(cxt, port, buad) scaleKit.connect(cxt, port, buad)
if (scaleKit is ZqScaleKit) { if (scaleKit is ZqScaleKit) {
LEDHub.connect() LEDHub.connect()
...@@ -56,7 +64,9 @@ object ScaleHub : ScaleBaseAction { ...@@ -56,7 +64,9 @@ object ScaleHub : ScaleBaseAction {
scaleKit.disconnect() scaleKit.disconnect()
LEDHub.clearLED() LEDHub.clearLED()
weightJob?.cancel() weightJob?.cancel()
weightJob = null
connectJob?.cancel() connectJob?.cancel()
connectJob = null
} }
...@@ -88,7 +98,11 @@ object ScaleHub : ScaleBaseAction { ...@@ -88,7 +98,11 @@ object ScaleHub : ScaleBaseAction {
//读取重量、皮重 //读取重量、皮重
override fun readData() { override fun readData() {
weightJob = GlobalScope.launch(context = Dispatchers.IO) { if (weightJob == null) {
weightJob = CoroutineScope(EmptyCoroutineContext)
}
connectJob?.launch(context = Dispatchers.IO) {
scaleKit.readData() scaleKit.readData()
collectWeightData() collectWeightData()
} }
......
...@@ -78,12 +78,10 @@ object SunmiScaleKit : ScaleBaseKit() { ...@@ -78,12 +78,10 @@ object SunmiScaleKit : ScaleBaseKit() {
override fun disconnect() { override fun disconnect() {
if (isScaleSuccess()) { super.disconnect()
stopReadData() mScaleManager?.onDestroy()
mScaleManager?.onDestroy() mScaleManager = null
mScaleManager = null Log.v(TAG, "disconnect")
Log.v(TAG, "disconnect")
}
} }
......
...@@ -48,7 +48,7 @@ object ZqScaleKit : ScaleBaseKit() { ...@@ -48,7 +48,7 @@ object ZqScaleKit : ScaleBaseKit() {
private var job: Job? = null private var job: Job? = null
override fun disconnect() { override fun disconnect() {
stopReadData() super.disconnect()
val ebDisconnect = zqeb.EB_Disconnect() val ebDisconnect = zqeb.EB_Disconnect()
Log.v(TAG, String.format("ebDisconnect: %s", ebDisconnect)) Log.v(TAG, String.format("ebDisconnect: %s", ebDisconnect))
} }
......
...@@ -24,6 +24,11 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction { ...@@ -24,6 +24,11 @@ abstract class ScaleBaseKit : ScaleBaseAction, ScaleUpdateAction {
val _livingWeight = MutableSharedFlow<LivingWeight>() val _livingWeight = MutableSharedFlow<LivingWeight>()
val livingWeight: SharedFlow<LivingWeight> = _livingWeight val livingWeight: SharedFlow<LivingWeight> = _livingWeight
override fun disconnect() {
stopReadData()
updateState(ConnectState(false,"断开连接"))
}
override fun updateState(state: ConnectState) { override fun updateState(state: ConnectState) {
GlobalScope.launch(context = Dispatchers.IO) { GlobalScope.launch(context = Dispatchers.IO) {
_connectState.emit(state) _connectState.emit(state)
......
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