以步骤背景条显示剩余检查时间并隐藏判定提示文字

This commit is contained in:
tfarcraw 2026-11-20 10:08:48 +08:00
parent c84a12beb0
commit edd3201c70
5 changed files with 125 additions and 15 deletions

View File

@ -57,6 +57,8 @@ pwsh -File tools/Publish-Avalonia.ps1
`--import-recipes 原数据根目录` 可以导入已有配方,`--check-standalone` / `--check-runtime` 检查本地手部模型和使用模拟 AI 的步骤门控,`--check-gpu` 额外要求手部推理使用 GPU;这些本地诊断不代表真实 D3 接口联调成功。
主界面步骤卡片以背景条显示已触发检查窗口的剩余时间,背景随剩余时间递减;确认完成后变绿,异常跳过后变红,不显示步骤名后的判定提示。
主界面每个步骤显示正常次数和异常次数:正常完成计入正常,异常跳过计入异常;按配方保存在 `workspace/step-counts.json`,复位、停止、重新应用配方或重启软件均保留;仅点击“重置计数”按钮,才会清零当前已应用配方的计数,不影响步骤进度及其他配方。
## 验证

View File

@ -100,21 +100,28 @@
<ItemsControl ItemsSource="{Binding StepCards}" Margin="0,8,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border x:Name="card" Padding="8,6" Margin="0,0,0,6" BorderBrush="{StaticResource Line}" BorderThickness="1" Classes.state5="{Binding IsCurrent}" Classes.state6="{Binding IsCurrent}" Classes.completed="{Binding IsMet}" Classes.skipped="{Binding IsSkipped}" Classes="panel stepCard">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="28" FontWeight="Bold" Foreground="{StaticResource FgDim}" Text="{Binding StepId, StringFormat={}{0}.}" />
<TextBlock Width="140" Text="{Binding Name}" />
<TextBlock Text="{Binding Message}" Foreground="{StaticResource FgDim}" TextTrimming="CharacterEllipsis" />
<TextBlock Text="已跳过/未通过" Foreground="#D97706" IsVisible="{Binding IsSkipped}" Margin="8,0,0,0" />
<Rectangle x:Name="dot" Width="8" Height="8" Margin="8,0,0,0" VerticalAlignment="Center" Fill="{StaticResource Ok}" Classes.state4="{Binding IsMet}" IsVisible="False" />
<Border x:Name="card" Padding="0" Margin="0,0,0,6" BorderBrush="{StaticResource Line}" BorderThickness="1" Classes.state5="{Binding IsCurrent}" Classes.state6="{Binding IsCurrent}" Classes.completed="{Binding IsMet}" Classes.skipped="{Binding IsSkipped}" Classes="panel stepCard">
<Grid ClipToBounds="True">
<Grid IsVisible="{Binding ShowRemainingCheckTime}" IsHitTestVisible="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding RemainingCheckWidth}" />
<ColumnDefinition Width="{Binding ElapsedCheckWidth}" />
</Grid.ColumnDefinitions>
<Border x:Name="RemainingCheckFill" Background="#244F65" CornerRadius="4" />
</Grid>
<StackPanel Margin="8,6">
<Grid ColumnDefinitions="28,*,Auto">
<TextBlock FontWeight="Bold" Foreground="{StaticResource FgDim}" Text="{Binding StepId, StringFormat={}{0}.}" />
<TextBlock Grid.Column="1" Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
<Rectangle Grid.Column="2" x:Name="dot" Width="8" Height="8" Margin="8,0,0,0" VerticalAlignment="Center" Fill="{StaticResource Ok}" Classes.state4="{Binding IsMet}" IsVisible="False" />
</Grid>
<WrapPanel Margin="28,3,0,0">
<TextBlock Text="{Binding ElapsedText, StringFormat=用时:{0}}" Foreground="{StaticResource FgDim}" FontSize="12" />
<TextBlock Text="{Binding NormalCount, StringFormat=正常次数:{0}}" FontSize="12" Margin="12,0,0,0" />
<TextBlock Text="{Binding AbnormalCount, StringFormat=异常次数:{0}}" FontSize="12" Margin="12,0,0,0" />
</WrapPanel>
</StackPanel>
<WrapPanel Margin="28,3,0,0">
<TextBlock Text="{Binding ElapsedText, StringFormat=用时:{0}}" Foreground="{StaticResource FgDim}" FontSize="12" />
<TextBlock Text="{Binding NormalCount, StringFormat=正常次数:{0}}" FontSize="12" Margin="12,0,0,0" />
<TextBlock Text="{Binding AbnormalCount, StringFormat=异常次数:{0}}" FontSize="12" Margin="12,0,0,0" />
</WrapPanel>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>

View File

@ -917,7 +917,11 @@ public class SopPageViewModel : ViewModelBase
AverageDurationText = FormatDuration(statistics.AverageDuration);
StandardDurationText = FormatDuration(!string.IsNullOrWhiteSpace(recipe) && Recipes.Contains(recipe)
? SopHelper.Engine.Training.SopStandardVideoDuration.Get(DesktopPlatform.Recipes.DirectoryFor(name)) : TimeSpan.Zero);
foreach (var stepCard in StepCards) UpdateStepCounts(stepCard, _engine.Statistics);
foreach (var stepCard in StepCards)
{
UpdateStepCounts(stepCard, _engine.Statistics);
UpdateCheckTime(stepCard);
}
if (IsRunning && _engine.CurrentStep is { } step && StepCards.FirstOrDefault(c => c.StepId == step.Id) is { } card)
card.ElapsedText = StepElapsedText;
}
@ -1049,6 +1053,13 @@ public class SopPageViewModel : ViewModelBase
card.AbnormalCount = counts.AbnormalCount;
}
private void UpdateCheckTime(StepCard card)
{
var progress = _engine.GetCheckWindowProgress(card.StepId);
card.ShowRemainingCheckTime = IsRunning && card.IsCurrent && !card.IsMet && !card.IsSkipped && progress.HasValue;
card.RemainingCheckFraction = progress is { } value ? value.RemainingMs / value.TotalMs : 0;
}
private void UpdateCard(StepSnapshot snapshot)
{
var card = StepCards.FirstOrDefault(c => c.StepId == snapshot.StepId);
@ -1058,6 +1069,7 @@ public class SopPageViewModel : ViewModelBase
card.IsCurrent = snapshot.IsCurrent;
card.IsMet = snapshot.IsMet;
card.IsSkipped = snapshot.IsSkipped;
UpdateCheckTime(card);
card.Message = snapshot.Message;
if (snapshot.Elapsed is { } elapsed) card.ElapsedText = FormatDuration(elapsed);
using var aiEvidence = snapshot.IsMet && snapshot.EvidenceJpeg is { } jpeg
@ -1122,6 +1134,21 @@ public class SopPageViewModel : ViewModelBase
/// </summary>
public class StepCard : ViewModelBase
{
private bool _showRemainingCheckTime;
private double _remainingCheckFraction;
public bool ShowRemainingCheckTime { get => _showRemainingCheckTime; set => SetProperty(ref _showRemainingCheckTime, value); }
public double RemainingCheckFraction
{
get => _remainingCheckFraction;
set
{
if (!SetProperty(ref _remainingCheckFraction, Math.Clamp(value, 0, 1))) return;
RaisePropertyChanged(nameof(RemainingCheckWidth));
RaisePropertyChanged(nameof(ElapsedCheckWidth));
}
}
public Avalonia.Controls.GridLength RemainingCheckWidth => new(RemainingCheckFraction, Avalonia.Controls.GridUnitType.Star);
public Avalonia.Controls.GridLength ElapsedCheckWidth => new(1 - RemainingCheckFraction, Avalonia.Controls.GridUnitType.Star);
private long _normalCount, _abnormalCount;
public long NormalCount { get => _normalCount; set => SetProperty(ref _normalCount, value); }
public long AbnormalCount { get => _abnormalCount; set => SetProperty(ref _abnormalCount, value); }

View File

@ -2,8 +2,20 @@
namespace SopHelper.Engine.Sop;
public readonly record struct StepCheckWindowProgress(double RemainingMs, double TotalMs);
public partial class SopEngine
{
/// <summary>读取当前步骤真实检查窗口的剩余时间,不触发检查或改变重试规则。</summary>
public StepCheckWindowProgress? GetCheckWindowProgress(int stepId)
{
if (State != EngineState.Running || StepAt(_index)?.Id != stepId
|| !_continuousChecks.TryGetValue(_index, out var state) || !state.BudgetStarted
|| state.Step.AiConfirmation?.CheckWindowMs is not > 0) return null;
var total = state.Step.AiConfirmation.CheckWindowMs;
return new(Math.Clamp(total - state.ElapsedMs, 0, total), total);
}
private const int HandSettleMs = 100;
private readonly Dictionary<int, ContinuousCheck> _continuousChecks = [];
public event Action<int, IReadOnlyList<VisionEvidenceFrame>>? DecisionCandidatesObserved;

View File

@ -87,6 +87,8 @@ internal static class Smoke
var userCameraBefore=File.Exists(userCameraPath)?File.ReadAllBytes(userCameraPath):null;
await Task.Delay(150);
var app=(App)Application.Current!;var window=lifetime.MainWindow!;
if(Environment.GetCommandLineArgs().Contains("--check-progress-only"))
{ await CheckRemainingWindowProgress(app,window);_exit=0;return; }
if(Environment.GetCommandLineArgs().Contains("--standard-sop-only"))
{ await CheckStandardSopPlayback(app,window,lifetime);_exit=0;return; }
if(Environment.GetCommandLineArgs().Contains("--step-counts-only"))
@ -664,6 +666,66 @@ internal static class Smoke
}
finally{sop.IsRunning=false;sop.ApplyRecipeCommand.Execute();}
}
private static async Task CheckRemainingWindowProgress(App app,Window window)
{
var model=app.Container.Resolve<SopPageViewModel>();
var engine=app.Container.Resolve<SopHelper.Engine.Sop.SopEngine>();
var store=new RecipeWorkspace(Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT")!);
const string name="剩余检查时间测试";
var config=new SopHelper.Engine.Sop.SopConfig();config.Sop.Name=name;
for(var id=1;id<=3;id++)config.Sop.Steps.Add(new(){Id=id,Name="步骤"+id,Description="零件到位",SaveCompletionImage=false,
Detection=new(){Method="time_elapsed",DurationMs=1,StableFrames=1},
AiConfirmation=new(){ImageMode="full_frame",CheckWindowMs=10000,RetryDelayMs=50}});
Directory.CreateDirectory(store.DirectoryFor(name));SopHelper.Engine.Sop.SopConfigLoader.Save(config,store.ConfigPath(name));
model.Recipes.Add(name+".yaml");model.SelectedRecipe=name+".yaml";model.ApplyRecipeCommand.Execute();
engine.Config.Sop.Settings.AutoResetOnComplete=false;
var client=new UiVisionDecision();engine.VisionDecisionClient=client;
var receive=typeof(SopPageViewModel).GetMethod("OnResultFrame",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance)!;
var frameId=0;
async Task Emit(double seconds)
{
using var frame=new Mat(48,64,MatType.CV_8UC3,new Scalar(30,60,90));
receive.Invoke(model,[new SopHelper.Engine.Common.FrameResult{FrameId=frameId++,SourceSeconds=seconds,
Timestamp=DateTime.UnixEpoch.AddSeconds(seconds),Size=(64,48)},frame]);
await Task.Delay(20);
}
model.IsRunning=true;engine.Start();
Check(engine.GetCheckWindowProgress(1) is null&&model.StepCards.All(c=>!c.ShowRemainingCheckTime),"No remaining-time background appears before the check window starts");
for(var i=0;i<10&&!model.StepCards[0].ShowRemainingCheckTime;i++)await Emit(0);
Check(model.StepCards[0].ShowRemainingCheckTime&&model.StepCards[0].RemainingCheckFraction>.95,"Triggered window starts with a full remaining-time background");
var before=model.StepCards[0].RemainingCheckFraction;await Task.Delay(150);
Check(model.StepCards[0].RemainingCheckFraction<before,"Remaining-time background refreshes even between incoming frames");
await Emit(5);
Check(Math.Abs(model.StepCards[0].RemainingCheckFraction-.5)<.02&&engine.GetCheckWindowProgress(1) is {TotalMs:10000},"Background tracks the engine's actual remaining check time");
Check(!model.StepCards[1].ShowRemainingCheckTime&&engine.GetCheckWindowProgress(2) is null,"Future steps do not consume the current step's window");
var view=window.GetVisualDescendants().OfType<SopPage>().Single();window.WindowState=WindowState.Normal;window.Width=1180;window.Height=820;window.UpdateLayout();
var card=view.GetVisualDescendants().OfType<Border>().First(b=>b.Name=="card");
var fill=card.GetVisualDescendants().OfType<Border>().Single(b=>b.Name=="RemainingCheckFill");
Check(fill.IsEffectivelyVisible&&fill.Bounds.Width>card.Bounds.Width*.4&&fill.Bounds.Width<card.Bounds.Width*.6
&&fill.Bounds.Height>card.Bounds.Height-6,"Remaining-time fill spans the card background with proportional width");
var cardTexts=card.GetVisualDescendants().OfType<TextBlock>().Select(t=>t.Text).ToArray();
Check(!cardTexts.Contains(model.StepCards[0].Message)&&!cardTexts.Contains("已跳过/未通过")&&cardTexts.Contains("步骤1"),"Step names are displayed without decision hints or skip text");
await Emit(12);
Check(model.StepCards[0].IsSkipped&&!model.StepCards[0].ShowRemainingCheckTime&&model.StepCards[0].AbnormalCount==1,"Window expiry still skips the step and replaces the background with its abnormal state");
client=new UiVisionDecision();engine.VisionDecisionClient=client;
for(var i=0;i<10&&!client.Called.Task.IsCompleted;i++)await Emit(12.1);
Check(client.Called.Task.IsCompleted,"The following step keeps the normal retry-and-check workflow");
client.Result.SetResult(new(true,.99,"确认完成"));
for(var i=0;i<10&&engine.CurrentStep?.Id==2;i++)await Emit(12.2);
Check(model.StepCards[1].IsMet&&!model.StepCards[1].ShowRemainingCheckTime&&model.StepCards[1].NormalCount==1,"Confirmed steps keep their completed background and normal count");
engine.VisionDecisionClient=new UiVisionDecision();
for(var i=0;i<10&&!model.StepCards[2].ShowRemainingCheckTime;i++)await Emit(12.3);
await Emit(17.3);await Task.Delay(40);window.UpdateLayout();
var screenshots=Path.GetFullPath(Path.Combine("..","scratch","screenshots"));Directory.CreateDirectory(screenshots);
using(var rendered=new RenderTargetBitmap(new PixelSize((int)window.Bounds.Width,(int)window.Bounds.Height)))
{rendered.Render(window);rendered.Save(Path.Combine(screenshots,"remaining-check-background.png"));}
engine.Stop();model.IsRunning=false;await Task.Delay(120);
Check(model.StepCards.All(c=>!c.ShowRemainingCheckTime),"Stopping clears active progress without leaving a stale time background");
engine.Reset();await Task.Delay(30);
Check(model.StepCards.All(c=>!c.ShowRemainingCheckTime)&&model.StepCards[0].AbnormalCount==1&&model.StepCards[1].NormalCount==1,"Reset clears progress while preserving accumulated results");
engine.Stop();
}
private static async Task CheckStandardSopPlayback(App app,Window window,IClassicDesktopStyleApplicationLifetime lifetime)
{
var model=app.Container.Resolve<SopPageViewModel>();