功能优化,增加心跳数据处理
This commit is contained in:
@@ -71,13 +71,13 @@ class HomeActivity : BaseActivity() {
|
||||
runCatching {
|
||||
receiveSerialPortData(data)
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}
|
||||
it.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
SerialPortManager.send(ACTIVE_CMD)
|
||||
|
||||
SerialPortManager.send(DEVICE_INFO_CMD)
|
||||
//SerialPortManager.send(DEVICE_INFO_CMD)
|
||||
}
|
||||
}
|
||||
TaskManager.startTask()
|
||||
@@ -90,47 +90,86 @@ class HomeActivity : BaseActivity() {
|
||||
}
|
||||
val cmd = data.substring(HEADER.length, HEADER.length + 2)
|
||||
when (cmd) {
|
||||
"01" -> {
|
||||
//心跳-01
|
||||
Log.d(TAG, "receiveSerialPortData:心跳01: $data")
|
||||
}
|
||||
|
||||
"81" -> {
|
||||
//心跳-81
|
||||
Log.d(TAG, "receiveSerialPortData:心跳08: $data")
|
||||
heartBeat(data)
|
||||
}
|
||||
|
||||
"03" -> {
|
||||
//获取设备状态
|
||||
val weightStartIndex = HEADER.length + 4 + 10
|
||||
val temperatureStartIndex = weightStartIndex + 10 * 8
|
||||
val temperatureHex = data.substring(temperatureStartIndex, 2)
|
||||
val temperature = hexToDec(temperatureHex)
|
||||
updateLeftStatus("1-1冷藏柜 $temperature°C / 87%")
|
||||
|
||||
var index = 1
|
||||
var count = weightStartIndex
|
||||
while (count < temperatureStartIndex) {
|
||||
val weightInfo = data.substring(count, count + 8)
|
||||
var weightHex = ""
|
||||
weightInfo.run {
|
||||
weightHex =
|
||||
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 = hexToDec(weightHex2)
|
||||
} else {
|
||||
//整数
|
||||
realWeight = hexToDec(weightHex)
|
||||
}
|
||||
weightArray.put(count, realWeight)
|
||||
count += 8
|
||||
index++
|
||||
}
|
||||
|
||||
val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO)
|
||||
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
|
||||
intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data)
|
||||
sendBroadcast(intent)
|
||||
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%")
|
||||
}
|
||||
|
||||
private fun getWeightInfo(start: Int, end: Int, data: String) {
|
||||
var index = 1
|
||||
var count = start
|
||||
while (count < end) {
|
||||
val weightInfo = data.substring(count, count + 8)
|
||||
var weightHex = ""
|
||||
weightInfo.run {
|
||||
weightHex =
|
||||
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 = hexToDec(weightHex2)
|
||||
} else {
|
||||
//整数
|
||||
realWeight = hexToDec(weightHex)
|
||||
}
|
||||
weightArray.put(index, realWeight)
|
||||
count += 8
|
||||
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)
|
||||
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
|
||||
intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data)
|
||||
sendBroadcast(intent)
|
||||
}
|
||||
|
||||
private val weightArray by lazy { SparseIntArray() }
|
||||
private var clickIndex = 0
|
||||
|
||||
|
||||
@@ -104,8 +104,11 @@ class ShelfActivity : BaseActivity() {
|
||||
binding.btnClearZero.setOnClickListener { v ->
|
||||
lifecycleScope.launch {
|
||||
val shelfNo = shelfModel?.shelfNo ?: 0
|
||||
val hex = shelfNo.toString(16).padStart(2, '0').uppercase()
|
||||
Log.d(TAG, "onCreate: hex=$hex")
|
||||
val zeroClearingCmd =
|
||||
"${HomeActivity.HEADER}0401${decToHex(shelfNo)}${HomeActivity.FOOTER}"
|
||||
"${HomeActivity.HEADER}0401${hex}${HomeActivity.FOOTER}"
|
||||
Log.d(TAG, "onCreate: zeroClearingCmd=$zeroClearingCmd")
|
||||
SerialPortManager.send(zeroClearingCmd)
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
|
||||
@@ -27,7 +27,8 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
||||
item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }"
|
||||
val weight = item.foodWeight ?: 0
|
||||
val binding = holder.binding
|
||||
val isBlankShelf = weight <= 0
|
||||
//val isBlankShelf = weight <= 0
|
||||
val isBlankShelf = item.foodName.isNullOrBlank()
|
||||
if (isBlankShelf) {
|
||||
binding.layoutCard.setCardBackgroundColor(getColor(R.color.bg_card_blue))
|
||||
binding.tvShelfName.run {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.shuwei.intelligent.shelves.serial
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import com.shuwei.intelligent.shelves.utils.hexToBytes
|
||||
import com.shuwei.intelligent.shelves.utils.toHexString2
|
||||
import io.github.jeadyx.jserialport.AndroidSerialPort
|
||||
@@ -38,7 +39,6 @@ object SerialPortManager {
|
||||
// 发送数据
|
||||
suspend fun send(data: String): Boolean {
|
||||
return try {
|
||||
// serialPort?.write(data.toByteArray())
|
||||
serialPort?.write(data.hexToBytes())
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
@@ -48,11 +48,12 @@ object SerialPortManager {
|
||||
}
|
||||
|
||||
// 启动接收协程
|
||||
@Suppress("DEPRECATION")
|
||||
@SuppressLint("DefaultLocale")
|
||||
fun startReceive(callback: (String) -> Unit) {
|
||||
scope.launch {
|
||||
while (isActive) {
|
||||
serialPort?.read()?.collect { buffer ->
|
||||
// val data = String(buffer)
|
||||
val data = buffer.toHexString2().toUpperCase()
|
||||
callback(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user