This commit is contained in:
马增飞
2026-01-12 18:03:41 +08:00
parent 247a332ff3
commit 9a222037ea
6 changed files with 152 additions and 27 deletions
@@ -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)
}
}