增加定时任务更新货柜数据过期状态
This commit is contained in:
@@ -32,8 +32,6 @@ import com.shuwei.intelligent.shelves.net.RespData
|
||||
import com.shuwei.intelligent.shelves.net.UiState
|
||||
import com.shuwei.intelligent.shelves.net.apiService
|
||||
import com.shuwei.intelligent.shelves.serial.SerialPortManager
|
||||
import com.shuwei.intelligent.shelves.task.SyncTask
|
||||
import com.shuwei.intelligent.shelves.task.TaskManager
|
||||
import com.shuwei.intelligent.shelves.utils.HexUtils
|
||||
import com.shuwei.intelligent.shelves.utils.IntervalExecutor
|
||||
import com.shuwei.intelligent.shelves.utils.binaryToHex
|
||||
@@ -42,9 +40,8 @@ import com.shuwei.intelligent.shelves.utils.ext.gone
|
||||
import com.shuwei.intelligent.shelves.utils.ext.toJsonString
|
||||
import com.shuwei.intelligent.shelves.utils.ext.toast
|
||||
import com.shuwei.intelligent.shelves.utils.ext.visible
|
||||
import com.shuwei.intelligent.shelves.utils.f2C
|
||||
import com.shuwei.intelligent.shelves.utils.hexToBinary
|
||||
import com.shuwei.intelligent.shelves.utils.hexToDec
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
@@ -85,7 +82,8 @@ class HomeActivity : BaseActivity() {
|
||||
//410激活码
|
||||
// const val ACTIVE_CMD = "${HEADER}000281C5D70A7D42894EA17650F5D0BB516838E7863C9ABD766C38958AFF0F3C062D${FOOTER}"
|
||||
//1楼餐厅
|
||||
const val ACTIVE_CMD = "${HEADER}000281C5D70A7D428B44A57454F5DACA241938938B4B9CBD73673E9C8E7FC0C4992D${FOOTER}"
|
||||
const val ACTIVE_CMD =
|
||||
"${HEADER}000281C5D70A7D428B44A57454F5DACA241938938B4B9CBD73673E9C8E7FC0C4992D${FOOTER}"
|
||||
|
||||
//华氏温度单位
|
||||
const val F_TEMP_CMD = "${HEADER}0F01390000${FOOTER}"
|
||||
@@ -174,7 +172,8 @@ class HomeActivity : BaseActivity() {
|
||||
|
||||
// TaskManager.startTask()
|
||||
|
||||
startTask()
|
||||
saveGoodsTask()
|
||||
overdueTask()
|
||||
|
||||
// var androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
|
||||
//// androidId = "d0e7a2d5f25c775c"
|
||||
@@ -598,7 +597,9 @@ class HomeActivity : BaseActivity() {
|
||||
lifecycleScope.launch {
|
||||
SerialPortManager.close()
|
||||
}
|
||||
TaskManager.cancelTask()
|
||||
// TaskManager.cancelTask()
|
||||
saveTaskJob?.cancel()
|
||||
overdueTaskJob?.cancel()
|
||||
EventBus.getDefault().unregister(this)
|
||||
super.onDestroy()
|
||||
}
|
||||
@@ -736,15 +737,44 @@ class HomeActivity : BaseActivity() {
|
||||
shelfAdapter.notifyItemChanged(list.indexOf(item))
|
||||
}
|
||||
|
||||
private val intervalExecutor by lazy { IntervalExecutor() }
|
||||
private val taskExecutor by lazy { IntervalExecutor() }
|
||||
private var saveTaskJob: Job?=null
|
||||
|
||||
private fun startTask() {
|
||||
intervalExecutor.startIntervalTaskWithInitialDelay(2*1000L,2*1000L) {
|
||||
/**
|
||||
* 保存定时任务
|
||||
*/
|
||||
private fun saveGoodsTask() {
|
||||
saveTaskJob = taskExecutor.startIntervalTaskWithInitialDelay(1 * 60 * 1000L, 5 * 60 * 1000L) {
|
||||
saveShelfGoodsList()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveShelfGoodsList() {
|
||||
private var overdueTaskJob: Job?=null
|
||||
|
||||
/**
|
||||
* 过期时间定时任务
|
||||
*/
|
||||
private fun overdueTask() {
|
||||
overdueTaskJob = taskExecutor.startIntervalTaskWithInitialDelay(5 * 1000L, 10 * 1000L) {
|
||||
updateOverdueState()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateOverdueState() {
|
||||
viewModel.updateOverdueState { state, items ->
|
||||
if (items.isNullOrEmpty()) return@updateOverdueState
|
||||
list.forEachIndexed { index, model ->
|
||||
//接口过期状态变化后更新状态
|
||||
if (items[index].overdue != model.overdue) {
|
||||
model.overdue = items[index].overdue
|
||||
shelfAdapter.notifyItemChanged(index)
|
||||
log("更新编号为${model.deviceNo}物品:${model.goodsName}的过期状态:${model.overdue}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveShelfGoodsList() {
|
||||
log("performSync: list:${list.toJsonString()}")
|
||||
val submitList = list.sortedBy { it.deviceNo }
|
||||
submitList.forEach {
|
||||
@@ -764,14 +794,8 @@ class HomeActivity : BaseActivity() {
|
||||
it.goodsList = submitList
|
||||
}
|
||||
log("performSync: body:${body.toJsonString()}")
|
||||
try {
|
||||
val resp = apiService.saveShelfGoodsList(body = body)
|
||||
log("performSync: resp:${resp.toJsonString()}")
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
viewModel.saveShelfGoodsList(body)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import com.shuwei.intelligent.shelves.net.Loading
|
||||
import com.shuwei.intelligent.shelves.net.NetViewModel
|
||||
import com.shuwei.intelligent.shelves.net.RespData
|
||||
import com.shuwei.intelligent.shelves.net.UiState
|
||||
import com.shuwei.intelligent.shelves.task.TaskManager
|
||||
import com.shuwei.intelligent.shelves.utils.DateTimeUtil
|
||||
import com.shuwei.intelligent.shelves.utils.KeyboardUtil
|
||||
import com.shuwei.intelligent.shelves.utils.ext.addOnActionSearchListener
|
||||
@@ -176,7 +175,7 @@ class ShelfActivity : BaseActivity() {
|
||||
}
|
||||
getGoodsList()
|
||||
|
||||
TaskManager.startDeviceTask()
|
||||
// TaskManager.startDeviceTask()
|
||||
}
|
||||
|
||||
private fun finishRefresh() {
|
||||
@@ -281,7 +280,7 @@ class ShelfActivity : BaseActivity() {
|
||||
override fun onDestroy() {
|
||||
// unregisterReceiver(receiver)
|
||||
EventBus.getDefault().unregister(this)
|
||||
TaskManager.cancelDeviceTask()
|
||||
// TaskManager.cancelDeviceTask()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user