diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasConstants.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasConstants.kt new file mode 100644 index 0000000..ac407f6 --- /dev/null +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasConstants.kt @@ -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( + Pair(CO, MOLECULAR_CO), + Pair(CH4, MOLECULAR_CH4), + Pair(H2S, MOLECULAR_H2S), +) + +/** + * ppm单位的气体量程 + */ +val gas_range_ppm = hashMapOf( + 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( + 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( + 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( + 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>( + Pair(UNIT_PPM, default_threshold_map_ppm), + Pair(UNIT_MGM3, default_threshold_map_mgm3), +) \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasUtilss.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasUtilss.kt index c4e3bcc..a63a2f9 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasUtilss.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasUtilss.kt @@ -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.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( - 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" } @@ -44,49 +15,80 @@ private fun getHighThresHoldKey(gasType: String): String { } /** - * 存储告警阈值低值 + * 存储告警阈值低值 保证存储的是ppm单位 + * 氧气是vol单位 */ -fun saveLowThreshold(gasType: String, value: Float) { - MMKVUtils.put(getLowThresHoldKey(gasType), value) +fun saveLowThreshold(gasType: String, value: Float, localGasUnit: String) { + 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) { - MMKVUtils.put(getHighThresHoldKey(gasType), value) +fun saveHighThreshold(gasType: String, value: Float, localGasUnit: String) { + 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 float = MMKVUtils.getFloat(lowThresholdKey) - if (float <= 0) { - return default_threshold_map[lowThresholdKey] ?: -1f + // ppm, 02 vol + var localVlaue = MMKVUtils.getFloat(lowThresholdKey) + 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 float = MMKVUtils.getFloat(highThresholdKey) - if (float <= 0) { - return default_threshold_map[highThresholdKey] ?: -1f + // ppm, O2 vol + var localVlaue = MMKVUtils.getFloat(highThresholdKey) + 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) } -/** - * ppm单位的气体量程 - */ -val gas_range_ppm = hashMapOf( - 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( - 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" - /** * 获取气体传感器量程 */ diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt index 53604f2..75f5ef2 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/ParseHelper.kt @@ -150,8 +150,8 @@ object ParseHelper { private fun insertGasData(gasType: String, value: Float, unit: String) { GlobalScope.launch { // 阈值范围 - val min = getGasLowThreshold(gasType) - val max = getGasHighThreshold(gasType) + val min = getGasLowThreshold(gasType, unit) + val max = getGasHighThreshold(gasType, unit) // 构造气体数据 val timeMillis = System.currentTimeMillis() diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt index 964a40f..f5c05c0 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt @@ -31,8 +31,8 @@ class SensorThresholdSetActivity : NoOptionsActivity() { mBinding.tvMin.append("$gasUnit)") mBinding.tvMax.append("$gasUnit)") - val gasLowThreshold = getGasLowThreshold(gasName) - val gasHighThreshold = getGasHighThreshold(gasName) + val gasLowThreshold = getGasLowThreshold(gasName, gasUnit) + val gasHighThreshold = getGasHighThreshold(gasName, gasUnit) if (gasLowThreshold > 0) { mBinding.etMin.setText(gasLowThreshold.toString()) @@ -52,12 +52,12 @@ class SensorThresholdSetActivity : NoOptionsActivity() { try { val min = mBinding.etMin.text.toString() if (min.isNotEmpty()) { - saveLowThreshold(gasName, min.toFloat()) + saveLowThreshold(gasName, min.toFloat(), getLocalGasUnit(gasName)) } val max = mBinding.etMax.text.toString() if (max.isNotEmpty()) { - saveHighThreshold(gasName, max.toFloat()) + saveHighThreshold(gasName, max.toFloat(), getLocalGasUnit(gasName)) } } catch (e: Exception) { showToast("只允许输入数值")