From c99df38ba1a915f012e9d5b52cb2de34c7d8a06b Mon Sep 17 00:00:00 2001 From: renwj Date: Mon, 4 Jul 2022 09:11:14 +0800 Subject: [PATCH] =?UTF-8?q?[=E7=8A=B6=E6=80=81=E6=A0=8F]=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=97=B6=E9=95=BF=E6=94=B9=E4=B8=BA1?= =?UTF-8?q?=E5=88=86=E9=92=9F=EF=BC=9B=E4=BC=98=E5=8C=96=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../status/StatusManager.kt | 57 +++++++++++++++++-- .../status/entity/Status.kt | 4 ++ .../status/flow/can/CanImpl.kt | 2 + .../status/flow/rtk/RTKImpl.kt | 13 +++-- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/StatusManager.kt b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/StatusManager.kt index 493d75bd49..819344deb6 100644 --- a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/StatusManager.kt +++ b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/StatusManager.kt @@ -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> 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() } diff --git a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/entity/Status.kt b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/entity/Status.kt index 302272dc85..562722debe 100644 --- a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/entity/Status.kt +++ b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/entity/Status.kt @@ -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 } diff --git a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/can/CanImpl.kt b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/can/CanImpl.kt index 049a174b4b..23a575867b 100644 --- a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/can/CanImpl.kt +++ b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/can/CanImpl.kt @@ -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(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) } diff --git a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/rtk/RTKImpl.kt b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/rtk/RTKImpl.kt index 5a2cbb3435..e6a49fe44a 100644 --- a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/rtk/RTKImpl.kt +++ b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/status/flow/rtk/RTKImpl.kt @@ -20,7 +20,7 @@ internal class RTKImpl(ctx: Context): IFlow(ctx), IMoGoAutopilotCarSt private var job: Job? = null - private val desc by lazy { + private val healthInfo by lazy { AtomicReference(null) } @@ -34,7 +34,7 @@ internal class RTKImpl(ctx: Context): IFlow(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(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() {