功能联调
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.serial.SerialPortManager
|
||||||
import com.shuwei.intelligent.shelves.task.TaskManager
|
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.hexToDec
|
import com.shuwei.intelligent.shelves.utils.hexToDec
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
@@ -56,7 +57,7 @@ class HomeActivity : BaseActivity() {
|
|||||||
binding = ActivityHomeBinding.inflate(layoutInflater)
|
binding = ActivityHomeBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
updateLeftStatus("1-1冷藏柜 -2°C / 87%")
|
updateLeftStatus("1-1冷藏柜 0°C / 0%")
|
||||||
|
|
||||||
initList()
|
initList()
|
||||||
initRecyclerView()
|
initRecyclerView()
|
||||||
@@ -68,11 +69,7 @@ class HomeActivity : BaseActivity() {
|
|||||||
SerialPortManager.startReceive { data ->
|
SerialPortManager.startReceive { data ->
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
// 更新UI显示接收数据
|
// 更新UI显示接收数据
|
||||||
runCatching {
|
receiveSerialPortData(data)
|
||||||
receiveSerialPortData(data)
|
|
||||||
}.onFailure {
|
|
||||||
it.printStackTrace()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SerialPortManager.send(ACTIVE_CMD)
|
SerialPortManager.send(ACTIVE_CMD)
|
||||||
@@ -83,46 +80,76 @@ class HomeActivity : BaseActivity() {
|
|||||||
TaskManager.startTask()
|
TaskManager.startTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun receiveSerialPortData(data: String) {
|
private val serialReader by lazy { StringBuilder() }
|
||||||
Log.d(TAG, "receiveSerialPortData: $data")
|
|
||||||
if (data.startsWith(HEADER).not()) {
|
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
|
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) {
|
when (cmd) {
|
||||||
"01" -> {
|
"0101" -> {
|
||||||
//心跳-01
|
//心跳-01
|
||||||
Log.d(TAG, "receiveSerialPortData:心跳01: $data")
|
Log.d(TAG, "receiveSerialPortData:心跳01: $data")
|
||||||
}
|
|
||||||
|
|
||||||
"81" -> {
|
|
||||||
//心跳-81
|
|
||||||
Log.d(TAG, "receiveSerialPortData:心跳08: $data")
|
|
||||||
heartBeat(data)
|
heartBeat(data)
|
||||||
}
|
}
|
||||||
|
// "8101" -> {
|
||||||
|
// //心跳-81
|
||||||
|
// Log.d(TAG, "receiveSerialPortData:心跳08: $data")
|
||||||
|
// heartBeat(data)
|
||||||
|
// }
|
||||||
|
|
||||||
"03" -> {
|
// "0302" -> {
|
||||||
//获取设备状态
|
// //获取设备状态
|
||||||
getDeviceInfo(data)
|
// getDeviceInfo(data)
|
||||||
}
|
// }
|
||||||
|
|
||||||
else -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun heartBeat(data: String) {
|
private fun heartBeat(data: String) {
|
||||||
val weightStartIndex = 2
|
runCatching {
|
||||||
val weightEndIndex = weightStartIndex + 10 * 8
|
if (data.length < 200) {
|
||||||
getWeightInfo(
|
return
|
||||||
start = weightStartIndex,
|
}
|
||||||
end = weightEndIndex,
|
val weightStartIndex = 14
|
||||||
data = data
|
val weightEndIndex = weightStartIndex + 12 * 8
|
||||||
)
|
getWeightInfo(
|
||||||
val temperatureStartIndex = (1 + 10 * 4 + 20 + 1 + 1 + 1 + 1) * 2
|
start = weightStartIndex,
|
||||||
val temperatureEndIndex = temperatureStartIndex + 2
|
end = weightEndIndex,
|
||||||
val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex)
|
data = data
|
||||||
val temperature = hexToDec(temperatureHex)
|
)
|
||||||
updateLeftStatus("1-1冷藏柜 $temperature°C / 87%")
|
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) {
|
private fun getWeightInfo(start: Int, end: Int, data: String) {
|
||||||
@@ -139,12 +166,18 @@ class HomeActivity : BaseActivity() {
|
|||||||
if (weightHex.first() in '8'..'F') {
|
if (weightHex.first() in '8'..'F') {
|
||||||
//负数
|
//负数
|
||||||
val weightHex2 = "0${weightHex.substring(1)}"
|
val weightHex2 = "0${weightHex.substring(1)}"
|
||||||
realWeight = hexToDec(weightHex2)
|
realWeight = -1*weightHex2.toInt()
|
||||||
|
//-1*hexToDec(weightHex2)
|
||||||
} else {
|
} else {
|
||||||
//整数
|
//整数
|
||||||
realWeight = hexToDec(weightHex)
|
realWeight = weightHex.toInt()
|
||||||
|
//hexToDec(weightHex)
|
||||||
}
|
}
|
||||||
weightArray.put(index, realWeight)
|
weightArray.put(index, realWeight)
|
||||||
|
|
||||||
|
list[index-1].foodWeight = realWeight
|
||||||
|
shelfAdapter.notifyItemChanged(index)
|
||||||
|
|
||||||
count += 8
|
count += 8
|
||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
@@ -152,7 +185,7 @@ 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 + 10 * 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)
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ class ShelfActivity : BaseActivity() {
|
|||||||
shelfModel?.let {
|
shelfModel?.let {
|
||||||
binding.tvShelfName.text = it.shelfName
|
binding.tvShelfName.text = it.shelfName
|
||||||
binding.tvFoodName.text = if (it.foodName.isNullOrBlank()) "-" else it.foodName
|
binding.tvFoodName.text = if (it.foodName.isNullOrBlank()) "-" else it.foodName
|
||||||
|
realWeight = it.foodWeight ?: 0
|
||||||
|
binding.tvFoodWeight.text = "${realWeight}克"
|
||||||
}
|
}
|
||||||
|
|
||||||
initRecyclerView()
|
initRecyclerView()
|
||||||
@@ -97,7 +99,6 @@ class ShelfActivity : BaseActivity() {
|
|||||||
setResult(RESULT_OK, intent)
|
setResult(RESULT_OK, intent)
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
binding.tvFoodWeight.text = "${realWeight}克"
|
|
||||||
binding.root.setOnClickListener { v ->
|
binding.root.setOnClickListener { v ->
|
||||||
KeyboardUtil.hideKeyboard(v.context, v)
|
KeyboardUtil.hideKeyboard(v.context, v)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
|||||||
item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }"
|
item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }"
|
||||||
val weight = item.foodWeight ?: 0
|
val weight = item.foodWeight ?: 0
|
||||||
val binding = holder.binding
|
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 = weight <= 0
|
||||||
val isBlankShelf = item.foodName.isNullOrBlank()
|
val isBlankShelf = item.foodName.isNullOrBlank()
|
||||||
if (isBlankShelf) {
|
if (isBlankShelf) {
|
||||||
@@ -39,10 +44,10 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
|||||||
setTextColor(getColor(R.color.white))
|
setTextColor(getColor(R.color.white))
|
||||||
text = "空"
|
text = "空"
|
||||||
}
|
}
|
||||||
binding.tvFoodWeight.run {
|
// binding.tvFoodWeight.run {
|
||||||
setTextColor(getColor(R.color.food_weight_blue))
|
// setTextColor(getColor(R.color.food_weight_blue))
|
||||||
text = "0千克"
|
// text = "0千克"
|
||||||
}
|
// }
|
||||||
binding.tvStoreDate.run {
|
binding.tvStoreDate.run {
|
||||||
setTextColor(getColor(R.color.food_weight_blue))
|
setTextColor(getColor(R.color.food_weight_blue))
|
||||||
text = "-"
|
text = "-"
|
||||||
@@ -58,10 +63,11 @@ class ShelfAdapter(list: MutableList<ShelfModel>) :
|
|||||||
setTextColor(getColor(R.color.food_name_black))
|
setTextColor(getColor(R.color.food_name_black))
|
||||||
text = item.foodName
|
text = item.foodName
|
||||||
}
|
}
|
||||||
binding.tvFoodWeight.run {
|
// binding.tvFoodWeight.run {
|
||||||
setTextColor(getColor(R.color.food_weight_orange))
|
// setTextColor(getColor(R.color.food_weight_orange))
|
||||||
text = "${weight/1000F}千克"
|
//// text = "${weight/1000F}千克"
|
||||||
}
|
// text = "${weight}克"
|
||||||
|
// }
|
||||||
binding.tvStoreDate.run {
|
binding.tvStoreDate.run {
|
||||||
setTextColor(getColor(R.color.black999))
|
setTextColor(getColor(R.color.black999))
|
||||||
text = item.storeDate
|
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:insetTop="0dp"
|
||||||
android:insetBottom="0dp"
|
android:insetBottom="0dp"
|
||||||
android:maxLines="1"
|
android:maxLines="1"
|
||||||
app:paddingStart="5dp"
|
|
||||||
app:paddingEnd="5dp"
|
|
||||||
android:insetRight="0dp"
|
android:insetRight="0dp"
|
||||||
android:insetLeft="0dp"
|
android:insetLeft="0dp"
|
||||||
android:paddingHorizontal="8dp"
|
android:paddingHorizontal="8dp"
|
||||||
|
|||||||
Reference in New Issue
Block a user