desc:修改测试数据库

main
xiaowusky 2 years ago
parent 8ebef88aa2
commit 3b0a81a33a

@ -0,0 +1,49 @@
package com.yinuo.safetywatcher
import com.yinuo.safetywatcher.watcher.db.DBUtils
import com.yinuo.safetywatcher.watcher.db.entity.Gas
import com.yinuo.safetywatcher.watcher.db.entity.GasType
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
object TestUtils {
fun insertData() {
GlobalScope.launch() {
// 插入类型
val typeDao = DBUtils.gasTypeDao()
val all = typeDao.getAll()
if (all.isNotEmpty()){
return@launch
}
val list = mutableListOf<GasType>()
list.add(GasType("CO"))
list.add(GasType("CO2"))
list.add(GasType("O2"))
typeDao.insertAll(list)
// 构造气体数据
val timeMillis = System.currentTimeMillis()
val gasDao = DBUtils.gasDao()
val gases = mutableListOf<Gas>()
gases.add(Gas(1, timeMillis - 90 * 1000, "CO", 1.0))
gases.add(Gas(2, timeMillis - 90 * 1000, "O2", 1.0))
gases.add(Gas(3, timeMillis - 90 * 1000, "CO2", 1.0))
gases.add(Gas(4, timeMillis - 60 * 1000, "CO2", 1.0))
gases.add(Gas(5, timeMillis - 60 * 1000, "CO", 1.0))
gases.add(Gas(6, timeMillis - 60 * 1000, "O2", 1.0))
gases.add(Gas(7, timeMillis - 30 * 1000, "O2", 1.0))
gases.add(Gas(8, timeMillis - 30 * 1000, "CO", 1.0))
gases.add(Gas(9, timeMillis - 30 * 1000, "CO2", 1.0))
gases.add(Gas(10, timeMillis, "CO", 1.0))
gases.add(Gas(11, timeMillis, "CO2", 1.0))
gases.add(Gas(12, timeMillis, "O2", 1.0))
gasDao.insertAll(gases)
}
}
}

@ -2,6 +2,7 @@ package com.yinuo.safetywatcher.watcher
import com.common.commonlib.CommonApplication
import com.lztek.toolkit.Lztek
import com.yinuo.safetywatcher.TestUtils
import com.yinuo.safetywatcher.watcher.db.DBUtils
import com.yinuo.safetywatcher.watcher.db.dao.WarningDao
import com.yinuo.safetywatcher.watcher.db.entity.Warning
@ -14,8 +15,10 @@ import kotlinx.coroutines.launch
class App : CommonApplication() {
override fun onCreate() {
super.onCreate()
LztekUtil.setObject(Lztek.create(this))
//LztekUtil.setObject(Lztek.create(this))
tryFixDbData()
TestUtils.insertData()
}

@ -12,6 +12,7 @@ abstract class BaseRvAdapter<T, B : ViewBinding, VH : BaseRvAdapter.BaseViewHold
private val mDatas = mutableListOf<T>()
fun setData(values: List<T>) {
mDatas.clear()
mDatas.addAll(values)
notifyDataSetChanged()
}

@ -18,7 +18,7 @@ interface GasDao {
suspend fun getAllByTime(startTime: Long, endTime: Long): List<Gas>
@Insert
suspend fun insertAll(vararg gases: Gas)
suspend fun insertAll(gases: List<Gas>)
@Insert
suspend fun insert(gas: Gas)

@ -14,6 +14,9 @@ interface GasTypeDao {
@Insert
suspend fun insert(gas: GasType)
@Insert
suspend fun insertAll(types: List<GasType>)
@Delete
suspend fun delete(gas: GasType)
}

@ -9,9 +9,9 @@ data class Gas(
@PrimaryKey var id: Int,
@ColumnInfo(name = "time") var time: Long,
@ColumnInfo(name = "gas_name") var gasName: String,
@ColumnInfo(name = "gas_value") var gasValue: Double,
@ColumnInfo(name = "unit") var unit: String,
@ColumnInfo(name = "threshold_low") var thresholdLow: Double,
@ColumnInfo(name = "threshold_high") var thresholdHigh: Double,
@ColumnInfo(name = "gas_value") var gasValue: Double = 0.0,
@ColumnInfo(name = "unit") var unit: String = "",
@ColumnInfo(name = "threshold_low") var thresholdLow: Double = 0.0,
@ColumnInfo(name = "threshold_high") var thresholdHigh: Double = 0.0,
@ColumnInfo(name = "sync_flag") var syncFlag: Boolean = false
)

@ -121,8 +121,15 @@ class QueryDataActivity : BaseActivity() {
val gasDao = DBUtils.gasDao()
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(gasList)
mAdapter.setData(sortList)
}
} else {
// 多气体分开创建list存储之后。 再分步
@ -167,12 +174,12 @@ class QueryDataActivity : BaseActivity() {
}
}
var newList = mutableListOf<Gas>()
gasNewMap.forEach { t, u ->
val newList = mutableListOf<Gas>()
gasNewMap.forEach { (t, u) ->
newList.addAll(u)
}
newList.sortWith { o1, o2 ->
(o1.time - o2.time).toInt()
(o2.time - o1.time).toInt()
}
launch(Dispatchers.Main) {
mAdapter.setData(newList)

@ -43,7 +43,7 @@ class TimeSettingActivity : NoOptionsActivity() {
}
mBingding.tvConfirm.setOnClickListener {
if (changeTime > 0) {
LztekUtil.getLztek().setSystemTime(changeTime)
LztekUtil.getLztek()?.setSystemTime(changeTime)
}
onBackPressed()
}

@ -16,13 +16,13 @@ object LztekUtil {
mLztek = value
}
fun getLztek(): Lztek {
return mLztek!!
fun getLztek(): Lztek? {
return mLztek
}
fun getSn(): String {
if (sn == null) {
val mac = getLztek().ethMac ?: "unKnow"
val mac = getLztek()?.ethMac ?: "unKnow"
val md = MessageDigest.getInstance("MD5") // 生成一个MD5加密计算摘要
md.update(mac.toByteArray()) // 计算md5函数
/**

Loading…
Cancel
Save