Newer
Older
CasicSmartTube / app / src / main / java / com / casic / smarttube / retrofit / RetrofitServiceManager.kt
package com.casic.smarttube.retrofit

import com.casic.smarttube.model.UserDetailModel
import com.casic.smarttube.utils.AuthenticationHelper
import com.casic.smarttube.utils.LocaleConstant
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.utils.RetrofitFactory.createRetrofit
import com.pengxh.kt.lite.utils.SaveKeyValues
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File


object RetrofitServiceManager {

    private val api by lazy {
        val httpConfig = SaveKeyValues.getValue(
            LocaleConstant.DEFAULT_SERVER_CONFIG, LocaleConstant.SERVER_BASE_URL
        ) as String
        createRetrofit<RetrofitService>(httpConfig)
    }
    private val gson = Gson()

    /**
     * 验证PublicKey
     */
    suspend fun authenticate(): String {
        return api.getPublicKey()
    }

    /**
     * 登录并获取Token
     */
    suspend fun login(sid: String, account: String, secretKey: String): String {
        return api.getLoginResult(sid, account, secretKey)
    }

    /**
     * 退出登录
     */
    suspend fun loginOut(): String {
        return api.loginOut(AuthenticationHelper.token!!)
    }

    /**
     * 获取用户信息
     */
    suspend fun getUserDetail(): String {
        return api.getUserDetail(AuthenticationHelper.token!!)
    }

    /**
     * 修改密码
     */
    suspend fun changePassword(oldPwd: String, newPwd: String): String {
        return api.changePassword(AuthenticationHelper.token!!, oldPwd, newPwd)
    }

    /**
     * 更新APK版本
     */
    suspend fun getApplicationVersion(): String {
        return api.getApplicationVersion(AuthenticationHelper.token!!)
    }

    /**
     * 地图设备列表
     */
    suspend fun getMapDeviceList(): String {
        return api.getMapDeviceList(AuthenticationHelper.token!!)
    }

    /**
     * 项目列表
     */
    suspend fun getProGroupList(): String {
        return api.getProGroupList(AuthenticationHelper.token!!)
    }

    /**
     * 根据项目ID查询该项目下的设备列表
     */
    suspend fun getDeviceListByGroup(wellGroupId: String): String {
        return api.getDeviceListByGroup(AuthenticationHelper.token!!, wellGroupId)
    }

    /**
     * 获取设备分页列表
     */
    suspend fun getDeviceListByPage(groupId: String, order: Int, page: Int): String {
        return api.getDeviceListByPage(
            AuthenticationHelper.token!!, groupId, order, page, LocaleConstant.PAGE_LIMIT
        )
    }

    /**
     * 获取设备详情
     */
    suspend fun getDeviceDetail(groupId: String, tubeId: String): String {
        return api.getDeviceDetail(AuthenticationHelper.token!!, groupId, tubeId)
    }

    /**
     * 根据部门获取区ID
     */
    suspend fun getAreaByDept(deptId: String): String {
        return api.getAreaByDept(AuthenticationHelper.token!!, deptId)
    }

    /**
     * 获取区/县等
     */
    suspend fun getDistrict(pid: String): String {
        return api.getDistrict(AuthenticationHelper.token!!, pid)
    }

    /**
     * 获取街道
     */
    suspend fun getStreet(pid: String): String {
        return api.getStreet(AuthenticationHelper.token!!, pid)
    }

    /**
     * 根据设备编号获取历史数据
     */
    suspend fun getDeviceHistoryData(
        groupId: String, devcode: String, beginTime: String?, endTime: String?
    ): String {
        return api.getDeviceHistoryData(
            AuthenticationHelper.token!!, groupId, devcode, beginTime, endTime
        )
    }

    /**
     * 上传图片
     */
    suspend fun uploadImage(image: File): String {
        val requestBody = image.asRequestBody("image/png".toMediaTypeOrNull())
        val imagePart = MultipartBody.Part.createFormData("file", image.name, requestBody)
        return api.uploadImage(AuthenticationHelper.token!!, imagePart)
    }

    /**
     * 获取设备最新数据
     */
    suspend fun getTubeLastData(groupId: String, devcode: String): String {
        return api.getTubeLastData(AuthenticationHelper.token!!, groupId, devcode)
    }

    /**
     * 添加设备
     */
    suspend fun addDevice(
        deviceCode: String,
        deviceName: String,
        ownerShip: String,
        interval: String,
        longitude: String,
        latitude: String,
        imagePaths: String,
        scene: String,
        addDeviceTime: String
    ): String {
        val paramObject = JSONObject()
        paramObject.put("deviceCode", deviceCode)
        paramObject.put("deviceName", deviceName)
        paramObject.put("projectName", ownerShip)
        paramObject.put("frequency", interval)
        paramObject.put("lng", longitude)
        paramObject.put("lat", latitude)
        paramObject.put("image", imagePaths)
        paramObject.put("description", scene)
        paramObject.put("createTime", addDeviceTime)

        val userDetailJson = SaveKeyValues.getValue(LocaleConstant.USER_DETAIL_KEY, "") as String
        if (userDetailJson.isNotBlank()) {
            val userData = gson.fromJson<UserDetailModel.Data>(
                userDetailJson, object : TypeToken<UserDetailModel.Data>() {}.type
            )
            paramObject.put("ownerId", userData.id)
            paramObject.put("deptId", userData.deptId)
        }

        paramObject.put("version", "19")
        val requestBody = paramObject.toString().toRequestBody(
            "application/json;charset=UTF-8".toMediaType()
        )
        return api.addDevice(AuthenticationHelper.token!!, requestBody)
    }

    /**
     * 获取项目编号列表
     */
    suspend fun getGroupList(): String {
        return api.getGroupList(AuthenticationHelper.token!!)
    }

    /**
     * 修改项目编号
     */
    suspend fun changeGroupId(oldGroupId: String, newGroupId: String): String {
        return api.changeGroupId(AuthenticationHelper.token!!, oldGroupId, newGroupId)
    }

    /**
     * 删除设备
     */
    suspend fun deleteDeviceById(ids: List<Long>): String {
        val requestBody = ids.toString().toRequestBody(
            "application/json;charset=UTF-8".toMediaType()
        )
        return api.deleteDeviceById(AuthenticationHelper.token!!, requestBody)
    }
}