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

main
xiaowusky 1 year ago
parent 59f5a86870
commit 8493c14eed

@ -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)
}
}
}

@ -32,10 +32,10 @@ object ParseHelper {
}
private val gasMap = hashMapOf<String, Gas>()
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)
}
}
}

@ -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

@ -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,
)

@ -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<T>(
private val portPath: String,
private val baudRate: Int,
private val helper: ISerialPortHelper<T>,
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<T>(
private fun openSerialPort() {
val mPort = helper.openSerialPort(portPath, baudRate)
mPort?.let {
mStartReceiveMsg = true
Thread(this).start()
}
}
/**
@ -42,64 +34,27 @@ class EasySerialPort<T>(
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",

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

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

Loading…
Cancel
Save