接口联调
This commit is contained in:
@@ -5,25 +5,37 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import android.util.SparseArray
|
||||
import android.util.SparseIntArray
|
||||
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.adapter.ShelfAdapter
|
||||
import com.shuwei.intelligent.shelves.base.BaseActivity
|
||||
import com.shuwei.intelligent.shelves.databinding.ActivityHomeBinding
|
||||
import com.shuwei.intelligent.shelves.model.ShelfModel
|
||||
import com.shuwei.intelligent.shelves.model.ShelfResult
|
||||
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.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.CoroutineStart
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import kotlin.getValue
|
||||
|
||||
class HomeActivity : BaseActivity() {
|
||||
|
||||
@@ -52,13 +64,15 @@ class HomeActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: ActivityHomeBinding
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private val viewModel: NetViewModel by viewModels()
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged", "HardwareIds")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityHomeBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
updateLeftStatus("1-1冷藏柜 0°C / 0%")
|
||||
updateLeftStatus("1-1冷藏柜 -°C / -%")
|
||||
|
||||
initList()
|
||||
initRecyclerView()
|
||||
@@ -77,18 +91,95 @@ class HomeActivity : BaseActivity() {
|
||||
|
||||
//SerialPortManager.send(DEVICE_INFO_CMD)
|
||||
}
|
||||
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.getShelfListUiState.collect { state ->
|
||||
when (state) {
|
||||
is UiState.Loading -> showProgress()
|
||||
is UiState.Success<*> -> updateUI(state.data)
|
||||
is UiState.Error -> showError(state.msg)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.getAccessTokenUiState.collect { state ->
|
||||
when (state) {
|
||||
is UiState.Loading -> {}
|
||||
is UiState.Success<*> -> getTokenSuccess(state.data)
|
||||
is UiState.Error -> UIUtils.toast(state.msg)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
TaskManager.startTask()
|
||||
|
||||
App.get().androidId = "53babf69-0d2f-3875-a649-f2e12f80ad4c1"
|
||||
// Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
|
||||
Log.d(TAG, "onCreate: androidId=${App.get().androidId}")
|
||||
|
||||
val swDpValue = resources?.configuration?.smallestScreenWidthDp
|
||||
Log.d(TAG, "onCreate: swDpValue=$swDpValue")
|
||||
|
||||
viewModel.getShelfList(deviceId = App.get().androidId)
|
||||
|
||||
|
||||
|
||||
|
||||
viewModel.getAccessToken(App.get().androidId)
|
||||
|
||||
// repeat(100) {
|
||||
// lifecycleScope.launch {
|
||||
// var data = "D6DADEDB01010C6102000000000000010000000500008000000000060000000500000004000080060000000100000010190080701800800000000000000000000000000000000000000000000000005037414645333633333334333731373056322E313069FFFF"
|
||||
// val data = "D6DADEDB01010C6102000000000000010000000500008000000000060000000500000004000080060000000100000010190080701800800000000000000000000000000000000000000000000000005037414645333633333334333731373056322E313069FFFF"
|
||||
// receiveSerialPortData(data)
|
||||
// delay(100)
|
||||
// delay(500)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
private fun getTokenSuccess(data: RespData<*>) {
|
||||
Log.d(TAG, "getTokenSuccess: $data")
|
||||
data.result?.let {
|
||||
//App.get().accessToken = it.toString()
|
||||
}
|
||||
}
|
||||
|
||||
private fun showProgress() {
|
||||
Loading.show(this)
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun updateUI(data: RespData<*>) {
|
||||
Loading.dismiss()
|
||||
if (data.result !is ShelfResult) {
|
||||
UIUtils.toast("数据错误")
|
||||
return
|
||||
}
|
||||
val tempList = data.result.containerGoodsList
|
||||
if (tempList.isNullOrEmpty()) {
|
||||
UIUtils.toast("数据为空")
|
||||
return
|
||||
}
|
||||
if (tempList.size < 10) {
|
||||
UIUtils.toast("数据错误")
|
||||
return
|
||||
}
|
||||
list.clear()
|
||||
val subList01 = tempList.subList(0, 5)
|
||||
val subList02 = tempList.subList(5, 10)
|
||||
repeat(5) { index ->
|
||||
list.add(subList01[index])
|
||||
list.add(subList02[index])
|
||||
}
|
||||
shelfAdapter.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun showError(message: String) {
|
||||
Loading.dismiss()
|
||||
UIUtils.toast(message)
|
||||
}
|
||||
|
||||
private val serialReader by lazy { StringBuilder() }
|
||||
|
||||
private fun receiveSerialPortData(srcData: String) {
|
||||
@@ -103,7 +194,7 @@ class HomeActivity : BaseActivity() {
|
||||
serialReader.clear()
|
||||
serialReader.append(srcData)
|
||||
return
|
||||
} else if (srcData.contains(FOOTER).not()){
|
||||
} else if (srcData.contains(FOOTER).not()) {
|
||||
serialReader.append(srcData)
|
||||
return
|
||||
} else {
|
||||
@@ -151,9 +242,9 @@ class HomeActivity : BaseActivity() {
|
||||
val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex)
|
||||
val temperature = hexToDec(temperatureHex)
|
||||
val tempC = f2C(temperature.toDouble())
|
||||
val showTemp = "%.1f".format(tempC.toDouble())
|
||||
val showTemp = "%.1f".format(tempC)
|
||||
|
||||
val percent = 10 * list.count { it.foodName.isNullOrBlank().not() }
|
||||
val percent = 10 * list.count { it.goodsName.isNullOrBlank().not() }
|
||||
|
||||
updateLeftStatus("1-1冷藏柜 ${showTemp}°C / ${percent}%")
|
||||
}.onFailure {
|
||||
@@ -175,18 +266,17 @@ class HomeActivity : BaseActivity() {
|
||||
if (weightHex.first() in '8'..'F') {
|
||||
//负数
|
||||
val weightHex2 = "0${weightHex.substring(1)}"
|
||||
realWeight = -1*weightHex2.toInt()
|
||||
//-1*hexToDec(weightHex2)
|
||||
realWeight = -1 * weightHex2.toInt()
|
||||
} else {
|
||||
//整数
|
||||
realWeight = weightHex.toInt()
|
||||
//hexToDec(weightHex)
|
||||
}
|
||||
weightArray.put(index, realWeight)
|
||||
val realWeightDouble = realWeight / 1000.0
|
||||
weightArray.put(index, realWeightDouble)
|
||||
|
||||
if (index <= 10) {
|
||||
list.first { it.shelfNo == index }.let {
|
||||
it.foodWeight = realWeight
|
||||
list.firstOrNull { it.deviceNo == index }?.let {
|
||||
it.weight = realWeightDouble
|
||||
}
|
||||
shelfAdapter.notifyItemChanged(index)
|
||||
}
|
||||
@@ -216,7 +306,7 @@ class HomeActivity : BaseActivity() {
|
||||
// sendBroadcast(intent)
|
||||
}
|
||||
|
||||
private val weightArray by lazy { SparseIntArray() }
|
||||
private val weightArray by lazy { SparseArray<Double?>() }
|
||||
private var clickIndex = 0
|
||||
|
||||
override fun onDestroy() {
|
||||
@@ -234,13 +324,13 @@ class HomeActivity : BaseActivity() {
|
||||
setOnDebouncedItemClick { adapter, view, position ->
|
||||
this@HomeActivity.clickIndex = position
|
||||
lifecycleScope.launch {
|
||||
val openCmd = if (list[position].shelfNo in 1..5)
|
||||
val openCmd = if (list[position].deviceNo in 1..5)
|
||||
LEFT_SHELF_OPEN_CMD
|
||||
else
|
||||
RIGHT_SHELF_OPEN_CMD
|
||||
val openState = SerialPortManager.send(openCmd)
|
||||
if (openState.not()) {
|
||||
UIUtils.toast("${list[position].foodName}柜门开启失败")
|
||||
UIUtils.toast("${list[position].goodsName}柜门开启失败")
|
||||
return@launch
|
||||
}
|
||||
launch(Intent(this@HomeActivity, ShelfActivity::class.java).also {
|
||||
@@ -250,9 +340,9 @@ class HomeActivity : BaseActivity() {
|
||||
val model =
|
||||
getSerializableExtra(ShelfActivity.SHELF_MODEL) as ShelfModel
|
||||
list[position].apply {
|
||||
foodName = model.foodName
|
||||
foodWeight = model.foodWeight
|
||||
storeDate = model.storeDate
|
||||
goodsName = model.goodsName
|
||||
weight = model.weight
|
||||
createTime = model.createTime
|
||||
}
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
@@ -271,18 +361,18 @@ class HomeActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun initList() {
|
||||
// val list: MutableList<ShelfModel> = mutableListOf()
|
||||
list.add(ShelfModel(shelfNo = 1))
|
||||
list.add(ShelfModel(shelfNo = 6))
|
||||
list.add(ShelfModel(shelfNo = 2))
|
||||
list.add(ShelfModel(shelfNo = 7))
|
||||
list.add(ShelfModel(shelfNo = 3))
|
||||
list.add(ShelfModel(shelfNo = 8))
|
||||
list.add(ShelfModel(shelfNo = 4))
|
||||
list.add(ShelfModel(shelfNo = 9))
|
||||
list.add(ShelfModel(shelfNo = 5))
|
||||
list.add(ShelfModel(shelfNo = 10))
|
||||
// viewModel.updateItems(list)
|
||||
//// val list: MutableList<ShelfModel> = mutableListOf()
|
||||
// list.add(ShelfModel(deviceNo = 1))
|
||||
// list.add(ShelfModel(deviceNo = 6))
|
||||
// list.add(ShelfModel(deviceNo = 2))
|
||||
// list.add(ShelfModel(deviceNo = 7))
|
||||
// list.add(ShelfModel(deviceNo = 3))
|
||||
// list.add(ShelfModel(deviceNo = 8))
|
||||
// list.add(ShelfModel(deviceNo = 4))
|
||||
// list.add(ShelfModel(deviceNo = 9))
|
||||
// list.add(ShelfModel(deviceNo = 5))
|
||||
// list.add(ShelfModel(deviceNo = 10))
|
||||
//// viewModel.updateItems(list)
|
||||
}
|
||||
|
||||
private val MM_DD_EEEE__HH_MM_SS = "MM月dd日 EEEE HH:mm:ss"
|
||||
|
||||
Reference in New Issue
Block a user