优化数据读取异常问题

This commit is contained in:
2025-08-07 19:32:04 +08:00
parent e7e062ebcb
commit eb66c2d2ff
@@ -19,6 +19,7 @@ import com.shuwei.intelligent.shelves.task.TaskManager
import com.shuwei.intelligent.shelves.utils.UIUtils import com.shuwei.intelligent.shelves.utils.UIUtils
import com.shuwei.intelligent.shelves.utils.f2C import com.shuwei.intelligent.shelves.utils.f2C
import com.shuwei.intelligent.shelves.utils.hexToDec import com.shuwei.intelligent.shelves.utils.hexToDec
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
@@ -78,6 +79,14 @@ class HomeActivity : BaseActivity() {
} }
} }
TaskManager.startTask() TaskManager.startTask()
// repeat(100) {
// lifecycleScope.launch {
// var data = "D6DADEDB01010C6102000000000000010000000500008000000000060000000500000004000080060000000100000010190080701800800000000000000000000000000000000000000000000000005037414645333633333334333731373056322E313069FFFF"
// receiveSerialPortData(data)
// delay(100)
// }
// }
} }
private val serialReader by lazy { StringBuilder() } private val serialReader by lazy { StringBuilder() }
@@ -137,12 +146,12 @@ class HomeActivity : BaseActivity() {
end = weightEndIndex, end = weightEndIndex,
data = data data = data
) )
val temperatureStartIndex = (1 + 12 * 4 + 20 + 1 + 1 + 1 + 1) * 2 val temperatureStartIndex = 158
val temperatureEndIndex = temperatureStartIndex + 2 val temperatureEndIndex = temperatureStartIndex + 2
val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex) val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex)
val temperature = hexToDec(temperatureHex) val temperature = hexToDec(temperatureHex)
// val tempC = f2C(temperature.toDouble()) val tempC = f2C(temperature.toDouble())
val showTemp = "%.1f".format(temperature) val showTemp = "%.1f".format(tempC.toDouble())
val percent = 10 * list.count { it.foodName.isNullOrBlank().not() } val percent = 10 * list.count { it.foodName.isNullOrBlank().not() }
@@ -175,8 +184,12 @@ class HomeActivity : BaseActivity() {
} }
weightArray.put(index, realWeight) weightArray.put(index, realWeight)
list[index-1].foodWeight = realWeight if (index <= 10) {
shelfAdapter.notifyItemChanged(index) list.first { it.shelfNo == index }.let {
it.foodWeight = realWeight
}
shelfAdapter.notifyItemChanged(index)
}
count += 8 count += 8
index++ index++
@@ -184,23 +197,23 @@ class HomeActivity : BaseActivity() {
} }
private fun getDeviceInfo(data: String) { private fun getDeviceInfo(data: String) {
val weightStartIndex = HEADER.length + 4 + 10 // val weightStartIndex = HEADER.length + 4 + 10
val temperatureStartIndex = weightStartIndex + 12 * 8 // val temperatureStartIndex = weightStartIndex + 12 * 8
val temperatureEndIndex = temperatureStartIndex + 2 // val temperatureEndIndex = temperatureStartIndex + 2
val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex) // 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%")
//
getWeightInfo( // getWeightInfo(
start = weightStartIndex, // start = weightStartIndex,
end = temperatureStartIndex, // end = temperatureStartIndex,
data = data // 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])
intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data) // intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data)
sendBroadcast(intent) // sendBroadcast(intent)
} }
private val weightArray by lazy { SparseIntArray() } private val weightArray by lazy { SparseIntArray() }