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,83 +110,77 @@ 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) { // 多气体分开创建list存储之后。 再分步
val sortList = mutableListOf<Gas>() val gasTypeDao = DBUtils.gasTypeDao()
sortList.apply { val gasTypes = gasTypeDao.getAll()
addAll(gasList) // 每种气体建立一个list
sortWith { o1, o2 -> val gasMap = HashMap<String, MutableList<Gas>>()
(o2.time - o1.time).toInt() // 预先创建分步合一之后的list
val gasNewMap = HashMap<String, MutableList<Gas>>()
gasTypes.forEach {
gasMap[it.type] = mutableListOf()
gasNewMap[it.type] = mutableListOf()
}
// 分开各个气体的数据
gasList.forEach {
val list = gasMap[it.gasName]
list?.add(it)
}
//根据步长分割数据填充新的列表
gasMap.forEach { (type, list) ->
val newMapList = gasNewMap[type]
var startGas: Gas? = null
var gasValue = 0.0
var count = 0
list.forEachIndexed { index, gas ->
if (startGas == null) {
startGas = gas
gasValue = 0.0
count = 0
} }
} if (gas.time - startGas!!.time > intervalMs) {
launch(Dispatchers.Main) { if (gasValue != 0.0) {
mAdapter.setData(sortList) startGas!!.gasValue = gasValue / count
} newMapList?.add(startGas!!)
} else {
// 多气体分开创建list存储之后。 再分步
val gasTypeDao = DBUtils.gasTypeDao()
val gasTypes = gasTypeDao.getAll()
// 每种气体建立一个list
val gasMap = HashMap<String, MutableList<Gas>>()
// 预先创建分步合一之后的list
val gasNewMap = HashMap<String, MutableList<Gas>>()
gasTypes.forEach {
gasMap[it.type] = mutableListOf()
gasNewMap[it.type] = mutableListOf()
}
// 分开各个气体的数据
gasList.forEach {
val list = gasMap[it.gasName]
list?.add(it)
}
//根据步长分割数据填充新的列表
gasMap.forEach { (type, list) ->
val newMapList = gasNewMap[type]
var gasValue = 0.0
var startGas: Gas? = null
list.forEachIndexed { index, gas ->
if (index % step == 0) {
if (gasValue != 0.0) {
startGas!!.gasValue = gasValue / step
newMapList?.add(startGas!!)
}
startGas = gas
gasValue = gas.gasValue
} else {
gasValue += gas.gasValue
} }
// 最后一个特殊处理 startGas = gas
if (list.size - 1 == index) { gasValue = gas.gasValue
// 不能整除的时候 count = 1
val dev = if (list.size % step == 0) step else list.size % step } else {
if (gasValue != 0.0) { gasValue += gas.gasValue
startGas!!.gasValue = gasValue / dev count++
newMapList?.add(startGas!!) }
}
// 最后一个特殊处理
if (list.size - 1 == index) {
if (gasValue != 0.0) {
startGas!!.gasValue = gasValue / count
newMapList?.add(startGas!!)
} }
} }
} }
}
val newList = mutableListOf<Gas>() val newList = mutableListOf<Gas>()
gasNewMap.forEach { (t, u) -> gasNewMap.forEach { (t, u) ->
newList.addAll(u) newList.addAll(u)
} }
newList.sortWith { o1, o2 -> newList.sortWith { o1, o2 ->
(o2.time - o1.time).toInt() (o2.time - o1.time).toInt()
} }
launch(Dispatchers.Main) { launch(Dispatchers.Main) {
mAdapter.setData(newList) mAdapter.setData(newList)
}
} }
} }
} }

Loading…
Cancel
Save