优化
This commit is contained in:
@@ -32,8 +32,10 @@ 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
|
||||
import com.shuwei.intelligent.shelves.utils.ext.copyTextToClipboard
|
||||
import com.shuwei.intelligent.shelves.utils.ext.gone
|
||||
@@ -76,14 +78,18 @@ class HomeActivity : BaseActivity() {
|
||||
|
||||
const val DEVICE_INFO_CMD = "${HEADER}0301000000000000000000000000${FOOTER}"
|
||||
|
||||
const val START_TEMP_CTRL_CMD= "${HEADER}0F01360000${FOOTER}"
|
||||
const val START_TEMP_CTRL_CMD = "${HEADER}0F01360000${FOOTER}"
|
||||
|
||||
//const val START_CMD= "${HEADER}0F01050020${FOOTER}"
|
||||
//const val ACTIVE_CMD = "${HEADER}000281C5D70A7D428948AD7254FADBB1516738938B4B9CBD73673E9D8E7F2E15062D$FOOTER"
|
||||
const val ACTIVE_CMD =
|
||||
"${HEADER}000281C5D70A7D42894EA17650F5D0BB516838E7863C9ABD766C38958AFF0F3C062D${FOOTER}"
|
||||
// const val ACTIVE_CMD = "${HEADER}000281C5D70A7D428948AD7254FADBB1516738938B4B9CBD73673E9D8E7F2E15062D$FOOTER"
|
||||
//410激活码
|
||||
// const val ACTIVE_CMD = "${HEADER}000281C5D70A7D42894EA17650F5D0BB516838E7863C9ABD766C38958AFF0F3C062D${FOOTER}"
|
||||
//1楼餐厅
|
||||
const val ACTIVE_CMD = "${HEADER}000281C5D70A7D428B44A57454F5DACA241938938B4B9CBD73673E9C8E7FC0C4992D${FOOTER}"
|
||||
|
||||
//华氏温度单位
|
||||
const val F_TEMP_CMD = "${HEADER}0F01390000${FOOTER}"
|
||||
|
||||
//摄氏温度单位
|
||||
const val C_TEMP_CMD = "${HEADER}0F01390001${FOOTER}"
|
||||
|
||||
@@ -166,7 +172,9 @@ class HomeActivity : BaseActivity() {
|
||||
sendCmd(C_TEMP_CMD)
|
||||
}, 5000)
|
||||
|
||||
TaskManager.startTask()
|
||||
// TaskManager.startTask()
|
||||
|
||||
startTask()
|
||||
|
||||
// var androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
|
||||
//// androidId = "d0e7a2d5f25c775c"
|
||||
@@ -390,7 +398,8 @@ class HomeActivity : BaseActivity() {
|
||||
"1001" -> {
|
||||
//温控信息上报
|
||||
val respCmd = "${HEADER}100200$FOOTER"
|
||||
val logMsg = "${LOG_PREFIX}[温控信息上报]${cmdFlag}指令:${data},发送应答指令:${respCmd}"
|
||||
val logMsg =
|
||||
"${LOG_PREFIX}[温控信息上报]${cmdFlag}指令:${data},发送应答指令:${respCmd}"
|
||||
log(logMsg)
|
||||
sendCmd(respCmd)
|
||||
val tempC = HexUtils.signedHexToDec(data.substring(0, 4)) * 0.1
|
||||
@@ -509,7 +518,7 @@ class HomeActivity : BaseActivity() {
|
||||
}
|
||||
weightArray.put(index, realWeight)
|
||||
|
||||
if (index <= 10) {
|
||||
if (index in 1..10 && list.size == 10) {
|
||||
list.firstOrNull { it.deviceNo == index }?.let {
|
||||
it.weight = realWeight.toDouble()
|
||||
val pos = list.indexOf(it)
|
||||
@@ -517,6 +526,12 @@ class HomeActivity : BaseActivity() {
|
||||
log("getWeightInfo: deviceNo=${it.deviceNo},realWeight=${realWeight}")
|
||||
EventBus.getDefault().post(SendWeightEvent(it.deviceNo, realWeight))
|
||||
}
|
||||
// val pos = index - 1
|
||||
// val item = list[pos]
|
||||
// item.weight = realWeight.toDouble()
|
||||
// shelfAdapter.notifyItemChanged(pos)
|
||||
// log("getWeightInfo: deviceNo=${item.deviceNo},realWeight=${realWeight}")
|
||||
// EventBus.getDefault().post(SendWeightEvent(item.deviceNo, realWeight))
|
||||
}
|
||||
|
||||
count += 8
|
||||
@@ -594,11 +609,11 @@ class HomeActivity : BaseActivity() {
|
||||
setOnItemClickListener { _, _, position ->
|
||||
shelfIndex = position
|
||||
lifecycleScope.launch {
|
||||
// val openCmd = if (list[position].deviceNo in 1..5)
|
||||
// LEFT_SHELF_OPEN_CMD
|
||||
// else
|
||||
// RIGHT_SHELF_OPEN_CMD
|
||||
// val openState = SerialPortManager.send(openCmd)
|
||||
val openCmd = if (list[position].deviceNo in 1..5)
|
||||
LEFT_SHELF_OPEN_CMD
|
||||
else
|
||||
RIGHT_SHELF_OPEN_CMD
|
||||
val openState = SerialPortManager.send(openCmd)
|
||||
// if (openState.not()) {
|
||||
// toast("${list[position].goodsName}柜门开启失败")
|
||||
// return@launch
|
||||
@@ -677,6 +692,8 @@ class HomeActivity : BaseActivity() {
|
||||
super.onResume()
|
||||
hideStatusBar()
|
||||
handler.post(updateTask)
|
||||
// D6DADEDB00010031323136334238423134303233303456322E313100FFFF
|
||||
// sendCmd("${HEADER}00010031323136334238423134303233303456322E313152${FOOTER}")
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@@ -718,4 +735,43 @@ class HomeActivity : BaseActivity() {
|
||||
}
|
||||
shelfAdapter.notifyItemChanged(list.indexOf(item))
|
||||
}
|
||||
|
||||
private val intervalExecutor by lazy { IntervalExecutor() }
|
||||
|
||||
private fun startTask() {
|
||||
intervalExecutor.startIntervalTaskWithInitialDelay(2*1000L,2*1000L) {
|
||||
saveShelfGoodsList()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveShelfGoodsList() {
|
||||
log("performSync: list:${list.toJsonString()}")
|
||||
val submitList = list.sortedBy { it.deviceNo }
|
||||
submitList.forEach {
|
||||
if (it.goodsId.isNullOrBlank()) {
|
||||
it.weight = it.weightBak
|
||||
}
|
||||
}
|
||||
// submitList.forEach {
|
||||
// it.temperature = HomeActivity.showTemperatureC
|
||||
// it.humidity = HomeActivity.showHumidity
|
||||
// }
|
||||
val body = ShelfBody().also {
|
||||
it.deviceId = App.deviceId
|
||||
it.canteenId = App.canteenId
|
||||
it.temperature = showTemperatureC
|
||||
it.humidity = showHumidity
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -363,7 +363,7 @@ class ShelfActivity : BaseActivity() {
|
||||
log("编号${event.shelfNo}货架获取重量成功:${weight}克,数据获取间隔时间为:${intervalTime}")
|
||||
binding.tvFoodWeight.text = if(weight < 1000) "${weight}克" else "%.3f千克".format(weight/1000.0)
|
||||
startTime = System.currentTimeMillis()
|
||||
window.decorView.postDelayed({ Loading.dismiss() }, 2000)
|
||||
window.decorView.postDelayed({ Loading.dismiss() }, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.shuwei.intelligent.shelves.utils.DateTimeUtil
|
||||
import com.shuwei.intelligent.shelves.utils.ext.gone
|
||||
import com.shuwei.intelligent.shelves.utils.ext.visible
|
||||
import java.util.Date
|
||||
import kotlin.math.abs
|
||||
|
||||
class ShelfAdapter(list: MutableList<ShelfModel>) :
|
||||
BaseQuickAdapter<ShelfModel, ShelfAdapter.VH>(list) {
|
||||
@@ -43,18 +44,21 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
||||
binding.ivStaleFood.gone()
|
||||
} else {
|
||||
runCatching {
|
||||
val shelfDate = stringToDate(item.putCabinetDay)
|
||||
if (shelfDate == null) {
|
||||
binding.ivStaleFood.gone()
|
||||
} else {
|
||||
val days = item.overdueDay!!.toInt()
|
||||
val resultDate = DateTimeUtil.addDays(shelfDate, days)
|
||||
if (resultDate.before(Date())) {
|
||||
binding.ivStaleFood.visible()
|
||||
} else {
|
||||
binding.ivStaleFood.gone()
|
||||
}
|
||||
// val shelfDate = stringToDate(item.putCabinetDay)
|
||||
binding.ivStaleFood.run {
|
||||
if (item.overdue == true) visible() else gone()
|
||||
}
|
||||
// if (shelfDate == null) {
|
||||
// binding.ivStaleFood.gone()
|
||||
// } else {
|
||||
// val days = item.overdueDay!!.toInt()
|
||||
// val resultDate = DateTimeUtil.addDays(shelfDate, days)
|
||||
// if (resultDate.before(Date())) {
|
||||
// binding.ivStaleFood.visible()
|
||||
// } else {
|
||||
// binding.ivStaleFood.gone()
|
||||
// }
|
||||
// }
|
||||
}.onFailure {
|
||||
binding.ivStaleFood.gone()
|
||||
}
|
||||
@@ -63,7 +67,7 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
||||
binding.tvFoodWeight.run {
|
||||
setTextColor(getColor(R.color.food_weight_orange))
|
||||
//val showWeight = if (weight == 0.toDouble()) "0克" else "%.3f千克".format(weight)
|
||||
text = if(weight < 1000) "${weight.toInt()}克" else "%.3f千克".format(weight/1000.0)
|
||||
text = if(abs(weight) < 1000) "${weight.toInt()}克" else "%.3f千克".format(weight/1000.0)
|
||||
//"${(weight*1000).roundToInt()}克"
|
||||
}
|
||||
//val isBlankShelf = weight <= 0
|
||||
|
||||
@@ -6,7 +6,7 @@ data class ShelfModel(
|
||||
var id: String? = null,
|
||||
var deviceNo: Int = 0,
|
||||
var shelfName: String? = null,
|
||||
var goodsId: String = "",
|
||||
var goodsId: String? = "",
|
||||
var goodsName: String? = null,
|
||||
var deviceId: String? = null,
|
||||
var weight: Double = 0.0,
|
||||
@@ -17,4 +17,5 @@ data class ShelfModel(
|
||||
var overdueDay: String? = null,
|
||||
var temperature: String? = null,
|
||||
var humidity: String = "0",
|
||||
var overdue:Boolean ?= false
|
||||
) : Serializable
|
||||
@@ -39,7 +39,7 @@ class SyncTask(appContext: Context, workerParams: WorkerParameters) :
|
||||
Log.d(TAG, "performSync: list:${list.toJsonString()}")
|
||||
val submitList = list.sortedBy { it.deviceNo }
|
||||
submitList.forEach {
|
||||
if (it.goodsId.isBlank()) {
|
||||
if (it.goodsId.isNullOrBlank()) {
|
||||
it.weight = it.weightBak
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.shuwei.intelligent.shelves.utils
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
class IntervalExecutor {
|
||||
|
||||
/**
|
||||
* 启动定时任务
|
||||
* @param delayMillis 延迟时间(毫秒)
|
||||
* @param action 要执行的方法
|
||||
* @return Job 可用于取消任务
|
||||
*/
|
||||
fun startIntervalTask(delayMillis: Long, action: suspend () -> Unit): Job {
|
||||
return CoroutineScope(Dispatchers.Default).launch {
|
||||
while (isActive) {
|
||||
action()
|
||||
delay(delayMillis)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动定时任务(带初始延迟)
|
||||
* @param initialDelay 初始延迟时间(毫秒)
|
||||
* @param delayMillis 后续执行间隔(毫秒)
|
||||
* @param action 要执行的方法
|
||||
* @return Job 可用于取消任务
|
||||
*/
|
||||
fun startIntervalTaskWithInitialDelay(
|
||||
initialDelay: Long,
|
||||
delayMillis: Long,
|
||||
action: suspend () -> Unit
|
||||
): Job {
|
||||
return CoroutineScope(Dispatchers.Default).launch {
|
||||
delay(initialDelay)
|
||||
while (isActive) {
|
||||
action()
|
||||
delay(delayMillis)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
fun main() = runBlocking {
|
||||
val executor = IntervalExecutor()
|
||||
|
||||
// 示例1:每隔10秒执行一次
|
||||
val job1 = executor.startIntervalTask(10000) {
|
||||
println("定时任务执行: ${System.currentTimeMillis()}")
|
||||
// 这里可以执行你的业务逻辑
|
||||
}
|
||||
|
||||
// 示例2:先延迟5秒,然后每隔3秒执行一次
|
||||
val job2 = executor.startIntervalTaskWithInitialDelay(5000, 3000) {
|
||||
println("带初始延迟的定时任务: ${System.currentTimeMillis()}")
|
||||
}
|
||||
|
||||
// 运行30秒后取消任务
|
||||
delay(30000)
|
||||
job1.cancel()
|
||||
job2.cancel()
|
||||
println("所有定时任务已取消")
|
||||
}
|
||||
Reference in New Issue
Block a user