将车聊聊工程代码加入到项目中,保证能跑起来了,后续做架构升级
Signed-off-by: 董宏宇 <martindhy@gmail.com>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package com.mogo.chat.net
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.mogo.chat.exception.ApiException
|
||||
import com.mogo.chat.exception.ApiException.Companion.NULL_REQUEST_DATA_API_EXCEPTION
|
||||
import com.mogo.chat.exception.CommonException.Companion.NETWORK_EXCEPTION
|
||||
import com.mogo.chat.exception.CommonException.Companion.NULL_EXCEPTION
|
||||
import com.mogo.chat.base.BaseResponse
|
||||
import kotlinx.coroutines.*
|
||||
import java.net.SocketTimeoutException
|
||||
import java.net.UnknownHostException
|
||||
import java.util.concurrent.TimeoutException
|
||||
|
||||
class Request<T> {
|
||||
private lateinit var loader: suspend () -> T
|
||||
|
||||
private var start: (() -> Unit)? = null
|
||||
|
||||
private var onSuccess: ((T) -> Unit)? = null
|
||||
|
||||
private var onError: ((java.lang.Exception) -> Unit)? = null
|
||||
|
||||
private var onComplete: (() -> Unit)? = null
|
||||
|
||||
private var addLifecycle: LifecycleOwner? = null
|
||||
|
||||
|
||||
infix fun loader(loader: suspend () -> T) {
|
||||
this.loader = loader
|
||||
}
|
||||
|
||||
infix fun start(start: (() -> Unit)?) {
|
||||
this.start = start
|
||||
}
|
||||
|
||||
infix fun onSuccess(onSuccess: ((T) -> Unit)?) {
|
||||
this.onSuccess = onSuccess
|
||||
}
|
||||
|
||||
infix fun onError(onError: ((java.lang.Exception) -> Unit)?) {
|
||||
this.onError = onError
|
||||
}
|
||||
|
||||
infix fun onComplete(onComplete: (() -> Unit)?) {
|
||||
this.onComplete = onComplete
|
||||
}
|
||||
|
||||
infix fun addLifecycle(addLifecycle: LifecycleOwner?) {
|
||||
this.addLifecycle = addLifecycle
|
||||
}
|
||||
|
||||
fun request() {
|
||||
|
||||
GlobalScope.launch(context = Dispatchers.Main) {
|
||||
|
||||
start?.invoke()
|
||||
try {
|
||||
val deferred = GlobalScope.async(Dispatchers.IO, start = CoroutineStart.LAZY) {
|
||||
loader()
|
||||
}
|
||||
val result = deferred.await()
|
||||
if (result != null && result is BaseResponse<*>) {
|
||||
if (result.code == 0) {
|
||||
onSuccess?.invoke(result)
|
||||
} else {
|
||||
throw ApiException(result.code, result.msg)
|
||||
}
|
||||
} else {
|
||||
throw NULL_REQUEST_DATA_API_EXCEPTION
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
//数据打点
|
||||
if (e == null) {
|
||||
onError?.invoke(NULL_EXCEPTION)
|
||||
}
|
||||
when (e) {
|
||||
is UnknownHostException -> onError?.invoke(NETWORK_EXCEPTION)
|
||||
is TimeoutException -> onError?.invoke(NETWORK_EXCEPTION)
|
||||
is SocketTimeoutException -> onError?.invoke(NETWORK_EXCEPTION)
|
||||
else -> onError?.invoke(java.lang.Exception(e.message))
|
||||
}
|
||||
} finally {
|
||||
onComplete?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> request(buildRequest: Request<T>.() -> Unit) {
|
||||
Request<T>().apply(buildRequest).request()
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.mogo.chat.net
|
||||
|
||||
import com.mogo.chat.base.BaseResponse
|
||||
import com.mogo.chat.model.bean.*
|
||||
import retrofit2.http.*
|
||||
|
||||
interface HttpApi {
|
||||
|
||||
//更改车机显示状态
|
||||
@FormUrlEncoded
|
||||
@POST("yycp-chat-service/car/circle/no/snStatus/v1")
|
||||
suspend fun switchCarStatus(@FieldMap status: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//获取半径周边范围内的活跃车机
|
||||
@FormUrlEncoded
|
||||
@POST("yycp-chat-service/car/circle/no/getSn/v1")
|
||||
suspend fun requestLiveCars(@FieldMap sns: Map<String, String>): BaseResponse<AllUnit>
|
||||
|
||||
//获取某台车机的视频直播流
|
||||
@FormUrlEncoded
|
||||
@POST("appDataService/integratedServices/app/push/no/livePush/v1")
|
||||
suspend fun requestLive(@FieldMap liveBroadcast: Map<String, String>): BaseResponse<LiveBroadcastResult>
|
||||
|
||||
//直播心跳
|
||||
@FormUrlEncoded
|
||||
@POST("appDataService/integratedServices/app/push/no/heartbeat/v1")
|
||||
suspend fun heartBeat(@FieldMap heartBeat: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//语音房间信息,原路径dataService
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/sender/no/createRoom/v1")
|
||||
suspend fun requestRoomInfo(@FieldMap roomInfo: Map<String, String>): BaseResponse<RoomInfo>
|
||||
|
||||
//语音状态同步,原路径dataService
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/voiceRoom/no/operate/v1")
|
||||
suspend fun requestConnectStatus(@Query("sn") sn: String, @FieldMap connectStatus: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
// 开始匹配
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/voiceRoom/no/findMatch/v1")
|
||||
suspend fun startMatch(@Query("sn") sn: String, @FieldMap match: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
// 取消匹配
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/voiceRoom/no/cancleMatch/v1")
|
||||
suspend fun cancelMatch(@Query("sn") sn: String, @FieldMap cancelMatch: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//邀请加入车队
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chat/no/inviteJoinTeam/v1")
|
||||
suspend fun inviteJoinVehicleTeam(@FieldMap inviteVehicleTeam: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//车队状态同步
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chat/no/operateTeamRoom/v1")
|
||||
suspend fun requestVehicleTeamConnectStatus(@FieldMap connectStatus: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//获取配置的加载图片
|
||||
@FormUrlEncoded
|
||||
@POST("deva/voiceGuideConfig/findVoiceGuideConfigBySn/v1")
|
||||
suspend fun getSplashConfig(@FieldMap config: Map<String, String>): BaseResponse<SplashConfig>
|
||||
|
||||
//获取配置的引导话题
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/topic/no/getNowTopic/v1")
|
||||
suspend fun getTopicGuideContent(@FieldMap topGuideContent: Map<String, String>): BaseResponse<TopicRequest>
|
||||
|
||||
// 获取关注状态
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chatFoucs/no/checkFocus/v1")
|
||||
suspend fun requestFocusStatus(@FieldMap focusSn: Map<String, String>): BaseResponse<FocusStatus>
|
||||
|
||||
//黑名单操作
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chatFoucs/no/operate/v1")
|
||||
suspend fun dealBlackList(@FieldMap blackList: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
// 添加关注
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chatFoucs/no/addFocus/v1")
|
||||
suspend fun dealFocus(@FieldMap focusSn: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//查询好友列表
|
||||
@JvmSuppressWildcards
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chatFoucs/no/getFocusPage/v1")
|
||||
suspend fun getFriendPage(@FieldMap focusSn: Map<String, Any>): BaseResponse<FriendData>
|
||||
|
||||
//查询粉丝列表
|
||||
@JvmSuppressWildcards
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chatFoucs/no/getByFocusPage/v1")
|
||||
suspend fun getFocusPage(@FieldMap focusSn: Map<String, Any>): BaseResponse<FocusData>
|
||||
|
||||
// 获取通话状态
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/chat/no/chatStatus/v1")
|
||||
suspend fun getChatStatus(@FieldMap chatStatus: Map<String, String>): BaseResponse<Any>
|
||||
|
||||
//查询用户是否在线
|
||||
@FormUrlEncoded
|
||||
@POST("/yycp-chat-service/car/queryOnLineBySn/v1")
|
||||
suspend fun isOnLine(@FieldMap onLine: Map<String, String>): BaseResponse<OnLineStatus>
|
||||
|
||||
@POST("/yycp-realtimeLocations/realTimeLocationServer/queryRsAncCarAndUserInfoBySns")
|
||||
suspend fun queryUserInfoBySnS(@Body userInfoBySnsRequest: UserInfoBySnsRequest): BaseResponse<UserInfoBySns>
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.mogo.chat.net
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
@PublishedApi
|
||||
internal var ThreadPool =
|
||||
newFixedThreadPoolContext(Runtime.getRuntime().availableProcessors() * 2, "ThreadPool")
|
||||
|
||||
|
||||
fun taskBlockOnMainThread(delayTime: Long = 0, job: suspend () -> Unit) = GlobalScope.launch(Dispatchers.Main) {
|
||||
delay(delayTime)
|
||||
job()
|
||||
}
|
||||
|
||||
/**
|
||||
* 并发执行,常用于最外层,延时操作
|
||||
* 特点带返回值
|
||||
*/
|
||||
fun <T> taskDelayAsync(delayTime: Long = 0, job: suspend () -> T) = GlobalScope.async(ThreadPool) {
|
||||
delay(delayTime)
|
||||
job()
|
||||
}
|
||||
|
||||
/**
|
||||
* 并发执行
|
||||
*/
|
||||
fun <T> taskAsync(job:suspend () -> T) = GlobalScope.async {
|
||||
job()
|
||||
}
|
||||
Reference in New Issue
Block a user