package cn.trans88.taxiappkotlin.net import cn.trans88.taxiappkotlin.Configurations import cn.trans88.taxiappkotlin.TaxiApp import cn.trans88.taxiappkotlin.ext.logd import cn.trans88.taxiappkotlin.ext.loge import cn.trans88.taxiappkotlin.logic.dao.AdvertiseDao import cn.trans88.taxiappkotlin.logic.dao.DaoUtil import cn.trans88.taxiappkotlin.logic.dao.RunTimeDataDao import cn.trans88.taxiappkotlin.logic.model.PlayLogger import cn.trans88.taxiappkotlin.logic.model.RunTimeData import cn.trans88.taxiappkotlin.logic.model.RuntimeJson import cn.trans88.taxiappkotlin.logic.network.ProcessingCommands import cn.trans88.taxiappkotlin.ui.advertise.AdvertiseType import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.zhouyou.http.EasyHttp import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody import com.zhouyou.http.callback.SimpleCallBack import com.zhouyou.http.exception.ApiException import okhttp3.Request import java.util.stream.Collectors /** * * @Author: LJH * @Time: 2024/2/28 * @description: */ object EasyHttpTool { /** * 提交运行时长 * @param runtimeJson String */ fun sendRunTimeToServer(runtimeJson: String){ "sendRunTimeToServer run".logd() val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull() val body = RequestBody.create(parse, runtimeJson) EasyHttp.post(Configurations.config(TaxiApp.instance()).logServiceUrl() + "postRunningTimeLog") .readTimeOut(80*1000) .writeTimeOut(80*1000) .connectTimeout(80*1000) .headers("Authorization", "Bearer ${Configurations.config(TaxiApp.instance()).appToken()}") .headers("Accept-Encoding", "gzip") .requestBody(body) .retryCount(5)//本次请求重试次数 .retryDelay(600)//本次请求重试延迟时间600ms .execute(object :SimpleCallBack(){ override fun onError(e: ApiException?) { "上传运行时长失败:${e?.message}".loge() } override fun onSuccess(t: String?) { "上传运行时长成功:${t}".loge() } }) } /** * 提交运行时长 */ fun sendRunTimeToServer(){ val sumTimeList = DaoUtil.getRunTimeDao().queryBuilder().where( RunTimeDataDao.Properties.IsUpload.eq(0)).list() "未提交的运行时长数量sumTimeList size: ${sumTimeList?.size}".logd() if (!sumTimeList.isNullOrEmpty()){ for (runTimeData in sumTimeList) { val runtimeJson = RuntimeJson(runTimeData.cardId,runTimeData.sumRuntime,runTimeData.startRuntime) val runtimeJsonStr = Gson().toJson(runtimeJson) "提交运行时长sendRunTimeToServer:$runtimeJsonStr".logd() val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull() val body = RequestBody.create(parse, runtimeJsonStr) EasyHttp.post(Configurations.config(TaxiApp.instance()).logServiceUrl() + "postRunningTimeLog") .readTimeOut(80*1000) .writeTimeOut(80*1000) .connectTimeout(80*1000) .headers("Authorization", "Bearer ${Configurations.config(TaxiApp.instance()).appToken()}") .headers("Accept-Encoding", "gzip") .requestBody(body) .retryCount(1)//本次请求重试次数 .retryDelay(1000)//本次请求重试延迟时间1000ms .execute(object :SimpleCallBack(){ override fun onError(e: ApiException?) { "上传运行时长失败:${e?.message}".loge() } override fun onSuccess(t: String?) { "上传运行时长成功:${t}".loge() runTimeData.isUpload = 1 DaoUtil.getRunTimeDao().update(runTimeData) "将提交的runTimeData标记成已上传".logd() } }) } // val lists:List = sumTimeList.map { // RuntimeJson(it.cardId,it.sumRuntime,it.startRuntime) // } // val runtimeJson = Gson().toJson(lists) } } /** * 提交日志摘要 * @param gpsJson String */ fun sendPlayLogToServer(logJson: String){ "提交日志摘要 $logJson".logd() val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull() val body = RequestBody.create(parse, logJson) EasyHttp.post(Configurations.config(TaxiApp.instance()).logServiceUrl() + "postPlayerLogPlus") .readTimeOut(80*1000) .writeTimeOut(80*1000) .connectTimeout(80*1000) .headers("Authorization", "Bearer ${Configurations.config(TaxiApp.instance()).appToken()}") .headers("Accept-Encoding", "gzip") .requestBody(body) .retryCount(5) //本次请求重试次数 .retryDelay(600) //本次请求重试延迟时间600ms .execute(object :SimpleCallBack(){ override fun onError(e: ApiException?) { "上传日志摘要失败:${e?.message}".loge() } override fun onSuccess(t: String?) { "上传日志摘要成功 $t".loge() } }) } /** * 提交GPS数据 * @param gpsJson String */ fun sendGPSToServer(gpsJson: String){ "提交GPS数据 $gpsJson".logd() val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull() val body = RequestBody.create(parse, gpsJson) EasyHttp.post(Configurations.config(TaxiApp.instance()).logServiceUrl() + "postGpsLocationLog") .readTimeOut(80*1000) .writeTimeOut(80*1000) .connectTimeout(80*1000) .headers("Authorization", "Bearer ${Configurations.config(TaxiApp.instance()).appToken()}") .requestBody(body) .retryCount(5) //本次请求重试次数 .retryDelay(600) //本次请求重试延迟时间600ms .syncRequest(true)//设置同步请求 .execute(object :SimpleCallBack(){ override fun onError(e: ApiException?) { "上传GPS数据失败:${e?.message}".loge() } override fun onSuccess(t: String?) { "上传GPS数据成功 $t".loge() } }) } /** * 上传100条播放日志 */ fun sendPlayLogToServer(dbPlayerLogs:List){ val logJson = ProcessingCommands.gson.toJson(dbPlayerLogs) "提交日志Json $logJson".logd() val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull() val body = RequestBody.create(parse, logJson) val url = Configurations.config(TaxiApp.instance()).logServiceUrl() + "postPlayerLog" try { val request = Request.Builder() .url(url) .addHeader("Authorization","Bearer ${Configurations.config(TaxiApp.instance()).appToken()}") .addHeader("Accept-Encoding","gzip") .post(body) .build() val response = OkHttpTool.getInstance().client.newCall(request).execute() "response code:${response?.code}".logd() "response body:${response?.body?.string().toString()}".logd() if (response.isSuccessful){ "上传日志成功 ".logd() DaoUtil.getPlayLogger().deleteInTx(dbPlayerLogs) "删除日志成功".logd() } else { "上传日志失败".loge() Thread.sleep(60*1000L) } } catch (e: Exception) { "提交失败Exception:${e.message}".loge() Thread.sleep(60*1000L) } // EasyHttp.post(url) // .readTimeOut(80*1000) // .writeTimeOut(80*1000) // .connectTimeout(80*1000) // .headers("Authorization", "Bearer ${Configurations.config(TaxiApp.instance()).appToken()}") // .headers("Accept-Encoding", "gzip") // .requestBody(body) // .retryCount(5) //本次请求重试次数 // .retryDelay(600) //本次请求重试延迟时间600ms // .syncRequest(true)//设置同步请求 // .execute(object :SimpleCallBack(){ // override fun onError(e: ApiException?) { // "上传日志失败:${e?.message}".loge() // try { // Thread.sleep(60*1000L) // } catch (e: Exception){ // "Exception:${e.message}".loge() // e.printStackTrace() // } // } // // override fun onSuccess(t: String?) { // "上传日志成功 $t".loge() // DaoUtil.getPlayLogger().deleteInTx(tenPlayLogs) // "删除日志成功".loge() // } // }) } }