[6.5.0] refactor: 通过公交站 线程优化;

This commit is contained in:
aibingbing
2024-07-11 17:32:27 +08:00
parent d171b1b334
commit 358278a915
3 changed files with 62 additions and 39 deletions

View File

@@ -2,6 +2,7 @@ package com.mogo.eagle.function.biz.v2x.busstation
import android.os.Handler
import android.os.HandlerThread
import android.os.Message
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.mogo.commons.utils.MogoAnalyticUtils
@@ -24,11 +25,7 @@ import com.mogo.map.entities.BusStation
import com.mogo.map.overlay.core.Level
import com.mogo.map.overlay.point.Point
import com.zhidaoauto.map.data.point.LonLatPoint
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import mogo.telematics.pad.MessagePad
import java.util.LinkedList
@@ -44,16 +41,6 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
const val TAG = "PassBusStationEventManager"
const val ANALYTICS_KEY = "biz_v2x_road_pass_bus_station_manager"
private val coroutineExceptionHandler =
CoroutineExceptionHandler { coroutineContext, exception ->
Logger.e(TAG, "coroutineExceptionHandler --> Handle $exception in $coroutineContext")
trackEvent("coroutineExceptionHandler --> Handle $exception in $coroutineContext")
isCalculateNearByStation.set(false)
}
private val mCoroutineScope: CoroutineScope =
CoroutineScope(SupervisorJob() + Dispatchers.IO + coroutineExceptionHandler)
// 距离当前车位置 X 米远的点集合用来查询根据点查询roadId, 然后用roadId查询路上的公交站点
private val farthestLocationList = ArrayList<LonLatPoint>()
@@ -134,11 +121,11 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
"removeNeedNotifiedBusStation --> ${it.toString()}"
)
if (HmiBuildConfig.isShowBusStationStrategyBorderPoint) {
mockOtherRetroGradeVehicleData(
it.getBusStationPoint().longitude,
it.getBusStationPoint().latitude,
currentLocation.heading
)
// mockOtherRetroGradeVehicleData(
// it.getBusStationPoint().longitude,
// it.getBusStationPoint().latitude,
// currentLocation.heading
// )
}
}
}
@@ -146,13 +133,13 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
// 开始提醒
if (busStationListNeedNotified.isNotEmpty() && !isNotifyRunnableRunning.get()) {
handler.removeCallbacks(notificationCheckRunnable)
handler.post(notificationCheckRunnable)
notificationHandler.removeCallbacks(notificationCheckRunnable)
notificationHandler.post(notificationCheckRunnable)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
handler?.postDelayed(this, 800L)
notificationHandler?.postDelayed(this, 800L)
}
}
}
@@ -195,7 +182,7 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
}
if (list.size >= 2) {
isNotifyRunnableRunning.set(true)
handler.postDelayed(this, 4000L)
notificationHandler.postDelayed(this, 4000L)
return
}
}
@@ -205,35 +192,60 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
}
private val handler by lazy {
val thread = HandlerThread("road_v2n_bus_station_calculate")
private val notificationHandler by lazy {
val thread = HandlerThread("road_v2n_bus_station_notification")
thread.start()
Handler(thread.looper)
}
private const val MSG_WHAT_CALCULATE = 0x1010 // 计算附近公交站点
private val calculateHandler by lazy {
val thread = HandlerThread("road_v2n_bus_station_calculate")
thread.start()
Handler(thread.looper) { msg ->
if (msg.what == MSG_WHAT_CALCULATE) {
val gnssInfo = msg.obj as? MogoLocation
gnssInfo?.also {
calculateNearByStation(it, 17, 10)
}
}
true
}
}
fun init() {
CallerChassisLocationWGS84ListenerManager.addListener(TAG, 1, this)
handler.removeCallbacks(checkDistanceRunnable)
handler.removeCallbacks(notificationCheckRunnable)
handler.post(checkDistanceRunnable)
notificationHandler.removeCallbacks(checkDistanceRunnable)
notificationHandler.removeCallbacks(notificationCheckRunnable)
notificationHandler.post(checkDistanceRunnable)
calculateHandler.removeMessages(MSG_WHAT_CALCULATE)
trackEvent("init")
}
fun unInit() {
CallerChassisLocationWGS84ListenerManager.removeListener(TAG)
mCoroutineScope?.cancel()
busStationNearByQueue.clear()
busStationListNeedNotified.clear()
handler.removeCallbacks(checkDistanceRunnable)
handler.removeCallbacks(notificationCheckRunnable)
handler.looper?.quit()
notificationHandler.removeCallbacks(checkDistanceRunnable)
notificationHandler.removeCallbacks(notificationCheckRunnable)
calculateHandler.removeMessages(MSG_WHAT_CALCULATE)
isCalculateNearByStation.set(false)
isNotifyRunnableRunning.set(false)
trackEvent("unInit")
}
override fun onChassisLocationWGS84(gnssInfo: MogoLocation) {
if (!isCalculateNearByStation.get()) {
isCalculateNearByStation.set(true)
calculateNearByStation(gnssInfo, 15, 10)
calculateHandler.removeMessages(MSG_WHAT_CALCULATE)
calculateHandler.sendMessage(
Message.obtain(
calculateHandler,
MSG_WHAT_CALCULATE,
gnssInfo
)
)
}
}
@@ -245,7 +257,7 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
private fun calculateNearByStation(
gnssInfo: MogoLocation, segmentSum: Int, metersPreSegment: Int
) {
mCoroutineScope.launch {
val result = kotlin.runCatching {
farthestLocationList.clear()
val heading = gnssInfo.heading
//每metersPreSegment 米一个点,获取 segmentSum * metersPreSegment 米范围内的坐标点
@@ -262,7 +274,8 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
busStationHDMarkerStrategy()
}
MogoData.mogoMapData.get()?.also { iMogoData ->
val busStationList = iMogoData.getBusStation(java.util.ArrayList(farthestLocationList.toMutableList()))
val busStationList =
iMogoData.getBusStation(java.util.ArrayList(farthestLocationList.toMutableList()))
val filteredBusStationList = busStationList.filter {
it.busStationPoints.isNotEmpty() &&
DrivingDirectionUtils.getDegreeOfCar2Poi(
@@ -291,6 +304,12 @@ object PassBusStationEventManager : IMoGoChassisLocationWGS84Listener {
isCalculateNearByStation.set(false)
}
}
if (result.isFailure) {
Logger.e(TAG, "error => ${result.exceptionOrNull()?.stackTraceToString()}")
isCalculateNearByStation.set(false)
} else {
isCalculateNearByStation.set(false)
}
}
fun trackEvent(msg: String) {

View File

@@ -4,7 +4,7 @@ import com.zhidaoauto.map.data.point.LonLatPoint
data class BusStation(
var busStationPoints: List<com.zhidaoauto.map.data.point.LonLatPoint>,
var id: Int,
var id: Long,
var roadId: Int,
var type: Int,
var notifyTime: Long = -1,
@@ -12,7 +12,11 @@ data class BusStation(
) {
fun getBusStationId(): String {
return "bus_station_${id}_${roadId}"
//id=0 需要计算出一个唯一id
val newId = getBusStationPoint()?.let {
it.latitude + it.longitude
} ?: 0
return "bus_station_${newId}_${roadId}"
}
/**

View File

@@ -295,8 +295,8 @@ object MapDataWrapper : IMogoData {
MapDataApi.getBusStation(routeList, object : IResult<RoutePath> {
override fun result(code: Int, result: RoutePath?) {
result?.steps?.forEach {
it?.busStations?.forEach {
val busStation = BusStation(it.busStationPoints, it.id, it.roadId, it.type, -1, -1)
it?.busStations.forEach {
val busStation = BusStation(it.busStationPoints, it.id.toLong() , it.roadId, it.type, -1, -1)
resultList.add(busStation)
}
}