118 lines
4.3 KiB
Dart
118 lines
4.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:ads_manager/data/manager_repository.dart';
|
|
|
|
Future<void> main(List<String> args) async {
|
|
final repo = ManagerRepository(stdout.writeln);
|
|
try {
|
|
await repo.init();
|
|
repo.settings.usbMode = args.contains('--usb');
|
|
repo.settings.relayOnly = args.contains('--relay-only');
|
|
final devices = await repo.devices();
|
|
stdout.writeln(
|
|
'API devices: ${devices.map((d) => "${d.id} busy=${d.busy} computer=${d.computerName} agent=${d.agentId}").join(", ")}',
|
|
);
|
|
final nodes = await repo.get('v2/nodes') as Map<String, dynamic>;
|
|
stdout.writeln('Relay nodes: ${(nodes["nodes"] as List<dynamic>).length}');
|
|
stdout.writeln(await repo.run(repo.settings.adb, ['devices', '-l']));
|
|
if (!args.contains('--connect')) {
|
|
return;
|
|
}
|
|
final device = devices.where((d) => !d.busy).firstOrNull;
|
|
if (device == null) {
|
|
stdout.writeln('SKIP: no idle device');
|
|
return;
|
|
}
|
|
final s = await repo.connect(device.id);
|
|
s.screenPageVisible = true;
|
|
for (var i = 0; i < 20 && !s.ready && !s.exited; i++) {
|
|
await Future<void>.delayed(const Duration(seconds: 3));
|
|
await repo.monitor();
|
|
}
|
|
if (!s.ready) {
|
|
throw StateError('ADB not ready: ${s.state}');
|
|
}
|
|
stdout.writeln(
|
|
'Remote model: ${await repo.run(s.adb, ['-s', s.endpoint, 'shell', 'getprop', 'ro.product.model'])}',
|
|
);
|
|
if (args.contains('--screen')) {
|
|
await repo.screen(s, embedded: args.contains('--embedded'));
|
|
await Future<void>.delayed(const Duration(seconds: 8));
|
|
if (s.screen == null) {
|
|
throw StateError('scrcpy exited early');
|
|
}
|
|
stdout.writeln('scrcpy process alive: ${s.screen!.pid}');
|
|
}
|
|
if (args.contains('--recover')) {
|
|
final original = s.process.pid;
|
|
final oldBridge = s.usb!.bridge!.pid;
|
|
final wantedMode = s.screenEmbedded;
|
|
await repo.stop(s.process);
|
|
var recovered = false;
|
|
for (var i = 0; i < 80; i++) {
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
await repo.monitor();
|
|
if (!s.recovering &&
|
|
s.ready &&
|
|
s.process.pid != original &&
|
|
s.usb!.bridge!.pid != oldBridge &&
|
|
s.screen != null) {
|
|
recovered = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!recovered) {
|
|
throw StateError(
|
|
'USB/client/screen automatic recovery did not complete: ${s.state}',
|
|
);
|
|
}
|
|
if (s.screenEmbedded != wantedMode) {
|
|
throw StateError('Screen mode was not preserved');
|
|
}
|
|
await Future<void>.delayed(const Duration(seconds: 8));
|
|
if (s.screen == null || !s.ready) {
|
|
throw StateError('Recovered screen did not remain running');
|
|
}
|
|
stdout.writeln('RECOVERY PASS: client + USB remount + scrcpy restored');
|
|
await repo.stopScreen(s);
|
|
for (var i = 0; i < 4; i++) {
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
await repo.monitor();
|
|
}
|
|
if (s.screen != null || s.screenWanted) {
|
|
throw StateError('Manual stop restarted screen');
|
|
}
|
|
}
|
|
if (args.contains('--usb')) {
|
|
final path = await repo.run(s.adb, ['-s', s.endpoint, 'get-devpath']);
|
|
// Windows ADB may return "unknown" for get-devpath. USB-only selection
|
|
// must either select this virtual serial or report multiple USB devices.
|
|
try {
|
|
final selected = await repo.run(s.adb, ['-d', 'get-serialno']);
|
|
if (selected.trim() != s.endpoint) {
|
|
throw StateError('USB-only selection did not include virtual device');
|
|
}
|
|
} on ProcessException catch (error) {
|
|
if (!error.message.contains('more than one')) {
|
|
rethrow;
|
|
}
|
|
}
|
|
stdout.writeln('Native USB-only selection verified; get-devpath=$path');
|
|
await repo.disconnect(s.id);
|
|
await Future<void>.delayed(const Duration(seconds: 2));
|
|
final listing = await repo.run(s.adb, ['devices']);
|
|
if (listing
|
|
.split('\n')
|
|
.any((line) => line.startsWith('${s.endpoint}\t'))) {
|
|
throw StateError('USB transport was not removed');
|
|
}
|
|
if (repo.reservedPorts.isNotEmpty) {
|
|
throw StateError('Ports leaked after USB disconnect');
|
|
}
|
|
}
|
|
stdout.writeln('LIVE PASS');
|
|
} finally {
|
|
await repo.shutdown();
|
|
}
|
|
}
|