功能优化,增加心跳数据处理
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user