优化运行日志上传重传机制

This commit is contained in:
sxy0 2024-11-06 15:49:04 +08:00
parent 0a20d978ef
commit 3f82f1198b
14 changed files with 86 additions and 61 deletions

View File

@ -29,19 +29,4 @@ class HttpServer {
server.listen(3389) server.listen(3389)
Log.d(TAG, "run: httpServer start... listen 3389") Log.d(TAG, "run: httpServer start... listen 3389")
} }
private fun getJSONFromReq(req: AsyncHttpServerRequest): JSONObject? {
var obj: JSONObject? = null
val body = req.getBody<AsyncHttpRequestBody<*>>()
if (body is JSONObjectBody) {
obj = body.get()
} else if (body is StringBody) {
try {
obj = JSONObject(body.get())
} catch (e: JSONException) {
e.printStackTrace()
}
}
return obj
}
} }

View File

@ -73,7 +73,7 @@ class TaskSocketServer(private val context: Context, port: Int) {
try { try {
//只保持一个链接,新的进来就把老的断开 //只保持一个链接,新的进来就把老的断开
val socket = soc!!.accept() val socket = soc!!.accept()
socket.soTimeout = 5000 * 5 socket.soTimeout = 5000
if (client != null) { if (client != null) {
client!!.close() client!!.close()
} }

View File

@ -19,7 +19,6 @@ import okhttp3.RequestBody
import com.zhouyou.http.callback.SimpleCallBack import com.zhouyou.http.callback.SimpleCallBack
import com.zhouyou.http.exception.ApiException import com.zhouyou.http.exception.ApiException
import okhttp3.Request import okhttp3.Request
import java.util.concurrent.CountDownLatch
/** /**
@ -29,49 +28,52 @@ import java.util.concurrent.CountDownLatch
* @description * @description
*/ */
object EasyHttpTool { object EasyHttpTool {
val TAG = this.javaClass.simpleName
/** /**
* 提交运行时长 * 提交运行时长
* @param runtimeJson String * @param runtimeJson String
*/ */
lateinit var latch: CountDownLatch // lateinit var latch: CountDownLatch
var isSendSuccess = false // var isSendSuccess = false
fun sendRunTimeToServer(runtimeJson: String) { fun sendRunTimeToServer(runtimeJson: String) {
"sendRunTimeToServer run".logd() "sendRunTimeToServer run".logd()
latch = CountDownLatch(1) // latch = CountDownLatch(1)
sendRuntimeToServerRequest(runtimeJson) sendRuntimeToServerRequest(runtimeJson)
Thread {
latch.await() // Thread {
var count = 10 // latch.await()
// 网络可用时每一分钟发一次请求 // var count = 10
while (!isSendSuccess && count > 0) { // // 网络可用时每一分钟发一次请求
latch = CountDownLatch(1) // while (!isSendSuccess && count > 0) {
sendRuntimeToServerRequest(runtimeJson) // latch = CountDownLatch(1)
Log.d("EasyHttpTool", "sendRunTimeToServer: 重发次数${count}") // sendRuntimeToServerRequest(runtimeJson)
latch.await() // Log.d("EasyHttpTool", "sendRunTimeToServer: 重发次数${count}")
count--; // latch.await()
Thread.sleep(1000 * 60) // count--;
} // Thread.sleep(1000 * 60)
// 网络不可用时监听网络状态是否发送请求 // }
if (!isSendSuccess) { // // 网络不可用时监听网络状态是否发送请求
var isRunning = true // if (!isSendSuccess) {
while (isRunning) { // var isRunning = true
val networkAvailable = NetworkChangeReceiver.isNetworkAvailable // while (isRunning) {
if (networkAvailable) { // val networkAvailable = NetworkChangeReceiver.isNetworkAvailable
latch = CountDownLatch(1) // if (networkAvailable) {
sendRuntimeToServerRequest(runtimeJson) // latch = CountDownLatch(1)
Log.d("EasyHttpTool", "sendRunTimeToServer: 重发网络${networkAvailable}") // sendRuntimeToServerRequest(runtimeJson)
latch.await() // Log.d("EasyHttpTool", "sendRunTimeToServer: 重发网络${networkAvailable}")
if (isSendSuccess) { // latch.await()
isRunning = false // if (isSendSuccess) {
} // isRunning = false
} // }
Thread.sleep(1000 * 60) // }
} // Thread.sleep(1000 * 60)
} // }
}.start() // }
// }.start()
} }
fun sendRuntimeToServerRequest(runtimeJson: String) { fun sendRuntimeToServerRequest(runtimeJson: String) {
Log.d("EasyHttpTool", "sendRuntimeToServerRequest: ") Log.d("EasyHttpTool", "sendRuntimeToServerRequest: ")
val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull() val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull()
@ -93,14 +95,14 @@ object EasyHttpTool {
.execute(object : SimpleCallBack<String>() { .execute(object : SimpleCallBack<String>() {
override fun onError(e: ApiException?) { override fun onError(e: ApiException?) {
"上传运行时长失败:${e?.message}".loge() "上传运行时长失败:${e?.message}".loge()
isSendSuccess = false // isSendSuccess = false
latch.countDown() // latch.countDown()
} }
override fun onSuccess(t: String?) { override fun onSuccess(t: String?) {
"上传运行时长成功:${t}".loge() "上传运行时长成功:${t}".loge()
isSendSuccess = true // isSendSuccess = true
latch.countDown() // latch.countDown()
} }
}) })
} }
@ -109,16 +111,51 @@ object EasyHttpTool {
/** /**
* 提交运行时长 * 提交运行时长
*/ */
private const val retryInterval: Long = 1000 * 60
// private const val retryInterval: Long = 1000
fun sendRunTimeToServer() { fun sendRunTimeToServer() {
val sumTimeList = DaoUtil.getRunTimeDao().queryBuilder().where( val sumTimeList = DaoUtil.getRunTimeDao().queryBuilder().where(
RunTimeDataDao.Properties.IsUpload.eq(0) RunTimeDataDao.Properties.IsUpload.eq(0)
).list() ).list()
"未提交的运行时长数量sumTimeList size: ${sumTimeList?.size}".logd() // "未提交的运行时长数量sumTimeList size: ${sumTimeList?.size}".logd()
Log.d(TAG, "sendRunTimeToServer: 未提交的运行时长数量: ${sumTimeList?.size}")
if (!sumTimeList.isNullOrEmpty()) { if (!sumTimeList.isNullOrEmpty()) {
for (runTimeData in sumTimeList) { for (runTimeData in sumTimeList) {
Thread {
sendRunLogToServer(runTimeData) var isSendSuccess = sendRunLogToServer(runTimeData)
var count = 10
// 是否需要重发
// 网络可用时每一分钟发一次请求
while (!isSendSuccess && count > 0) {
isSendSuccess = sendRunLogToServer(runTimeData)
Log.d(TAG, "sendRunTimeToServer: 重发次数${11 - count}")
if (isSendSuccess) {
Log.d(TAG, "sendRunTimeToServer: 发送成功 重发次数${11 - count}")
}
count--;
Thread.sleep(retryInterval)
}
// 网络不可用时监听网络状态是否发送请求
if (!isSendSuccess) {
var isRunning = true
while (isRunning) {
var available = NetworkChangeReceiver.isNetworkAvailable
Log.d(TAG, "sendRunTimeToServer: 重发网络$available")
if (available) {
isSendSuccess = sendRunLogToServer(runTimeData)
if (isSendSuccess) {
isRunning = false
Log.d(TAG, "sendRunTimeToServer: 发送成功重发网络$available")
}
}
Thread.sleep(retryInterval)
}
}
Log.d(
"EasyHttpTool",
"sendRunTimeToServer: 运行日志发送成功${runTimeData.cardId}"
)
}.start()
// val runtimeJson = RuntimeJson(runTimeData.cardId,runTimeData.sumRuntime,runTimeData.startRuntime) // val runtimeJson = RuntimeJson(runTimeData.cardId,runTimeData.sumRuntime,runTimeData.startRuntime)
// val runtimeJsonStr = Gson().toJson(runtimeJson) // val runtimeJsonStr = Gson().toJson(runtimeJson)
@ -163,7 +200,7 @@ object EasyHttpTool {
/** /**
* 上传1条运行日志 * 上传1条运行日志
*/ */
fun sendRunLogToServer(runTimeData: RunTimeData) { fun sendRunLogToServer(runTimeData: RunTimeData): Boolean {
// val logJson = ProcessingCommands.gson.toJson(dbPlayerLogs) // val logJson = ProcessingCommands.gson.toJson(dbPlayerLogs)
val runtimeJson = val runtimeJson =
RuntimeJson(runTimeData.cardId, runTimeData.sumRuntime, runTimeData.startRuntime) RuntimeJson(runTimeData.cardId, runTimeData.sumRuntime, runTimeData.startRuntime)
@ -191,11 +228,14 @@ object EasyHttpTool {
runTimeData.isUpload = 1 runTimeData.isUpload = 1
DaoUtil.getRunTimeDao().update(runTimeData) DaoUtil.getRunTimeDao().update(runTimeData)
"将提交的runTimeData标记成已上传".logd() "将提交的runTimeData标记成已上传".logd()
return true
} else { } else {
"上传运行时长失败".loge() "上传运行时长失败".loge()
return false
} }
} catch (e: Exception) { } catch (e: Exception) {
"上传运行时长失败:${e?.message}".loge() "上传运行时长失败:${e?.message}".loge()
return false
} }
} }

View File

@ -1,6 +1,6 @@
<resources> <resources>
<string name="app_name">TaxiApp</string> <!-- <string name="app_name">TaxiApp</string>-->
<!-- <string name="app_name">VehiclePlayer</string>--> <string name="app_name">VehiclePlayer</string>
<string name="title_activity_top_level">TopLevelActivity</string> <string name="title_activity_top_level">TopLevelActivity</string>
<!-- <string name="media_resource_base">android.resource://net.sysolution.taxiapp/</string>--> <!-- <string name="media_resource_base">android.resource://net.sysolution.taxiapp/</string>-->