desc:时间步长

main
xiaowusky 2 years ago
parent 36c2ae491d
commit 582cbdc668

@ -45,17 +45,21 @@ object TestUtils {
val gasDao = DBUtils.gasDao() val gasDao = DBUtils.gasDao()
val gases = mutableListOf<Gas>() val gases = mutableListOf<Gas>()
gases.add(Gas(timeMillis - 90 * 1000, "CO", 1.0)) gases.add(Gas(timeMillis - 90 * 1000, "CO", 5.0))
gases.add(Gas(timeMillis - 90 * 1000, "O2", 1.0)) gases.add(Gas(timeMillis - 90 * 1000, "O2", 5.0))
gases.add(Gas(timeMillis - 90 * 1000, "CO2", 1.0)) gases.add(Gas(timeMillis - 90 * 1000, "CO2", 5.0))
gases.add(Gas(timeMillis - 60 * 1000, "CO2", 1.0)) gases.add(Gas(timeMillis - 60 * 1000, "CO2", 4.0))
gases.add(Gas(timeMillis - 60 * 1000, "CO", 1.0)) gases.add(Gas(timeMillis - 60 * 1000, "CO", 4.0))
gases.add(Gas(timeMillis - 60 * 1000, "O2", 1.0)) gases.add(Gas(timeMillis - 60 * 1000, "O2", 4.0))
gases.add(Gas(timeMillis - 30 * 1000, "O2", 1.0)) gases.add(Gas(timeMillis - 30 * 1000, "O2", 3.0))
gases.add(Gas(timeMillis - 30 * 1000, "CO", 1.0)) gases.add(Gas(timeMillis - 30 * 1000, "CO", 3.0))
gases.add(Gas(timeMillis - 30 * 1000, "CO2", 1.0)) gases.add(Gas(timeMillis - 30 * 1000, "CO2", 3.0))
gases.add(Gas(timeMillis - 15_000, "CO", 2.0))
gases.add(Gas(timeMillis - 15_000, "CO2", 2.0))
gases.add(Gas(timeMillis - 15_000, "O2", 2.0))
gases.add(Gas(timeMillis, "CO", 1.0)) gases.add(Gas(timeMillis, "CO", 1.0))
gases.add(Gas(timeMillis, "CO2", 1.0)) gases.add(Gas(timeMillis, "CO2", 1.0))

@ -5,18 +5,17 @@ import android.view.View
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.common.commonlib.db.DBUtils
import com.common.commonlib.db.entity.Gas
import com.yinuo.safetywatcher.R import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.TestUtils import com.yinuo.safetywatcher.TestUtils
import com.yinuo.safetywatcher.databinding.ActivityQueryDataBinding import com.yinuo.safetywatcher.databinding.ActivityQueryDataBinding
import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter
import com.yinuo.safetywatcher.watcher.base.BaseActivity import com.yinuo.safetywatcher.watcher.base.BaseActivity
import com.yinuo.safetywatcher.watcher.constant.DEFAULT_QUERY_TIME_INTERVAL import com.yinuo.safetywatcher.watcher.constant.DEFAULT_QUERY_TIME_INTERVAL
import com.yinuo.safetywatcher.watcher.constant.TimeStep import com.yinuo.safetywatcher.watcher.constant.TimeStep
import com.common.commonlib.db.DBUtils import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter
import com.common.commonlib.db.entity.Gas
import com.yinuo.safetywatcher.watcher.utils.DateUtils
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog import com.yinuo.safetywatcher.watcher.utils.DateUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -111,29 +110,17 @@ class QueryDataActivity : BaseActivity() {
private fun queryData() { private fun queryData() {
lifecycleScope.launch { lifecycleScope.launch {
// 根据步长,多个数据合一。合一步 // 根据步长,多个数据合一。间隔时
val step = when (timeStep) { val intervalMs = when (timeStep) {
TimeStep.SECOND_30 -> 1 TimeStep.SECOND_30 -> 30_000
TimeStep.MINUTE_1 -> 2 TimeStep.MINUTE_1 -> 60_000
TimeStep.MINUTE_3 -> 6 TimeStep.MINUTE_3 -> 180_000
TimeStep.MINUTE_5 -> 10 TimeStep.MINUTE_5 -> 300_000
else -> 1 else -> 1
} }
// 全量数据 // 全量数据
val gasDao = DBUtils.gasDao() val gasDao = DBUtils.gasDao()
val gasList = gasDao.getAllByTime(startTime, endTime) val gasList = gasDao.getAllByTime(startTime, endTime)
if (step == 1) {
val sortList = mutableListOf<Gas>()
sortList.apply {
addAll(gasList)
sortWith { o1, o2 ->
(o2.time - o1.time).toInt()
}
}
launch(Dispatchers.Main) {
mAdapter.setData(sortList)
}
} else {
// 多气体分开创建list存储之后。 再分步 // 多气体分开创建list存储之后。 再分步
val gasTypeDao = DBUtils.gasTypeDao() val gasTypeDao = DBUtils.gasTypeDao()
val gasTypes = gasTypeDao.getAll() val gasTypes = gasTypeDao.getAll()
@ -153,25 +140,32 @@ class QueryDataActivity : BaseActivity() {
//根据步长分割数据填充新的列表 //根据步长分割数据填充新的列表
gasMap.forEach { (type, list) -> gasMap.forEach { (type, list) ->
val newMapList = gasNewMap[type] val newMapList = gasNewMap[type]
var gasValue = 0.0
var startGas: Gas? = null var startGas: Gas? = null
var gasValue = 0.0
var count = 0
list.forEachIndexed { index, gas -> list.forEachIndexed { index, gas ->
if (index % step == 0) { if (startGas == null) {
startGas = gas
gasValue = 0.0
count = 0
}
if (gas.time - startGas!!.time > intervalMs) {
if (gasValue != 0.0) { if (gasValue != 0.0) {
startGas!!.gasValue = gasValue / step startGas!!.gasValue = gasValue / count
newMapList?.add(startGas!!) newMapList?.add(startGas!!)
} }
startGas = gas startGas = gas
gasValue = gas.gasValue gasValue = gas.gasValue
count = 1
} else { } else {
gasValue += gas.gasValue gasValue += gas.gasValue
count++
} }
// 最后一个特殊处理 // 最后一个特殊处理
if (list.size - 1 == index) { if (list.size - 1 == index) {
// 不能整除的时候
val dev = if (list.size % step == 0) step else list.size % step
if (gasValue != 0.0) { if (gasValue != 0.0) {
startGas!!.gasValue = gasValue / dev startGas!!.gasValue = gasValue / count
newMapList?.add(startGas!!) newMapList?.add(startGas!!)
} }
} }
@ -190,7 +184,6 @@ class QueryDataActivity : BaseActivity() {
} }
} }
} }
}
private fun doExportData() { private fun doExportData() {
showLoadingDialog(R.string.export_data_tip) showLoadingDialog(R.string.export_data_tip)

Loading…
Cancel
Save