From eb7e805abded3097d8cde50dce39d642b24c04c5 Mon Sep 17 00:00:00 2001 From: chenfufeng Date: Fri, 12 Nov 2021 15:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AF=E7=BA=A2=E7=81=AF=E9=A2=84=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenfufeng --- .../eagle/core/function/v2x/V2XProvider.kt | 3 + .../redlightwarning/RedLightWarningManager.kt | 117 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/redlightwarning/RedLightWarningManager.kt diff --git a/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/V2XProvider.kt b/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/V2XProvider.kt index c2da09d47c..07ad9dc633 100644 --- a/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/V2XProvider.kt +++ b/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/V2XProvider.kt @@ -5,6 +5,7 @@ import com.alibaba.android.arouter.facade.annotation.Route import com.mogo.eagle.core.data.constants.MogoServicePaths.PATH_V2X_MODULE import com.mogo.eagle.core.function.api.base.IMoGoFunctionServerProvider import com.mogo.eagle.core.function.call.trafficlight.CallTrafficLightManager +import com.mogo.eagle.core.function.v2x.redlightwarning.RedLightWarningManager import com.mogo.eagle.core.function.v2x.speedlimit.SpeedLimitDataManager import com.mogo.eagle.core.function.v2x.vip.VipCarManager @@ -18,9 +19,11 @@ class V2XProvider : IMoGoFunctionServerProvider { CallTrafficLightManager.getTrafficLightProvider().initTrafficLightServer(context) VipCarManager.INSTANCE.initServer(context) SpeedLimitDataManager.getInstance().start(); + RedLightWarningManager.INSTANCE.listenTrafficLight() } override fun onDestroy() { VipCarManager.INSTANCE.destroy() + RedLightWarningManager.INSTANCE.onDestroy() } } \ No newline at end of file diff --git a/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/redlightwarning/RedLightWarningManager.kt b/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/redlightwarning/RedLightWarningManager.kt new file mode 100644 index 0000000000..c523056355 --- /dev/null +++ b/core/function-impl/mogo-core-function-v2x/src/main/java/com/mogo/eagle/core/function/v2x/redlightwarning/RedLightWarningManager.kt @@ -0,0 +1,117 @@ +package com.mogo.eagle.core.function.v2x.redlightwarning + +import com.mogo.eagle.core.data.trafficlight.* +import com.mogo.eagle.core.data.trafficlight.TrafficLightStatusHelper.getCurrentRoadTrafficLight +import com.mogo.eagle.core.function.api.trafficlight.IMoGoTrafficLightListener +import com.mogo.eagle.core.function.call.hmi.CallerHmiManager +import com.mogo.eagle.core.function.call.map.CallerMapLocationListenerManager +import com.mogo.eagle.core.function.call.trafficlight.CallTrafficLightListenerManager +import com.mogo.eagle.core.utilcode.util.ThreadUtils +import com.mogo.module.common.enums.EventTypeEnum +import com.mogo.utils.logger.Logger +import kotlin.math.abs + + +class RedLightWarningManager : IMoGoTrafficLightListener { + + companion object { + + const val TAG = "RedLightWarningManager" + + val INSTANCE: RedLightWarningManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + RedLightWarningManager() + } + } + + override fun onTrafficLightStatus(trafficLightResult: TrafficLightResult) { + getCurrentRoadTrafficLight(trafficLightResult)?.let { + handleRedLightWarning(it) + } + } + + fun listenTrafficLight() { + CallTrafficLightListenerManager.registerTrafficLightListener(TAG, this) + } + + private fun handleRedLightWarning(trafficLightStatus: TrafficLightStatus) { + // 路口100m闯红灯预警 + CallerMapLocationListenerManager.getCurrentLocation()?.let { + val distance = 100 + val remainTime = trafficLightStatus.remain + val speed = it.speed + if (speed == 0f) return + val arriveTime = distance / speed + + when { + trafficLightStatus.isRed() -> { + // 到达路口时红灯还没走完 + if (arriveTime <= remainTime) { + redLightWarning() + } else if (arriveTime > remainTime) { + greenLightWarning() + } + } + trafficLightStatus.isYellow() -> { + // 到达路口时黄灯还没走完 + if (arriveTime <= remainTime) { + redLightWarning() + } + } + trafficLightStatus.isGreen() -> { + // 到达路口时绿灯已经走完 + if (arriveTime > remainTime) { + redLightWarning() + } else if (arriveTime < remainTime) { + greenLightWarning() + } + } + } + } ?: run { + Logger.e(TAG, "CurrentLocation is null!") + } + } + + /** + * 闯红灯预警 + */ + private fun redLightWarning() { + ThreadUtils.runOnUiThread { + CallerHmiManager.showWarningV2X( + EventTypeEnum.TYPE_USECASE_ID_IVP.poiType.toInt(), + EventTypeEnum.TYPE_USECASE_ID_IVP.content, + EventTypeEnum.TYPE_USECASE_ID_IVP.tts, + EventTypeEnum.TYPE_USECASE_ID_IVP.poiType, + null + ) + } + } + + /** + * 绿灯通行提示 + */ + private fun greenLightWarning() { + ThreadUtils.runOnUiThread { + val content = String.format( + EventTypeEnum.getWarningContent(EventTypeEnum.TYPE_USECASE_ID_IVP_GREEN.poiType), + 50 + ) + val tts = String.format( + EventTypeEnum.getWarningTts(EventTypeEnum.TYPE_USECASE_ID_IVP_GREEN.poiType), + 50 + ) + CallerHmiManager.showWarningV2X( + EventTypeEnum.TYPE_USECASE_ID_IVP_GREEN.poiType.toInt(), + content, tts, + EventTypeEnum.TYPE_USECASE_ID_IVP_GREEN.poiType, null + ) + } + } + + private fun fEqual(a: Float, b: Float): Boolean { + return abs(a - b) < 0.000001 + } + + fun onDestroy() { + CallTrafficLightListenerManager.unRegisterTrafficLightListener(TAG) + } +} \ No newline at end of file