Newer
Older
CasicSmartTube / app / src / main / java / com / casic / smarttube / vm / DeviceViewModel.kt
package com.casic.smarttube.vm

import androidx.lifecycle.MutableLiveData
import com.casic.smarttube.base.BaseApplication
import com.casic.smarttube.extensions.separateResponseCode
import com.casic.smarttube.extensions.toErrorMessage
import com.casic.smarttube.model.DeviceDetailModel
import com.casic.smarttube.model.DeviceHistoryDataModel
import com.casic.smarttube.model.LastDataModel
import com.casic.smarttube.model.MapDeviceModel
import com.casic.smarttube.utils.retrofit.RetrofitServiceManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.extensions.launch
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.vm.BaseViewModel
import com.pengxh.kt.lite.vm.LoadState

class DeviceViewModel : BaseViewModel() {

    private val gson = Gson()
    val mapDeviceModel = MutableLiveData<MapDeviceModel>()
    val deviceDetailModel = MutableLiveData<DeviceDetailModel>()
    val historyDataModel = MutableLiveData<DeviceHistoryDataModel>()
    val lastDataModel = MutableLiveData<LastDataModel>()

    fun obtainMapDeviceList() = launch({
        val response = RetrofitServiceManager.obtainMapDeviceList()
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            mapDeviceModel.value = gson.fromJson<MapDeviceModel>(
                response, object : TypeToken<MapDeviceModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })

    fun obtainDeviceDetail(tubeId: String) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.obtainDeviceDetail(tubeId)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            deviceDetailModel.value = gson.fromJson<DeviceDetailModel>(
                response, object : TypeToken<DeviceDetailModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })

    fun obtainDeviceHistoryData(devcode: String, beginTime: String?, endTime: String?) = launch({
        val response = RetrofitServiceManager.obtainDeviceHistoryData(devcode, beginTime, endTime)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            historyDataModel.value = gson.fromJson<DeviceHistoryDataModel>(
                response, object : TypeToken<DeviceHistoryDataModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })

    fun obtainTubeLastData(devcode: String) = launch({
        val response = RetrofitServiceManager.obtainTubeLastData(devcode)
        when (response.separateResponseCode()) {
            200 -> {
                lastDataModel.value = gson.fromJson<LastDataModel>(
                    response, object : TypeToken<LastDataModel>() {}.type
                )
            }
            500 -> {
                lastDataModel.value = LastDataModel()
            }
            else -> {
                response.toErrorMessage().show(BaseApplication.obtainInstance())
            }
        }
    }, {
        it.printStackTrace()
    })
}