desc:调整代码-标准化

main
xiaowusky 2 years ago
parent 7c3d2f2e4a
commit ae262075c6

@ -1,70 +0,0 @@
package com.yinuo.safetywatcher.watcher.constant
import com.common.commonlib.utils.MMKVUtils
const val CO_MIN = 0f
const val CO_MAX = 0f
const val O2_MIN = 0f
const val O2_MAX = 0f
const val CH4_MIN = 0f
const val CH4_MAX = 0f
const val H2S_MIN = 0f
const val H2S_MAX = 0f
val default_map = hashMapOf<String, Float>(
Pair("CO_min", CO_MIN),
Pair("CO_max", CO_MAX),
Pair("O2_min", O2_MIN),
Pair("O2_max", O2_MAX),
Pair("CH4_min", CH4_MIN),
Pair("CH4_max", CH4_MAX),
Pair("H2S_min", H2S_MIN),
Pair("H2S_max", H2S_MAX)
)
fun getLowThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_min"
}
fun getHighThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_max"
}
fun saveLowThreshold(gasType: String, value: Float){
MMKVUtils.put(getLowThresHoldKey(gasType), value)
}
fun saveHighThreshold(gasType: String, value: Float){
MMKVUtils.put(getHighThresHoldKey(gasType), value)
}
fun getGasLowThreshold(gasType: String): Float {
val lowThresholdKey = getLowThresHoldKey(gasType)
val float = MMKVUtils.getFloat(lowThresholdKey)
if (float <= 0) {
return default_map[lowThresholdKey] ?: -1f
}
return float
}
fun getGasHighThreshold(gasType: String): Float {
val highThresholdKey = getHighThresHoldKey(gasType)
val float = MMKVUtils.getFloat(highThresholdKey)
if (float <= 0) {
return default_map[highThresholdKey] ?: -1f
}
return float
}
fun getGasUnit(gasType: String): String {
return when (gasType.uppercase()) {
"CO" -> "ppm"
"O2" -> "%VOL"
"CH4" -> "%LEL"
"H2S" -> "ppm"
else -> ""
}
}

@ -8,7 +8,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
object GasUtils {
object GasPortUtils {
private const val BAUD_RATE = 9600
private const val PORT_PATH = "/dev/ttyS1"
const val FULL_MSG_SIZE = 25

@ -0,0 +1,102 @@
package com.yinuo.safetywatcher.watcher.port
import com.common.commonlib.utils.MMKVUtils
import com.yinuo.safetywatcher.watcher.port.cmd.CH4
import com.yinuo.safetywatcher.watcher.port.cmd.CO
import com.yinuo.safetywatcher.watcher.port.cmd.H2S
import com.yinuo.safetywatcher.watcher.port.cmd.O2
// 后缀
const val MIN_SUFFIX = "min"
const val MAX_SUFFIX = "max"
// 各个气体报警高低默认值,默认单位
const val CO_MIN = 0f
const val CO_MAX = 0f
const val O2_MIN = 0f
const val O2_MAX = 0f
const val CH4_MIN = 0f
const val CH4_MAX = 0f
const val H2S_MIN = 0f
const val H2S_MAX = 0f
// 气体默认阈值表
val default_threshold_map = hashMapOf<String, Float>(
Pair("${CO}_$MIN_SUFFIX", CO_MIN),
Pair("${CO}_$MAX_SUFFIX", CO_MAX),
Pair("${O2}_$MIN_SUFFIX", O2_MIN),
Pair("${O2}_$MAX_SUFFIX", O2_MAX),
Pair("${CH4}_$MIN_SUFFIX", CH4_MIN),
Pair("${CH4}_$MAX_SUFFIX", CH4_MAX),
Pair("${H2S}_$MIN_SUFFIX", H2S_MIN),
Pair("${H2S}_$MAX_SUFFIX", H2S_MAX)
)
private fun getLowThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_$MIN_SUFFIX"
}
private fun getHighThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_$MAX_SUFFIX"
}
/**
* 存储告警阈值低值
*/
fun saveLowThreshold(gasType: String, value: Float) {
MMKVUtils.put(getLowThresHoldKey(gasType), value)
}
/**
* 存储告警阈值高值
*/
fun saveHighThreshold(gasType: String, value: Float) {
MMKVUtils.put(getHighThresHoldKey(gasType), value)
}
/**
* 获取高度阈值低值
*/
fun getGasLowThreshold(gasType: String): Float {
val lowThresholdKey = getLowThresHoldKey(gasType)
val float = MMKVUtils.getFloat(lowThresholdKey)
if (float <= 0) {
return default_threshold_map[lowThresholdKey] ?: -1f
}
return float
}
/**
* 获取告警阈值高值
*/
fun getGasHighThreshold(gasType: String): Float {
val highThresholdKey = getHighThresHoldKey(gasType)
val float = MMKVUtils.getFloat(highThresholdKey)
if (float <= 0) {
return default_threshold_map[highThresholdKey] ?: -1f
}
return float
}
/**
* 获取本地气体单位
*/
fun getLocalGasUnit(gasType: String): String {
return when (gasType.uppercase()) {
O2 -> "%VOL"
CO, CH4, H2S -> getDefaultUnit(gasType)
else -> ""
}
}
private fun getDefaultUnit(gasType: String): String {
val localUnit = MMKVUtils.getString("unit_$gasType")
if (!localUnit.isNullOrEmpty()) {
return localUnit
}
return "ppm"
}

@ -11,8 +11,6 @@ import com.common.serialport.utils.HexUtils
import com.yinuo.library.vlc.TxtOverlay
import com.yinuo.library.vlc.utils.LogUtils
import com.yinuo.safetywatcher.watcher.constant.GAS_CLOUD_UPLOAD_SIZE_ONCE
import com.yinuo.safetywatcher.watcher.constant.getGasHighThreshold
import com.yinuo.safetywatcher.watcher.constant.getGasLowThreshold
import com.yinuo.safetywatcher.watcher.net.DevicesApi
import com.yinuo.safetywatcher.watcher.port.cmd.CH4
import com.yinuo.safetywatcher.watcher.port.cmd.CO
@ -40,7 +38,7 @@ object ParseHelper {
fun parse(it: ByteArray) {
try {
if (it.isNotEmpty() && it.size >= GasUtils.FULL_MSG_SIZE) {
if (it.isNotEmpty() && it.size >= GasPortUtils.FULL_MSG_SIZE) {
LogUtils.v("receive msg, ${HexUtils.byteArrToHex(it)}")
val gasIndex = it[0].toInt()
val status = it[14].toInt()
@ -86,7 +84,7 @@ object ParseHelper {
if (unitBinaryString.length < 16) {
val offset = 16 - unitBinaryString.length
repeat(offset) {
unitBinaryString = "0" + unitBinaryString;
unitBinaryString = "0$unitBinaryString"
}
}
unitBinaryString = unitBinaryString.reversed()
@ -237,7 +235,7 @@ object ParseHelper {
flagRunnable = FlagRunnable(gasName)
mPortRunnable[index] = flagRunnable
// 如果一段时间内没有收到消息,认为连接断开
mHandler.postDelayed(flagRunnable, GasUtils.CHECK_TIME)
mHandler.postDelayed(flagRunnable, GasPortUtils.CHECK_TIME)
}
/**

@ -21,7 +21,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.constant.STORAGE_WARN_THRESHOLD
import com.yinuo.safetywatcher.watcher.port.GasUtils
import com.yinuo.safetywatcher.watcher.port.GasPortUtils
import com.yinuo.safetywatcher.watcher.services.HeartbeatService
import com.yinuo.safetywatcher.watcher.ui.view.ConfirmDialog
import com.yinuo.safetywatcher.watcher.utils.BatteryHelper
@ -172,7 +172,7 @@ class HomeActivity : NoOptionsActivity() {
watchSensor(DELAY_TIME_CHECK_SENSOR)
}
watchSensor(DELAY_TIME_CHECK_SENSOR)
GasUtils.initPort()
GasPortUtils.initPort()
}
/**
@ -216,7 +216,7 @@ class HomeActivity : NoOptionsActivity() {
LogUtils.w("watchSensorRunnable start")
AppData.setSensorData(false)
changeViewStatus()
GasUtils.initPort()
GasPortUtils.initPort()
watchSensor(DELAY_TIME_CHECK_SENSOR)
}

@ -1,15 +1,14 @@
package com.yinuo.safetywatcher.watcher.ui
import android.view.View
import android.widget.Toast
import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.databinding.ActivitySensorThresholdBinding
import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity
import com.yinuo.safetywatcher.watcher.constant.getGasHighThreshold
import com.yinuo.safetywatcher.watcher.constant.getGasLowThreshold
import com.yinuo.safetywatcher.watcher.constant.getGasUnit
import com.yinuo.safetywatcher.watcher.constant.saveHighThreshold
import com.yinuo.safetywatcher.watcher.constant.saveLowThreshold
import com.yinuo.safetywatcher.watcher.port.getGasHighThreshold
import com.yinuo.safetywatcher.watcher.port.getGasLowThreshold
import com.yinuo.safetywatcher.watcher.port.getLocalGasUnit
import com.yinuo.safetywatcher.watcher.port.saveHighThreshold
import com.yinuo.safetywatcher.watcher.port.saveLowThreshold
import com.yinuo.safetywatcher.watcher.utils.showToast
class SensorThresholdSetActivity : NoOptionsActivity() {
@ -28,7 +27,7 @@ class SensorThresholdSetActivity : NoOptionsActivity() {
override fun initView() {
gasName = intent.getStringExtra("GasType")!!
val gasUnit = getGasUnit(gasName)
val gasUnit = getLocalGasUnit(gasName)
mBinding.tvMin.append("$gasUnit)")
mBinding.tvMax.append("$gasUnit)")

Loading…
Cancel
Save