desc:代码优化

main
xiaowusky 1 year ago
parent fc80796938
commit c9c7a4838f

@ -2,13 +2,14 @@ package com.yinuo.safetywatcher.watcher.port
import com.common.serialport.ComMultiPortUtils import com.common.serialport.ComMultiPortUtils
import com.yinuo.safetywatcher.watcher.port.cmd.CMD import com.yinuo.safetywatcher.watcher.port.cmd.CMD
import android.os.Process
object GasPortUtils { object GasPortUtils {
private const val BAUD_RATE = 9600 private const val BAUD_RATE = 9600
private const val PORT_PATH = "/dev/ttyS9" private const val PORT_PATH = "/dev/ttyS6"
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 = 230L private const val READ_MSG_INTERVAL = 300L
private var mInitFlag = false private var mInitFlag = false
private var readGasMsgThread: Thread? = null; private var readGasMsgThread: Thread? = null;
@ -34,6 +35,7 @@ object GasPortUtils {
if (readGasMsgThread == null) { if (readGasMsgThread == null) {
readGasMsgThread = Thread { readGasMsgThread = Thread {
try { try {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
while (true) { while (true) {
// 延时一段时间后,发送指令读取气体数据 // 延时一段时间后,发送指令读取气体数据
Thread.sleep(READ_MSG_INTERVAL) Thread.sleep(READ_MSG_INTERVAL)

@ -35,6 +35,7 @@ object ParseHelper {
fun parse(it: ByteArray, index: Byte) { fun parse(it: ByteArray, index: Byte) {
try { try {
Thread.sleep(0)
if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) { if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) {
val gasIndex = index.toInt() val gasIndex = index.toInt()
val status = it[14].toInt() val status = it[14].toInt()
@ -123,17 +124,22 @@ object ParseHelper {
} }
private val overlayBuilder: StringBuilder = java.lang.StringBuilder() private val overlayBuilder: StringBuilder = java.lang.StringBuilder()
private var lastTipTime = -1L
private fun setOverlayData() { private fun setOverlayData() {
overlayBuilder.clear() val currentTimeMillis = System.currentTimeMillis()
gasMap.forEach { item -> if (currentTimeMillis - lastTipTime > 1000) {
val gas = item.value lastTipTime = currentTimeMillis
val time = gas.time overlayBuilder.clear()
// 3S内的数据我们认为有效 gasMap.forEach { item ->
if (System.currentTimeMillis() - time <= 3000) { val gas = item.value
overlayBuilder.append("${gas.gasName}: ${gas.gasValue} ${gas.unit}").append("@") val time = gas.time
// 3S内的数据我们认为有效
if (currentTimeMillis - time <= 3000) {
overlayBuilder.append("${gas.gasName}: ${gas.gasValue} ${gas.unit}").append("@")
}
} }
TxtOverlay.setShowTip(overlayBuilder.toString())
} }
TxtOverlay.setShowTip(overlayBuilder.toString())
} }
/** /**
@ -253,13 +259,27 @@ object ParseHelper {
*/ */
@OptIn(DelicateCoroutinesApi::class) @OptIn(DelicateCoroutinesApi::class)
private fun updateGasTypeDb(type: GasTypeEnum, sensorIndex: Int, status: Int) { private fun updateGasTypeDb(type: GasTypeEnum, sensorIndex: Int, status: Int) {
GlobalScope.launch(Dispatchers.IO) { if (statusChange(type.desc, status)) {
val typeDao = DBUtils.gasTypeDao() GlobalScope.launch(Dispatchers.Unconfined) {
val gasType = typeDao.getByName(type.desc) val typeDao = DBUtils.gasTypeDao()
GasCache.setStatus(type.desc, getShowStatus(CommonApplication.getContext(), status)) val gasType = typeDao.getByName(type.desc)
gasType?.status = status GasCache.setStatus(type.desc, getShowStatus(CommonApplication.getContext(), status))
gasType?.sensorIndex = sensorIndex gasType?.status = status
typeDao.insert(gasType ?: GasType(type.desc, status, sensorIndex)) gasType?.sensorIndex = sensorIndex
typeDao.insert(gasType ?: GasType(type.desc, status, sensorIndex))
}
}
}
private val statusCache = hashMapOf<String, Int>()
private fun statusChange(desc: String, status1: Int): Boolean {
if (statusCache.containsKey(desc)) {
val oldStatus = statusCache[desc]
statusCache[desc] = status1
return oldStatus != status1
} else {
statusCache[desc] = status1
return true
} }
} }
@ -285,7 +305,9 @@ object ParseHelper {
private val sensorIndex: Int private val sensorIndex: Int
) : Runnable { ) : Runnable {
override fun run() { override fun run() {
updateGasTypeDb(gasName, sensorIndex, GasPortStatus.OUTLINE) GlobalScope.launch(Dispatchers.IO) {
updateGasTypeDb(gasName, sensorIndex, GasPortStatus.OUTLINE)
}
} }
} }
} }

@ -56,9 +56,12 @@ enum class GasTypeEnum(
TYPE_UNKNOW(-1, "UNKNOW") TYPE_UNKNOW(-1, "UNKNOW")
} }
val gasTypeMap by lazy {
GasTypeEnum.values()
}
fun getGasTypeEnumByCode(code: Int): GasTypeEnum { fun getGasTypeEnumByCode(code: Int): GasTypeEnum {
val codes = GasTypeEnum.values(); gasTypeMap.forEach {
codes.forEach {
if (code == it.code) { if (code == it.code) {
return it; return it;
} }
@ -67,8 +70,7 @@ fun getGasTypeEnumByCode(code: Int): GasTypeEnum {
} }
fun getGasTypeEnumByDesc(desc: String): GasTypeEnum { fun getGasTypeEnumByDesc(desc: String): GasTypeEnum {
val codes = GasTypeEnum.values(); gasTypeMap.forEach {
codes.forEach {
if (desc == it.desc) { if (desc == it.desc) {
return it; return it;
} }

@ -160,11 +160,12 @@ class HomeActivity : NoOptionsActivity() {
val errorRes = val errorRes =
if (!AppData.hasCameraData() && !AppData.hasSensorData()) R.drawable.ic_nosingal else R.drawable.ic_icon if (!AppData.hasCameraData() && !AppData.hasSensorData()) R.drawable.ic_nosingal else R.drawable.ic_icon
mBinding.errorView.setImageResource(errorRes) mBinding.errorView.setImageResource(errorRes)
}
}
// tipView private fun showTipView() {
mBinding.tipView.visibility = TxtOverlay.getOverlayBitmap()?.let {
if (AppData.hasSensorData()) View.VISIBLE else View.GONE mBinding.tipView.setImageBitmap(it)
mBinding.tipView.setImageBitmap(TxtOverlay.getOverlayBitmap())
} }
} }
@ -176,11 +177,6 @@ class HomeActivity : NoOptionsActivity() {
AppData.setCameraData(false) AppData.setCameraData(false)
changeViewStatus() changeViewStatus()
if (NetworkHelper.isNetworkConnect(this@HomeActivity)) { if (NetworkHelper.isNetworkConnect(this@HomeActivity)) {
// if (!isLoadingShowing() && !AppData.hasSensorData()) {
// showLoadingDialog(R.string.connecting_camera)
// }
// mClient?.play(CAMERA_URL)
// watchCamera(DELAY_TIME_OPEN_CAMERA)
setForCamera() setForCamera()
} else { } else {
watchCamera(DELAY_TIME_CHECK_CAMERA) watchCamera(DELAY_TIME_CHECK_CAMERA)
@ -199,10 +195,13 @@ class HomeActivity : NoOptionsActivity() {
// 设置传感器 // 设置传感器
private fun setForSensor() { private fun setForSensor() {
TxtOverlay.setTipChangeListener { TxtOverlay.setTipChangeListener {
// 有传感器数据回调 if (!AppData.hasSensorData()) {
AppData.setSensorData(true) // 有传感器数据回调
closeLoadingDialog() AppData.setSensorData(true)
changeViewStatus() closeLoadingDialog()
changeViewStatus()
}
showTipView()
setWarnView() setWarnView()
watchSensor(DELAY_TIME_CHECK_SENSOR) watchSensor(DELAY_TIME_CHECK_SENSOR)
} }

@ -39,14 +39,12 @@ class SensorActivity : NoOptionsActivity() {
private fun queryData() { private fun queryData() {
lifecycleScope.launch { lifecycleScope.launch {
while (true) { val gasTypeDao = DBUtils.gasTypeDao()
val gasTypeDao = DBUtils.gasTypeDao() val gasTypes = gasTypeDao.getAll()
val gasTypes = gasTypeDao.getAll() launch(Dispatchers.Main) {
launch(Dispatchers.Main) { mAdapter.setData(gasTypes)
mAdapter.setData(gasTypes)
}
delay(5000L)
} }
delay(5000L)
} }
} }
} }

@ -235,6 +235,11 @@ public class MediaCodecManager {
long currentTimeUs = (System.nanoTime() - mTime) / 1000;//通过控制时间轴,达到暂停录制,继续录制的效果 long currentTimeUs = (System.nanoTime() - mTime) / 1000;//通过控制时间轴,达到暂停录制,继续录制的效果
codec.queueInputBuffer(index, 0, data.length, currentTimeUs, 0); codec.queueInputBuffer(index, 0, data.length, currentTimeUs, 0);
try {
Thread.sleep(0);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
@Override @Override
@ -278,6 +283,11 @@ public class MediaCodecManager {
} }
} }
codec.releaseOutputBuffer(index, false); codec.releaseOutputBuffer(index, false);
try {
Thread.sleep(0);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
@Override @Override

@ -23,7 +23,6 @@ object TxtOverlay {
fun setShowTip(string: String) { fun setShowTip(string: String) {
mToDoShowTip = string mToDoShowTip = string
buildOverlayBitmap() buildOverlayBitmap()
mTipChangeListener?.invoke()
} }
// 上一次展示的时间 // 上一次展示的时间
@ -40,7 +39,7 @@ object TxtOverlay {
// 时间格式化字符串 // 时间格式化字符串
private val dateFormat = SimpleDateFormat("yy-MM-dd HH:mm:ss") private val dateFormat = SimpleDateFormat("yy-MM-dd HH:mm:ss")
fun buildOverlayBitmap() { private fun buildOverlayBitmap() {
if (TextUtils.isEmpty(mToDoShowTip)) { if (TextUtils.isEmpty(mToDoShowTip)) {
return return
} }
@ -58,15 +57,22 @@ object TxtOverlay {
Color.WHITE Color.WHITE
) )
yuv = YUVUtils.getYUVByBitmap(bmp) yuv = YUVUtils.getYUVByBitmap(bmp)
mTipChangeListener?.invoke()
} }
} }
fun getOverlayBitmap(): Bitmap? { fun getOverlayBitmap(): Bitmap? {
return bmp; if (!TextUtils.isEmpty(mToDoShowTip)) {
return bmp
}
return null
} }
fun getOverlayYuv(): ByteArray? { fun getOverlayYuv(): ByteArray? {
return yuv; if (!TextUtils.isEmpty(mToDoShowTip)) {
return yuv
}
return null
} }
fun setTipChangeListener(onChange: () -> Unit) { fun setTipChangeListener(onChange: () -> Unit) {

@ -1325,7 +1325,7 @@ public class EasyPlayerClient implements Client.SourceCallBack {
if (rr != null) rr.send(RESULT_VIDEO_SIZE, bundle); if (rr != null) rr.send(RESULT_VIDEO_SIZE, bundle);
} }
} }
// Log.d(TAG, String.format("queue size :%d", mQueue.size())); Log.d(TAG, String.format("queue size :%d", mQueue.size()));
try { try {
mQueue.put(frameInfo); mQueue.put(frameInfo);
} catch (InterruptedException e) { } catch (InterruptedException e) {

@ -39,7 +39,6 @@ class EasySerialPort<T>(
*/ */
open fun write(data: ByteArray) { open fun write(data: ByteArray) {
try { try {
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()
@ -51,7 +50,7 @@ class EasySerialPort<T>(
val size = ips?.read(mReadBytes) val size = ips?.read(mReadBytes)
if (size != null) { if (size != null) {
if (size > 0) { if (size > 0) {
Log.i("EasySerialPort", "index $sensorIndex ;read msg ${HexUtils.byteArrToHex(mReadBytes)}") // Log.i("EasySerialPort", "index $sensorIndex ;read msg ${HexUtils.byteArrToHex(mReadBytes)}")
mReceiver.invoke(mReadBytes, sensorIndex) mReceiver.invoke(mReadBytes, sensorIndex)
} }
} }

Loading…
Cancel
Save