闯红灯预警

Signed-off-by: chenfufeng <chenfufeng@zhidaoauto.com>
This commit is contained in:
chenfufeng
2021-11-12 15:39:27 +08:00
parent 01505b4266
commit eb7e805abd
2 changed files with 120 additions and 0 deletions

View File

@@ -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()
}
}

View File

@@ -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)
}
}