build: prepare Flutter management center for agent integration
This commit is contained in:
1 parent
05d4d5fea6
commit
e91fa0235c
34 files changed
+3481
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../data/manager_repository.dart';
|
||||
|
||||
class ManagerModel extends ChangeNotifier {
|
||||
late final ManagerRepository repository;
|
||||
List<Device> devices = [];
|
||||
List<Map<String, dynamic>> nodes = [];
|
||||
final List<String> logs = [];
|
||||
final Set<String> pending = {};
|
||||
String notice = '正在初始化', adbListing = '';
|
||||
bool refreshing = false, closing = false, polling = false;
|
||||
Timer? timer;
|
||||
int ticks = 0;
|
||||
ManagerModel() {
|
||||
repository = ManagerRepository(log);
|
||||
}
|
||||
void log(String text) {
|
||||
logs.insert(0, '${DateTime.now().toString().substring(11, 19)} $text');
|
||||
if (logs.length > 500) {
|
||||
logs.removeLast();
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> action(Future<void> Function() operation) async {
|
||||
try {
|
||||
await operation();
|
||||
} catch (e) {
|
||||
notice = e.toString();
|
||||
log(notice);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
await action(() async {
|
||||
await repository.init();
|
||||
notice = '准备就绪';
|
||||
});
|
||||
await refresh();
|
||||
timer = Timer.periodic(const Duration(seconds: 3), (_) async {
|
||||
if (closing || refreshing || polling) {
|
||||
return;
|
||||
}
|
||||
polling = true;
|
||||
try {
|
||||
await action(repository.monitor);
|
||||
} finally {
|
||||
polling = false;
|
||||
}
|
||||
if (++ticks % 4 == 0) {
|
||||
await refresh();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (refreshing || closing) {
|
||||
return;
|
||||
}
|
||||
refreshing = true;
|
||||
notifyListeners();
|
||||
await action(() async {
|
||||
devices = await repository.devices();
|
||||
final response = await repository.get('v2/nodes') as Map<String, dynamic>;
|
||||
nodes = (response['nodes'] as List<dynamic>).cast<Map<String, dynamic>>();
|
||||
notice =
|
||||
'已同步 ${devices.length} 台设备 · ${DateTime.now().toString().substring(11, 19)}';
|
||||
});
|
||||
refreshing = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> deviceAction(String id, Future<void> Function() fn) async {
|
||||
if (closing || !pending.add(id)) {
|
||||
return;
|
||||
}
|
||||
notifyListeners();
|
||||
try {
|
||||
await action(fn);
|
||||
} finally {
|
||||
pending.remove(id);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> connect(Device d) => deviceAction(d.id, () async {
|
||||
await repository.connect(d.id);
|
||||
log('已启动 ${d.id} 的连接');
|
||||
});
|
||||
Future<void> disconnect(String id) =>
|
||||
deviceAction(id, () => repository.disconnect(id));
|
||||
Future<void> localAdb() => action(() async {
|
||||
adbListing = await repository.run(repository.settings.adb, [
|
||||
'devices',
|
||||
'-l',
|
||||
]);
|
||||
});
|
||||
Future<void> shutdown() async {
|
||||
closing = true;
|
||||
timer?.cancel();
|
||||
while (pending.isNotEmpty) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
}
|
||||
await repository.shutdown();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user