调试通过获取设备信息实时更新二级页面重量,首页和二级页面列表页增加空数据展示

This commit is contained in:
2025-08-11 14:22:34 +08:00
parent 9e5ede4a8e
commit 0d99c4f22a
11 changed files with 312 additions and 142 deletions
@@ -2,6 +2,7 @@ package com.shuwei.intelligent.shelves
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
@@ -12,6 +13,7 @@ import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter4.util.setOnDebouncedItemClick
import com.shuwei.intelligent.shelves.adapter.ShelfAdapter
@@ -25,20 +27,26 @@ import com.shuwei.intelligent.shelves.net.RespData
import com.shuwei.intelligent.shelves.net.UiState
import com.shuwei.intelligent.shelves.serial.SerialPortManager
import com.shuwei.intelligent.shelves.task.TaskManager
import com.shuwei.intelligent.shelves.utils.UIUtils
import com.shuwei.intelligent.shelves.utils.binaryToHex
import com.shuwei.intelligent.shelves.utils.ext.gone
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.delay
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.getValue
class HomeActivity : BaseActivity() {
companion object {
private const val TAG = "HomeActivity"
private const val MM_DD_EEEE__HH_MM_SS = "MM月dd日 EEEE HH:mm:ss"
//协议头
const val HEADER = "D6DADEDB"
@@ -63,6 +71,8 @@ class HomeActivity : BaseActivity() {
private val viewModel: NetViewModel by viewModels()
private val weightArray by lazy { SparseArray<Double?>() }
@SuppressLint("NotifyDataSetChanged", "HardwareIds")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -71,7 +81,6 @@ class HomeActivity : BaseActivity() {
updateLeftStatus("1-1冷藏柜 -°C / -%")
initList()
initRecyclerView()
lifecycleScope.launch {
@@ -94,7 +103,7 @@ class HomeActivity : BaseActivity() {
when (state) {
is UiState.Loading -> {}
is UiState.Success<*> -> getTokenSuccess(state.data)
is UiState.Error -> UIUtils.toast(state.msg)
is UiState.Error -> toast(state.msg)
else -> {}
}
}
@@ -113,7 +122,8 @@ class HomeActivity : BaseActivity() {
}
TaskManager.startTask()
val androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
var androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
// androidId = "d0e7a2d5f25c775c"
Log.d(TAG, "onCreate: androidId=${androidId}")
App.getInstance().deviceId = androidId
//"53babf69-0d2f-3875-a649-f2e12f80ad4c1"
@@ -130,6 +140,29 @@ class HomeActivity : BaseActivity() {
// receiveSerialPortData(data)
// delay(500)
// }
// }
// lifecycleScope.launch {
// repeat(100) {
// val sb = StringBuilder().apply {
//// 610200000000000001000000050000800000000006000000050000000400008006000000010000001019008070180080
// repeat(12) {
// val num = (0..99).random()
// val num2 = (0..99).random()
// val num3 = (0..99).random()
// val num4 = (0..1).random()
// val numStr = if (num <= 9) "0${num}" else "" + num
// val numStr2 = if (num2 <= 9) "0${num2}" else "" + num2
// val numStr3 = if (num3 <= 9) "0${num3}" else "" + num3
// val numStr4 = if (num4 == 1) "80" else "00"
// append(numStr).append(numStr2).append(numStr3).append(numStr4)
// }
// }
// val data =
// "${HEADER}0302000000000C${sb}5037414645333633333334333731373056322E313069${FOOTER}"
// receiveSerialPortData(data)
// delay(500)
// }
// }
}
@@ -148,19 +181,20 @@ class HomeActivity : BaseActivity() {
@SuppressLint("NotifyDataSetChanged")
private fun updateUI(data: RespData<*>) {
binding.include?.root?.gone()
Loading.dismiss()
if (data.result !is ShelfResult) {
UIUtils.toast("数据错误")
loadEmptyView()
return
}
App.getInstance().canteenId = data.result.canteenId
val tempList = data.result.containerGoodsList
if (tempList.isNullOrEmpty()) {
UIUtils.toast("数据为空")
loadEmptyView()
return
}
if (tempList.size < 10) {
UIUtils.toast("数据错误")
toast("数据错误")
return
}
list.clear()
@@ -175,7 +209,8 @@ class HomeActivity : BaseActivity() {
private fun showError(message: String) {
Loading.dismiss()
UIUtils.toast(message)
toast(message)
loadEmptyView()
}
private val serialReader by lazy { StringBuilder() }
@@ -199,7 +234,7 @@ class HomeActivity : BaseActivity() {
val endIndex = srcData.indexOf(FOOTER) + FOOTER.length
serialReader.append(srcData.substring(0, endIndex))
Log.d(TAG, "receiveSerialPortData:serialReader: $serialReader")
data = serialReader.toString().replace(",","")
data = serialReader.toString().replace(",", "")
}
val cmd = data.substring(HEADER.length, HEADER.length + 4)
@@ -215,10 +250,10 @@ class HomeActivity : BaseActivity() {
// heartBeat(data)
// }
// "0302" -> {
// //获取设备状态
// getDeviceInfo(data)
// }
"0302" -> {
//获取设备状态
getDeviceInfo(data)
}
else -> {}
}
@@ -262,13 +297,16 @@ class HomeActivity : BaseActivity() {
substring(6, 8) + substring(4, 6) + substring(2, 4) + substring(0, 2)
}
var realWeight = 0
if (weightHex.first() in '8'..'F') {
//负数
val weightHex2 = "0${weightHex.substring(1)}"
realWeight = -1 * weightHex2.toInt()
} else {
//整数
var firstByteBinary = hexToBinary(weightHex.substring(0, 2))
if (firstByteBinary.startsWith("0")) {
//正数
realWeight = weightHex.toInt()
} else {
//负数
firstByteBinary = "0${firstByteBinary.substring(1)}"
val firstByteHex = binaryToHex(firstByteBinary)
weightHex = firstByteHex + weightHex.substring(2)
realWeight = -1 * weightHex.toInt()
}
val realWeightDouble = realWeight / 1000.0
weightArray.put(index, realWeightDouble)
@@ -287,27 +325,33 @@ class HomeActivity : BaseActivity() {
}
private fun getDeviceInfo(data: String) {
// val weightStartIndex = HEADER.length + 4 + 10
// val temperatureStartIndex = weightStartIndex + 12 * 8
// val temperatureEndIndex = temperatureStartIndex + 2
// val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex)
// val temperature = hexToDec(temperatureHex)
// updateLeftStatus("1-1冷藏柜 $temperature°C / 87%")
//
// getWeightInfo(
// start = weightStartIndex,
// end = temperatureStartIndex,
// data = data
// )
//
// val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO)
// intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
// intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data)
// sendBroadcast(intent)
Log.d(TAG, "getDeviceInfo: data=$data")
val weightStartIndex = HEADER.length + 14
val temperatureStartIndex = weightStartIndex + 12 * 8
val temperatureEndIndex = temperatureStartIndex + 2
val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex)
val temperature = hexToDec(temperatureHex)
val tempC = f2C(temperature.toDouble())
val showTemp = "%.1f".format(tempC)
val percent = 10 * list.count { it.goodsName.isNullOrBlank().not() }
updateLeftStatus("1-1冷藏柜 ${showTemp}°C / ${percent}%")
getWeightInfo(
start = weightStartIndex,
end = temperatureStartIndex,
data = data
)
if (list.isNotEmpty()) {
val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO + list[shelfIndex].deviceNo)
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[shelfIndex])
sendBroadcast(intent)
}
}
private val weightArray by lazy { SparseArray<Double?>() }
private var clickIndex = 0
private var shelfIndex = 0
override fun onDestroy() {
lifecycleScope.launch {
@@ -319,10 +363,9 @@ class HomeActivity : BaseActivity() {
@Suppress("DEPRECATION")
private val shelfAdapter by lazy {
// val list = viewModel.getCurrentItems()
ShelfAdapter(list).apply {
setOnDebouncedItemClick { adapter, view, position ->
this@HomeActivity.clickIndex = position
this@HomeActivity.shelfIndex = position
lifecycleScope.launch {
val openCmd = if (list[position].deviceNo in 1..5)
LEFT_SHELF_OPEN_CMD
@@ -330,7 +373,7 @@ class HomeActivity : BaseActivity() {
RIGHT_SHELF_OPEN_CMD
val openState = SerialPortManager.send(openCmd)
if (openState.not()) {
UIUtils.toast("${list[position].goodsName}柜门开启失败")
toast("${list[position].goodsName}柜门开启失败")
return@launch
}
launch(Intent(this@HomeActivity, ShelfActivity::class.java).also {
@@ -358,26 +401,15 @@ class HomeActivity : BaseActivity() {
layoutManager =
GridLayoutManager(this@HomeActivity, 2, GridLayoutManager.VERTICAL, false)
adapter = shelfAdapter
itemAnimator.let {
if (it is DefaultItemAnimator) {
it.supportsChangeAnimations = false
}
}
}
}
private fun initList() {
//// val list: MutableList<ShelfModel> = mutableListOf()
// list.add(ShelfModel(deviceNo = 1))
// list.add(ShelfModel(deviceNo = 6))
// list.add(ShelfModel(deviceNo = 2))
// list.add(ShelfModel(deviceNo = 7))
// list.add(ShelfModel(deviceNo = 3))
// list.add(ShelfModel(deviceNo = 8))
// list.add(ShelfModel(deviceNo = 4))
// list.add(ShelfModel(deviceNo = 9))
// list.add(ShelfModel(deviceNo = 5))
// list.add(ShelfModel(deviceNo = 10))
//// viewModel.updateItems(list)
}
private val MM_DD_EEEE__HH_MM_SS = "MM月dd日 EEEE HH:mm:ss"
private fun updateDateTime() {
val sdf = SimpleDateFormat(MM_DD_EEEE__HH_MM_SS, Locale.CHINA)
val dateTime = sdf.format(Date())
@@ -402,4 +434,19 @@ class HomeActivity : BaseActivity() {
super.onPause()
handler.removeCallbacks(updateTask)
}
@SuppressLint("NotifyDataSetChanged")
private fun loadEmptyView() {
list.clear()
shelfAdapter.notifyDataSetChanged()
binding.include?.let {
it.root.visible()
it.root.setOnClickListener {
viewModel.getAccessToken(App.getInstance().deviceId)
}
it.ivEmptyIcon.setImageResource(R.drawable.ic_empty_white)
it.tvEmptyContent.setTextColor(Color.WHITE)
}
}
}