功能优化,增加心跳数据处理

This commit is contained in:
2025-08-07 11:07:04 +08:00
parent 005e7eae53
commit c409621e28
5 changed files with 92 additions and 44 deletions
@@ -77,7 +77,7 @@ class HomeActivity : BaseActivity() {
} }
SerialPortManager.send(ACTIVE_CMD) SerialPortManager.send(ACTIVE_CMD)
SerialPortManager.send(DEVICE_INFO_CMD) //SerialPortManager.send(DEVICE_INFO_CMD)
} }
} }
TaskManager.startTask() TaskManager.startTask()
@@ -90,17 +90,45 @@ class HomeActivity : BaseActivity() {
} }
val cmd = data.substring(HEADER.length, HEADER.length + 2) val cmd = data.substring(HEADER.length, HEADER.length + 2)
when (cmd) { when (cmd) {
"01" -> {
//心跳-01
Log.d(TAG, "receiveSerialPortData:心跳01: $data")
}
"81" -> {
//心跳-81
Log.d(TAG, "receiveSerialPortData:心跳08: $data")
heartBeat(data)
}
"03" -> { "03" -> {
//获取设备状态 //获取设备状态
val weightStartIndex = HEADER.length + 4 + 10 getDeviceInfo(data)
val temperatureStartIndex = weightStartIndex + 10 * 8 }
val temperatureHex = data.substring(temperatureStartIndex, 2)
else -> {}
}
}
private fun heartBeat(data: String) {
val weightStartIndex = 2
val weightEndIndex = weightStartIndex + 10 * 8
getWeightInfo(
start = weightStartIndex,
end = weightEndIndex,
data = data
)
val temperatureStartIndex = (1 + 10 * 4 + 20 + 1 + 1 + 1 + 1) * 2
val temperatureEndIndex = temperatureStartIndex + 2
val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex)
val temperature = hexToDec(temperatureHex) val temperature = hexToDec(temperatureHex)
updateLeftStatus("1-1冷藏柜 $temperature°C / 87%") updateLeftStatus("1-1冷藏柜 $temperature°C / 87%")
}
private fun getWeightInfo(start: Int, end: Int, data: String) {
var index = 1 var index = 1
var count = weightStartIndex var count = start
while (count < temperatureStartIndex) { while (count < end) {
val weightInfo = data.substring(count, count + 8) val weightInfo = data.substring(count, count + 8)
var weightHex = "" var weightHex = ""
weightInfo.run { weightInfo.run {
@@ -116,10 +144,25 @@ class HomeActivity : BaseActivity() {
//整数 //整数
realWeight = hexToDec(weightHex) realWeight = hexToDec(weightHex)
} }
weightArray.put(count, realWeight) weightArray.put(index, realWeight)
count += 8 count += 8
index++ index++
} }
}
private fun getDeviceInfo(data: String) {
val weightStartIndex = HEADER.length + 4 + 10
val temperatureStartIndex = weightStartIndex + 10 * 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) val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO)
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex]) intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
@@ -127,10 +170,6 @@ class HomeActivity : BaseActivity() {
sendBroadcast(intent) sendBroadcast(intent)
} }
else -> {}
}
}
private val weightArray by lazy { SparseIntArray() } private val weightArray by lazy { SparseIntArray() }
private var clickIndex = 0 private var clickIndex = 0
@@ -104,8 +104,11 @@ class ShelfActivity : BaseActivity() {
binding.btnClearZero.setOnClickListener { v -> binding.btnClearZero.setOnClickListener { v ->
lifecycleScope.launch { lifecycleScope.launch {
val shelfNo = shelfModel?.shelfNo ?: 0 val shelfNo = shelfModel?.shelfNo ?: 0
val hex = shelfNo.toString(16).padStart(2, '0').uppercase()
Log.d(TAG, "onCreate: hex=$hex")
val zeroClearingCmd = val zeroClearingCmd =
"${HomeActivity.HEADER}0401${decToHex(shelfNo)}${HomeActivity.FOOTER}" "${HomeActivity.HEADER}0401${hex}${HomeActivity.FOOTER}"
Log.d(TAG, "onCreate: zeroClearingCmd=$zeroClearingCmd")
SerialPortManager.send(zeroClearingCmd) SerialPortManager.send(zeroClearingCmd)
} }
KeyboardUtil.hideKeyboard(v.context, v) KeyboardUtil.hideKeyboard(v.context, v)
@@ -27,7 +27,8 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }" item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }"
val weight = item.foodWeight ?: 0 val weight = item.foodWeight ?: 0
val binding = holder.binding val binding = holder.binding
val isBlankShelf = weight <= 0 //val isBlankShelf = weight <= 0
val isBlankShelf = item.foodName.isNullOrBlank()
if (isBlankShelf) { if (isBlankShelf) {
binding.layoutCard.setCardBackgroundColor(getColor(R.color.bg_card_blue)) binding.layoutCard.setCardBackgroundColor(getColor(R.color.bg_card_blue))
binding.tvShelfName.run { binding.tvShelfName.run {
@@ -1,5 +1,6 @@
package com.shuwei.intelligent.shelves.serial package com.shuwei.intelligent.shelves.serial
import android.annotation.SuppressLint
import com.shuwei.intelligent.shelves.utils.hexToBytes import com.shuwei.intelligent.shelves.utils.hexToBytes
import com.shuwei.intelligent.shelves.utils.toHexString2 import com.shuwei.intelligent.shelves.utils.toHexString2
import io.github.jeadyx.jserialport.AndroidSerialPort import io.github.jeadyx.jserialport.AndroidSerialPort
@@ -38,7 +39,6 @@ object SerialPortManager {
// 发送数据 // 发送数据
suspend fun send(data: String): Boolean { suspend fun send(data: String): Boolean {
return try { return try {
// serialPort?.write(data.toByteArray())
serialPort?.write(data.hexToBytes()) serialPort?.write(data.hexToBytes())
true true
} catch (e: Exception) { } catch (e: Exception) {
@@ -48,11 +48,12 @@ object SerialPortManager {
} }
// 启动接收协程 // 启动接收协程
@Suppress("DEPRECATION")
@SuppressLint("DefaultLocale")
fun startReceive(callback: (String) -> Unit) { fun startReceive(callback: (String) -> Unit) {
scope.launch { scope.launch {
while (isActive) { while (isActive) {
serialPort?.read()?.collect { buffer -> serialPort?.read()?.collect { buffer ->
// val data = String(buffer)
val data = buffer.toHexString2().toUpperCase() val data = buffer.toHexString2().toUpperCase()
callback(data) callback(data)
} }
+8 -4
View File
@@ -161,9 +161,12 @@
android:id="@+id/rvSearch" android:id="@+id/rvSearch"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginStart="36dp" android:paddingStart="36dp"
android:layout_marginTop="36dp" android:paddingBottom="12dp"
android:layout_marginEnd="36dp" android:layout_marginTop="24dp"
android:paddingTop="12dp"
android:paddingEnd="36dp"
android:clipToPadding="false"
android:layout_weight="1" android:layout_weight="1"
android:overScrollMode="never" android:overScrollMode="never"
tools:listitem="@layout/list_item_search" tools:listitem="@layout/list_item_search"
@@ -171,11 +174,12 @@
app:spanCount="2" app:spanCount="2"
tools:itemCount="10"/> tools:itemCount="10"/>
<!-- android:layout_marginTop="48dp"-->
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btnConfirm" android:id="@+id/btnConfirm"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="80dp" android:layout_height="80dp"
android:layout_marginTop="48dp" android:layout_marginTop="24dp"
android:layout_marginEnd="48dp" android:layout_marginEnd="48dp"
android:layout_marginStart="48dp" android:layout_marginStart="48dp"
android:layout_marginBottom="48dp" android:layout_marginBottom="48dp"