diff --git a/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt index 1b88011..4141991 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/TestUtils.kt @@ -45,17 +45,21 @@ object TestUtils { val gasDao = DBUtils.gasDao() val gases = mutableListOf() - gases.add(Gas(timeMillis - 90 * 1000, "CO", 1.0)) - gases.add(Gas(timeMillis - 90 * 1000, "O2", 1.0)) - gases.add(Gas(timeMillis - 90 * 1000, "CO2", 1.0)) + gases.add(Gas(timeMillis - 90 * 1000, "CO", 5.0)) + gases.add(Gas(timeMillis - 90 * 1000, "O2", 5.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, "CO", 1.0)) - gases.add(Gas(timeMillis - 60 * 1000, "O2", 1.0)) + gases.add(Gas(timeMillis - 60 * 1000, "CO2", 4.0)) + gases.add(Gas(timeMillis - 60 * 1000, "CO", 4.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, "CO", 1.0)) - gases.add(Gas(timeMillis - 30 * 1000, "CO2", 1.0)) + gases.add(Gas(timeMillis - 30 * 1000, "O2", 3.0)) + gases.add(Gas(timeMillis - 30 * 1000, "CO", 3.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, "CO2", 1.0)) diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/QueryDataActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/QueryDataActivity.kt index fd1240d..e3ac3ef 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/QueryDataActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/QueryDataActivity.kt @@ -5,18 +5,17 @@ import android.view.View import androidx.activity.result.contract.ActivityResultContracts import androidx.lifecycle.lifecycleScope 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.TestUtils 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.constant.DEFAULT_QUERY_TIME_INTERVAL import com.yinuo.safetywatcher.watcher.constant.TimeStep -import com.common.commonlib.db.DBUtils -import com.common.commonlib.db.entity.Gas -import com.yinuo.safetywatcher.watcher.utils.DateUtils +import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter 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.launch @@ -111,83 +110,77 @@ class QueryDataActivity : BaseActivity() { private fun queryData() { lifecycleScope.launch { - // 根据步长,多个数据合一。合一步长 - val step = when (timeStep) { - TimeStep.SECOND_30 -> 1 - TimeStep.MINUTE_1 -> 2 - TimeStep.MINUTE_3 -> 6 - TimeStep.MINUTE_5 -> 10 + // 根据步长,多个数据合一。间隔时长 + val intervalMs = when (timeStep) { + TimeStep.SECOND_30 -> 30_000 + TimeStep.MINUTE_1 -> 60_000 + TimeStep.MINUTE_3 -> 180_000 + TimeStep.MINUTE_5 -> 300_000 else -> 1 } // 全量数据 val gasDao = DBUtils.gasDao() val gasList = gasDao.getAllByTime(startTime, endTime) - if (step == 1) { - val sortList = mutableListOf() - sortList.apply { - addAll(gasList) - sortWith { o1, o2 -> - (o2.time - o1.time).toInt() + // 多气体,分开创建list,存储之后。 再分步 + val gasTypeDao = DBUtils.gasTypeDao() + val gasTypes = gasTypeDao.getAll() + // 每种气体建立一个list + val gasMap = HashMap>() + // 预先创建分步合一之后的list + val gasNewMap = HashMap>() + 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 } - } - launch(Dispatchers.Main) { - mAdapter.setData(sortList) - } - } else { - // 多气体,分开创建list,存储之后。 再分步 - val gasTypeDao = DBUtils.gasTypeDao() - val gasTypes = gasTypeDao.getAll() - // 每种气体建立一个list - val gasMap = HashMap>() - // 预先创建分步合一之后的list - val gasNewMap = HashMap>() - 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 + if (gas.time - startGas!!.time > intervalMs) { + if (gasValue != 0.0) { + startGas!!.gasValue = gasValue / count + newMapList?.add(startGas!!) } - // 最后一个特殊处理 - if (list.size - 1 == index) { - // 不能整除的时候 - val dev = if (list.size % step == 0) step else list.size % step - if (gasValue != 0.0) { - startGas!!.gasValue = gasValue / dev - newMapList?.add(startGas!!) - } + startGas = gas + gasValue = gas.gasValue + count = 1 + } else { + gasValue += gas.gasValue + count++ + } + + // 最后一个特殊处理 + if (list.size - 1 == index) { + if (gasValue != 0.0) { + startGas!!.gasValue = gasValue / count + newMapList?.add(startGas!!) } } } + } - val newList = mutableListOf() - gasNewMap.forEach { (t, u) -> - newList.addAll(u) - } - newList.sortWith { o1, o2 -> - (o2.time - o1.time).toInt() - } - launch(Dispatchers.Main) { - mAdapter.setData(newList) - } + val newList = mutableListOf() + gasNewMap.forEach { (t, u) -> + newList.addAll(u) + } + newList.sortWith { o1, o2 -> + (o2.time - o1.time).toInt() + } + launch(Dispatchers.Main) { + mAdapter.setData(newList) } } }