opt Block Strategy

This commit is contained in:
tongchenfei
2020-12-28 15:36:10 +08:00
parent 06da34567f
commit 003e085aa8
2 changed files with 26 additions and 14 deletions

View File

@@ -3,24 +3,14 @@ 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
import com.mogo.commons.network.Utils
import com.mogo.map.MogoLatLng
import com.mogo.module.share.bean.AverateSpeedResponse
import com.mogo.module.share.constant.HttpConstant
import com.mogo.module.share.manager.ServiceApisManager
import com.mogo.module.share.net.ShareApiService
import com.mogo.service.IMogoServiceApis
import com.mogo.service.share.IMogoTanluProvider
import com.mogo.service.share.TanluUploadParams
import com.mogo.utils.logger.Logger
import com.mogo.utils.network.RequestOptions
import com.mogo.utils.network.utils.GsonUtil
import com.mogo.utils.storage.SharedPrefsMgr
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.RequestBody
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
@@ -35,7 +25,7 @@ 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 TIME_FAULT_TOLERANT = 60 * 60 * 1000L
private const val DISTANCE_RECORD_TIME = 3 * 60 * 1000L
private const val MORNING_PEAK_START_MINUTE = 7 * 60 + 30
@@ -48,6 +38,8 @@ private const val DISTANCE_MORNING_PEAK_COUNT = "DISTANCE_MORNING_PEAK_COUNT"
private const val DISTANCE_NIGHT_PEAK_COUNT = "DISTANCE_NIGHT_PEAK_COUNT"
private const val DISTANCE_NORMAL_COUNT = "DISTANCE_NORMAL_COUNT"
private const val START_BLOCK_COUNT_DATE = "START_BLOCK_COUNT_DATE"
private const val SPEED_MORNING_PEAK_COUNT = "SPEED_MORNING_PEAK_COUNT"
private const val SPEED_NIGHT_PEAK_COUNT = "SPEED_NIGHT_PEAK_COUNT"
@@ -118,8 +110,20 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService
lastLon = lon
lastLat = lat
}
if (current - startRecordTime >= DISTANCE_RECORD_TIME) {
val diff = current - startRecordTime
if (diff in DISTANCE_RECORD_TIME until TIME_FAULT_TOLERANT) {
// 重置count data
val countDate = SharedPrefsMgr.getInstance(context).getLong(START_BLOCK_COUNT_DATE, 0)
if (countDate == 0L || isDifferentDay(countDate, System.currentTimeMillis())) {
// 不是同一天,重置各种计数,重新开始计数
SharedPrefsMgr.getInstance(context).putLong(START_BLOCK_COUNT_DATE, System.currentTimeMillis())
SharedPrefsMgr.getInstance(context).putInt(DISTANCE_MORNING_PEAK_COUNT, 0)
SharedPrefsMgr.getInstance(context).putInt(DISTANCE_NIGHT_PEAK_COUNT, 0)
SharedPrefsMgr.getInstance(context).putInt(DISTANCE_NORMAL_COUNT, 0)
SharedPrefsMgr.getInstance(context).putInt(SPEED_NORMAL_COUNT, 0)
SharedPrefsMgr.getInstance(context).putInt(SPEED_NIGHT_PEAK_COUNT, 0)
SharedPrefsMgr.getInstance(context).putInt(SPEED_MORNING_PEAK_COUNT, 0)
}
var blockFromSpeed = false
var blockFromDistance = false
@@ -299,4 +303,11 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService
val minute = Calendar.getInstance().get(Calendar.MINUTE) + hour * 60
return minute in NIGHT_PEAK_START_MINUTE..NIGHT_PEAK_STOP_MINUTE
}
private fun isDifferentDay(a: Long, b: Long): Boolean {
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
val aDate = dateFormat.format(a)
val bDate = dateFormat.format(b)
return aDate != bDate
}
}