diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt b/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt index 74d47c8..de1c921 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/HomeActivity.kt @@ -71,13 +71,13 @@ class HomeActivity : BaseActivity() { runCatching { receiveSerialPortData(data) }.onFailure { - it.printStackTrace() - } + it.printStackTrace() + } } } SerialPortManager.send(ACTIVE_CMD) - SerialPortManager.send(DEVICE_INFO_CMD) + //SerialPortManager.send(DEVICE_INFO_CMD) } } TaskManager.startTask() @@ -90,47 +90,86 @@ class HomeActivity : BaseActivity() { } val cmd = data.substring(HEADER.length, HEADER.length + 2) when (cmd) { + "01" -> { + //心跳-01 + Log.d(TAG, "receiveSerialPortData:心跳01: $data") + } + + "81" -> { + //心跳-81 + Log.d(TAG, "receiveSerialPortData:心跳08: $data") + heartBeat(data) + } + "03" -> { //获取设备状态 - val weightStartIndex = HEADER.length + 4 + 10 - val temperatureStartIndex = weightStartIndex + 10 * 8 - val temperatureHex = data.substring(temperatureStartIndex, 2) - val temperature = hexToDec(temperatureHex) - updateLeftStatus("1-1冷藏柜 $temperature°C / 87%") - - var index = 1 - var count = weightStartIndex - while (count < temperatureStartIndex) { - val weightInfo = data.substring(count, count + 8) - var weightHex = "" - weightInfo.run { - weightHex = - substring(6, 8) + substring(4, 6) + substring(2, 4) + substring(0, 2) - } - var realWeight = 0 - if (weightHex.first() in '8'..'F') { - //负数 - val weightHex2 = "0${weightHex.substring(1)}" - realWeight = hexToDec(weightHex2) - } else { - //整数 - realWeight = hexToDec(weightHex) - } - weightArray.put(count, realWeight) - count += 8 - index++ - } - - val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO) - intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex]) - intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data) - sendBroadcast(intent) + 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%") + } + + private fun getWeightInfo(start: Int, end: Int, data: String) { + var index = 1 + var count = start + while (count < end) { + val weightInfo = data.substring(count, count + 8) + var weightHex = "" + weightInfo.run { + weightHex = + substring(6, 8) + substring(4, 6) + substring(2, 4) + substring(0, 2) + } + var realWeight = 0 + if (weightHex.first() in '8'..'F') { + //负数 + val weightHex2 = "0${weightHex.substring(1)}" + realWeight = hexToDec(weightHex2) + } else { + //整数 + realWeight = hexToDec(weightHex) + } + weightArray.put(index, realWeight) + count += 8 + index++ + } + } + + private fun getDeviceInfo(data: String) { + val weightStartIndex = HEADER.length + 4 + 10 + val temperatureStartIndex = weightStartIndex + 10 * 8 + val temperatureEndIndex = temperatureStartIndex + 2 + val temperatureHex = data.substring(temperatureStartIndex, temperatureEndIndex) + val temperature = hexToDec(temperatureHex) + updateLeftStatus("1-1冷藏柜 $temperature°C / 87%") + + getWeightInfo( + start = weightStartIndex, + end = temperatureStartIndex, + data = data + ) + + val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO) + intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex]) + intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data) + sendBroadcast(intent) + } + private val weightArray by lazy { SparseIntArray() } private var clickIndex = 0 diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt b/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt index 6b4f318..56dc448 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/ShelfActivity.kt @@ -104,8 +104,11 @@ class ShelfActivity : BaseActivity() { binding.btnClearZero.setOnClickListener { v -> lifecycleScope.launch { val shelfNo = shelfModel?.shelfNo ?: 0 + val hex = shelfNo.toString(16).padStart(2, '0').uppercase() + Log.d(TAG, "onCreate: hex=$hex") val zeroClearingCmd = - "${HomeActivity.HEADER}0401${decToHex(shelfNo)}${HomeActivity.FOOTER}" + "${HomeActivity.HEADER}0401${hex}${HomeActivity.FOOTER}" + Log.d(TAG, "onCreate: zeroClearingCmd=$zeroClearingCmd") SerialPortManager.send(zeroClearingCmd) } KeyboardUtil.hideKeyboard(v.context, v) diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt b/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt index 3ad9563..27d7224 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/adapter/ShelfAdapter.kt @@ -27,7 +27,8 @@ class ShelfAdapter(list: MutableList) : item.shelfName = "货架-${if (shelfNo < 10) "0${shelfNo}" else "$shelfNo" }" val weight = item.foodWeight ?: 0 val binding = holder.binding - val isBlankShelf = weight <= 0 + //val isBlankShelf = weight <= 0 + val isBlankShelf = item.foodName.isNullOrBlank() if (isBlankShelf) { binding.layoutCard.setCardBackgroundColor(getColor(R.color.bg_card_blue)) binding.tvShelfName.run { diff --git a/app/src/main/java/com/shuwei/intelligent/shelves/serial/SerialPortManager.kt b/app/src/main/java/com/shuwei/intelligent/shelves/serial/SerialPortManager.kt index 095a422..34e3c55 100644 --- a/app/src/main/java/com/shuwei/intelligent/shelves/serial/SerialPortManager.kt +++ b/app/src/main/java/com/shuwei/intelligent/shelves/serial/SerialPortManager.kt @@ -1,5 +1,6 @@ package com.shuwei.intelligent.shelves.serial +import android.annotation.SuppressLint import com.shuwei.intelligent.shelves.utils.hexToBytes import com.shuwei.intelligent.shelves.utils.toHexString2 import io.github.jeadyx.jserialport.AndroidSerialPort @@ -38,7 +39,6 @@ object SerialPortManager { // 发送数据 suspend fun send(data: String): Boolean { return try { -// serialPort?.write(data.toByteArray()) serialPort?.write(data.hexToBytes()) true } catch (e: Exception) { @@ -48,11 +48,12 @@ object SerialPortManager { } // 启动接收协程 + @Suppress("DEPRECATION") + @SuppressLint("DefaultLocale") fun startReceive(callback: (String) -> Unit) { scope.launch { while (isActive) { serialPort?.read()?.collect { buffer -> -// val data = String(buffer) val data = buffer.toHexString2().toUpperCase() callback(data) } diff --git a/app/src/main/res/layout/activity_shelf.xml b/app/src/main/res/layout/activity_shelf.xml index a5b97f3..70d3f05 100644 --- a/app/src/main/res/layout/activity_shelf.xml +++ b/app/src/main/res/layout/activity_shelf.xml @@ -161,9 +161,12 @@ android:id="@+id/rvSearch" android:layout_width="match_parent" android:layout_height="0dp" - android:layout_marginStart="36dp" - android:layout_marginTop="36dp" - android:layout_marginEnd="36dp" + android:paddingStart="36dp" + android:paddingBottom="12dp" + android:layout_marginTop="24dp" + android:paddingTop="12dp" + android:paddingEnd="36dp" + android:clipToPadding="false" android:layout_weight="1" android:overScrollMode="never" tools:listitem="@layout/list_item_search" @@ -171,11 +174,12 @@ app:spanCount="2" tools:itemCount="10"/> +