desc:气体报警阈值

main
xiaowusky 2 years ago
parent 96c768e485
commit f06c194210

@ -0,0 +1,107 @@
package com.yinuo.safetywatcher.watcher.port
import com.yinuo.safetywatcher.watcher.port.cmd.CH4
import com.yinuo.safetywatcher.watcher.port.cmd.CO
import com.yinuo.safetywatcher.watcher.port.cmd.H2S
// 后缀
const val MIN_SUFFIX = "min"
const val MAX_SUFFIX = "max"
// 各个气体报警高低默认值,默认单位
// ppm -1代表不考虑低值
const val CO_MIN = -1f
// 20mg/m3
const val CO_MAX_MGM3 = 20f
//vol
const val O2_MIN = 19.5f
const val O2_MAX = 23.5f
//lel
const val CH4_MIN = -1f
const val CH4_MAX_LEL = 25f
//ppm
const val H2S_MIN = -1f
// 10mg/m3
const val H2S_MAX_MGM3 = 10f
// 单位
const val UNIT_VOL = "%VOL"
const val UNIT_PPM = "ppm"
const val UNIT_MGM3 = "mg/m3"
const val UNIT_LEL = "%LEL"
/**
* 气体分子质量
*/
const val MOLECULAR_CO = 28
const val MOLECULAR_H2S = 34
const val MOLECULAR_CH4 = 16
val molecular_map = hashMapOf<String, Int>(
Pair(CO, MOLECULAR_CO),
Pair(CH4, MOLECULAR_CH4),
Pair(H2S, MOLECULAR_H2S),
)
/**
* ppm单位的气体量程
*/
val gas_range_ppm = hashMapOf<String, String>(
Pair(CO, "0-1000 $UNIT_PPM"),
Pair(CH4, "0-50000 $UNIT_PPM"),
Pair(H2S, "0-100 $UNIT_PPM")
)
/**
* mg/m3单位的气体量程
*
* ppm -> mg/m3 X=M.C/22.4
* X污染物以每标立方米的毫克数表示的浓度值
* C污染物以 ppm 表示的浓度值
* M污染物的分之子量
*
* CO分子量 = 28 28/22.4 * 1000
* CH4分子量 = 16 16/22.4 * 50000
* H2S分子量 = 34 34/22.4 * 100
*/
val gas_range_mgm3 = hashMapOf<String, String>(
Pair(CO, "0-1250 $UNIT_MGM3"),
Pair(CH4, "0-35714 $UNIT_MGM3"),
Pair(H2S, "0-151 $UNIT_MGM3")
)
/**
* 氧气的量程
*/
const val O2_RANGE = "0-30 $UNIT_VOL"
// 气体默认阈值表
val default_threshold_map_ppm = hashMapOf<String, Float>(
Pair("${CO}_$MIN_SUFFIX", CO_MIN),
Pair("${CO}_$MAX_SUFFIX", mgm3ToPpm(CO_MAX_MGM3, MOLECULAR_CO)),
Pair("${CH4}_$MIN_SUFFIX", CH4_MIN),
Pair("${CH4}_$MAX_SUFFIX", ch4Lel2ppm(CH4_MAX_LEL)),
Pair("${H2S}_$MIN_SUFFIX", H2S_MIN),
Pair("${H2S}_$MAX_SUFFIX", mgm3ToPpm(H2S_MAX_MGM3, MOLECULAR_H2S))
)
// 气体默认阈值表
val default_threshold_map_mgm3 = hashMapOf<String, Float>(
Pair("${CO}_$MIN_SUFFIX", CO_MIN),
Pair("${CO}_$MAX_SUFFIX", CO_MAX_MGM3),
Pair("${CH4}_$MIN_SUFFIX", CH4_MIN),
Pair("${CH4}_$MAX_SUFFIX", ppm2mgm3(ch4Lel2ppm(CH4_MAX_LEL), MOLECULAR_CH4)),
Pair("${H2S}_$MIN_SUFFIX", H2S_MIN),
Pair("${H2S}_$MAX_SUFFIX", H2S_MAX_MGM3)
)
val default_threshold_map_unit = hashMapOf<String, HashMap<String, Float>>(
Pair(UNIT_PPM, default_threshold_map_ppm),
Pair(UNIT_MGM3, default_threshold_map_mgm3),
)

@ -6,35 +6,6 @@ import com.yinuo.safetywatcher.watcher.port.cmd.CO
import com.yinuo.safetywatcher.watcher.port.cmd.H2S import com.yinuo.safetywatcher.watcher.port.cmd.H2S
import com.yinuo.safetywatcher.watcher.port.cmd.O2 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 { private fun getLowThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_$MIN_SUFFIX" return "${gasType.uppercase()}_$MIN_SUFFIX"
} }
@ -44,49 +15,80 @@ private fun getHighThresHoldKey(gasType: String): String {
} }
/** /**
* 存储告警阈值低值 * 存储告警阈值低值 保证存储的是ppm单位
* 氧气是vol单位
*/ */
fun saveLowThreshold(gasType: String, value: Float) { fun saveLowThreshold(gasType: String, value: Float, localGasUnit: String) {
MMKVUtils.put(getLowThresHoldKey(gasType), value) var localValue = value
if (O2 != gasType && UNIT_MGM3 == localGasUnit) {
val molecular = molecular_map[gasType]
molecular?.let {
localValue = mgm3ToPpm(value, it)
}
}
MMKVUtils.put(getLowThresHoldKey(gasType), localValue)
} }
/** /**
* 存储告警阈值高值 * 存储告警阈值高值 保证存储的是ppm单位
* 氧气是vol单位
*/ */
fun saveHighThreshold(gasType: String, value: Float) { fun saveHighThreshold(gasType: String, value: Float, localGasUnit: String) {
MMKVUtils.put(getHighThresHoldKey(gasType), value) var localValue = value
if (O2 != gasType && UNIT_MGM3 == localGasUnit) {
val molecular = molecular_map[gasType]
molecular?.let {
localValue = mgm3ToPpm(value, it)
}
}
MMKVUtils.put(getHighThresHoldKey(gasType), localValue)
} }
/** /**
* 获取高度阈值低值 * 获取高度阈值低值
*/ */
fun getGasLowThreshold(gasType: String): Float { fun getGasLowThreshold(gasType: String, unit: String): Float {
val lowThresholdKey = getLowThresHoldKey(gasType) val lowThresholdKey = getLowThresHoldKey(gasType)
val float = MMKVUtils.getFloat(lowThresholdKey) // ppm, 02 vol
if (float <= 0) { var localVlaue = MMKVUtils.getFloat(lowThresholdKey)
return default_threshold_map[lowThresholdKey] ?: -1f if (localVlaue <= 0) {
// 氧气特殊处理
if (O2 == gasType) {
return O2_MIN
}
return (default_threshold_map_unit[unit])?.get(lowThresholdKey) ?: -1f
} else {
if (O2 != gasType && UNIT_MGM3 == unit) {
val molecular = molecular_map[gasType]
molecular?.let { localVlaue = ppm2mgm3(localVlaue, it) }
}
} }
return float return localVlaue
} }
/** /**
* 获取告警阈值高值 * 获取告警阈值高值
*/ */
fun getGasHighThreshold(gasType: String): Float { fun getGasHighThreshold(gasType: String, unit: String): Float {
val highThresholdKey = getHighThresHoldKey(gasType) val highThresholdKey = getHighThresHoldKey(gasType)
val float = MMKVUtils.getFloat(highThresholdKey) // ppm, O2 vol
if (float <= 0) { var localVlaue = MMKVUtils.getFloat(highThresholdKey)
return default_threshold_map[highThresholdKey] ?: -1f if (localVlaue <= 0) {
// 氧气特殊处理
if (O2 == gasType) {
return O2_MAX
}
return (default_threshold_map_unit[unit])?.get(highThresholdKey) ?: -1f
} else {
if (O2 != gasType && UNIT_MGM3 == unit) {
val molecular = molecular_map[gasType]
molecular?.let { localVlaue = ppm2mgm3(localVlaue, it) }
}
} }
return float return localVlaue
} }
const val UNIT_VOL = "%VOL"
const val UNIT_PPM = "ppm"
const val UNIT_MGM3 = "mg/m3"
const val UNIT_LEL = "%LEL"
/** /**
* 获取本地气体单位 * 获取本地气体单位
*/ */
@ -110,45 +112,6 @@ fun saveGasUnit(gasType: String, unit: String) {
MMKVUtils.put("unit_$gasType", unit) MMKVUtils.put("unit_$gasType", unit)
} }
/**
* ppm单位的气体量程
*/
val gas_range_ppm = hashMapOf<String, String>(
Pair(CO, "0-1000 $UNIT_PPM"),
Pair(CH4, "0-50000 $UNIT_PPM"),
Pair(H2S, "0-100 $UNIT_PPM")
)
/**
* mg/m3单位的气体量程
*
* ppm -> mg/m3 X=M.C/22.4
* X污染物以每标立方米的毫克数表示的浓度值
* C污染物以 ppm 表示的浓度值
* M污染物的分之子量
*
* CO分子量 = 28 28/22.4 * 1000
* CH4分子量 = 16 16/22.4 * 50000
* H2S分子量 = 34 34/22.4 * 100
*/
val gas_range_mgm3 = hashMapOf<String, String>(
Pair(CO, "0-1250 $UNIT_MGM3"),
Pair(CH4, "0-35714 $UNIT_MGM3"),
Pair(H2S, "0-151 $UNIT_MGM3")
)
/**
* 气体分子质量
*/
const val MOLECULAR_CO = 28
const val MOLECULAR_H2S = 34
const val MOLECULAR_CH4 = 16
/**
* 氧气的量程
*/
const val O2_RANGE = "0-30 $UNIT_VOL"
/** /**
* 获取气体传感器量程 * 获取气体传感器量程
*/ */

@ -150,8 +150,8 @@ object ParseHelper {
private fun insertGasData(gasType: String, value: Float, unit: String) { private fun insertGasData(gasType: String, value: Float, unit: String) {
GlobalScope.launch { GlobalScope.launch {
// 阈值范围 // 阈值范围
val min = getGasLowThreshold(gasType) val min = getGasLowThreshold(gasType, unit)
val max = getGasHighThreshold(gasType) val max = getGasHighThreshold(gasType, unit)
// 构造气体数据 // 构造气体数据
val timeMillis = System.currentTimeMillis() val timeMillis = System.currentTimeMillis()

@ -31,8 +31,8 @@ class SensorThresholdSetActivity : NoOptionsActivity() {
mBinding.tvMin.append("$gasUnit)") mBinding.tvMin.append("$gasUnit)")
mBinding.tvMax.append("$gasUnit)") mBinding.tvMax.append("$gasUnit)")
val gasLowThreshold = getGasLowThreshold(gasName) val gasLowThreshold = getGasLowThreshold(gasName, gasUnit)
val gasHighThreshold = getGasHighThreshold(gasName) val gasHighThreshold = getGasHighThreshold(gasName, gasUnit)
if (gasLowThreshold > 0) { if (gasLowThreshold > 0) {
mBinding.etMin.setText(gasLowThreshold.toString()) mBinding.etMin.setText(gasLowThreshold.toString())
@ -52,12 +52,12 @@ class SensorThresholdSetActivity : NoOptionsActivity() {
try { try {
val min = mBinding.etMin.text.toString() val min = mBinding.etMin.text.toString()
if (min.isNotEmpty()) { if (min.isNotEmpty()) {
saveLowThreshold(gasName, min.toFloat()) saveLowThreshold(gasName, min.toFloat(), getLocalGasUnit(gasName))
} }
val max = mBinding.etMax.text.toString() val max = mBinding.etMax.text.toString()
if (max.isNotEmpty()) { if (max.isNotEmpty()) {
saveHighThreshold(gasName, max.toFloat()) saveHighThreshold(gasName, max.toFloat(), getLocalGasUnit(gasName))
} }
} catch (e: Exception) { } catch (e: Exception) {
showToast("只允许输入数值") showToast("只允许输入数值")

Loading…
Cancel
Save