[状态栏]定时请求时长改为1分钟;优化定时逻辑

This commit is contained in:
renwj
2022-07-04 09:11:14 +08:00
parent d2c35d07e4
commit c99df38ba1
4 changed files with 64 additions and 12 deletions

View File

@@ -7,8 +7,11 @@ import androidx.lifecycle.*
import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.Lifecycle.Event.ON_CREATE
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
import com.mogo.eagle.core.function.api.autopilot.IMoGoAutopilotStatusListener
import com.mogo.eagle.core.function.call.autopilot.*
import com.mogo.eagle.core.utilcode.kotlin.*
import com.mogo.eagle.core.utilcode.util.AppStateManager
import com.mogo.eagle.core.utilcode.util.IAppStateListener
import com.zhjt.mogo_core_function_devatools.ext.*
import com.zhjt.mogo_core_function_devatools.status.entity.CanStatus
import com.zhjt.mogo_core_function_devatools.status.entity.GpsStatus
@@ -28,17 +31,57 @@ import com.zhjt.mogo_core_function_devatools.status.flow.trace.TracingImpl
import com.zhjt.mogo_core_function_devatools.status.ui.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import mogo_msg.MogoReportMsg
object StatusManager {
private const val TAG = "StatusManager"
private lateinit var model: StatusModel
private var hidePop: (() -> Unit)? = null
private var timer: Job? = null
private val listener = object : IMoGoAutopilotStatusListener {
override fun onAutopilotGuardian(guardianInfo: MogoReportMsg.MogoReportMessage?) {
super.onAutopilotGuardian(guardianInfo)
guardianInfo?.code?.takeIf {
it.contains("RTK_STATUS", true) || it.contains("CAN", true)
}?.run {
req()
}
}
}
private val appStateListener = object : IAppStateListener {
override fun onAppStateChanged(isForeground: Boolean) {
if (isForeground) {
req()
} else {
timer?.cancel()
}
}
}
private val flows: ArrayList<IFlow<out Status>> by lazy {
ArrayList()
}
private fun req() {
timer?.cancel()
model.viewModelScope.launch(Dispatchers.IO) {
CallerAutoPilotManager.sendStatusQueryReq()
while (true) {
delay(60000) //一分钟主动请求一次
CallerAutoPilotManager.sendStatusQueryReq()
}
}.also {
timer = it
}
}
fun init(ctx: Context) {
val owner = ctx as? ViewModelStoreOwner ?: throw IllegalStateException("ctx: $ctx is a instance of ViewModelStoreOwner.")
model = ViewModelProvider(owner).get(StatusModel::class.java)
@@ -56,6 +99,9 @@ object StatusManager {
private fun onCreate(ctx: Context) {
val values = model.status.value?.second ?: throw IllegalStateException("state is not right.")
CallerAutoPilotStatusListenerManager.addListener(TAG, listener)
AppStateManager.registerAppStateListener(appStateListener)
req()
values.map {
when (it) {
is CanStatus -> CanImpl(ctx)
@@ -79,12 +125,6 @@ object StatusManager {
for (f in flows) {
f.onCreate()
}
ctx.lifeCycleScope.launch(Dispatchers.IO) {
while (true) {
CallerAutoPilotManager.sendStatusQueryReq()
delay(1000)
}
}
}
ctx.normalPop(content,
width = 665.PX,
@@ -94,7 +134,12 @@ object StatusManager {
isFocusable = false).also { hidePop = it }
}
private fun onDestroy(ctx: Context) {
CallerAutoPilotStatusListenerManager.removeListener(TAG)
AppStateManager.unRegisterAppStateListener(appStateListener)
timer?.cancel()
flows.forEach {
it.onDestroy()
}

View File

@@ -87,6 +87,10 @@ internal class RTKStatus(var enabled: Boolean = false, var desc: String = "RTK")
if (javaClass != other?.javaClass) return false
other as RTKStatus
if (enabled != other.enabled) return false
if (desc != other.desc) {
return false
}
return true
}

View File

@@ -1,6 +1,7 @@
package com.zhjt.mogo_core_function_devatools.status.flow.can
import android.content.*
import android.util.Log
import chassis.Chassis.GearPosition
import chassis.Chassis.LightSwitch
import com.mogo.eagle.core.function.api.autopilot.*
@@ -73,6 +74,7 @@ internal class CanImpl(ctx: Context): IFlow<CanStatus>(ctx), IMoGoAutopilotVehic
override fun onAutopilotStatusRespByQuery(status: StatusInfo) {
val state = status.healthInfoList?.find { "can_adapter".equals(it.name, true) }?.state?.ordinal
Log.d(TAG, "state: $state")
if (state != null) {
this.state.set(state)
}

View File

@@ -20,7 +20,7 @@ internal class RTKImpl(ctx: Context): IFlow<RTKStatus>(ctx), IMoGoAutopilotCarSt
private var job: Job? = null
private val desc by lazy {
private val healthInfo by lazy {
AtomicReference<HealthInfo>(null)
}
@@ -34,7 +34,7 @@ internal class RTKImpl(ctx: Context): IFlow<RTKStatus>(ctx), IMoGoAutopilotCarSt
private fun isRTKEnabled(): Boolean {
val code = CallerAutoPilotStatusListenerManager.getAutoPilotReportMessageCode()
val gnssInfo = CallerAutopilotCarStatusListenerManager.getCurrentGnssInfo()
val status = desc.get()
val status = healthInfo.get()
return CallerAutoPilotManager.isConnected() && (
code != "EHW_RTK" &&
code != "EHW_GNSS" &&
@@ -54,14 +54,15 @@ internal class RTKImpl(ctx: Context): IFlow<RTKStatus>(ctx), IMoGoAutopilotCarSt
}
override fun onAutopilotStatusRespByQuery(status: StatusInfo) {
val state = status.healthInfoList?.find { "localization".equals(it.name, true) }
if (state != null) {
desc.set(state)
val info = status.healthInfoList?.find { "localization".equals(it.name, true) }
Log.d(TAG, "info: $info")
if (info != null) {
healthInfo.set(info)
}
}
private fun getDesc(): String {
return desc.get()?.desc?.uppercase() ?: "RTK"
return healthInfo.get()?.desc?.uppercase() ?: "RTK"
}
private fun timeOutCheck() {