接口联调

This commit is contained in:
2025-08-08 13:34:18 +08:00
parent eb66c2d2ff
commit 65199f4c70
32 changed files with 689 additions and 115 deletions
@@ -5,34 +5,39 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter4.util.setOnDebouncedItemClick
import com.shuwei.intelligent.shelves.HomeActivity.Companion.ACTIVE_CMD
import com.shuwei.intelligent.shelves.HomeActivity.Companion.DEVICE_INFO_CMD
import com.shuwei.intelligent.shelves.utils.ext.addOnActionSearchListener
import com.shuwei.intelligent.shelves.utils.ext.clickWithDebounce
import com.shuwei.intelligent.shelves.utils.ext.toast
import com.shuwei.intelligent.shelves.adapter.SearchAdapter
import com.shuwei.intelligent.shelves.base.BaseActivity
import com.shuwei.intelligent.shelves.databinding.ActivityShelfBinding
import com.shuwei.intelligent.shelves.model.SearchModel
import com.shuwei.intelligent.shelves.model.GoodsModel
import com.shuwei.intelligent.shelves.model.GoodsRecord
import com.shuwei.intelligent.shelves.model.ShelfModel
import com.shuwei.intelligent.shelves.net.Loading
import com.shuwei.intelligent.shelves.net.NetViewModel
import com.shuwei.intelligent.shelves.net.RespData
import com.shuwei.intelligent.shelves.net.UiState
import com.shuwei.intelligent.shelves.serial.SerialPortManager
import com.shuwei.intelligent.shelves.utils.DateTimeUtil
import com.shuwei.intelligent.shelves.utils.KeyboardUtil
import com.shuwei.intelligent.shelves.utils.decToHex
import com.shuwei.intelligent.shelves.utils.UIUtils
import com.shuwei.intelligent.shelves.utils.ext.addOnActionSearchListener
import com.shuwei.intelligent.shelves.utils.ext.clickWithDebounce
import com.shuwei.intelligent.shelves.utils.ext.toJsonString
import com.shuwei.intelligent.shelves.utils.ext.toast
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.random.Random
@SuppressLint("NotifyDataSetChanged")
class ShelfActivity : BaseActivity() {
@@ -53,6 +58,9 @@ class ShelfActivity : BaseActivity() {
private var realWeight: Int = 0
private val viewModel: NetViewModel by viewModels()
@SuppressLint("UnspecifiedRegisterReceiverFlag")
@Suppress("DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) {
@@ -64,8 +72,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.tvFoodName.text = if (it.goodsName.isNullOrBlank()) "-" else it.goodsName
realWeight = ((it.weight ?: 0.0) * 1000).toInt()
binding.tvFoodWeight.text = "${realWeight}"
}
@@ -77,7 +85,11 @@ class ShelfActivity : BaseActivity() {
toast(hint.toString())
return@addOnActionSearchListener
}
loadSearchData()
// loadSearchData()
viewModel.getGoodsList(
canteenId = "1599670511042785282",
goodsName = text.toString().trim()
)
KeyboardUtil.hideKeyboard(context, this)
}
}
@@ -89,9 +101,9 @@ class ShelfActivity : BaseActivity() {
}
val data = ShelfModel(
shelfName = shelfModel?.shelfName,
foodName = list[clickIndex].foodName,
foodWeight = realWeight,
storeDate = DateTimeUtil.formatDateTime(dateTime = Date())
goodsName = list[clickIndex].goodsName,
weight = realWeight / 1000.0,
createTime = DateTimeUtil.formatDateTime(dateTime = Date())
)
val intent = Intent().apply {
putExtra(SHELF_MODEL, data)
@@ -104,7 +116,7 @@ class ShelfActivity : BaseActivity() {
}
binding.btnClearZero.setOnClickListener { v ->
lifecycleScope.launch {
val shelfNo = shelfModel?.shelfNo ?: 0
val shelfNo = shelfModel?.deviceNo ?: 0
val hex = shelfNo.toString(16).padStart(2, '0').uppercase()
Log.d(TAG, "onCreate: hex=$hex")
val zeroClearingCmd =
@@ -117,9 +129,51 @@ class ShelfActivity : BaseActivity() {
lifecycleScope.launch {
SerialPortManager.send(DEVICE_INFO_CMD)
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.getGoodsListUiState.collect { state ->
when (state) {
is UiState.Loading -> showProgress()
is UiState.Success<*> -> updateUI(state.data)
is UiState.Error -> showError(state.msg)
else -> {}
}
}
}
}
registerReceiver(receiver, IntentFilter(RECEIVER_DEVICE_INFO))
viewModel.getGoodsList(
canteenId = "1599670511042785282"
)
}
private fun showProgress() {
Loading.show(this)
}
@SuppressLint("NotifyDataSetChanged")
private fun updateUI(data: RespData<*>) {
Loading.dismiss()
Log.d(TAG, "updateUI: ${data.toJsonString()}")
if (data.result !is GoodsRecord) {
UIUtils.toast("数据错误")
return
}
if (data.result.records.isNullOrEmpty()) {
UIUtils.toast("数据错误")
return
}
list.clear()
list.addAll(data.result.records)
searchAdapter.notifyDataSetChanged()
}
private fun showError(message: String) {
Loading.dismiss()
UIUtils.toast(message)
}
private val receiver = object : BroadcastReceiver() {
@@ -127,7 +181,8 @@ class ShelfActivity : BaseActivity() {
intent?.let {
val data = it.getStringExtra(RECEIVER_DEVICE_INFO)
Log.d(TAG, "onReceive: $data")
realWeight = it.getIntExtra(SHELF_WEIGHT, 0)
val weight = it.getDoubleExtra(SHELF_WEIGHT, 0.0)
realWeight = (weight * 1000).toInt()
binding.tvFoodWeight.text = "${realWeight}"
}
}
@@ -142,28 +197,28 @@ class ShelfActivity : BaseActivity() {
// realWeight = Random.nextInt(1, 5001)
// }
private fun loadSearchData() {
list.add(SearchModel(foodId = 1000, foodName = "菲律宾乳猪五花肉"))
list.add(SearchModel(foodId = 1001, foodName = "金针菇"))
list.add(SearchModel(foodId = 1002, foodName = "大菠菜"))
list.add(SearchModel(foodId = 1003, foodName = "清远麻鸡块"))
list.add(SearchModel(foodId = 1004, foodName = "普罗旺斯番茄"))
list.add(SearchModel(foodId = 1005, foodName = "长条茄子"))
list.add(SearchModel(foodId = 1006, foodName = "得莫利鱼"))
list.add(SearchModel(foodId = 1007, foodName = "土豆丝"))
list.add(SearchModel(foodId = 1008, foodName = "辣椒炒肉"))
list.add(SearchModel(foodId = 1009, foodName = "雪菜烧豆腐"))
list.add(SearchModel(foodId = 1010, foodName = "冬虫夏草炝拌芥兰苗"))
list.add(
SearchModel(
foodId = 1011,
foodName = "蘑菇玉米山药莲藕胡萝卜红枣莲子枸杞清炖排骨汤"
)
)
searchAdapter.notifyDataSetChanged()
}
// private fun loadSearchData() {
// list.add(SearchModel(foodId = 1000, foodName = "菲律宾乳猪五花肉"))
// list.add(SearchModel(foodId = 1001, foodName = "金针菇"))
// list.add(SearchModel(foodId = 1002, foodName = "大菠菜"))
// list.add(SearchModel(foodId = 1003, foodName = "清远麻鸡块"))
// list.add(SearchModel(foodId = 1004, foodName = "普罗旺斯番茄"))
// list.add(SearchModel(foodId = 1005, foodName = "长条茄子"))
// list.add(SearchModel(foodId = 1006, foodName = "得莫利鱼"))
// list.add(SearchModel(foodId = 1007, foodName = "土豆丝"))
// list.add(SearchModel(foodId = 1008, foodName = "辣椒炒肉"))
// list.add(SearchModel(foodId = 1009, foodName = "雪菜烧豆腐"))
// list.add(SearchModel(foodId = 1010, foodName = "冬虫夏草炝拌芥兰苗"))
// list.add(
// SearchModel(
// foodId = 1011,
// foodName = "蘑菇玉米山药莲藕胡萝卜红枣莲子枸杞清炖排骨汤"
// )
// )
// searchAdapter.notifyDataSetChanged()
// }
private val list: MutableList<SearchModel> = mutableListOf()
private val list: MutableList<GoodsModel> = mutableListOf()
private val searchAdapter by lazy {
SearchAdapter(list).apply {
setOnDebouncedItemClick { adapter, v, position ->
@@ -171,7 +226,7 @@ class ShelfActivity : BaseActivity() {
list.forEach { it.isClicked = false }
list[position].isClicked = true
notifyDataSetChanged()
binding.tvFoodName.text = list[position].foodName
binding.tvFoodName.text = list[position].goodsName
KeyboardUtil.hideKeyboard(v.context, v)
}
}