135 lines
3.5 KiB
Dart
135 lines
3.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../data/manager_repository.dart';
|
|
import '../data/agent_repository.dart';
|
|
import '../data/agent_service.dart';
|
|
import 'agent_model.dart';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
class ManagerModel extends ChangeNotifier {
|
|
late final ManagerRepository repository;
|
|
late final AgentModel agents;
|
|
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);
|
|
agents = AgentModel(AgentRepository(AgentService(repository)));
|
|
}
|
|
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();
|
|
await agents.initialize(
|
|
adminFile: repository.root == null
|
|
? p.join(p.dirname(Platform.resolvedExecutable), 'admin.env')
|
|
: p.join(repository.root!.path, 'config', 'private', 'admin.env'),
|
|
server: repository.settings.server,
|
|
origin: repository.settings.origin,
|
|
);
|
|
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',
|
|
]);
|
|
});
|
|
@override
|
|
void dispose() {
|
|
agents.dispose();
|
|
timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> shutdown() async {
|
|
closing = true;
|
|
agents.dispose();
|
|
timer?.cancel();
|
|
while (pending.isNotEmpty) {
|
|
await Future<void>.delayed(const Duration(milliseconds: 50));
|
|
}
|
|
await repository.shutdown();
|
|
}
|
|
}
|