feat(device): 添加USB设备通信支持和程序参数优化

- 在AndroidManifest.xml中添加USB Host权限和设备过滤器配置
- 新增设备控制国际化词条包括速度档位、吹气时间等
- 重构数据库结构将速度相关字段统一为档位数值存储
- 添加通用KV存储方法用于settings表数据读写
- 优化首页导航实现tab间跳转和状态保持功能
- 更新程序详情页面布局和参数表单界面
- 移除模拟运行器相关测试代码
- 添加USB串口通信依赖包usb_serial
This commit is contained in:
Developer
2026-06-04 15:13:36 +08:00
parent 67e2c7c76c
commit d53c41c300
24 changed files with 795 additions and 635 deletions

View File

@@ -20,7 +20,7 @@ class DatabaseService {
return await openDatabase(
path,
version: 2,
version: 3,
onCreate: _onCreate,
onUpgrade: _onUpgrade,
);
@@ -34,7 +34,9 @@ class DatabaseService {
code TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
created_at TEXT NOT NULL,
status INTEGER DEFAULT 1
status INTEGER DEFAULT 1,
temperature INTEGER DEFAULT 50,
airflow_time INTEGER DEFAULT 60
)
''');
@@ -49,10 +51,8 @@ class DatabaseService {
mix_time INTEGER DEFAULT 0,
magnet_time INTEGER DEFAULT 0,
volume INTEGER DEFAULT 0,
mix_speed TEXT DEFAULT '中速',
blow_speed TEXT DEFAULT '中速',
blow_time INTEGER DEFAULT 0,
needle_speed INTEGER DEFAULT 5,
speed INTEGER DEFAULT 5,
FOREIGN KEY (program_id) REFERENCES programs(id) ON DELETE CASCADE
)
''');
@@ -82,6 +82,15 @@ class DatabaseService {
// 初始化默认密码
await db.insert('settings', {'key': 'password', 'value': '123456'});
}
if (oldVersion < 3) {
// v3: 重构字段
// programs 增加 temperature, airflow_time
await db.execute('ALTER TABLE programs ADD COLUMN temperature INTEGER DEFAULT 50');
await db.execute('ALTER TABLE programs ADD COLUMN airflow_time INTEGER DEFAULT 60');
// steps 删除 mix_speed, blow_speed, needle_speed增加 speed
// sqflite 不支持 DROP COLUMN旧字段保留但不再使用
await db.execute('ALTER TABLE steps ADD COLUMN speed INTEGER DEFAULT 5');
}
}
Future<void> close() async {
@@ -91,6 +100,30 @@ class DatabaseService {
}
}
/// 通用 KV 读:读取 settings 表中 [key] 对应的 value不存在返回 null
Future<String?> readSetting(String key) async {
final db = await database;
final rows = await db.query(
'settings',
columns: ['value'],
where: 'key = ?',
whereArgs: [key],
limit: 1,
);
if (rows.isEmpty) return null;
return rows.first['value'] as String?;
}
/// 通用 KV 写:以 INSERT OR REPLACE 写入 settings 表
Future<void> writeSetting(String key, String value) async {
final db = await database;
await db.insert(
'settings',
{'key': key, 'value': value},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
/// 初始化测试数据(仅调试模式使用)
Future<void> initTestData() async {
final db = await database;
@@ -103,11 +136,11 @@ class DatabaseService {
// 插入测试程序并添加步骤
final testPrograms = [
{'code': 'P001', 'name': '标准检测程序', 'created_at': '2026-05-19', 'status': 1},
{'code': 'P002', 'name': '快速检测程序', 'created_at': '2026-05-18', 'status': 1},
{'code': 'P003', 'name': '深度检测程序', 'created_at': '2026-05-17', 'status': 1},
{'code': 'P004', 'name': '样本预处理程序', 'created_at': '2026-05-16', 'status': 0},
{'code': 'P005', 'name': '磁珠分离程序', 'created_at': '2026-05-15', 'status': 1},
{'code': 'P001', 'name': '标准检测程序', 'created_at': '2026-05-19', 'status': 1, 'temperature': 50, 'airflow_time': 60},
{'code': 'P002', 'name': '快速检测程序', 'created_at': '2026-05-18', 'status': 1, 'temperature': 45, 'airflow_time': 30},
{'code': 'P003', 'name': '深度检测程序', 'created_at': '2026-05-17', 'status': 1, 'temperature': 60, 'airflow_time': 90},
{'code': 'P004', 'name': '样本预处理程序', 'created_at': '2026-05-16', 'status': 0, 'temperature': 50, 'airflow_time': 60},
{'code': 'P005', 'name': '磁珠分离程序', 'created_at': '2026-05-15', 'status': 1, 'temperature': 55, 'airflow_time': 60},
];
for (final program in testPrograms) {
@@ -123,10 +156,8 @@ class DatabaseService {
'mix_time': 60,
'magnet_time': 0,
'volume': 100,
'mix_speed': '中速',
'blow_speed': '中速',
'blow_time': 0,
'needle_speed': 5,
'speed': 5,
},
{
'program_id': programId,
@@ -136,10 +167,8 @@ class DatabaseService {
'mix_time': 0,
'magnet_time': 30,
'volume': 0,
'mix_speed': '中速',
'blow_speed': '中速',
'blow_time': 0,
'needle_speed': 5,
'speed': 5,
},
{
'program_id': programId,
@@ -149,10 +178,8 @@ class DatabaseService {
'mix_time': 0,
'magnet_time': 0,
'volume': 0,
'mix_speed': '中速',
'blow_speed': '高速',
'blow_time': 10,
'needle_speed': 8,
'speed': 8,
},
];