fix: preserve discovery results and clarify outdated agent errors
This commit is contained in:
1 parent
e9cf55f597
commit
c568749d23
2 files changed
+11
-4
No files matched your search
@@ -13,7 +13,7 @@
|
||||
<Grid Grid.Row="1" ColumnDefinitions="300,*">
|
||||
<Border Classes="agentCard" Margin="0,0,14,0"><Grid RowDefinitions="Auto,*"><TextBlock Text="Agent / 被控电脑" FontSize="18" FontWeight="SemiBold"/><ListBox x:Name="AgentList" Grid.Row="1" ItemsSource="{Binding Agents}" SelectedItem="{Binding SelectedAgent}" Margin="0,10,0,0"><ListBox.ItemTemplate><DataTemplate><StackPanel Spacing="5" Margin="4,8"><TextBlock Text="{Binding Id}" FontSize="16"/><TextBlock Text="{Binding Description}" TextWrapping="Wrap" Foreground="#A8B7CC"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Border>
|
||||
<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="10"><TextBlock Text="{Binding SelectedAgentDetails}" 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>
|
||||
|
||||
@@ -40,6 +40,7 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
|
||||
foreach(var row in rows.Deserialize<DiscoveredDevice[]>(new JsonSerializerOptions(JsonSerializerDefaults.Web)) ?? [])DiscoveredDevices.Add(row);
|
||||
Notice=$"发现 {DiscoveredDevices.Count} 台 ADB 设备;选择未添加且状态为 device 的设备。USB 接入仍须启用 TCP adbd。";
|
||||
});
|
||||
public string SelectedAgentDetails => SelectedAgent?.Details ?? "请选择 Agent。";
|
||||
public ObservableCollection<ManagedAgent> Agents { get; } = new();
|
||||
public ObservableCollection<ManagedDevice> AgentDevices { get; } = new();
|
||||
private readonly Func<(string Server,string Origin)> server;
|
||||
@@ -89,6 +90,7 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
|
||||
partial void OnEnrollmentTokenChanged(string value){if(value.Length>0)secrets.Add(value);}
|
||||
partial void OnSelectedAgentChanged(ManagedAgent? value)
|
||||
{
|
||||
OnPropertyChanged(nameof(SelectedAgentDetails));
|
||||
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();
|
||||
@@ -140,7 +142,12 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
|
||||
await process.StandardInput.WriteAsync(JsonSerializer.Serialize(request));process.StandardInput.Close();
|
||||
try { await process.WaitForExitAsync(lifetime.Token).WaitAsync(TimeSpan.FromSeconds(20),lifetime.Token); }
|
||||
catch { if(!process.HasExited)process.Kill(entireProcessTree:true);throw; }
|
||||
if(process.ExitCode!=0)throw new InvalidOperationException((await error).Trim());
|
||||
if(process.ExitCode!=0) {
|
||||
var message=(await error).Trim();
|
||||
if(message.Contains("unknown variant") && message.Contains("devices_discover"))
|
||||
message="本机 adsctl 或 Agent 版本过旧,不支持设备发现。请同步更新两者后重启 Agent。";
|
||||
throw new InvalidOperationException(message);
|
||||
}
|
||||
using var value=JsonDocument.Parse(await output);return value.RootElement.Clone();
|
||||
}
|
||||
private Task<JsonElement> CallAsync(object request,string? agent=null) {
|
||||
@@ -159,12 +166,12 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
|
||||
}else rows=await AgentManagementApi.ListAsync(Options(),lifetime.Token);
|
||||
if(local!=LocalMode||path!=ConfigPath||source!=server())return;
|
||||
listedSource=SourceKey();
|
||||
var discovered=DiscoveredDevices.ToArray();var cursor=logCursor;var instance=logInstance;var text=RemoteLogText;
|
||||
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 discovered=DiscoveredDevices.ToArray();var cursor=logCursor;var instance=logInstance;var text=RemoteLogText;
|
||||
SelectedAgent=chosen;
|
||||
if(selected==chosen?.Id){foreach(var item in discovered)DiscoveredDevices.Add(item);logCursor=cursor;logInstance=instance;RemoteLogText=text;}
|
||||
if(selected==chosen?.Id){DiscoveredDevices.Clear();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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user