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.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 mapDeviceModel = MutableLiveData<MapDeviceModel>() val deviceDetailModel = MutableLiveData<DeviceDetailModel>() val historyDataModel = MutableLiveData<DeviceHistoryDataModel>() val lastDataModel = MutableLiveData<LastDataModel>() val deleteResult = MutableLiveData<CommonResultModel>() 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(groupId: String, tubeId: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.obtainDeviceDetail(groupId, 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( groupId: String, devcode: String, beginTime: String?, endTime: String? ) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.obtainDeviceHistoryData(groupId, devcode, beginTime, endTime) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success historyDataModel.value = gson.fromJson<DeviceHistoryDataModel>( response, object : TypeToken<DeviceHistoryDataModel>() {}.type ) } else { loadState.value = LoadState.Fail response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) fun obtainTubeLastData(groupId: String, devcode: String) = launch({ val response = RetrofitServiceManager.obtainTubeLastData(groupId, 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() }) fun addDevice( deviceCode: String, deviceName: String, ownerShip: String, interval: String, longitude: String, latitude: String, imagePaths: String, scene: String, addDeviceTime: String, userId: String, deptId: String ) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.addDevice( deviceCode, deviceName, ownerShip, interval, longitude, latitude, imagePaths, scene, addDeviceTime, userId, deptId ) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) fun deleteDeviceById(ids: List<Long>) = launch({ val response = RetrofitServiceManager.deleteDeviceById(ids) val responseCode = response.separateResponseCode() if (responseCode == 200) { deleteResult.value = gson.fromJson<CommonResultModel>( response, object : TypeToken<CommonResultModel>() {}.type ) } else { response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) }