From 8493c14eeddadefcfa6962cc23fe1f0f3a0c80cf Mon Sep 17 00:00:00 2001 From: xiaowusky Date: Tue, 21 Nov 2023 15:33:57 +0800 Subject: [PATCH] =?UTF-8?q?desc:=E4=BC=A0=E6=84=9F=E5=99=A8=E8=AF=BB?= =?UTF-8?q?=E5=86=99=E4=BD=BF=E7=94=A8=E5=90=8C=E4=B8=80=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../watcher/port/GasPortUtils.kt | 6 +- .../safetywatcher/watcher/port/ParseHelper.kt | 22 +++--- .../safetywatcher/watcher/ui/HomeActivity.kt | 1 - .../com/common/commonlib/db/entity/GasType.kt | 1 + .../com/common/serialport/EasySerialPort.kt | 73 ++++--------------- .../common/serialport/utils/MultiPortUtils.kt | 2 +- .../serialport/utils/SinglePortUtils.kt | 2 +- 7 files changed, 32 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt index 79187a3..2ba074c 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt @@ -8,7 +8,7 @@ object GasPortUtils { private const val PORT_PATH = "/dev/ttyS9" const val FULL_MSG_SIZE = 25 const val CHECK_TIME = 10000L - private const val READ_MSG_INTERVAL = 250L + private const val READ_MSG_INTERVAL = 230L private var mInitFlag = false private var readGasMsgThread: Thread? = null; @@ -20,9 +20,9 @@ object GasPortUtils { private fun openPorts() { if (!mInitFlag) { ComMultiPortUtils.releaseAll() - ComMultiPortUtils.openPort(PORT_PATH, BAUD_RATE) { + ComMultiPortUtils.openPort(PORT_PATH, BAUD_RATE) { array, index -> mInitFlag = true - ParseHelper.parse(it) + ParseHelper.parse(array, index) } } } diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt index a7d33ce..c2d871f 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt @@ -32,10 +32,10 @@ object ParseHelper { } private val gasMap = hashMapOf() - fun parse(it: ByteArray) { + fun parse(it: ByteArray, index: Byte) { try { if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) { - val gasIndex = it[0].toInt() + val gasIndex = index.toInt() val status = it[14].toInt() val gasType = getGasTypeEnumByCode(it[19].toInt()) if (gasType == GasTypeEnum.TYPE_UNKNOW) { @@ -45,18 +45,18 @@ object ParseHelper { when (status) { // 预热 0 -> { - updateGasTypeDb(gasType, GasPortStatus.PRE_HOT) + updateGasTypeDb(gasType, gasIndex, GasPortStatus.PRE_HOT) setFlag(gasIndex, gasType) } // 正常 1 -> { - updateGasTypeDb(gasType, GasPortStatus.OK) + updateGasTypeDb(gasType, gasIndex, GasPortStatus.OK) setFlag(gasIndex, gasType) parseGasData(gasType, it) } // 故障 2, 3, 7 -> { - updateGasTypeDb(gasType, GasPortStatus.ERROR) + updateGasTypeDb(gasType, gasIndex, GasPortStatus.ERROR) setFlag(gasIndex, gasType) } @@ -254,12 +254,13 @@ object ParseHelper { * 更新数据库 */ @OptIn(DelicateCoroutinesApi::class) - private fun updateGasTypeDb(type: GasTypeEnum, status: Int) { + private fun updateGasTypeDb(type: GasTypeEnum, sensorIndex: Int, status: Int) { GlobalScope.launch { val typeDao = DBUtils.gasTypeDao() val gasType = typeDao.getByName(type.desc) gasType?.status = status - typeDao.insert(gasType ?: GasType(type.desc, status)) + gasType?.sensorIndex = sensorIndex + typeDao.insert(gasType ?: GasType(type.desc, status, sensorIndex)) } } @@ -271,7 +272,7 @@ object ParseHelper { if (flagRunnable != null) { mHandler.removeCallbacks(flagRunnable) } - flagRunnable = FlagRunnable(type) + flagRunnable = FlagRunnable(type, index) mPortRunnable[index] = flagRunnable // 如果一段时间内没有收到消息,认为连接断开 mHandler.postDelayed(flagRunnable, GasPortUtils.CHECK_TIME) @@ -281,10 +282,11 @@ object ParseHelper { * 离线检查 */ class FlagRunnable( - private val gasName: GasTypeEnum + private val gasName: GasTypeEnum, + private val sensorIndex: Int ) : Runnable { override fun run() { - updateGasTypeDb(gasName, GasPortStatus.OUTLINE) + updateGasTypeDb(gasName, sensorIndex, GasPortStatus.OUTLINE) } } } \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/HomeActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/HomeActivity.kt index 089a823..531eb93 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/HomeActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/HomeActivity.kt @@ -24,7 +24,6 @@ import com.yinuo.safetywatcher.watcher.utils.BatteryHelper import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.RecordHelper import com.yinuo.safetywatcher.watcher.utils.SimHelper -import com.yinuo.safetywatcher.watcher.utils.SoundUtils import com.yinuo.safetywatcher.watcher.utils.WifiHelper import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope diff --git a/library-common/src/main/java/com/common/commonlib/db/entity/GasType.kt b/library-common/src/main/java/com/common/commonlib/db/entity/GasType.kt index 2266947..c97a88b 100644 --- a/library-common/src/main/java/com/common/commonlib/db/entity/GasType.kt +++ b/library-common/src/main/java/com/common/commonlib/db/entity/GasType.kt @@ -8,5 +8,6 @@ import androidx.room.PrimaryKey data class GasType( @PrimaryKey @ColumnInfo(name = "gas_name") var type: String, @ColumnInfo(name = "status") var status: Int = 3, + @ColumnInfo(name = "sensor_index") var sensorIndex: Int = -1, @ColumnInfo(name = "nick_name") var nickName: String? = null, ) diff --git a/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt b/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt index e63451e..7898e6f 100644 --- a/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt +++ b/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt @@ -1,7 +1,6 @@ package com.common.serialport import android.util.Log -import com.common.queue.TaskScheduler import com.common.serialport.inter.ISerialPortHelper import com.common.serialport.utils.HexUtils import java.io.IOException @@ -10,13 +9,10 @@ class EasySerialPort( private val portPath: String, private val baudRate: Int, private val helper: ISerialPortHelper, - private val mReceiver: (ByteArray) -> Unit -) : Runnable { + private val mReceiver: (ByteArray, Byte) -> Unit +) { private var mStartReceiveMsg = true private var mAutoRetryConnect = false - private val mTaskScheduler by lazy { - TaskScheduler() - } private val mReadBytes by lazy { ByteArray(25) @@ -28,10 +24,6 @@ class EasySerialPort( private fun openSerialPort() { val mPort = helper.openSerialPort(portPath, baudRate) - mPort?.let { - mStartReceiveMsg = true - Thread(this).start() - } } /** @@ -42,64 +34,27 @@ class EasySerialPort( helper.closePort() } - /** - * 循环读消息 - */ - override fun run() { - while (mStartReceiveMsg) { - try { - val ips = helper?.inputStream() -// val readByte = ByteArray(25) - val size = ips?.read(mReadBytes) - if (size != null) { - if (size > 0) { - Log.i("EasySerialPort", "read msg ${HexUtils.byteArrToHex(mReadBytes)}") - mReceiver.invoke(mReadBytes) - } - } - Thread.sleep(50L) - } catch (e: IOException) { - Log.e( - "EasySerialPort", - "read msg error; path = " + portPath + ", error msg = " + e.message - ) - e.printStackTrace() - if (mAutoRetryConnect) { - closePort() - openSerialPort() - } - } - } - } - /** * 写消息 */ open fun write(data: ByteArray) { -// val task = SendMsgTask(data) { -// try { -// val outputStream = helper?.outputStream() -// outputStream?.write(it) -// outputStream?.flush() -// } catch (e: IOException) { -// Log.e( -// "EasySerialPort", -// "write msg error; path = " + portPath + ", error msg = " + e.message -// ) -// e.printStackTrace() -// if (mAutoRetryConnect) { -// closePort() -// openSerialPort() -// } -// } -// } -// task.attachScheduler(mTaskScheduler) -// task.enqueue() try { Log.i("EasySerialPort", "send msg ${HexUtils.byteArrToHex(data)}") val outputStream = helper?.outputStream() outputStream?.write(data) outputStream?.flush() + + val sensorIndex = data[2] + Thread.sleep(20L) + // 写完之后读取数据 + val ips = helper?.inputStream() + val size = ips?.read(mReadBytes) + if (size != null) { + if (size > 0) { + Log.i("EasySerialPort", "index $sensorIndex ;read msg ${HexUtils.byteArrToHex(mReadBytes)}") + mReceiver.invoke(mReadBytes, sensorIndex) + } + } } catch (e: IOException) { Log.e( "EasySerialPort", diff --git a/library-serialPort/src/main/java/com/common/serialport/utils/MultiPortUtils.kt b/library-serialPort/src/main/java/com/common/serialport/utils/MultiPortUtils.kt index 29046f5..88069a2 100644 --- a/library-serialPort/src/main/java/com/common/serialport/utils/MultiPortUtils.kt +++ b/library-serialPort/src/main/java/com/common/serialport/utils/MultiPortUtils.kt @@ -27,7 +27,7 @@ abstract class MultiPortUtils { fun openPort( portPath: String, baudRate: Int, - mReceiver: (ByteArray) -> Unit + mReceiver: (ByteArray, Byte) -> Unit ) { if (mPortMaps.containsKey(portPath)) { return diff --git a/library-serialPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt b/library-serialPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt index 6dbbb74..8748e56 100644 --- a/library-serialPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt +++ b/library-serialPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt @@ -22,7 +22,7 @@ abstract class SinglePortUtils { fun initParams( portPath: String, baudRate: Int, - mReceiver: (ByteArray) -> Unit + mReceiver: (ByteArray, Byte) -> Unit ) { mEasyPort = EasySerialPort(portPath, baudRate, generateHelper(), mReceiver) }