优化拥堵策略上报逻辑
This commit is contained in:
@@ -5,6 +5,7 @@ import com.alibaba.android.arouter.facade.annotation.Route
|
||||
import com.alibaba.android.arouter.facade.template.IProvider
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.mogo.commons.debug.DebugConfig
|
||||
import com.mogo.module.share.adas.AdasFrontCarDistanceY
|
||||
import com.mogo.module.share.constant.ACCIDENT_STRATEGY_SHARE_PUSH_TYPE
|
||||
import com.mogo.module.share.constant.S_TAG
|
||||
import com.mogo.module.share.strategyreceiver.AccidentStrategyReceiver
|
||||
@@ -12,6 +13,9 @@ import com.mogo.module.share.strategyreceiver.BlockStrategy
|
||||
import com.mogo.service.IMogoServiceApis
|
||||
import com.mogo.service.MogoServicePaths
|
||||
import com.mogo.utils.logger.Logger
|
||||
import com.mogo.utils.network.utils.GsonUtil
|
||||
import org.json.JSONObject
|
||||
import java.lang.Exception
|
||||
|
||||
/**
|
||||
* 策略上报入口,服务端策略上报Push的接收地,接收后再调用tanlu相关接口进行视频抓取
|
||||
@@ -30,8 +34,26 @@ class StrategyShareProvider : IProvider {
|
||||
// if(!DebugConfig.isLauncher()) {
|
||||
apis.registerCenterApi.registerMogoLocationListener(MogoServicePaths.PATH_STRATEGY_SHARE) {
|
||||
// Logger.d(S_TAG,"定位发生变化,准备记录速度: ${it.speed}")
|
||||
|
||||
blockStrategy.recordSpeed(it.speed * 3.6F)
|
||||
blockStrategy.recordLocation(it.latitude, it.longitude)
|
||||
}
|
||||
|
||||
apis.adasControllerApi.addAdasDataCallback {
|
||||
Logger.d(S_TAG, "收到adas数据===$it")
|
||||
try {
|
||||
val oriJson = JSONObject(it)
|
||||
val action = oriJson.optInt("action", -1)
|
||||
if (action == 0) {
|
||||
// adas返回前车距离,单位是米
|
||||
val frontDistanceItem = GsonUtil.objectFromJson(it, AdasFrontCarDistanceY::class.java)
|
||||
val distance = frontDistanceItem.result.distanceY.toDouble().toInt()
|
||||
blockStrategy.refreshFrontDistance(distance)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.e(S_TAG, e, "解析adas数据异常")
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.mogo.module.share.adas;
|
||||
|
||||
/**
|
||||
* @author nie yunlong
|
||||
* @des
|
||||
* @date 2020/10/20
|
||||
*/
|
||||
public class AdasDataInfo {
|
||||
/**
|
||||
* 距离
|
||||
*/
|
||||
private String distanceY;
|
||||
|
||||
public String getDistanceY() {
|
||||
return distanceY;
|
||||
}
|
||||
|
||||
public void setDistanceY(String distanceY) {
|
||||
this.distanceY = distanceY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.mogo.module.share.adas;
|
||||
|
||||
/**
|
||||
* @author nie yunlong
|
||||
* @des 正前方距离y
|
||||
* @date 2020/10/20
|
||||
*/
|
||||
public class AdasFrontCarDistanceY extends BasePushMsgModel<AdasDataInfo> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.mogo.module.share.adas;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author nie yunlong
|
||||
* @des
|
||||
* @date 2020/10/20
|
||||
*/
|
||||
public class BasePushMsgModel<T> implements Serializable {
|
||||
//行为
|
||||
private int action;
|
||||
//结果
|
||||
private T result;
|
||||
|
||||
public int getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(int action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(T result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.mogo.module.share.strategyreceiver
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.SystemClock
|
||||
import android.util.ArrayMap
|
||||
import com.mogo.commons.network.SubscribeImpl
|
||||
@@ -32,6 +33,8 @@ private const val SPEED_RECORD_TIME_INTERVAL = 2 * 60 * 1000L
|
||||
private const val STOP_FLAG_THRESHOLD = 30
|
||||
private const val TAG = "BlockUploadStrategy"
|
||||
|
||||
private const val DISTANCE_RECORD_TIME = 3 * 60 * 1000L
|
||||
|
||||
/**
|
||||
* 用于计算拥堵策略
|
||||
*/
|
||||
@@ -44,6 +47,10 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService
|
||||
*/
|
||||
private var stopFlag = 0
|
||||
|
||||
private var currentSpeed: Int = 0
|
||||
|
||||
private var startRecordDistanceTime = 0L
|
||||
|
||||
/**
|
||||
* 拥堵速度计算上报策略
|
||||
* 计算[SPEED_RECORD_TIME_INTERVAL]内的平均速度,并上报
|
||||
@@ -57,6 +64,7 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService
|
||||
* @param speed 当前速度,单位需要是Km/h
|
||||
*/
|
||||
fun recordSpeed(speed: Float) {
|
||||
currentSpeed = speed.toInt()
|
||||
val current = SystemClock.uptimeMillis()
|
||||
if (startRecordTime == 0L) {
|
||||
// 数据初始化
|
||||
@@ -93,6 +101,60 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService
|
||||
}
|
||||
}
|
||||
|
||||
private var lastLat: Double = 0.0
|
||||
private var lastLon: Double = 0.0
|
||||
|
||||
/**
|
||||
* 用于记录行进的距离
|
||||
*/
|
||||
private var tripDistance: Int = 0
|
||||
|
||||
/**
|
||||
* 距离策略上报
|
||||
*
|
||||
* 1. 过去3分钟,行驶距离<1km
|
||||
* 2. 前车距离<5m,获取不到前车距离时,默认此项满足
|
||||
* 3. 当前车速<40km/h
|
||||
*
|
||||
* 满足上述条件,自动上报拥堵
|
||||
*/
|
||||
fun recordLocation(lat: Double, lon: Double) {
|
||||
if (lastLat == 0.0) {
|
||||
// 定位初始化,只判断一个就行了
|
||||
lastLon = lon
|
||||
lastLat = lat
|
||||
} else {
|
||||
// 记录行进距离
|
||||
tripDistance += com.mogo.module.service.Utils.calculateLineDistance(lastLon, lastLat, lon, lat).toInt()
|
||||
val current = SystemClock.uptimeMillis()
|
||||
if (startRecordDistanceTime == 0L) {
|
||||
startRecordDistanceTime = current
|
||||
}
|
||||
|
||||
if (current - startRecordDistanceTime >= DISTANCE_RECORD_TIME) {
|
||||
// 超过阈值,准备判断是否拥堵
|
||||
startRecordDistanceTime = 0L
|
||||
if (tripDistance < 1000 && isClose() && currentSpeed < 40) {
|
||||
Logger.d(TAG, "根据距离,判定为拥堵,准备上报")
|
||||
val p = TanluUploadParams(IMogoTanluProvider.TYPE_BLOCK, IMogoTanluProvider.UPLOAD_FROM_STRATEGY_BLOCK_AUTO)
|
||||
val latLon = MogoLatLng(lat, lon)
|
||||
p.location = latLon
|
||||
apis.tanluApi.uploadRoadCondition(p)
|
||||
} else {
|
||||
Logger.d(TAG, "根据距离,没有判定为拥堵, tripDistance: $tripDistance, frontDistance: $frontDistance, currentSpeed: $currentSpeed")
|
||||
}
|
||||
tripDistance = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 没有有效前车距离或者前车距离小于5m
|
||||
*/
|
||||
private fun isClose(): Boolean {
|
||||
return frontDistance in 1..4
|
||||
}
|
||||
|
||||
private fun uploadAverageSpeed(average: Float) {
|
||||
val params = ArrayMap<String, Any>()
|
||||
params["speed"] = average.toInt()
|
||||
@@ -115,4 +177,26 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val MSG_FRONT_DISTANCE_CHECK = 1001
|
||||
const val FRONT_DISTANCE_CHECK_DELAY = 30_000L
|
||||
}
|
||||
|
||||
private val handler = Handler(Handler.Callback {
|
||||
if (it.what == MSG_FRONT_DISTANCE_CHECK) {
|
||||
frontDistance = -1
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
|
||||
@Volatile
|
||||
private var frontDistance: Int = -1
|
||||
fun refreshFrontDistance(distance: Int) {
|
||||
handler.removeMessages(MSG_FRONT_DISTANCE_CHECK)
|
||||
frontDistance = distance
|
||||
handler.sendEmptyMessageDelayed(MSG_FRONT_DISTANCE_CHECK, FRONT_DISTANCE_CHECK_DELAY)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user