feat: discover ADB devices from agent management

This commit is contained in:
tongtong committed 2026-11-12 22:30:05 +08:00
1 parent 8ecfe880cc
commit 3a7bb071f0
12 files changed
+132 -5

No files matched your search

+3
View File
@@ -3,6 +3,9 @@ using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Ads.Desktop;
public sealed record DiscoveredDevice(string Serial,string State,string Details,bool Configured) {
public string Description=>$"{Serial} · {State} · {(Configured ? "已添加" : "未添加")} · {Details}";
}
public sealed record ManagedDevice(
string Id, string? Target, [property:JsonPropertyName("adb_serial")] string? AdbSerial,
[property:JsonPropertyName("adb_server")] string? AdbServer,
@@ -15,6 +15,8 @@
<ScrollViewer Grid.Column="1"><StackPanel Spacing="14">
<Border Classes="agentCard"><StackPanel Spacing="10"><TextBlock Text="{Binding SelectedAgent.Details}" TextWrapping="Wrap" Foreground="#66D9C0"/><WrapPanel><Button x:Name="LoadAgentConfigButton" Content="读取配置" Command="{Binding LoadConfigCommand}"/><Button Content="重启 Agent" Command="{Binding RestartAgentCommand}"/></WrapPanel><TextBlock Text="重启由监督进程执行,设备连接会短暂中断。" FontSize="12" Foreground="#A8B7CC"/></StackPanel></Border>
<Border Classes="agentCard"><StackPanel Spacing="8">
<Button Content="发现 ADB 设备" Command="{Binding DiscoverDevicesCommand}" HorizontalAlignment="Left"/>
<ListBox ItemsSource="{Binding DiscoveredDevices}" SelectedItem="{Binding SelectedDiscoveredDevice}" MaxHeight="160"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Description}" TextWrapping="Wrap"/></DataTemplate></ListBox.ItemTemplate></ListBox>
<TextBlock Text="手机管理" FontSize="18" FontWeight="SemiBold"/>
<ListBox x:Name="ManagedDevicesList" ItemsSource="{Binding AgentDevices}" SelectedItem="{Binding SelectedManagedDevice}" MinHeight="70" MaxHeight="180"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Description}" TextWrapping="Wrap" Margin="3,7"/></DataTemplate></ListBox.ItemTemplate></ListBox>
<WrapPanel><Button Content="启用" Command="{Binding EnableDeviceCommand}"/><Button Content="停用" Command="{Binding DisableDeviceCommand}"/><Button Content="移除配置" Command="{Binding RemoveDeviceCommand}"/></WrapPanel>
@@ -24,6 +24,22 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
[ObservableProperty] private bool isBusy;
[ObservableProperty] private bool autoRefresh = true;
[ObservableProperty] private bool followLogs;
public ObservableCollection<DiscoveredDevice> DiscoveredDevices { get; } = new();
[ObservableProperty] private DiscoveredDevice? selectedDiscoveredDevice;
partial void OnSelectedDiscoveredDeviceChanged(DiscoveredDevice? value) {
if(value is null)return;
if(value.Configured || value.State!="device"){Notice="该设备已添加或尚未授权/离线。";return;}
DeviceId="phone-"+Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes((SelectedAgent?.Id ?? "")+":"+value.Serial)))[..12].ToLowerInvariant();
var tcp=System.Net.IPEndPoint.TryParse(value.Serial,out _);
Target=tcp?value.Serial:"";AdbSerial=tcp?"":value.Serial;
Notice=value.Configured?"该设备已添加。":"已填入添加表单;确认设备 ID 后点击添加手机。";
}
[RelayCommand(CanExecute=nameof(CanManage))] private Task DiscoverDevicesAsync()=>Guard(async()=>{
var rows=await CallAsync(new{op="devices_discover"});
DiscoveredDevices.Clear();SelectedDiscoveredDevice=null;
foreach(var row in rows.Deserialize<DiscoveredDevice[]>(new JsonSerializerOptions(JsonSerializerDefaults.Web)) ?? [])DiscoveredDevices.Add(row);
Notice=$"发现 {DiscoveredDevices.Count} 台 ADB 设备;选择未添加且状态为 device 的设备。USB 接入仍须启用 TCP adbd。";
});
public ObservableCollection<ManagedAgent> Agents { get; } = new();
public ObservableCollection<ManagedDevice> AgentDevices { get; } = new();
private readonly Func<(string Server,string Origin)> server;
@@ -61,7 +77,7 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
var selected=SelectedManagedDevice?.Id; AgentDevices.Clear();
foreach(var device in value?.Status.Devices ?? [])AgentDevices.Add(device);
SelectedManagedDevice=AgentDevices.FirstOrDefault(d=>d.Id==selected) ?? AgentDevices.FirstOrDefault();
logCursor=0;logInstance="";RemoteLogText="";UpdateCommands();
DiscoveredDevices.Clear();SelectedDiscoveredDevice=null;logCursor=0;logInstance="";RemoteLogText="";UpdateCommands();
}
partial void OnSelectedManagedDeviceChanged(ManagedDevice? value)=>UpdateCommands();
partial void OnIsBusyChanged(bool value)=>UpdateCommands();
@@ -72,7 +88,7 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
private bool CanLocal()=>!IsBusy && LocalMode;
private void UpdateCommands()
{
LoadConfigCommand.NotifyCanExecuteChanged();SaveConfigCommand.NotifyCanExecuteChanged();AddDeviceCommand.NotifyCanExecuteChanged();
DiscoverDevicesCommand.NotifyCanExecuteChanged();LoadConfigCommand.NotifyCanExecuteChanged();SaveConfigCommand.NotifyCanExecuteChanged();AddDeviceCommand.NotifyCanExecuteChanged();
RemoveDeviceCommand.NotifyCanExecuteChanged();EnableDeviceCommand.NotifyCanExecuteChanged();DisableDeviceCommand.NotifyCanExecuteChanged();
ReadLogsCommand.NotifyCanExecuteChanged();RestartAgentCommand.NotifyCanExecuteChanged();
InitializeLocalCommand.NotifyCanExecuteChanged();StartLocalCommand.NotifyCanExecuteChanged();InstallServiceCommand.NotifyCanExecuteChanged();
@@ -131,9 +147,9 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
Agents.Clear();foreach(var row in rows)Agents.Add(row);
var chosen=Agents.FirstOrDefault(r=>r.Id==selected) ?? Agents.FirstOrDefault(r=>r.Online) ?? Agents.FirstOrDefault();
// Preserve log cursor and draft configuration during periodic metadata refreshes.
var cursor=logCursor;var instance=logInstance;var text=RemoteLogText;
var discovered=DiscoveredDevices.ToArray();var cursor=logCursor;var instance=logInstance;var text=RemoteLogText;
SelectedAgent=chosen;
if(selected==chosen?.Id){logCursor=cursor;logInstance=instance;RemoteLogText=text;}
if(selected==chosen?.Id){foreach(var item in discovered)DiscoveredDevices.Add(item);logCursor=cursor;logInstance=instance;RemoteLogText=text;}
SelectedManagedDevice=AgentDevices.FirstOrDefault(d=>d.Id==selectedDevice) ?? AgentDevices.FirstOrDefault();
Notice=$"{rows.Length} 个 Agent · {rows.Count(r=>r.Online)} 个在线";lastError=null;
}