|
|
|
@ -19,46 +19,46 @@ import com.test.Bluetooh_736_2.Models.PresetScheme;
|
|
|
|
|
public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
|
|
|
|
// 数据库基本信息
|
|
|
|
|
private static final String DATABASE_NAME = "BattlefieldDetection.db";
|
|
|
|
|
private static final String DATABASE_NAME = "BattlefieldDetection.db"; // 数据库名称
|
|
|
|
|
private static final int DATABASE_VERSION = 3;
|
|
|
|
|
|
|
|
|
|
// 数据表名
|
|
|
|
|
private static final String TABLE_SENSOR_DATA = "sensor_data";
|
|
|
|
|
private static final String TABLE_PRESET_SCHEMES = "preset_schemes";
|
|
|
|
|
private static final String TABLE_SENSOR_DATA = "sensor_data"; // 传感器数据表名
|
|
|
|
|
private static final String TABLE_PRESET_SCHEMES = "preset_schemes"; // 预设方案表名
|
|
|
|
|
|
|
|
|
|
// 传感器数据表字段
|
|
|
|
|
private static final String COLUMN_ID = "id";
|
|
|
|
|
private static final String COLUMN_TIMESTAMP = "timestamp";
|
|
|
|
|
private static final String COLUMN_TEMPERATURE = "temperature";
|
|
|
|
|
private static final String COLUMN_HUMIDITY = "humidity";
|
|
|
|
|
private static final String COLUMN_LIGHT = "light";
|
|
|
|
|
private static final String COLUMN_DISTANCE = "distance";
|
|
|
|
|
private static final String COLUMN_TIME_MINUTES = "time_minutes";
|
|
|
|
|
private static final String COLUMN_MODE = "mode";
|
|
|
|
|
private static final String COLUMN_IS_TRIGGERED = "is_triggered";
|
|
|
|
|
private static final String COLUMN_TRIGGER_CONDITIONS = "trigger_conditions";
|
|
|
|
|
private static final String COLUMN_ID = "id"; // ID
|
|
|
|
|
private static final String COLUMN_TIMESTAMP = "timestamp"; // 时间戳
|
|
|
|
|
private static final String COLUMN_TEMPERATURE = "temperature"; // 温度
|
|
|
|
|
private static final String COLUMN_HUMIDITY = "humidity"; // 湿度
|
|
|
|
|
private static final String COLUMN_LIGHT = "light"; // 光照
|
|
|
|
|
private static final String COLUMN_DISTANCE = "distance"; // 距离
|
|
|
|
|
private static final String COLUMN_TIME_MINUTES = "time_minutes"; // 时间(分钟)
|
|
|
|
|
private static final String COLUMN_MODE = "mode"; // 模式
|
|
|
|
|
private static final String COLUMN_IS_TRIGGERED = "is_triggered"; // 触发状态
|
|
|
|
|
private static final String COLUMN_TRIGGER_CONDITIONS = "trigger_conditions"; // 触发条件
|
|
|
|
|
|
|
|
|
|
// 预设方案表字段
|
|
|
|
|
private static final String COLUMN_PRESET_ID = "preset_id";
|
|
|
|
|
private static final String COLUMN_PRESET_NAME = "preset_name";
|
|
|
|
|
private static final String COLUMN_PRESET_DESCRIPTION = "preset_description";
|
|
|
|
|
private static final String COLUMN_PRESET_TEMP_THRESHOLD = "temp_threshold";
|
|
|
|
|
private static final String COLUMN_PRESET_HUMI_THRESHOLD = "humi_threshold";
|
|
|
|
|
private static final String COLUMN_PRESET_DISTANCE_THRESHOLD = "distance_threshold";
|
|
|
|
|
private static final String COLUMN_PRESET_LIGHT_THRESHOLD = "light_threshold";
|
|
|
|
|
private static final String COLUMN_PRESET_RECORD_INTERVAL = "record_interval";
|
|
|
|
|
private static final String COLUMN_PRESET_AUTO_RECORD = "auto_record";
|
|
|
|
|
private static final String COLUMN_PRESET_CREATED_TIME = "created_time";
|
|
|
|
|
private static final String COLUMN_PRESET_IS_DEFAULT = "is_default";
|
|
|
|
|
private static final String COLUMN_PRESET_ID = "preset_id"; // ID
|
|
|
|
|
private static final String COLUMN_PRESET_NAME = "preset_name"; // 预设方案名称
|
|
|
|
|
private static final String COLUMN_PRESET_DESCRIPTION = "preset_description"; // 预设方案描述
|
|
|
|
|
private static final String COLUMN_PRESET_TEMP_THRESHOLD = "temp_threshold"; // 温度阈值
|
|
|
|
|
private static final String COLUMN_PRESET_HUMI_THRESHOLD = "humi_threshold"; // 湿度阈值
|
|
|
|
|
private static final String COLUMN_PRESET_DISTANCE_THRESHOLD = "distance_threshold"; // 距离阈值
|
|
|
|
|
private static final String COLUMN_PRESET_LIGHT_THRESHOLD = "light_threshold"; // 光照阈值
|
|
|
|
|
private static final String COLUMN_PRESET_RECORD_INTERVAL = "record_interval"; // 记录间隔
|
|
|
|
|
private static final String COLUMN_PRESET_AUTO_RECORD = "auto_record"; // 自动记录
|
|
|
|
|
private static final String COLUMN_PRESET_CREATED_TIME = "created_time"; // 创建时间
|
|
|
|
|
private static final String COLUMN_PRESET_IS_DEFAULT = "is_default"; // 是否默认
|
|
|
|
|
|
|
|
|
|
public DatabaseHelper(Context context) {
|
|
|
|
|
public DatabaseHelper(Context context) { // 构造函数
|
|
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
|
public void onCreate(SQLiteDatabase db) { // 创建数据库
|
|
|
|
|
// 创建传感器数据表
|
|
|
|
|
String CREATE_SENSOR_TABLE = "CREATE TABLE " + TABLE_SENSOR_DATA + "("
|
|
|
|
|
String CREATE_SENSOR_TABLE = "CREATE TABLE " + TABLE_SENSOR_DATA + "(" // 创建传感器数据表
|
|
|
|
|
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
|
|
|
+ COLUMN_TIMESTAMP + " TEXT NOT NULL,"
|
|
|
|
|
+ COLUMN_TEMPERATURE + " INTEGER,"
|
|
|
|
@ -73,7 +73,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
db.execSQL(CREATE_SENSOR_TABLE);
|
|
|
|
|
|
|
|
|
|
// 创建预设方案表
|
|
|
|
|
String CREATE_PRESET_TABLE = "CREATE TABLE " + TABLE_PRESET_SCHEMES + "("
|
|
|
|
|
String CREATE_PRESET_TABLE = "CREATE TABLE " + TABLE_PRESET_SCHEMES + "(" // 创建预设方案表
|
|
|
|
|
+ COLUMN_PRESET_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
|
|
|
+ COLUMN_PRESET_NAME + " TEXT NOT NULL,"
|
|
|
|
|
+ COLUMN_PRESET_DESCRIPTION + " TEXT,"
|
|
|
|
@ -93,40 +93,40 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 升级数据库
|
|
|
|
|
// 升级数据库时处理版本兼容
|
|
|
|
|
if (oldVersion < 2) {
|
|
|
|
|
// 版本1 -> 版本2:添加触发状态和触发条件字段
|
|
|
|
|
try {
|
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE_SENSOR_DATA + " ADD COLUMN " + COLUMN_IS_TRIGGERED + " INTEGER DEFAULT 0");
|
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE_SENSOR_DATA + " ADD COLUMN " + COLUMN_TRIGGER_CONDITIONS + " TEXT");
|
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE_SENSOR_DATA + " ADD COLUMN " + COLUMN_IS_TRIGGERED + " INTEGER DEFAULT 0"); // 添加触发状态字段
|
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE_SENSOR_DATA + " ADD COLUMN " + COLUMN_TRIGGER_CONDITIONS + " TEXT"); // 添加触发条件字段
|
|
|
|
|
Log.d("DatabaseHelper", "数据库升级成功:添加触发状态字段");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e("DatabaseHelper", "数据库升级失败,重新创建表", e);
|
|
|
|
|
// 如果升级失败,删除旧表并创建新表
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SENSOR_DATA);
|
|
|
|
|
onCreate(db);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SENSOR_DATA); // 删除旧表
|
|
|
|
|
onCreate(db); // 创建新表
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (oldVersion < 3) {
|
|
|
|
|
// 版本2 -> 版本3:添加预设方案表
|
|
|
|
|
try {
|
|
|
|
|
String CREATE_PRESET_TABLE = "CREATE TABLE " + TABLE_PRESET_SCHEMES + "("
|
|
|
|
|
+ COLUMN_PRESET_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
|
|
|
+ COLUMN_PRESET_NAME + " TEXT NOT NULL,"
|
|
|
|
|
+ COLUMN_PRESET_DESCRIPTION + " TEXT,"
|
|
|
|
|
+ COLUMN_PRESET_TEMP_THRESHOLD + " INTEGER DEFAULT 45,"
|
|
|
|
|
+ COLUMN_PRESET_HUMI_THRESHOLD + " INTEGER DEFAULT 35,"
|
|
|
|
|
+ COLUMN_PRESET_DISTANCE_THRESHOLD + " INTEGER DEFAULT 10,"
|
|
|
|
|
+ COLUMN_PRESET_LIGHT_THRESHOLD + " INTEGER DEFAULT 70,"
|
|
|
|
|
+ COLUMN_PRESET_RECORD_INTERVAL + " INTEGER DEFAULT 1000,"
|
|
|
|
|
+ COLUMN_PRESET_AUTO_RECORD + " INTEGER DEFAULT 1,"
|
|
|
|
|
+ COLUMN_PRESET_CREATED_TIME + " TEXT NOT NULL,"
|
|
|
|
|
+ COLUMN_PRESET_IS_DEFAULT + " INTEGER DEFAULT 0"
|
|
|
|
|
String CREATE_PRESET_TABLE = "CREATE TABLE " + TABLE_PRESET_SCHEMES + "(" // 创建预设方案表
|
|
|
|
|
+ COLUMN_PRESET_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," // 预设方案ID
|
|
|
|
|
+ COLUMN_PRESET_NAME + " TEXT NOT NULL," // 预设方案名称
|
|
|
|
|
+ COLUMN_PRESET_DESCRIPTION + " TEXT," // 预设方案描述
|
|
|
|
|
+ COLUMN_PRESET_TEMP_THRESHOLD + " INTEGER DEFAULT 45," // 温度阈值
|
|
|
|
|
+ COLUMN_PRESET_HUMI_THRESHOLD + " INTEGER DEFAULT 35," // 湿度阈值
|
|
|
|
|
+ COLUMN_PRESET_DISTANCE_THRESHOLD + " INTEGER DEFAULT 10," // 距离阈值
|
|
|
|
|
+ COLUMN_PRESET_LIGHT_THRESHOLD + " INTEGER DEFAULT 70," // 光照阈值
|
|
|
|
|
+ COLUMN_PRESET_RECORD_INTERVAL + " INTEGER DEFAULT 1000," // 记录间隔
|
|
|
|
|
+ COLUMN_PRESET_AUTO_RECORD + " INTEGER DEFAULT 1," // 自动记录
|
|
|
|
|
+ COLUMN_PRESET_CREATED_TIME + " TEXT NOT NULL," // 创建时间
|
|
|
|
|
+ COLUMN_PRESET_IS_DEFAULT + " INTEGER DEFAULT 0" // 是否默认
|
|
|
|
|
+ ")";
|
|
|
|
|
db.execSQL(CREATE_PRESET_TABLE);
|
|
|
|
|
insertDefaultPresets(db);
|
|
|
|
|
db.execSQL(CREATE_PRESET_TABLE); // 执行创建预设方案表的SQL语句
|
|
|
|
|
insertDefaultPresets(db); // 插入默认预设方案
|
|
|
|
|
Log.d("DatabaseHelper", "数据库升级成功:添加预设方案表");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e("DatabaseHelper", "添加预设方案表失败", e);
|
|
|
|
@ -138,277 +138,277 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
* 添加传感器数据记录
|
|
|
|
|
*/
|
|
|
|
|
public long addSensorData(int temperature, int humidity, int light, int distance, int timeMinutes, String mode, boolean isTriggered, String triggerConditions) {
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(COLUMN_TIMESTAMP, getCurrentTimestamp());
|
|
|
|
|
values.put(COLUMN_TEMPERATURE, temperature);
|
|
|
|
|
values.put(COLUMN_HUMIDITY, humidity);
|
|
|
|
|
values.put(COLUMN_LIGHT, light);
|
|
|
|
|
values.put(COLUMN_DISTANCE, distance);
|
|
|
|
|
values.put(COLUMN_TIME_MINUTES, timeMinutes);
|
|
|
|
|
values.put(COLUMN_MODE, mode);
|
|
|
|
|
values.put(COLUMN_IS_TRIGGERED, isTriggered ? 1 : 0);
|
|
|
|
|
values.put(COLUMN_TRIGGER_CONDITIONS, triggerConditions);
|
|
|
|
|
|
|
|
|
|
long id = db.insert(TABLE_SENSOR_DATA, null, values);
|
|
|
|
|
db.close();
|
|
|
|
|
return id;
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase(); // 获取可写数据库
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues(); // 创建ContentValues对象
|
|
|
|
|
values.put(COLUMN_TIMESTAMP, getCurrentTimestamp()); // 设置时间戳
|
|
|
|
|
values.put(COLUMN_TEMPERATURE, temperature); // 设置温度
|
|
|
|
|
values.put(COLUMN_HUMIDITY, humidity); // 设置湿度
|
|
|
|
|
values.put(COLUMN_LIGHT, light); // 设置光照
|
|
|
|
|
values.put(COLUMN_DISTANCE, distance); // 设置距离
|
|
|
|
|
values.put(COLUMN_TIME_MINUTES, timeMinutes); // 设置时间(分钟)
|
|
|
|
|
values.put(COLUMN_MODE, mode); // 设置模式
|
|
|
|
|
values.put(COLUMN_IS_TRIGGERED, isTriggered ? 1 : 0); // 设置触发状态
|
|
|
|
|
values.put(COLUMN_TRIGGER_CONDITIONS, triggerConditions); // 设置触发条件
|
|
|
|
|
|
|
|
|
|
long id = db.insert(TABLE_SENSOR_DATA, null, values); // 插入数据
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return id; // 返回ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加传感器数据记录(兼容旧版本)
|
|
|
|
|
*/
|
|
|
|
|
public long addSensorData(int temperature, int humidity, int light, int distance, int timeMinutes, String mode) {
|
|
|
|
|
return addSensorData(temperature, humidity, light, distance, timeMinutes, mode, false, "");
|
|
|
|
|
public long addSensorData(int temperature, int humidity, int light, int distance, int timeMinutes, String mode) { // 添加传感器数据记录(兼容旧版本)
|
|
|
|
|
return addSensorData(temperature, humidity, light, distance, timeMinutes, mode, false, ""); // 调用添加传感器数据记录的方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有传感器数据
|
|
|
|
|
*/
|
|
|
|
|
public List<SensorData> getAllSensorData() {
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>();
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_SENSOR_DATA + " ORDER BY " + COLUMN_TIMESTAMP + " DESC";
|
|
|
|
|
public List<SensorData> getAllSensorData() { // 获取所有传感器数据
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>(); // 创建SensorData列表
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_SENSOR_DATA + " ORDER BY " + COLUMN_TIMESTAMP + " DESC"; // 创建查询语句
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null); // 执行查询
|
|
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor);
|
|
|
|
|
dataList.add(data);
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor); // 创建SensorData对象
|
|
|
|
|
dataList.add(data); // 添加到列表
|
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return dataList;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return dataList; // 返回数据列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取最近N条记录
|
|
|
|
|
*/
|
|
|
|
|
public List<SensorData> getRecentData(int limit) {
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>();
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_SENSOR_DATA + " ORDER BY " + COLUMN_TIMESTAMP + " DESC LIMIT " + limit;
|
|
|
|
|
public List<SensorData> getRecentData(int limit) { // 获取最近N条记录
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>(); // 创建SensorData列表
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_SENSOR_DATA + " ORDER BY " + COLUMN_TIMESTAMP + " DESC LIMIT " + limit; // 创建查询语句
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null); // 执行查询
|
|
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor);
|
|
|
|
|
dataList.add(data);
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor); // 创建SensorData对象
|
|
|
|
|
dataList.add(data); // 添加到列表
|
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return dataList;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return dataList; // 返回数据列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定日期范围的数据
|
|
|
|
|
*/
|
|
|
|
|
public List<SensorData> getDataByDateRange(String startDate, String endDate) {
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>();
|
|
|
|
|
public List<SensorData> getDataByDateRange(String startDate, String endDate) { // 获取指定日期范围的数据
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>(); // 创建SensorData列表
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_SENSOR_DATA +
|
|
|
|
|
" WHERE " + COLUMN_TIMESTAMP + " BETWEEN '" + startDate + "' AND '" + endDate + "'" +
|
|
|
|
|
" WHERE " + COLUMN_TIMESTAMP + " BETWEEN '" + startDate + "' AND '" + endDate + "'" + // 创建查询语句
|
|
|
|
|
" ORDER BY " + COLUMN_TIMESTAMP + " ASC";
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null); // 执行查询
|
|
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor);
|
|
|
|
|
dataList.add(data);
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor); // 创建SensorData对象
|
|
|
|
|
dataList.add(data); // 添加到列表
|
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return dataList;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return dataList; // 返回数据列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按触发状态筛选数据
|
|
|
|
|
*/
|
|
|
|
|
public List<SensorData> getDataByTriggerStatus(boolean isTriggered) {
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>();
|
|
|
|
|
public List<SensorData> getDataByTriggerStatus(boolean isTriggered) { // 按触发状态筛选数据
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>(); // 创建SensorData列表
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_SENSOR_DATA +
|
|
|
|
|
" WHERE " + COLUMN_IS_TRIGGERED + " = ?" +
|
|
|
|
|
" WHERE " + COLUMN_IS_TRIGGERED + " = ?" + // 创建查询语句
|
|
|
|
|
" ORDER BY " + COLUMN_TIMESTAMP + " DESC";
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, new String[]{isTriggered ? "1" : "0"});
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, new String[]{isTriggered ? "1" : "0"}); // 执行查询
|
|
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor);
|
|
|
|
|
dataList.add(data);
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor); // 创建SensorData对象
|
|
|
|
|
dataList.add(data); // 添加到列表
|
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return dataList;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return dataList; // 返回数据列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按时间区段和触发状态筛选数据
|
|
|
|
|
*/
|
|
|
|
|
public List<SensorData> getDataByDateRangeAndTriggerStatus(String startDate, String endDate, Boolean isTriggered) {
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>();
|
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
|
|
|
queryBuilder.append("SELECT * FROM ").append(TABLE_SENSOR_DATA)
|
|
|
|
|
public List<SensorData> getDataByDateRangeAndTriggerStatus(String startDate, String endDate, Boolean isTriggered) { // 按时间区段和触发状态筛选数据
|
|
|
|
|
List<SensorData> dataList = new ArrayList<>(); // 创建SensorData列表
|
|
|
|
|
StringBuilder queryBuilder = new StringBuilder(); // 创建StringBuilder对象
|
|
|
|
|
queryBuilder.append("SELECT * FROM ").append(TABLE_SENSOR_DATA) // 创建查询语句
|
|
|
|
|
.append(" WHERE ").append(COLUMN_TIMESTAMP)
|
|
|
|
|
.append(" BETWEEN '").append(startDate).append("' AND '").append(endDate).append("'");
|
|
|
|
|
.append(" BETWEEN '").append(startDate).append("' AND '").append(endDate).append("'"); // 创建查询语句
|
|
|
|
|
|
|
|
|
|
if (isTriggered != null) {
|
|
|
|
|
if (isTriggered != null) { // 如果触发状态不为空
|
|
|
|
|
queryBuilder.append(" AND ").append(COLUMN_IS_TRIGGERED).append(" = ").append(isTriggered ? "1" : "0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryBuilder.append(" ORDER BY ").append(COLUMN_TIMESTAMP).append(" ASC");
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery(queryBuilder.toString(), null);
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery(queryBuilder.toString(), null); // 执行查询
|
|
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor);
|
|
|
|
|
dataList.add(data);
|
|
|
|
|
SensorData data = createSensorDataFromCursor(cursor); // 创建SensorData对象
|
|
|
|
|
dataList.add(data); // 添加到列表
|
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return dataList;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return dataList; // 返回数据列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取数据统计信息
|
|
|
|
|
*/
|
|
|
|
|
public DataStatistics getDataStatistics() {
|
|
|
|
|
DataStatistics stats = new DataStatistics();
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
public DataStatistics getDataStatistics() { // 获取数据统计信息
|
|
|
|
|
DataStatistics stats = new DataStatistics(); // 创建DataStatistics对象
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
|
|
|
|
|
// 获取总记录数
|
|
|
|
|
Cursor countCursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_SENSOR_DATA, null);
|
|
|
|
|
if (countCursor.moveToFirst()) {
|
|
|
|
|
stats.setTotalRecords(countCursor.getInt(0));
|
|
|
|
|
Cursor countCursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_SENSOR_DATA, null); // 执行查询
|
|
|
|
|
if (countCursor.moveToFirst()) { // 如果游标移动到第一行
|
|
|
|
|
stats.setTotalRecords(countCursor.getInt(0)); // 设置总记录数
|
|
|
|
|
}
|
|
|
|
|
countCursor.close();
|
|
|
|
|
countCursor.close(); // 关闭游标
|
|
|
|
|
|
|
|
|
|
// 获取温度统计
|
|
|
|
|
Cursor tempCursor = db.rawQuery("SELECT MIN(" + COLUMN_TEMPERATURE + "), MAX(" + COLUMN_TEMPERATURE + "), AVG(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_SENSOR_DATA, null);
|
|
|
|
|
if (tempCursor.moveToFirst()) {
|
|
|
|
|
stats.setMinTemperature(tempCursor.getInt(0));
|
|
|
|
|
stats.setMaxTemperature(tempCursor.getInt(1));
|
|
|
|
|
stats.setAvgTemperature(tempCursor.getFloat(2));
|
|
|
|
|
Cursor tempCursor = db.rawQuery("SELECT MIN(" + COLUMN_TEMPERATURE + "), MAX(" + COLUMN_TEMPERATURE + "), AVG(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_SENSOR_DATA, null); // 执行查询
|
|
|
|
|
if (tempCursor.moveToFirst()) { // 如果游标移动到第一行
|
|
|
|
|
stats.setMinTemperature(tempCursor.getInt(0)); // 设置最小温度
|
|
|
|
|
stats.setMaxTemperature(tempCursor.getInt(1)); // 设置最大温度
|
|
|
|
|
stats.setAvgTemperature(tempCursor.getFloat(2)); // 设置平均温度
|
|
|
|
|
}
|
|
|
|
|
tempCursor.close();
|
|
|
|
|
tempCursor.close(); // 关闭游标
|
|
|
|
|
|
|
|
|
|
// 获取湿度统计
|
|
|
|
|
Cursor humiCursor = db.rawQuery("SELECT MIN(" + COLUMN_HUMIDITY + "), MAX(" + COLUMN_HUMIDITY + "), AVG(" + COLUMN_HUMIDITY + ") FROM " + TABLE_SENSOR_DATA, null);
|
|
|
|
|
if (humiCursor.moveToFirst()) {
|
|
|
|
|
stats.setMinHumidity(humiCursor.getInt(0));
|
|
|
|
|
stats.setMaxHumidity(humiCursor.getInt(1));
|
|
|
|
|
stats.setAvgHumidity(humiCursor.getFloat(2));
|
|
|
|
|
Cursor humiCursor = db.rawQuery("SELECT MIN(" + COLUMN_HUMIDITY + "), MAX(" + COLUMN_HUMIDITY + "), AVG(" + COLUMN_HUMIDITY + ") FROM " + TABLE_SENSOR_DATA, null); // 执行查询
|
|
|
|
|
if (humiCursor.moveToFirst()) { // 如果游标移动到第一行
|
|
|
|
|
stats.setMinHumidity(humiCursor.getInt(0)); // 设置最小湿度
|
|
|
|
|
stats.setMaxHumidity(humiCursor.getInt(1)); // 设置最大湿度
|
|
|
|
|
stats.setAvgHumidity(humiCursor.getFloat(2)); // 设置平均湿度
|
|
|
|
|
}
|
|
|
|
|
humiCursor.close();
|
|
|
|
|
humiCursor.close(); // 关闭游标
|
|
|
|
|
|
|
|
|
|
// 获取光照统计
|
|
|
|
|
Cursor lightCursor = db.rawQuery("SELECT MIN(" + COLUMN_LIGHT + "), MAX(" + COLUMN_LIGHT + "), AVG(" + COLUMN_LIGHT + ") FROM " + TABLE_SENSOR_DATA, null);
|
|
|
|
|
if (lightCursor.moveToFirst()) {
|
|
|
|
|
stats.setMinLight(lightCursor.getInt(0));
|
|
|
|
|
stats.setMaxLight(lightCursor.getInt(1));
|
|
|
|
|
stats.setAvgLight(lightCursor.getFloat(2));
|
|
|
|
|
Cursor lightCursor = db.rawQuery("SELECT MIN(" + COLUMN_LIGHT + "), MAX(" + COLUMN_LIGHT + "), AVG(" + COLUMN_LIGHT + ") FROM " + TABLE_SENSOR_DATA, null); // 执行查询
|
|
|
|
|
if (lightCursor.moveToFirst()) { // 如果游标移动到第一行
|
|
|
|
|
stats.setMinLight(lightCursor.getInt(0)); // 设置最小光照
|
|
|
|
|
stats.setMaxLight(lightCursor.getInt(1)); // 设置最大光照
|
|
|
|
|
stats.setAvgLight(lightCursor.getFloat(2)); // 设置平均光照
|
|
|
|
|
}
|
|
|
|
|
lightCursor.close();
|
|
|
|
|
lightCursor.close(); // 关闭游标
|
|
|
|
|
|
|
|
|
|
db.close();
|
|
|
|
|
return stats;
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return stats; // 返回数据统计信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除指定天数之前的数据
|
|
|
|
|
*/
|
|
|
|
|
public int deleteOldData(int daysToKeep) {
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
public int deleteOldData(int daysToKeep) { // 删除指定天数之前的数据
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase(); // 获取可写数据库
|
|
|
|
|
|
|
|
|
|
// 计算删除时间点
|
|
|
|
|
long cutoffTime = System.currentTimeMillis() - (daysToKeep * 24 * 60 * 60 * 1000L);
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
|
|
|
|
|
String cutoffDate = sdf.format(new Date(cutoffTime));
|
|
|
|
|
long cutoffTime = System.currentTimeMillis() - (daysToKeep * 24 * 60 * 60 * 1000L); // 计算删除时间点
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); // 创建SimpleDateFormat对象
|
|
|
|
|
String cutoffDate = sdf.format(new Date(cutoffTime)); // 计算删除时间点
|
|
|
|
|
|
|
|
|
|
int deletedRows = db.delete(TABLE_SENSOR_DATA, COLUMN_TIMESTAMP + " < ?", new String[]{cutoffDate});
|
|
|
|
|
db.close();
|
|
|
|
|
return deletedRows;
|
|
|
|
|
int deletedRows = db.delete(TABLE_SENSOR_DATA, COLUMN_TIMESTAMP + " < ?", new String[]{cutoffDate}); // 删除数据
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return deletedRows; // 返回删除的行数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清空所有数据
|
|
|
|
|
*/
|
|
|
|
|
public void clearAllData() {
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
db.delete(TABLE_SENSOR_DATA, null, null);
|
|
|
|
|
db.close();
|
|
|
|
|
public void clearAllData() { // 清空所有数据
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase(); // 获取可写数据库
|
|
|
|
|
db.delete(TABLE_SENSOR_DATA, null, null); // 删除所有数据
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从Cursor创建SensorData对象
|
|
|
|
|
*/
|
|
|
|
|
private SensorData createSensorDataFromCursor(Cursor cursor) {
|
|
|
|
|
SensorData data = new SensorData();
|
|
|
|
|
data.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
|
|
|
|
|
data.setTimestamp(cursor.getString(cursor.getColumnIndex(COLUMN_TIMESTAMP)));
|
|
|
|
|
data.setTemperature(cursor.getInt(cursor.getColumnIndex(COLUMN_TEMPERATURE)));
|
|
|
|
|
data.setHumidity(cursor.getInt(cursor.getColumnIndex(COLUMN_HUMIDITY)));
|
|
|
|
|
data.setLight(cursor.getInt(cursor.getColumnIndex(COLUMN_LIGHT)));
|
|
|
|
|
data.setDistance(cursor.getInt(cursor.getColumnIndex(COLUMN_DISTANCE)));
|
|
|
|
|
data.setTimeMinutes(cursor.getInt(cursor.getColumnIndex(COLUMN_TIME_MINUTES)));
|
|
|
|
|
data.setMode(cursor.getString(cursor.getColumnIndex(COLUMN_MODE)));
|
|
|
|
|
private SensorData createSensorDataFromCursor(Cursor cursor) { // 从Cursor创建SensorData对象
|
|
|
|
|
SensorData data = new SensorData(); // 创建SensorData对象
|
|
|
|
|
data.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID))); // 设置ID
|
|
|
|
|
data.setTimestamp(cursor.getString(cursor.getColumnIndex(COLUMN_TIMESTAMP))); // 设置时间戳
|
|
|
|
|
data.setTemperature(cursor.getInt(cursor.getColumnIndex(COLUMN_TEMPERATURE))); // 设置温度
|
|
|
|
|
data.setHumidity(cursor.getInt(cursor.getColumnIndex(COLUMN_HUMIDITY))); // 设置湿度
|
|
|
|
|
data.setLight(cursor.getInt(cursor.getColumnIndex(COLUMN_LIGHT))); // 设置光照
|
|
|
|
|
data.setDistance(cursor.getInt(cursor.getColumnIndex(COLUMN_DISTANCE))); // 设置距离
|
|
|
|
|
data.setTimeMinutes(cursor.getInt(cursor.getColumnIndex(COLUMN_TIME_MINUTES))); // 设置时间(分钟)
|
|
|
|
|
data.setMode(cursor.getString(cursor.getColumnIndex(COLUMN_MODE))); // 设置模式
|
|
|
|
|
|
|
|
|
|
// 处理新增字段(兼容旧数据)
|
|
|
|
|
int triggeredColumnIndex = cursor.getColumnIndex(COLUMN_IS_TRIGGERED);
|
|
|
|
|
if (triggeredColumnIndex >= 0) {
|
|
|
|
|
data.setTriggered(cursor.getInt(triggeredColumnIndex) == 1);
|
|
|
|
|
int triggeredColumnIndex = cursor.getColumnIndex(COLUMN_IS_TRIGGERED); // 获取触发状态列索引
|
|
|
|
|
if (triggeredColumnIndex >= 0) { // 如果触发状态列存在
|
|
|
|
|
data.setTriggered(cursor.getInt(triggeredColumnIndex) == 1); // 设置触发状态
|
|
|
|
|
} else {
|
|
|
|
|
data.setTriggered(false);
|
|
|
|
|
data.setTriggered(false); // 设置触发状态
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int conditionsColumnIndex = cursor.getColumnIndex(COLUMN_TRIGGER_CONDITIONS);
|
|
|
|
|
if (conditionsColumnIndex >= 0) {
|
|
|
|
|
data.setTriggerConditions(cursor.getString(conditionsColumnIndex));
|
|
|
|
|
int conditionsColumnIndex = cursor.getColumnIndex(COLUMN_TRIGGER_CONDITIONS); // 获取触发条件列索引
|
|
|
|
|
if (conditionsColumnIndex >= 0) { // 如果触发条件列存在
|
|
|
|
|
data.setTriggerConditions(cursor.getString(conditionsColumnIndex)); // 设置触发条件
|
|
|
|
|
} else {
|
|
|
|
|
data.setTriggerConditions("");
|
|
|
|
|
data.setTriggerConditions(""); // 设置触发条件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
return data; // 返回SensorData对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前时间戳
|
|
|
|
|
*/
|
|
|
|
|
private String getCurrentTimestamp() {
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
|
|
|
|
|
return sdf.format(new Date());
|
|
|
|
|
private String getCurrentTimestamp() { // 获取当前时间戳
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); // 创建SimpleDateFormat对象
|
|
|
|
|
return sdf.format(new Date()); // 返回当前时间戳
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取数据库记录总数
|
|
|
|
|
*/
|
|
|
|
|
public int getRecordCount() {
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_SENSOR_DATA, null);
|
|
|
|
|
int count = 0;
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
count = cursor.getInt(0);
|
|
|
|
|
public int getRecordCount() { // 获取数据库记录总数
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_SENSOR_DATA, null); // 执行查询
|
|
|
|
|
int count = 0; // 记录总数
|
|
|
|
|
if (cursor.moveToFirst()) { // 如果游标移动到第一行
|
|
|
|
|
count = cursor.getInt(0); // 设置记录总数
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return count;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return count; // 返回记录总数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ========== 预设方案管理方法 ==========
|
|
|
|
@ -416,163 +416,163 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
/**
|
|
|
|
|
* 插入默认预设方案
|
|
|
|
|
*/
|
|
|
|
|
private void insertDefaultPresets(SQLiteDatabase db) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
private void insertDefaultPresets(SQLiteDatabase db) { // 插入默认预设方案
|
|
|
|
|
ContentValues values = new ContentValues(); // 创建ContentValues对象
|
|
|
|
|
|
|
|
|
|
// 默认方案1:普通检测
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, "普通检测");
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, "适用于一般环境的检测设置");
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, 45);
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, 35);
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, 10);
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, 70);
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, 1000);
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, 1);
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp());
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 1);
|
|
|
|
|
db.insert(TABLE_PRESET_SCHEMES, null, values);
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, "普通检测"); // 设置预设方案名称
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, "适用于一般环境的检测设置"); // 设置预设方案描述
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, 45); // 设置温度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, 35); // 设置湿度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, 10); // 设置距离阈值
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, 70); // 设置光照阈值
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, 1000); // 设置记录间隔
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, 1); // 设置自动记录
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp()); // 设置创建时间
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 1); // 设置是否默认
|
|
|
|
|
db.insert(TABLE_PRESET_SCHEMES, null, values); // 插入数据
|
|
|
|
|
|
|
|
|
|
// 默认方案2:高精度检测
|
|
|
|
|
values.clear();
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, "高精度检测");
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, "高频率记录,适用于精密监控");
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, 40);
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, 30);
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, 5);
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, 80);
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, 500);
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, 1);
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp());
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 1);
|
|
|
|
|
db.insert(TABLE_PRESET_SCHEMES, null, values);
|
|
|
|
|
values.clear(); // 清空ContentValues对象
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, "高精度检测"); // 设置预设方案名称
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, "高频率记录,适用于精密监控"); // 设置预设方案描述
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, 40); // 设置温度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, 30); // 设置湿度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, 5); // 设置距离阈值
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, 80); // 设置光照阈值
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, 500); // 设置记录间隔
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, 1); // 设置自动记录
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp()); // 设置创建时间
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 1); // 设置是否默认
|
|
|
|
|
db.insert(TABLE_PRESET_SCHEMES, null, values); // 插入数据
|
|
|
|
|
|
|
|
|
|
// 默认方案3:节能检测
|
|
|
|
|
values.clear();
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, "节能检测");
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, "低频率记录,适用于长期监控");
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, 50);
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, 40);
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, 15);
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, 60);
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, 5000);
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, 1);
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp());
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 1);
|
|
|
|
|
db.insert(TABLE_PRESET_SCHEMES, null, values);
|
|
|
|
|
values.clear(); // 清空ContentValues对象
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, "节能检测"); // 设置预设方案名称
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, "低频率记录,适用于长期监控"); // 设置预设方案描述
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, 50); // 设置温度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, 40); // 设置湿度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, 15); // 设置距离阈值
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, 60); // 设置光照阈值
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, 5000); // 设置记录间隔
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, 1); // 设置自动记录
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp()); // 设置创建时间
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 1); // 设置是否默认
|
|
|
|
|
db.insert(TABLE_PRESET_SCHEMES, null, values); // 插入数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有预设方案
|
|
|
|
|
*/
|
|
|
|
|
public List<PresetScheme> getAllPresetSchemes() {
|
|
|
|
|
List<PresetScheme> schemes = new ArrayList<>();
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_PRESET_SCHEMES + " ORDER BY " + COLUMN_PRESET_IS_DEFAULT + " DESC, " + COLUMN_PRESET_CREATED_TIME + " DESC";
|
|
|
|
|
public List<PresetScheme> getAllPresetSchemes() { // 获取所有预设方案
|
|
|
|
|
List<PresetScheme> schemes = new ArrayList<>(); // 创建PresetScheme列表
|
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_PRESET_SCHEMES + " ORDER BY " + COLUMN_PRESET_IS_DEFAULT + " DESC, " + COLUMN_PRESET_CREATED_TIME + " DESC"; // 创建查询语句
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null); // 执行查询
|
|
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
PresetScheme scheme = createPresetSchemeFromCursor(cursor);
|
|
|
|
|
schemes.add(scheme);
|
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
|
PresetScheme scheme = createPresetSchemeFromCursor(cursor); // 创建PresetScheme对象
|
|
|
|
|
schemes.add(scheme); // 添加到列表
|
|
|
|
|
} while (cursor.moveToNext()); // 移动到下一行
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return schemes;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return schemes; // 返回预设方案列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加预设方案
|
|
|
|
|
*/
|
|
|
|
|
public long addPresetScheme(String name, String description, int tempThreshold, int humiThreshold,
|
|
|
|
|
int distanceThreshold, int lightThreshold, int recordInterval, boolean autoRecord) {
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, name);
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, description);
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, tempThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, humiThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, distanceThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, lightThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, recordInterval);
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, autoRecord ? 1 : 0);
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp());
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 0);
|
|
|
|
|
|
|
|
|
|
long id = db.insert(TABLE_PRESET_SCHEMES, null, values);
|
|
|
|
|
db.close();
|
|
|
|
|
return id;
|
|
|
|
|
int distanceThreshold, int lightThreshold, int recordInterval, boolean autoRecord) { // 添加预设方案
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase(); // 获取可写数据库
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues(); // 创建ContentValues对象
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, name); // 设置预设方案名称
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, description); // 设置预设方案描述
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, tempThreshold); // 设置温度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, humiThreshold); // 设置湿度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, distanceThreshold); // 设置距离阈值
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, lightThreshold); // 设置光照阈值
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, recordInterval); // 设置记录间隔
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, autoRecord ? 1 : 0); // 设置自动记录
|
|
|
|
|
values.put(COLUMN_PRESET_CREATED_TIME, getCurrentTimestamp()); // 设置创建时间
|
|
|
|
|
values.put(COLUMN_PRESET_IS_DEFAULT, 0); // 设置是否默认
|
|
|
|
|
|
|
|
|
|
long id = db.insert(TABLE_PRESET_SCHEMES, null, values); // 插入数据
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return id; // 返回ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新预设方案
|
|
|
|
|
*/
|
|
|
|
|
public int updatePresetScheme(long id, String name, String description, int tempThreshold, int humiThreshold,
|
|
|
|
|
int distanceThreshold, int lightThreshold, int recordInterval, boolean autoRecord) {
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, name);
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, description);
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, tempThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, humiThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, distanceThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, lightThreshold);
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, recordInterval);
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, autoRecord ? 1 : 0);
|
|
|
|
|
|
|
|
|
|
int rowsAffected = db.update(TABLE_PRESET_SCHEMES, values, COLUMN_PRESET_ID + " = ?", new String[]{String.valueOf(id)});
|
|
|
|
|
db.close();
|
|
|
|
|
return rowsAffected;
|
|
|
|
|
int distanceThreshold, int lightThreshold, int recordInterval, boolean autoRecord) { // 更新预设方案
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase(); // 获取可写数据库
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues(); // 创建ContentValues对象
|
|
|
|
|
values.put(COLUMN_PRESET_NAME, name); // 设置预设方案名称
|
|
|
|
|
values.put(COLUMN_PRESET_DESCRIPTION, description); // 设置预设方案描述
|
|
|
|
|
values.put(COLUMN_PRESET_TEMP_THRESHOLD, tempThreshold); // 设置温度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_HUMI_THRESHOLD, humiThreshold); // 设置湿度阈值
|
|
|
|
|
values.put(COLUMN_PRESET_DISTANCE_THRESHOLD, distanceThreshold); // 设置距离阈值
|
|
|
|
|
values.put(COLUMN_PRESET_LIGHT_THRESHOLD, lightThreshold); // 设置光照阈值
|
|
|
|
|
values.put(COLUMN_PRESET_RECORD_INTERVAL, recordInterval); // 设置记录间隔
|
|
|
|
|
values.put(COLUMN_PRESET_AUTO_RECORD, autoRecord ? 1 : 0); // 设置自动记录
|
|
|
|
|
|
|
|
|
|
int rowsAffected = db.update(TABLE_PRESET_SCHEMES, values, COLUMN_PRESET_ID + " = ?", new String[]{String.valueOf(id)}); // 更新数据
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return rowsAffected; // 返回受影响的行数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除预设方案(不能删除默认方案)
|
|
|
|
|
*/
|
|
|
|
|
public int deletePresetScheme(long id) {
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
int rowsAffected = db.delete(TABLE_PRESET_SCHEMES, COLUMN_PRESET_ID + " = ? AND " + COLUMN_PRESET_IS_DEFAULT + " = 0", new String[]{String.valueOf(id)});
|
|
|
|
|
db.close();
|
|
|
|
|
return rowsAffected;
|
|
|
|
|
public int deletePresetScheme(long id) { // 删除预设方案
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase(); // 获取可写数据库
|
|
|
|
|
int rowsAffected = db.delete(TABLE_PRESET_SCHEMES, COLUMN_PRESET_ID + " = ? AND " + COLUMN_PRESET_IS_DEFAULT + " = 0", new String[]{String.valueOf(id)}); // 删除数据
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return rowsAffected; // 返回受影响的行数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据ID获取预设方案
|
|
|
|
|
*/
|
|
|
|
|
public PresetScheme getPresetSchemeById(long id) {
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
public PresetScheme getPresetSchemeById(long id) { // 根据ID获取预设方案
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase(); // 获取可读数据库
|
|
|
|
|
Cursor cursor = db.query(TABLE_PRESET_SCHEMES, null, COLUMN_PRESET_ID + " = ?",
|
|
|
|
|
new String[]{String.valueOf(id)}, null, null, null);
|
|
|
|
|
new String[]{String.valueOf(id)}, null, null, null); // 执行查询
|
|
|
|
|
|
|
|
|
|
PresetScheme scheme = null;
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
scheme = createPresetSchemeFromCursor(cursor);
|
|
|
|
|
PresetScheme scheme = null; // 创建PresetScheme对象
|
|
|
|
|
if (cursor.moveToFirst()) { // 如果游标移动到第一行
|
|
|
|
|
scheme = createPresetSchemeFromCursor(cursor); // 创建PresetScheme对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.close();
|
|
|
|
|
db.close();
|
|
|
|
|
return scheme;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
db.close(); // 关闭数据库
|
|
|
|
|
return scheme; // 返回PresetScheme对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从Cursor创建PresetScheme对象
|
|
|
|
|
*/
|
|
|
|
|
private PresetScheme createPresetSchemeFromCursor(Cursor cursor) {
|
|
|
|
|
PresetScheme scheme = new PresetScheme();
|
|
|
|
|
scheme.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_PRESET_ID)));
|
|
|
|
|
scheme.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PRESET_NAME)));
|
|
|
|
|
scheme.setDescription(cursor.getString(cursor.getColumnIndex(COLUMN_PRESET_DESCRIPTION)));
|
|
|
|
|
scheme.setTempThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_TEMP_THRESHOLD)));
|
|
|
|
|
scheme.setHumiThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_HUMI_THRESHOLD)));
|
|
|
|
|
scheme.setDistanceThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_DISTANCE_THRESHOLD)));
|
|
|
|
|
scheme.setLightThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_LIGHT_THRESHOLD)));
|
|
|
|
|
scheme.setRecordInterval(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_RECORD_INTERVAL)));
|
|
|
|
|
scheme.setAutoRecord(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_AUTO_RECORD)) == 1);
|
|
|
|
|
scheme.setCreatedTime(cursor.getString(cursor.getColumnIndex(COLUMN_PRESET_CREATED_TIME)));
|
|
|
|
|
scheme.setIsDefault(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_IS_DEFAULT)) == 1);
|
|
|
|
|
return scheme;
|
|
|
|
|
private PresetScheme createPresetSchemeFromCursor(Cursor cursor) { // 从Cursor创建PresetScheme对象
|
|
|
|
|
PresetScheme scheme = new PresetScheme(); // 创建PresetScheme对象
|
|
|
|
|
scheme.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_PRESET_ID))); // 设置ID
|
|
|
|
|
scheme.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PRESET_NAME))); // 设置名称
|
|
|
|
|
scheme.setDescription(cursor.getString(cursor.getColumnIndex(COLUMN_PRESET_DESCRIPTION))); // 设置描述
|
|
|
|
|
scheme.setTempThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_TEMP_THRESHOLD))); // 设置温度阈值
|
|
|
|
|
scheme.setHumiThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_HUMI_THRESHOLD))); // 设置湿度阈值
|
|
|
|
|
scheme.setDistanceThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_DISTANCE_THRESHOLD))); // 设置距离阈值
|
|
|
|
|
scheme.setLightThreshold(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_LIGHT_THRESHOLD))); // 设置光照阈值
|
|
|
|
|
scheme.setRecordInterval(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_RECORD_INTERVAL))); // 设置记录间隔
|
|
|
|
|
scheme.setAutoRecord(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_AUTO_RECORD)) == 1); // 设置自动记录
|
|
|
|
|
scheme.setCreatedTime(cursor.getString(cursor.getColumnIndex(COLUMN_PRESET_CREATED_TIME))); // 设置创建时间
|
|
|
|
|
scheme.setIsDefault(cursor.getInt(cursor.getColumnIndex(COLUMN_PRESET_IS_DEFAULT)) == 1); // 设置是否默认
|
|
|
|
|
return scheme; // 返回PresetScheme对象
|
|
|
|
|
}
|
|
|
|
|
}
|