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

@ -35,6 +35,7 @@ object ParseHelper {
fun parse(it: ByteArray, index: Byte) {
try {
Thread.sleep(0)
if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) {
val gasIndex = index.toInt()
val status = it[14].toInt()
@ -123,17 +124,22 @@ object ParseHelper {
}
private val overlayBuilder: StringBuilder = java.lang.StringBuilder()
private var lastTipTime = -1L
private fun setOverlayData() {
overlayBuilder.clear()
gasMap.forEach { item ->
val gas = item.value
val time = gas.time
// 3S内的数据我们认为有效
if (System.currentTimeMillis() - time <= 3000) {
overlayBuilder.append("${gas.gasName}: ${gas.gasValue} ${gas.unit}").append("@")
val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis - lastTipTime > 1000) {
lastTipTime = currentTimeMillis
overlayBuilder.clear()
gasMap.forEach { item ->
val gas = item.value
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)
private fun updateGasTypeDb(type: GasTypeEnum, sensorIndex: Int, status: Int) {
GlobalScope.launch(Dispatchers.IO) {
val typeDao = DBUtils.gasTypeDao()
val gasType = typeDao.getByName(type.desc)
GasCache.setStatus(type.desc, getShowStatus(CommonApplication.getContext(), status))
gasType?.status = status
gasType?.sensorIndex = sensorIndex
typeDao.insert(gasType ?: GasType(type.desc, status, sensorIndex))
if (statusChange(type.desc, status)) {
GlobalScope.launch(Dispatchers.Unconfined) {
val typeDao = DBUtils.gasTypeDao()
val gasType = typeDao.getByName(type.desc)
GasCache.setStatus(type.desc, getShowStatus(CommonApplication.getContext(), status))
gasType?.status = status
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
) : Runnable {
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")
}
val gasTypeMap by lazy {
GasTypeEnum.values()
}
fun getGasTypeEnumByCode(code: Int): GasTypeEnum {
val codes = GasTypeEnum.values();
codes.forEach {
gasTypeMap.forEach {
if (code == it.code) {
return it;
}
@ -67,8 +70,7 @@ fun getGasTypeEnumByCode(code: Int): GasTypeEnum {
}
fun getGasTypeEnumByDesc(desc: String): GasTypeEnum {
val codes = GasTypeEnum.values();
codes.forEach {
gasTypeMap.forEach {
if (desc == it.desc) {
return it;
}

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

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

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

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

@ -1325,7 +1325,7 @@ public class EasyPlayerClient implements Client.SourceCallBack {
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 {
mQueue.put(frameInfo);
} catch (InterruptedException e) {

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

Loading…
Cancel
Save