desc:传感器读写使用同一线程

main
xiaowusky 1 year ago
parent 59f5a86870
commit 8493c14eed

@ -8,7 +8,7 @@ object GasPortUtils {
private const val PORT_PATH = "/dev/ttyS9" private const val PORT_PATH = "/dev/ttyS9"
const val FULL_MSG_SIZE = 25 const val FULL_MSG_SIZE = 25
const val CHECK_TIME = 10000L 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 mInitFlag = false
private var readGasMsgThread: Thread? = null; private var readGasMsgThread: Thread? = null;
@ -20,9 +20,9 @@ object GasPortUtils {
private fun openPorts() { private fun openPorts() {
if (!mInitFlag) { if (!mInitFlag) {
ComMultiPortUtils.releaseAll() ComMultiPortUtils.releaseAll()
ComMultiPortUtils.openPort(PORT_PATH, BAUD_RATE) { ComMultiPortUtils.openPort(PORT_PATH, BAUD_RATE) { array, index ->
mInitFlag = true mInitFlag = true
ParseHelper.parse(it) ParseHelper.parse(array, index)
} }
} }
} }

@ -32,10 +32,10 @@ object ParseHelper {
} }
private val gasMap = hashMapOf<String, Gas>() private val gasMap = hashMapOf<String, Gas>()
fun parse(it: ByteArray) { fun parse(it: ByteArray, index: Byte) {
try { try {
if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) { if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) {
val gasIndex = it[0].toInt() val gasIndex = index.toInt()
val status = it[14].toInt() val status = it[14].toInt()
val gasType = getGasTypeEnumByCode(it[19].toInt()) val gasType = getGasTypeEnumByCode(it[19].toInt())
if (gasType == GasTypeEnum.TYPE_UNKNOW) { if (gasType == GasTypeEnum.TYPE_UNKNOW) {
@ -45,18 +45,18 @@ object ParseHelper {
when (status) { when (status) {
// 预热 // 预热
0 -> { 0 -> {
updateGasTypeDb(gasType, GasPortStatus.PRE_HOT) updateGasTypeDb(gasType, gasIndex, GasPortStatus.PRE_HOT)
setFlag(gasIndex, gasType) setFlag(gasIndex, gasType)
} }
// 正常 // 正常
1 -> { 1 -> {
updateGasTypeDb(gasType, GasPortStatus.OK) updateGasTypeDb(gasType, gasIndex, GasPortStatus.OK)
setFlag(gasIndex, gasType) setFlag(gasIndex, gasType)
parseGasData(gasType, it) parseGasData(gasType, it)
} }
// 故障 // 故障
2, 3, 7 -> { 2, 3, 7 -> {
updateGasTypeDb(gasType, GasPortStatus.ERROR) updateGasTypeDb(gasType, gasIndex, GasPortStatus.ERROR)
setFlag(gasIndex, gasType) setFlag(gasIndex, gasType)
} }
@ -254,12 +254,13 @@ object ParseHelper {
* 更新数据库 * 更新数据库
*/ */
@OptIn(DelicateCoroutinesApi::class) @OptIn(DelicateCoroutinesApi::class)
private fun updateGasTypeDb(type: GasTypeEnum, status: Int) { private fun updateGasTypeDb(type: GasTypeEnum, sensorIndex: Int, status: Int) {
GlobalScope.launch { GlobalScope.launch {
val typeDao = DBUtils.gasTypeDao() val typeDao = DBUtils.gasTypeDao()
val gasType = typeDao.getByName(type.desc) val gasType = typeDao.getByName(type.desc)
gasType?.status = status 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) { if (flagRunnable != null) {
mHandler.removeCallbacks(flagRunnable) mHandler.removeCallbacks(flagRunnable)
} }
flagRunnable = FlagRunnable(type) flagRunnable = FlagRunnable(type, index)
mPortRunnable[index] = flagRunnable mPortRunnable[index] = flagRunnable
// 如果一段时间内没有收到消息,认为连接断开 // 如果一段时间内没有收到消息,认为连接断开
mHandler.postDelayed(flagRunnable, GasPortUtils.CHECK_TIME) mHandler.postDelayed(flagRunnable, GasPortUtils.CHECK_TIME)
@ -281,10 +282,11 @@ object ParseHelper {
* 离线检查 * 离线检查
*/ */
class FlagRunnable( class FlagRunnable(
private val gasName: GasTypeEnum private val gasName: GasTypeEnum,
private val sensorIndex: Int
) : Runnable { ) : Runnable {
override fun run() { override fun run() {
updateGasTypeDb(gasName, GasPortStatus.OUTLINE) updateGasTypeDb(gasName, sensorIndex, GasPortStatus.OUTLINE)
} }
} }
} }

@ -24,7 +24,6 @@ import com.yinuo.safetywatcher.watcher.utils.BatteryHelper
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.RecordHelper import com.yinuo.safetywatcher.watcher.utils.RecordHelper
import com.yinuo.safetywatcher.watcher.utils.SimHelper import com.yinuo.safetywatcher.watcher.utils.SimHelper
import com.yinuo.safetywatcher.watcher.utils.SoundUtils
import com.yinuo.safetywatcher.watcher.utils.WifiHelper import com.yinuo.safetywatcher.watcher.utils.WifiHelper
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope

@ -8,5 +8,6 @@ import androidx.room.PrimaryKey
data class GasType( data class GasType(
@PrimaryKey @ColumnInfo(name = "gas_name") var type: String, @PrimaryKey @ColumnInfo(name = "gas_name") var type: String,
@ColumnInfo(name = "status") var status: Int = 3, @ColumnInfo(name = "status") var status: Int = 3,
@ColumnInfo(name = "sensor_index") var sensorIndex: Int = -1,
@ColumnInfo(name = "nick_name") var nickName: String? = null, @ColumnInfo(name = "nick_name") var nickName: String? = null,
) )

@ -1,7 +1,6 @@
package com.common.serialport package com.common.serialport
import android.util.Log import android.util.Log
import com.common.queue.TaskScheduler
import com.common.serialport.inter.ISerialPortHelper import com.common.serialport.inter.ISerialPortHelper
import com.common.serialport.utils.HexUtils import com.common.serialport.utils.HexUtils
import java.io.IOException import java.io.IOException
@ -10,13 +9,10 @@ class EasySerialPort<T>(
private val portPath: String, private val portPath: String,
private val baudRate: Int, private val baudRate: Int,
private val helper: ISerialPortHelper<T>, private val helper: ISerialPortHelper<T>,
private val mReceiver: (ByteArray) -> Unit private val mReceiver: (ByteArray, Byte) -> Unit
) : Runnable { ) {
private var mStartReceiveMsg = true private var mStartReceiveMsg = true
private var mAutoRetryConnect = false private var mAutoRetryConnect = false
private val mTaskScheduler by lazy {
TaskScheduler()
}
private val mReadBytes by lazy { private val mReadBytes by lazy {
ByteArray(25) ByteArray(25)
@ -28,10 +24,6 @@ class EasySerialPort<T>(
private fun openSerialPort() { private fun openSerialPort() {
val mPort = helper.openSerialPort(portPath, baudRate) val mPort = helper.openSerialPort(portPath, baudRate)
mPort?.let {
mStartReceiveMsg = true
Thread(this).start()
}
} }
/** /**
@ -42,64 +34,27 @@ class EasySerialPort<T>(
helper.closePort() 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) { 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 { try {
Log.i("EasySerialPort", "send msg ${HexUtils.byteArrToHex(data)}") Log.i("EasySerialPort", "send msg ${HexUtils.byteArrToHex(data)}")
val outputStream = helper?.outputStream() val outputStream = helper?.outputStream()
outputStream?.write(data) outputStream?.write(data)
outputStream?.flush() 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) { } catch (e: IOException) {
Log.e( Log.e(
"EasySerialPort", "EasySerialPort",

@ -27,7 +27,7 @@ abstract class MultiPortUtils<T> {
fun openPort( fun openPort(
portPath: String, portPath: String,
baudRate: Int, baudRate: Int,
mReceiver: (ByteArray) -> Unit mReceiver: (ByteArray, Byte) -> Unit
) { ) {
if (mPortMaps.containsKey(portPath)) { if (mPortMaps.containsKey(portPath)) {
return return

@ -22,7 +22,7 @@ abstract class SinglePortUtils<T> {
fun initParams( fun initParams(
portPath: String, portPath: String,
baudRate: Int, baudRate: Int,
mReceiver: (ByteArray) -> Unit mReceiver: (ByteArray, Byte) -> Unit
) { ) {
mEasyPort = EasySerialPort(portPath, baudRate, generateHelper(), mReceiver) mEasyPort = EasySerialPort(portPath, baudRate, generateHelper(), mReceiver)
} }

Loading…
Cancel
Save