package com.casic.smarttube.vm import android.content.Context import androidx.lifecycle.MutableLiveData import com.casic.smarttube.extensions.separateResponseCode import com.casic.smarttube.extensions.toErrorMessage import com.casic.smarttube.model.CommonResultModel 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.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.pengxh.kt.lite.base.BaseViewModel import com.pengxh.kt.lite.extensions.launch import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.utils.LoadState class DeviceViewModel : BaseViewModel() { private val gson = Gson() val mapDeviceData = MutableLiveData<MapDeviceModel>() val deviceDetailData = MutableLiveData<DeviceDetailModel>() val historyDataData = MutableLiveData<DeviceHistoryDataModel>() val lastDataData = MutableLiveData<LastDataModel>() val deleteResultData = MutableLiveData<CommonResultModel>() fun getMapDeviceList(context: Context) = launch({ val response = RetrofitServiceManager.getMapDeviceList() val responseCode = response.separateResponseCode() if (responseCode == 200) { mapDeviceData.value = gson.fromJson<MapDeviceModel>( response, object : TypeToken<MapDeviceModel>() {}.type ) } else { response.toErrorMessage().show(context) } }, { it.printStackTrace() }) fun getDeviceDetail(context: Context, groupId: String, tubeId: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.getDeviceDetail(groupId, tubeId) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success deviceDetailData.value = gson.fromJson<DeviceDetailModel>( response, object : TypeToken<DeviceDetailModel>() {}.type ) } else { loadState.value = LoadState.Fail response.toErrorMessage().show(context) } }, { loadState.value = LoadState.Fail it.printStackTrace() }) fun getDeviceHistoryData( context: Context, groupId: String, devcode: String, beginTime: String?, endTime: String? ) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.getDeviceHistoryData(groupId, devcode, beginTime, endTime) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success historyDataData.value = gson.fromJson<DeviceHistoryDataModel>( response, object : TypeToken<DeviceHistoryDataModel>() {}.type ) } else { loadState.value = LoadState.Fail response.toErrorMessage().show(context) } }, { loadState.value = LoadState.Fail it.printStackTrace() }) fun getTubeLastData(context: Context, groupId: String, devcode: String) = launch({ val response = RetrofitServiceManager.getTubeLastData(groupId, devcode) when (response.separateResponseCode()) { 200 -> { lastDataData.value = gson.fromJson<LastDataModel>( response, object : TypeToken<LastDataModel>() {}.type ) } 500 -> { lastDataData.value = LastDataModel() } else -> { response.toErrorMessage().show(context) } } }, { loadState.value = LoadState.Fail it.printStackTrace() }) fun addDevice( context: Context, deviceCode: String, deviceName: String, ownerShip: String, interval: String, longitude: String, latitude: String, imagePaths: String, scene: String, addDeviceTime: String ) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.addDevice( deviceCode, deviceName, ownerShip, interval, longitude, latitude, imagePaths, scene, addDeviceTime ) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail response.toErrorMessage().show(context) } }, { loadState.value = LoadState.Fail it.printStackTrace() }) fun deleteDeviceById(context: Context, ids: List<Long>) = launch({ val response = RetrofitServiceManager.deleteDeviceById(ids) val responseCode = response.separateResponseCode() if (responseCode == 200) { deleteResultData.value = gson.fromJson<CommonResultModel>( response, object : TypeToken<CommonResultModel>() {}.type ) } else { response.toErrorMessage().show(context) } }, { it.printStackTrace() }) }