启动时自动应用上次选择的配方并提示加载失败

This commit is contained in:
tfarcraw 2026-11-20 09:33:08 +08:00
parent 0acfdd92cc
commit f0574a2e3c
3 changed files with 27 additions and 5 deletions

View File

@ -19,7 +19,7 @@ pwsh -File tools/Start-HandAiD3.ps1
## 操作流程
1. 在“运行设置”中选择 USB(UVC)相机,新建或选择配方。抽帧、完成后自动复位和复位延时保存在独立的 `workspace/runtime-settings.json`,下次开始运行时生效,切换配方不会覆盖。
1. 软件启动时自动应用上次选中的配方(不可用时应用列表中的可用配方),加载步骤和区域,等待手动开始采集。在“运行设置”中选择 USB(UVC)相机,新建或选择配方。抽帧、完成后自动复位和复位延时保存在独立的 `workspace/runtime-settings.json`,下次开始运行时生效,切换配方不会覆盖。
2. 配方编辑器包含“步骤、SOP流程视频、Agent助手”三页。区域导航和参数位于步骤页播放器下方,暂停视频后可直接拖动画框;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和自动曝光使用下拉菜单选择,关闭自动曝光后可填写以毫秒为单位的手动曝光时间(留空保留当前时间)。USB 采集统一使用 OpenCV:Windows 自动使用设备可用的兼容接口(DirectShow/MSMF),枚举结果记录内部接口标记,保证连接时沿用同一设备索引体系;Linux 使用 V4L2,无需选择采集方式。曝光控制分别适配 Windows 的对数档位及 V4L2 的原始控制值,Windows 会匹配最接近的曝光档位;设备不支持或控制失败时静默跳过。已移除 Windows 专属驱动属性页,旧设置中的采集方式及补偿字段会被忽略。设置按设备保存在 workspace/camera-device-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读采集模式,状态栏分别显示 USB 实测采集和渲染 fps。
3. 步骤名描述操作,“说明”填写送检单帧能看到的静态完成状态,AI 用说明判断图片。触发规则支持手在区域内、手进入后离开区域、A→B 移动和持续计时。
4. 送检可选整张画面、固定区域或跟随手部;跟随手部可以设置扩展距离、最小范围和位置保留时间。

View File

@ -157,6 +157,12 @@ public class SopPageViewModel : ViewModelBase
_statisticsTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
_statisticsTimer.Tick += (_, _) => RefreshStatistics();
_statisticsTimer.Start();
try { ApplyRecipe(); }
catch (Exception error)
{
StatusText = "自动应用配方失败,请检查配方后重试";
Log("warn", "自动应用配方失败:" + error.Message);
}
RefreshStatistics();
}

View File

@ -87,6 +87,16 @@ 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("--startup-recipe-only"))
{
var model=app.Container.Resolve<SopPageViewModel>();
Check(model.SelectedRecipe is not null&&model.ActiveRecipe==model.SelectedRecipe&&model.StepCards.Count>0,
"Startup applies the selected recipe and populates step cards");
Check(!model.IsRunning&&!Camera.IsConnected&&!Camera.IsGrabbing&&Camera.ConnectCount==0,
"Startup application never connects or starts the camera");
CheckRecipePreferences(app);
_exit=0;return;
}
if(Environment.GetCommandLineArgs().Contains("--time-display-only"))
{
foreach(var (duration,expected) in new[]{(TimeSpan.Zero,"00:00:00"),(TimeSpan.FromMilliseconds(999),"00:00:00"),
@ -221,7 +231,7 @@ internal static class Smoke
if(_visionOnly){await CheckVisionConfirmation(app,sop);_exit=0;return;}
var sopView=window.GetVisualDescendants().OfType<SopPage>().Single();
Check(!sopView.GetVisualDescendants().Any(c=>c is ComboBox or Slider),"Operator page contains no camera, recipe or parameter editors");
Check(sop.ActiveRecipe=="未应用配方","No recipe is reported active before applying");
Check(sop.ActiveRecipe==sop.SelectedRecipe&&!sop.IsRunning,"Startup applies the selected recipe without starting acquisition");
Check(!string.IsNullOrWhiteSpace(sop.SelectedRecipe)&&sopView.GetVisualDescendants().OfType<TextBlock>().Any(t=>t.Text==sop.SelectedRecipe),"Operator page displays the selected recipe before applying or starting");
navigation.SelectedIndex=1;await Task.Delay(100);
var settings=window.GetVisualDescendants().OfType<SopHelper.VVMs.RuntimeSettings.RuntimeSettings>().Single();
@ -494,6 +504,8 @@ internal static class Smoke
var file=name+".yaml";var directory=store.DirectoryFor(name);
Directory.CreateDirectory(directory);
var config=new SopHelper.Engine.Sop.SopConfig();config.Sop.Name=name;
config.Sop.Regions["work"]=new(){X2=64,Y2=48};
config.Sop.Steps.Add(new(){Id=1,Name="启动步骤",Detection=new(){Method="object_present",TargetObject="part"}});
SopHelper.Engine.Sop.SopConfigLoader.Save(config,store.ConfigPath(name));
void WithVm(Action<SopPageViewModel> check)
{
@ -512,11 +524,15 @@ internal static class Smoke
vm.SelectedRecipe=null;vm.SelectedRecipe="not-a-recipe.yaml";
Check(JsonSerializer.Deserialize<string>(File.ReadAllText(path))==file,"Transient empty and invalid selections do not overwrite the saved recipe");
});
WithVm(vm=>Check(vm.SelectedRecipe==file&&vm.ActiveRecipe=="未应用配方"&&!vm.IsRunning,"Restart restores the last selected recipe without applying it or starting acquisition"));
WithVm(vm=>Check(vm.SelectedRecipe==file&&vm.ActiveRecipe==file&&!vm.IsRunning
&&vm.StepCards.Single().Name=="启动步骤"&&vm.Rois.Single().Name=="work","Restart restores and applies the preferred recipe including steps and regions without acquisition"));
File.WriteAllText(store.ConfigPath(name),"sop: [invalid yaml");
WithVm(vm=>Check(vm.SelectedRecipe==file&&vm.ActiveRecipe=="未应用配方"&&!vm.IsRunning
&&vm.StatusText.Contains("自动应用配方失败"),"An invalid startup recipe reports its application failure without crashing or acquiring"));
File.Delete(store.ConfigPath(name));
WithVm(vm=>Check(vm.SelectedRecipe is not null&&vm.Recipes.Contains(vm.SelectedRecipe)&&vm.SelectedRecipe!=file,"A deleted preferred recipe falls back to an available recipe"));
WithVm(vm=>Check(vm.SelectedRecipe is not null&&vm.Recipes.Contains(vm.SelectedRecipe)&&vm.SelectedRecipe!=file&&vm.ActiveRecipe==vm.SelectedRecipe&&!vm.IsRunning,"A deleted preferred recipe falls back to and applies an available recipe"));
File.WriteAllText(path,"{invalid-json");
WithVm(vm=>Check(vm.SelectedRecipe is not null&&vm.Recipes.Contains(vm.SelectedRecipe),"Corrupt recipe preference does not prevent startup"));
WithVm(vm=>Check(vm.SelectedRecipe is not null&&vm.Recipes.Contains(vm.SelectedRecipe)&&vm.ActiveRecipe==vm.SelectedRecipe,"Corrupt recipe preference still applies an available recipe"));
}
finally
{