desc:创建数据库

main
xiaowusky 2 years ago
parent 76a09ce971
commit 5dbe3fef20

@ -60,6 +60,11 @@ dependencies {
implementation rootProject.ext.dependencies.retrofit implementation rootProject.ext.dependencies.retrofit
// rxjava // rxjava
implementation rootProject.ext.dependencies.rxjava implementation rootProject.ext.dependencies.rxjava
// room
var room_version = "2.5.0"
implementation("androidx.room:room-runtime:$room_version")
annotationProcessor("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-ktx:$room_version")
implementation 'com.google.android.material:material:1.5.0' implementation 'com.google.android.material:material:1.5.0'
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0' annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0'

@ -243,30 +243,30 @@ public class MainActivity extends AppCompatActivity {
} }
public void onUVCCamera(View view) { public void onUVCCamera(View view) {
// Intent intent = new Intent(this, UVCActivity.class); Intent intent = new Intent(this, UVCActivity.class);
// Intent intent = new Intent(this, ProVideoActivity.class); // Intent intent = new Intent(this, ProVideoActivity.class);
// String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mvtest.mp4"; // String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mvtest.mp4";
// intent.putExtra("videoPath", path); // intent.putExtra("videoPath", path);
// startActivity(intent); startActivity(intent);
List<List<SimpleCellValue>> allData = new ArrayList<>(); // List<List<SimpleCellValue>> allData = new ArrayList<>();
List<SimpleCellValue> row1 = new ArrayList<>(); // List<SimpleCellValue> row1 = new ArrayList<>();
row1.add(new SimpleCellValue("5.22")); // row1.add(new SimpleCellValue("5.22"));
row1.add(new SimpleCellValue("1")); // row1.add(new SimpleCellValue("1"));
row1.add(new SimpleCellValue("2")); // row1.add(new SimpleCellValue("2"));
row1.add(new SimpleCellValue("3")); // row1.add(new SimpleCellValue("3"));
List<SimpleCellValue> row2 = new ArrayList<>(); // List<SimpleCellValue> row2 = new ArrayList<>();
row2.add(new SimpleCellValue("5.23")); // row2.add(new SimpleCellValue("5.23"));
row2.add(new SimpleCellValue("4")); // row2.add(new SimpleCellValue("4"));
row2.add(new SimpleCellValue("5")); // row2.add(new SimpleCellValue("5"));
row2.add(new SimpleCellValue("6")); // row2.add(new SimpleCellValue("6"));
//
allData.add(row1); // allData.add(row1);
allData.add(row2); // allData.add(row2);
//
ExcelUtils.INSTANCE.writeStringListToExcel(allData, this); // ExcelUtils.INSTANCE.writeStringListToExcel(allData, this);
} }
public void OnSaveRecord(View view) { public void OnSaveRecord(View view) {

@ -5,7 +5,6 @@ import com.lztek.toolkit.Lztek
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
class App : CommonApplication() { class App : CommonApplication() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
LztekUtil.setObject(Lztek.create(this)) LztekUtil.setObject(Lztek.create(this))

@ -0,0 +1,33 @@
package com.yinuo.safetywatcher.watcher.db
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.common.commonlib.CommonApplication
import com.yinuo.safetywatcher.watcher.db.dao.GasDao
import com.yinuo.safetywatcher.watcher.db.dao.WarningDao
import com.yinuo.safetywatcher.watcher.db.entity.Gas
import com.yinuo.safetywatcher.watcher.db.entity.Warning
@Database(entities = [Warning::class, Gas::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun warningDao(): WarningDao
abstract fun gasDao(): GasDao
}
object DBUtils {
val db by lazy {
Room.databaseBuilder(
CommonApplication.getContext()!!,
AppDatabase::class.java, "watcher_db"
).build()
}
fun warningDao(): WarningDao {
return db.warningDao()
}
fun gasDao(): GasDao {
return db.gasDao()
}
}

@ -0,0 +1,26 @@
package com.yinuo.safetywatcher.watcher.db.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.yinuo.safetywatcher.watcher.db.entity.Gas
import com.yinuo.safetywatcher.watcher.db.entity.Warning
@Dao
interface GasDao {
@Query("SELECT * FROM gas")
suspend fun getAll(): List<Gas>
@Query("SELECT * FROM gas WHERE gas_name IS :name AND time BETWEEN :startTime AND :endTime")
suspend fun findByName(name: String, startTime: Long, endTime: Long): List<Warning>
@Insert
suspend fun insertAll(vararg gases: Gas)
@Insert
suspend fun insert(gas: Gas)
@Delete
suspend fun delete(gas: Gas)
}

@ -0,0 +1,25 @@
package com.yinuo.safetywatcher.watcher.db.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.yinuo.safetywatcher.watcher.db.entity.Warning
@Dao
interface WarningDao {
@Query("SELECT * FROM warning")
suspend fun getAll(): List<Warning>
@Query("SELECT * FROM warning WHERE gas_name IS :name AND start_time BETWEEN :startTime AND :endTime")
suspend fun findByName(name: String, startTime: Long, endTime: Long): List<Warning>
@Insert
suspend fun insertAll(vararg warnings: Warning)
@Insert
suspend fun insert(warning: Warning)
@Delete
suspend fun delete(warning: Warning)
}

@ -0,0 +1,16 @@
package com.yinuo.safetywatcher.watcher.db.entity
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class Gas(
@PrimaryKey val id: Int,
@ColumnInfo(name = "time") val time: Long,
@ColumnInfo(name = "gas_name") val gasName: String,
@ColumnInfo(name = "gas_value") val gasValue: Double,
@ColumnInfo(name = "unit") val unit: String,
@ColumnInfo(name = "threshold_low") val thresholdLow: Double,
@ColumnInfo(name = "threshold_high") val thresholdHigh: Double,
)

@ -0,0 +1,17 @@
package com.yinuo.safetywatcher.watcher.db.entity
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class Warning(
@PrimaryKey val id: Int,
@ColumnInfo(name = "gas_name") val gasName: String,
@ColumnInfo(name = "gas_value") val gasValue: Double,
@ColumnInfo(name = "unit") val unit: String,
@ColumnInfo(name = "threshold_low") val thresholdLow: Double,
@ColumnInfo(name = "threshold_high") val thresholdHigh: Double,
@ColumnInfo(name = "start_time") val startTime: Long,
@ColumnInfo(name = "end_time") val endTime: Long,
)
Loading…
Cancel
Save