功能联调
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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,11 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
||||
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<ShelfModel>) :
|
||||
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<ShelfModel>) :
|
||||
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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.shuwei.intelligent.shelves.utils
|
||||
|
||||
fun f2C(fahrenheit: Double): Double {
|
||||
return (fahrenheit - 32) * 5 / 9
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user