功能联调
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user