From e7e062ebcb3748decbfcf1523d5df78886bda397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=A2=9E=E9=A3=9E?= <331023091@qq.com> Date: Thu, 7 Aug 2025 18:58:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=81=94=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../intelligent/shelves/HomeActivity.kt | 103 ++++++++++++------ .../intelligent/shelves/ShelfActivity.kt | 3 +- .../shelves/adapter/ShelfAdapter.kt | 22 ++-- .../shuwei/intelligent/shelves/utils/F2C.kt | 5 + app/src/main/res/layout/list_item_search.xml | 2 - 5 files changed, 89 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/com/shuwei/intelligent/shelves/utils/F2C.kt diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt b/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt index de1c921..9fdaaf8 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt @@ -17,6 +17,7 @@ import com.shuwei.intelligent.shelves.model.ShelfModel 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.f2C import com.shuwei.intelligent.shelves.utils.hexToDec import kotlinx.coroutines.launch import java.text.SimpleDateFormat @@ -56,7 +57,7 @@ class HomeActivity : BaseActivity() { binding = ActivityHomeBinding.inflate(layoutInflater) setContentView(binding.root) - updateLeftStatus("1-1冷藏柜 -2°C / 87%") + updateLeftStatus("1-1冷藏柜 0°C / 0%") initList() initRecyclerView() @@ -68,11 +69,7 @@ class HomeActivity : BaseActivity() { SerialPortManager.startReceive { data -> runOnUiThread { // 更新UI显示接收数据 - runCatching { - receiveSerialPortData(data) - }.onFailure { - it.printStackTrace() - } + receiveSerialPortData(data) } } SerialPortManager.send(ACTIVE_CMD) @@ -83,46 +80,76 @@ class HomeActivity : BaseActivity() { TaskManager.startTask() } - private fun receiveSerialPortData(data: String) { - Log.d(TAG, "receiveSerialPortData: $data") - if (data.startsWith(HEADER).not()) { + private val serialReader by lazy { StringBuilder() } + + private fun receiveSerialPortData(srcData: String) { + Log.d(TAG, "receiveSerialPortData: $srcData") + var data = "" + if (srcData.startsWith(HEADER) && srcData.contains(FOOTER)) { + serialReader.clear() + val endIndex = srcData.indexOf(FOOTER) + serialReader.append(srcData.substring(0, endIndex)) + data = serialReader.toString() + } else if (srcData.startsWith(HEADER) && srcData.contains(FOOTER).not()) { + serialReader.clear() + serialReader.append(srcData) return + } else if (srcData.contains(FOOTER).not()){ + serialReader.append(srcData) + return + } else { + val endIndex = srcData.indexOf(FOOTER) + FOOTER.length + serialReader.append(srcData.substring(0, endIndex)) + data = serialReader.toString() } - val cmd = data.substring(HEADER.length, HEADER.length + 2) + + val cmd = data.substring(HEADER.length, HEADER.length + 4) when (cmd) { - "01" -> { + "0101" -> { //心跳-01 Log.d(TAG, "receiveSerialPortData:心跳01: $data") - } - - "81" -> { - //心跳-81 - Log.d(TAG, "receiveSerialPortData:心跳08: $data") heartBeat(data) } +// "8101" -> { +// //心跳-81 +// Log.d(TAG, "receiveSerialPortData:心跳08: $data") +// heartBeat(data) +// } - "03" -> { - //获取设备状态 - getDeviceInfo(data) - } +// "0302" -> { +// //获取设备状态 +// getDeviceInfo(data) +// } 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) - updateLeftStatus("1-1冷藏柜 $temperature°C / 87%") + runCatching { + if (data.length < 200) { + return + } + val weightStartIndex = 14 + val weightEndIndex = weightStartIndex + 12 * 8 + getWeightInfo( + start = weightStartIndex, + end = weightEndIndex, + data = data + ) + val temperatureStartIndex = (1 + 12 * 4 + 20 + 1 + 1 + 1 + 1) * 2 + val temperatureEndIndex = temperatureStartIndex + 2 + val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex) + val temperature = hexToDec(temperatureHex) +// val tempC = f2C(temperature.toDouble()) + val showTemp = "%.1f".format(temperature) + + val percent = 10 * list.count { it.foodName.isNullOrBlank().not() } + + updateLeftStatus("1-1冷藏柜 ${showTemp}°C / ${percent}%") + }.onFailure { + it.printStackTrace() + } } private fun getWeightInfo(start: Int, end: Int, data: String) { @@ -139,12 +166,18 @@ class HomeActivity : BaseActivity() { if (weightHex.first() in '8'..'F') { //负数 val weightHex2 = "0${weightHex.substring(1)}" - realWeight = hexToDec(weightHex2) + realWeight = -1*weightHex2.toInt() + //-1*hexToDec(weightHex2) } else { //整数 - realWeight = hexToDec(weightHex) + realWeight = weightHex.toInt() + //hexToDec(weightHex) } weightArray.put(index, realWeight) + + list[index-1].foodWeight = realWeight + shelfAdapter.notifyItemChanged(index) + count += 8 index++ } @@ -152,7 +185,7 @@ class HomeActivity : BaseActivity() { private fun getDeviceInfo(data: String) { val weightStartIndex = HEADER.length + 4 + 10 - val temperatureStartIndex = weightStartIndex + 10 * 8 + val temperatureStartIndex = weightStartIndex + 12 * 8 val temperatureEndIndex = temperatureStartIndex + 2 val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex) val temperature = hexToDec(temperatureHex) diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt b/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt index 56dc448..7e3d49a 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt @@ -65,6 +65,8 @@ class ShelfActivity : BaseActivity() { shelfModel?.let { binding.tvShelfName.text = it.shelfName binding.tvFoodName.text = if (it.foodName.isNullOrBlank()) "-" else it.foodName + realWeight = it.foodWeight ?: 0 + binding.tvFoodWeight.text = "${realWeight}克" } initRecyclerView() @@ -97,7 +99,6 @@ class ShelfActivity : BaseActivity() { setResult(RESULT_OK, intent) finish() } - binding.tvFoodWeight.text = "${realWeight}克" binding.root.setOnClickListener { v -> KeyboardUtil.hideKeyboard(v.context, v) } diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt b/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt index 27d7224..d5edebf 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt @@ -27,6 +27,11 @@ class ShelfAdapter(list: MutableList) : item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }" val weight = item.foodWeight ?: 0 val binding = holder.binding + binding.tvFoodWeight.run { + setTextColor(getColor(R.color.food_weight_orange)) + val showWeight = if(weight == 0) "0" else "%.3f".format(weight/1000F) + text = "${showWeight}千克" + } //val isBlankShelf = weight <= 0 val isBlankShelf = item.foodName.isNullOrBlank() if (isBlankShelf) { @@ -39,10 +44,10 @@ class ShelfAdapter(list: MutableList) : setTextColor(getColor(R.color.white)) text = "空" } - binding.tvFoodWeight.run { - setTextColor(getColor(R.color.food_weight_blue)) - text = "0千克" - } +// binding.tvFoodWeight.run { +// setTextColor(getColor(R.color.food_weight_blue)) +// text = "0千克" +// } binding.tvStoreDate.run { setTextColor(getColor(R.color.food_weight_blue)) text = "-" @@ -58,10 +63,11 @@ class ShelfAdapter(list: MutableList) : setTextColor(getColor(R.color.food_name_black)) text = item.foodName } - binding.tvFoodWeight.run { - setTextColor(getColor(R.color.food_weight_orange)) - text = "${weight/1000F}千克" - } +// binding.tvFoodWeight.run { +// setTextColor(getColor(R.color.food_weight_orange)) +//// text = "${weight/1000F}千克" +// text = "${weight}克" +// } binding.tvStoreDate.run { setTextColor(getColor(R.color.black999)) text = item.storeDate diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/utils/F2C.kt b/app/src/main/java/com/shuwei/intelligent/shelves/utils/F2C.kt new file mode 100644 index 0000000..3977ba4 --- /dev/null +++ b/app/src/main/java/com/shuwei/intelligent/shelves/utils/F2C.kt @@ -0,0 +1,5 @@ +package com.shuwei.intelligent.shelves.utils + +fun f2C(fahrenheit: Double): Double { + return (fahrenheit - 32) * 5 / 9 +} \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_search.xml b/app/src/main/res/layout/list_item_search.xml index 5c00083..8a1df3b 100644 --- a/app/src/main/res/layout/list_item_search.xml +++ b/app/src/main/res/layout/list_item_search.xml @@ -34,8 +34,6 @@ android:insetTop="0dp" android:insetBottom="0dp" android:maxLines="1" - app:paddingStart="5dp" - app:paddingEnd="5dp" android:insetRight="0dp" android:insetLeft="0dp" android:paddingHorizontal="8dp"