desc:代码优化

main
xiaowusky 1 year ago
parent 71c12c6da3
commit ff1c62092d

@ -17,6 +17,7 @@ object GasPortUtils {
private const val READ_MSG_INTERVAL = 180L
private var mInitFlag = false
private var readGasMsgThread: Thread? = null;
private var toDoCmdList: ArrayList<ByteArray> = ArrayList<ByteArray>()
fun initPort() {
openPorts()
@ -26,9 +27,9 @@ object GasPortUtils {
private fun openPorts() {
if (!mInitFlag) {
ComMultiPortUtils.releaseAll()
ComMultiPortUtils.openPort(PORT_PATH, BAUD_RATE) { array, index ->
ComMultiPortUtils.openPort(PORT_PATH, BAUD_RATE) { array ->
mInitFlag = true
ParseHelper.parse(array, index)
ParseHelper.parse(array)
}
}
}
@ -56,6 +57,13 @@ object GasPortUtils {
ComMultiPortUtils.sendMsg(PORT_PATH, CMD.SENSOR_4)
Thread.sleep(READ_MSG_INTERVAL)
ComMultiPortUtils.sendMsg(PORT_PATH, CMD.SENSOR_5)
if (toDoCmdList.isNotEmpty()) {
toDoCmdList.forEach {
Thread.sleep(READ_MSG_INTERVAL)
ComMultiPortUtils.sendMsg(PORT_PATH, it)
}
toDoCmdList.clear()
}
}
} catch (e: Exception) {
e.printStackTrace()
@ -67,18 +75,14 @@ object GasPortUtils {
}
fun setSpeed(speed: Int) {
GlobalScope.launch(Dispatchers.IO) {
ComMultiPortUtils.sendMsg(PORT_PATH, CMD.buildSpeedCmd(speed))
}
toDoCmdList.add(CMD.buildSpeedCmd(speed))
}
fun calibrationSensor(index: Int, value: Int) {
GlobalScope.launch(Dispatchers.IO) {
if (value == 0) {
ComMultiPortUtils.sendMsg(PORT_PATH, CalibrationCmd(index, value).buildZeroCmd())
} else {
ComMultiPortUtils.sendMsg(PORT_PATH, CalibrationCmd(index, value).buildSPANCmd())
}
if (value == 0) {
toDoCmdList.add(CalibrationCmd(index, value).buildZeroCmd())
} else {
toDoCmdList.add(CalibrationCmd(index, value).buildSPANCmd())
}
}
}

@ -38,8 +38,12 @@ object ParseHelper {
}
private val gasMap = hashMapOf<String, Gas>()
private val unConvertValueMap = hashMapOf<String, Float>()
private var mWarnListener: ((String, Warning?) -> Unit)? = null;
fun parse(it: ByteArray, index: Byte) {
fun setWarnChangeListener(onChange: (String, Warning?) -> Unit) {
mWarnListener = onChange
}
fun parse(it: ByteArray) {
try {
Thread.sleep(0)
if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) {
@ -191,11 +195,13 @@ object ParseHelper {
val insertWarning = Warning(
typeEnum.desc, value, unit, System.currentTimeMillis(), min, max, overRange
)
mWarnListener?.invoke(typeEnum.desc, insertWarning)
warningDao.insert(insertWarning)
return insertWarning
} else {
warning.gasValue = value
warning.overRange = overRange
mWarnListener?.invoke(typeEnum.desc, warning)
warningDao.update(warning)
return warning
}
@ -211,11 +217,13 @@ object ParseHelper {
val insertWarning = Warning(
typeEnum.desc, value, unit, System.currentTimeMillis(), min, max, overRange
)
mWarnListener?.invoke(typeEnum.desc, insertWarning)
warningDao.insert(insertWarning)
return insertWarning
} else {
warning.gasValue = value
warning.overRange = overRange
mWarnListener?.invoke(typeEnum.desc, warning)
warningDao.update(warning)
return warning
}
@ -226,6 +234,7 @@ object ParseHelper {
}
}
}
mWarnListener?.invoke(typeEnum.desc, null)
return null
}

@ -7,6 +7,7 @@ import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import com.common.commonlib.db.DBUtils
import com.common.commonlib.db.entity.Warning
import com.common.commonlib.utils.LogUtils
import com.common.commonlib.utils.NetworkHelper
import com.yinuo.safetywatcher.R
@ -18,6 +19,7 @@ import com.yinuo.safetywatcher.watcher.constant.DELAY_TIME_CHECK_CAMERA
import com.yinuo.safetywatcher.watcher.constant.DELAY_TIME_CHECK_SENSOR
import com.yinuo.safetywatcher.watcher.constant.DELAY_TIME_OPEN_CAMERA
import com.yinuo.safetywatcher.watcher.port.GasPortUtils
import com.yinuo.safetywatcher.watcher.port.ParseHelper
import com.yinuo.safetywatcher.watcher.port.getShowWarnTxt
import com.yinuo.safetywatcher.watcher.services.HeartbeatService
import com.yinuo.safetywatcher.watcher.ui.view.ConfirmDialog
@ -203,6 +205,7 @@ class HomeActivity : NoOptionsActivity() {
}
private val warnMap = hashMapOf<String, Warning?>()
// 设置传感器
private fun setForSensor() {
TxtOverlay.setTipChangeListener {
@ -213,9 +216,12 @@ class HomeActivity : NoOptionsActivity() {
closeLoadingDialog()
changeViewStatus()
}
setWarnView()
watchSensor(DELAY_TIME_CHECK_SENSOR)
}
ParseHelper.setWarnChangeListener { name, warning ->
warnMap[name] = warning
setWarnView()
}
watchSensor(DELAY_TIME_CHECK_SENSOR)
GasPortUtils.initPort()
}
@ -225,13 +231,10 @@ class HomeActivity : NoOptionsActivity() {
*/
private fun setWarnView() {
GlobalScope.launch(Dispatchers.IO) {
val warningDao = DBUtils.warningDao()
val warningList = warningDao.queryWarningsNoEndTime()
val builder = StringBuilder()
warningList?.forEachIndexed { index, it ->
builder.append(getShowWarnTxt(it))
if (index != warningList.size - 1) {
builder.append("\n")
warnMap.forEach { (index, it) ->
it?.let {
builder.append(getShowWarnTxt(it))
}
}
launch(Dispatchers.Main) {

@ -9,7 +9,7 @@ class EasySerialPort<T>(
private val portPath: String,
private val baudRate: Int,
private val helper: ISerialPortHelper<T>,
private val mReceiver: (ByteArray, Byte) -> Unit
private val mReceiver: (ByteArray) -> Unit
) {
private var mStartReceiveMsg = true
private var mAutoRetryConnect = false
@ -44,8 +44,11 @@ class EasySerialPort<T>(
outputStream?.write(data)
outputStream?.flush()
Log.i("EasySerialPort", "send msg ${HexUtils.byteArrToHex(data)}")
val cmd = data[1]
val sensorIndex = data[2]
var offset = 1
if (data[0] ==0xFF.toByte()){
offset = 2
}
val cmd = data[offset]
// 写完之后读取数据
val ips = helper?.inputStream()
val size = ips?.read(mReadBytes)
@ -53,10 +56,10 @@ class EasySerialPort<T>(
if (size > 0) {
Log.i(
"EasySerialPort",
"index $sensorIndex ;read msg ${HexUtils.byteArrToHex(mReadBytes)}"
"cmd $cmd ;read msg ${HexUtils.byteArrToHex(mReadBytes)}"
)
if (cmd.toInt() == 0x03 && sensorIndex < 5) {
mReceiver.invoke(mReadBytes, sensorIndex)
if (mReadBytes[0].toInt() == 0x01 && mReadBytes[1].toInt() == 0x03 && mReadBytes[2].toInt() == 0x14) {
mReceiver.invoke(mReadBytes)
}
}
}

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

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

Loading…
Cancel
Save