From 1922c664cb50af8822bf4d2648ace556ef5214bc Mon Sep 17 00:00:00 2001 From: wanglei Date: Fri, 4 Dec 2026 09:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=BB=93=E6=9D=9F=E5=9B=BE?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=90=8E=E4=BF=9D=E7=95=99=E5=B7=B2=E5=8F=91?= =?UTF-8?q?=E8=BD=AE=E6=AC=A1=E5=BA=8F=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../Recipes/RecipeWorkspace.cs | 40 +++++++++++++++++-- tests/RecipePackage.Smoke/Program.cs | 23 ++++++++++- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5ae282c..b76b270 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Windows DirectShow 设置分辨率时可能把 MJPG 重置为 YUY2,因此程 SOP 流程视频可在配方编辑器中录制和播放,独立保存在该配方的 `sop_stand` 目录。主界面的“播放标准 SOP”在主预览区播放当前已应用配方的视频,检测流程使用视频帧,每次播完间隔 5 秒从头重播并开始新一轮检测。播放前会停止正在进行的相机采集。配方导入导出在“运行设置”的配方行:选中配方点击“导出”,把配方定义和标准 SOP 视频打包成单个 `.bwsrecipe` 文件(zip,含 `package.json` 清单:配方名、导出时间、导出程序版本、每个文件的大小和 SHA-256)。关键帧已内联在配方 YAML 中,包内只有配方文件和 `sop_stand` 视频,不带运行图片、录像和已移除功能遗留的数据集/模型。 -每轮检测开始时,在当前配方的 `completion-images` 下创建轮次目录,例如 `2026-12-04_15-30-25-0`。每个配方的序号每天从 0 开始,程序重启后按当天已有目录续号。开启“保存结束图”的步骤通过后,将判定图片以步骤名保存到本轮目录。 +每轮检测开始时,在当前配方的 `completion-images` 下创建轮次目录,例如 `2026-12-04_15-30-25-0`。每个配方的序号每天从 0 开始,程序重启或清理轮次目录后仍按已发出的序号续号。开启“保存结束图”的步骤通过后,将判定图片以步骤名保存到本轮目录。 “导入”选择配方包后先检查:清单缺失、包被改动、大小与清单不符、格式版本高于当前程序或包含越界路径都会拒绝并给出原因。同名配方弹窗选择覆盖(现有配方整目录移入 `workspace/deleted-recipes`,可找回)或另存为 `-2` 等新名称;取消不动盘上文件。导入解压到配方目录内的暂存目录,逐文件校验通过后整目录一次移动到位,中断不会留下半个配方,也不留 `.importing-*`。导入的新配方不会自动应用,区域坐标按相机分辨率标定,需要复核后手动点击“应用”。导入导出不需要登录以外的额外权限:操作员点击只弹出权限提示,工艺师和管理员可用。 diff --git a/src/SopHelper.CrossPlatform.Engine/Recipes/RecipeWorkspace.cs b/src/SopHelper.CrossPlatform.Engine/Recipes/RecipeWorkspace.cs index 0879c67..9a96609 100644 --- a/src/SopHelper.CrossPlatform.Engine/Recipes/RecipeWorkspace.cs +++ b/src/SopHelper.CrossPlatform.Engine/Recipes/RecipeWorkspace.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Security.Cryptography; using System.Text; +using System.Text.Json; using SopHelper.Engine.Sop; namespace SopHelper.Engine.Recipes; @@ -50,14 +51,16 @@ public sealed class RecipeWorkspace(string root) return Path.Combine(RecipesDirectory, name); } - /// 在指定配方下建立轮次图片目录,按本地开始日期从零编号,并从当天现有目录续号。 + /// 在指定配方下建立轮次图片目录,按本地开始日期从零编号。移除图片目录后仍保留已发出的序号。 /// 已应用的配方名。 /// 检测轮次开始时的本地时间。 /// 新建轮次目录的完整路径。 public string CreateCompletionImageRound(string name, DateTime startedAt) { const string timestampFormat = "yyyy-MM-dd_HH-mm-ss"; - var root = Path.Combine(DirectoryFor(name), "completion-images"); + var recipeDirectory = DirectoryFor(name); + var root = Path.Combine(recipeDirectory, "completion-images"); + var sequencePath = Path.Combine(recipeDirectory, ".completion-image-sequences.json"); var lockPath = OperatingSystem.IsWindows() ? root.ToUpperInvariant() : root; var lockName = "AISOP-CompletionImages-" + Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(lockPath))); using var archiveLock = new Mutex(false, lockName); @@ -68,7 +71,7 @@ public sealed class RecipeWorkspace(string root) } catch (AbandonedMutexException) { - // 前一实例退出后同步锁已交给当前线程,从磁盘现存目录恢复序号。 + // 前一实例退出后同步锁已交给当前线程,可继续读取已提交的序号记录。 } try @@ -77,7 +80,17 @@ public sealed class RecipeWorkspace(string root) Directory.CreateDirectory(root); var date = startedAt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); var timestamp = startedAt.ToString(timestampFormat, CultureInfo.InvariantCulture); - var next = 0L; + Dictionary sequences; + try + { + using var source = File.OpenRead(sequencePath); + sequences = JsonSerializer.Deserialize>(source) + ?? throw new InvalidDataException("配方结束图序号记录为空"); + } + catch (FileNotFoundException) { sequences = []; } + + var next = sequences.TryGetValue(date, out var last) ? checked(last + 1) : 0L; + // 首次升级时用仍在磁盘上的轮次补齐历史,也兼容额外恢复的归档目录。 foreach (var directory in Directory.EnumerateDirectories(root, date + "_*")) { var entry = Path.GetFileName(directory).AsSpan(); @@ -88,12 +101,31 @@ public sealed class RecipeWorkspace(string root) next = Math.Max(next, checked(sequence + 1)); } + // 先持久化取号结果再发布目录。中途失败可能留空号,但已发出的序号不会被再次分配。 + sequences[date] = next; + SaveCompletionImageSequences(sequencePath, sequences); var path = Path.Combine(root, timestamp + "-" + next.ToString(CultureInfo.InvariantCulture)); return Directory.CreateDirectory(path).FullName; } finally { archiveLock.ReleaseMutex(); } } + /// 在同步锁内写入同目录暂存文件,再替换序号记录,避免中途失败留下半份记录。 + private static void SaveCompletionImageSequences(string path, Dictionary sequences) + { + var temporary = path + ".tmp"; + try + { + using (var target = File.Create(temporary)) + { + JsonSerializer.Serialize(target, sequences); + target.Flush(flushToDisk: true); + } + File.Move(temporary, path, overwrite: true); + } + finally { if (File.Exists(temporary)) File.Delete(temporary); } + } + /// 校验跨平台文件名限制、系统保留名及 UTF-8 文件名长度;合法时返回 null。 public static string? GetNameError(string name) { diff --git a/tests/RecipePackage.Smoke/Program.cs b/tests/RecipePackage.Smoke/Program.cs index 8eed91c..42f534b 100644 --- a/tests/RecipePackage.Smoke/Program.cs +++ b/tests/RecipePackage.Smoke/Program.cs @@ -67,6 +67,7 @@ internal static class Program AtomicYamlSave(); LegacyWorkspace(); CompletionImageRoundNumbering(); + CompletionImageRoundAfterCleanup(); RoundTrip(); OverwriteAndRename(); RejectedPackages(); @@ -89,7 +90,7 @@ internal static class Program "当天首轮使用规定的日期时间格式并从 0 开始"); var reopened = new RecipeWorkspace(workspace.Root); Check(Path.GetFileName(reopened.CreateCompletionImageRound(recipe, startedAt)) == "2026-12-04_15-30-25-1", - "重新打开工作区后,同秒启动仍从当天现有目录续号"); + "重新打开工作区后,同秒启动仍从当天已发序号续号"); var sameDay = new string[12]; Parallel.For(0, sameDay.Length, i => @@ -107,6 +108,26 @@ internal static class Program "每个配方各自按天编号"); } + /// 升级时从已有轮次接续,清理末尾目录后仍以已发出的最大序号为准。 + private static void CompletionImageRoundAfterCleanup() + { + var workspace = new RecipeWorkspace(Path.Combine(Root, "round-cleanup")); + const string recipe = "清理轮次"; + var startedAt = new DateTime(2026, 12, 4, 15, 30, 25); + var archive = Path.Combine(workspace.DirectoryFor(recipe), "completion-images"); + var oldRounds = Enumerable.Range(89, 11) + .Select(sequence => Directory.CreateDirectory(Path.Combine(archive, $"2026-12-04_15-30-24-{sequence}")).FullName) + .ToArray(); + var last = workspace.CreateCompletionImageRound(recipe, startedAt); + Check(Path.GetFileName(last) == "2026-12-04_15-30-25-100", "没有序号记录时从现有最大轮次续号"); + + foreach (var path in oldRounds.Skip(1).Append(last)) Directory.Delete(path); + Check(Directory.EnumerateDirectories(archive).Single() == oldRounds[0], "清理序号 90 到 100 后只剩 89"); + var reopened = new RecipeWorkspace(workspace.Root); + var next = reopened.CreateCompletionImageRound(recipe, startedAt.AddSeconds(1)); + Check(Path.GetFileName(next) == "2026-12-04_15-30-26-101", "删除轮次目录并重启后从 101 续号"); + } + /// 旧训练文件即使已损坏,也不应阻塞配方和标准视频迁移。 private static void LegacyWorkspace() {