desc:气体展示的时候需要进行单位转换

main
xiaowusky 2 years ago
parent b54b850a5b
commit 7b53e16843

@ -85,6 +85,7 @@ fun getGasHighThreshold(gasType: String): Float {
const val UNIT_VOL = "%VOL"
const val UNIT_PPM = "ppm"
const val UNIT_MGM3 = "mg/m3"
const val UNIT_LEL = "%LEL"
/**
* 获取本地气体单位
@ -168,6 +169,76 @@ fun ppm2mgm3(ppmValue: Double, molecular: Int): Double {
return ppmValue * molecular / 22.4
}
fun mgm3ToPpm(mValue: Double, molecular: Int): Double {
return mValue * 22.4 / molecular
}
fun ch4Lel2ppm(lelValue: Double): Double {
return lelValue * 10000 / 20.0
}
/**
* 传感器返回数据气体浓度 单位转换
* 氧气O2单位固定不用转换
* 其他三种气体可能需要转换
*/
fun convertData(
gasType: String,
value: Double,
unit: String,
localGasUnit: String
): Double {
var retValue = value
if (CH4 == gasType) {
retValue = convertCH4(unit, value, localGasUnit)
}
// h2s 和 co均只有两种可能场景ppm->mgm3 mgm3->ppm
else if (H2S == gasType) {
if (UNIT_MGM3 == localGasUnit) {
retValue = ppm2mgm3(retValue, MOLECULAR_H2S)
} else if (UNIT_PPM == localGasUnit) {
retValue = mgm3ToPpm(retValue, MOLECULAR_H2S)
}
} else if (CO == gasType) {
if (UNIT_MGM3 == localGasUnit) {
retValue = ppm2mgm3(retValue, MOLECULAR_CO)
} else if (UNIT_PPM == localGasUnit) {
retValue = mgm3ToPpm(retValue, MOLECULAR_CO)
}
}
return retValue
}
/**
* ch4 有三种可能单位 lel ppm mg/m3, 其中lel不会作为localGasUnit
* 三种情况
* 1.lel转ppm或者mgm3
* 2.ppm转mgm3
* 3.mgm3转ppm
*/
private fun convertCH4(
unit: String,
value: Double,
localGasUnit: String
): Double {
var ret = value
if (UNIT_LEL == unit) {
// to ppm
ret = ch4Lel2ppm(value)
// 目标是mg/mg3
if (UNIT_MGM3 == localGasUnit) {
ret = ppm2mgm3(ret, MOLECULAR_CH4)
}
} else if (UNIT_PPM == unit) {
// 目标是mg/mg3
if (UNIT_MGM3 == localGasUnit) {
ret = ppm2mgm3(ret, MOLECULAR_CH4)
}
} else if (UNIT_MGM3 == unit) {
// 目标是mg/mg3
if (UNIT_PPM == localGasUnit) {
ret = mgm3ToPpm(ret, MOLECULAR_CH4)
}
}
return ret
}

@ -243,33 +243,4 @@ object ParseHelper {
updateGasTypeDb(gasName, GasPortStatus.OUTLINE)
}
}
/**
* 气体浓度 单位转换
* 氧气O2单位固定不用转换
* 其他三种气体可能需要转换
*/
private fun convertData(
gasType: String,
value: Double,
unit: String,
localGasUnit: String
): Double {
var retValue = value
if (CH4 == gasType) {
retValue = ch4Lel2ppm(value)
if (UNIT_MGM3 == localGasUnit) {
retValue = ppm2mgm3(retValue, MOLECULAR_CH4)
}
} else if (H2S == gasType) {
if (UNIT_MGM3 == localGasUnit) {
retValue = ppm2mgm3(retValue, MOLECULAR_H2S)
}
} else if (CO == gasType) {
if (UNIT_MGM3 == localGasUnit) {
retValue = ppm2mgm3(retValue, MOLECULAR_CO)
}
}
return retValue
}
}

@ -14,6 +14,8 @@ import com.yinuo.safetywatcher.databinding.ActivityQueryDataBinding
import com.yinuo.safetywatcher.watcher.base.BaseActivity
import com.yinuo.safetywatcher.watcher.constant.DEFAULT_QUERY_TIME_INTERVAL
import com.yinuo.safetywatcher.watcher.constant.TimeStep
import com.yinuo.safetywatcher.watcher.port.convertData
import com.yinuo.safetywatcher.watcher.port.getLocalGasUnit
import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.utils.ChartBridge
@ -136,7 +138,10 @@ class QueryDataActivity : BaseActivity() {
}
// 全量数据
val gasDao = DBUtils.gasDao()
val gasList = gasDao.getAllByTime(startTime, endTime)
var gasList = gasDao.getAllByTime(startTime, endTime)
gasList = gasList.map {
tryConvertData(it)
}
// 多气体分开创建list存储之后。 再分步
val gasMap = gasList.groupBy {
it.gasName
@ -151,7 +156,7 @@ class QueryDataActivity : BaseActivity() {
var count = 0
list.forEachIndexed { index, gas ->
if (startGas == null) {
startGas = gas
startGas = gas.copy()
tempTime = gas.time - ((gas.time - startTime) % intervalMs)
gasValue = 0.0
count = 0
@ -163,7 +168,7 @@ class QueryDataActivity : BaseActivity() {
newMapList.add(startGas!!)
tempTime += intervalMs
startGas = gas
startGas = gas.copy()
gasValue = gas.gasValue
count = 1
}
@ -206,6 +211,16 @@ class QueryDataActivity : BaseActivity() {
}
}
private fun tryConvertData(it: Gas): Gas {
val localGasUnit = getLocalGasUnit(it.gasName)
val unit = it.unit
if (unit != localGasUnit) {
it.gasValue = convertData(it.gasName, it.gasValue, unit, localGasUnit)
it.unit = localGasUnit
}
return it
}
private fun doExportData() {
val usbPath = StorageUtils.getStoragePath(this@QueryDataActivity)
if (usbPath.isNullOrEmpty()) {
@ -214,7 +229,12 @@ class QueryDataActivity : BaseActivity() {
}
showLoadingDialog(R.string.export_data_tip, false)
lifecycleScope.launch {
TestUtils.testExportExcel(this@QueryDataActivity, usbPath, mAdapter._data, loadingDialog)
TestUtils.testExportExcel(
this@QueryDataActivity,
usbPath,
mAdapter._data,
loadingDialog
)
launch(Dispatchers.Main) {
showToast(getString(R.string.export_success))
}

@ -2,9 +2,9 @@ package com.yinuo.safetywatcher.watcher.ui.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import com.common.commonlib.db.entity.Gas
import com.yinuo.safetywatcher.databinding.LayoutItemHistoryBinding
import com.yinuo.safetywatcher.watcher.base.BaseRvAdapter
import com.common.commonlib.db.entity.Gas
import com.yinuo.safetywatcher.watcher.utils.DateUtils
class HistoryDataAdapter :
@ -14,7 +14,7 @@ class HistoryDataAdapter :
BaseRvAdapter.BaseViewHolder<Gas, LayoutItemHistoryBinding>(binding) {
override fun bindView(data: Gas) {
binding.tvTime.text = formatTime(data.time)
binding.tvSensor.text = data.gasName
binding.tvSensor.text = "${data.gasName} : ${data.gasValue} ${data.unit}"
}
private fun formatTime(time: Long): CharSequence? {

Loading…
Cancel
Save