结束图轮次目录按日期和每日序号命名
This commit is contained in:
parent
f107a3fdca
commit
a24c1eca1c
@ -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` 下创建轮次目录。开启“保存结束图”的步骤通过后,将判定图片以步骤名保存到本轮目录。
|
||||
每轮检测开始时,在当前配方的 `completion-images` 下创建轮次目录,例如 `2026-12-04_15-30-25-0`。每个配方的序号每天从 0 开始,程序重启后按当天已有目录续号。开启“保存结束图”的步骤通过后,将判定图片以步骤名保存到本轮目录。
|
||||
|
||||
“导入”选择配方包后先检查:清单缺失、包被改动、大小与清单不符、格式版本高于当前程序或包含越界路径都会拒绝并给出原因。同名配方弹窗选择覆盖(现有配方整目录移入 `workspace/deleted-recipes`,可找回)或另存为 `-2` 等新名称;取消不动盘上文件。导入解压到配方目录内的暂存目录,逐文件校验通过后整目录一次移动到位,中断不会留下半个配方,也不留 `.importing-*`。导入的新配方不会自动应用,区域坐标按相机分辨率标定,需要复核后手动点击“应用”。导入导出不需要登录以外的额外权限:操作员点击只弹出权限提示,工艺师和管理员可用。
|
||||
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Avalonia.Media;
|
||||
@ -1171,14 +1170,8 @@ public class SopPageViewModel : ViewModelBase
|
||||
|
||||
try
|
||||
{
|
||||
var recipeDirectory = DesktopPlatform.Recipes.DirectoryFor(Path.GetFileNameWithoutExtension(ActiveRecipe));
|
||||
var root = Path.Combine(recipeDirectory, "completion-images");
|
||||
var timestamp = startedAt.ToString("yyyyMMdd-HHmmss-fff", CultureInfo.InvariantCulture);
|
||||
var directory = Path.Combine(root, timestamp);
|
||||
for (var suffix = 2; Directory.Exists(directory) || File.Exists(directory); suffix++)
|
||||
directory = Path.Combine(root, $"{timestamp}-{suffix}");
|
||||
|
||||
_roundImageDirectory = Directory.CreateDirectory(directory).FullName;
|
||||
_roundImageDirectory = DesktopPlatform.Recipes.CreateCompletionImageRound(
|
||||
Path.GetFileNameWithoutExtension(ActiveRecipe), startedAt);
|
||||
}
|
||||
catch (Exception error) { Log("warn", "本轮结束图目录创建失败:" + error.Message); }
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
// 配方工作区:统一管理配方及关联数据的目录、导入迁移和可回退的删除归档。
|
||||
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using SopHelper.Engine.Sop;
|
||||
|
||||
namespace SopHelper.Engine.Recipes;
|
||||
@ -46,6 +49,51 @@ public sealed class RecipeWorkspace(string root)
|
||||
if (GetNameError(name) is { } error) throw new ArgumentException(error, nameof(name));
|
||||
return Path.Combine(RecipesDirectory, name);
|
||||
}
|
||||
|
||||
/// <summary>在指定配方下建立轮次图片目录,按本地开始日期从零编号,并从当天现有目录续号。</summary>
|
||||
/// <param name="name">已应用的配方名。</param>
|
||||
/// <param name="startedAt">检测轮次开始时的本地时间。</param>
|
||||
/// <returns>新建轮次目录的完整路径。</returns>
|
||||
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 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);
|
||||
try
|
||||
{
|
||||
if (!archiveLock.WaitOne(TimeSpan.FromSeconds(2)))
|
||||
throw new IOException("配方结束图目录正在被其他实例使用");
|
||||
}
|
||||
catch (AbandonedMutexException)
|
||||
{
|
||||
// 前一实例退出后同步锁已交给当前线程,从磁盘现存目录恢复序号。
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 将取号和建目录放在同一临界区,避免同配方的两个实例获得相同的日序号。
|
||||
Directory.CreateDirectory(root);
|
||||
var date = startedAt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
||||
var timestamp = startedAt.ToString(timestampFormat, CultureInfo.InvariantCulture);
|
||||
var next = 0L;
|
||||
foreach (var directory in Directory.EnumerateDirectories(root, date + "_*"))
|
||||
{
|
||||
var entry = Path.GetFileName(directory).AsSpan();
|
||||
if (entry.Length <= timestamp.Length + 1 || entry[timestamp.Length] != '-'
|
||||
|| !DateTime.TryParseExact(entry[..timestamp.Length], timestampFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out _)
|
||||
|| !long.TryParse(entry[(timestamp.Length + 1)..], NumberStyles.None, CultureInfo.InvariantCulture, out var sequence))
|
||||
continue;
|
||||
next = Math.Max(next, checked(sequence + 1));
|
||||
}
|
||||
|
||||
var path = Path.Combine(root, timestamp + "-" + next.ToString(CultureInfo.InvariantCulture));
|
||||
return Directory.CreateDirectory(path).FullName;
|
||||
}
|
||||
finally { archiveLock.ReleaseMutex(); }
|
||||
}
|
||||
|
||||
/// <summary>校验跨平台文件名限制、系统保留名及 UTF-8 文件名长度;合法时返回 null。</summary>
|
||||
public static string? GetNameError(string name)
|
||||
{
|
||||
|
||||
@ -1200,8 +1200,8 @@ internal static class Smoke
|
||||
sop.IsRunning=true;engine.Start();
|
||||
// 第一帧之前就应有空目录,待应用的配方不能改变本轮路径。
|
||||
var first=Directory.GetDirectories(archiveRoot).Single();
|
||||
Check(DateTime.TryParseExact(Path.GetFileName(first),"yyyyMMdd-HHmmss-fff",System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.None,out _)&&Directory.GetFiles(first).Length==0,"开始检测时即创建日期时间目录");
|
||||
Check(DateTime.TryParseExact(Path.GetFileName(first),"yyyy-MM-dd_HH-mm-ss'-0'",System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.None,out _)&&Directory.GetFiles(first).Length==0,"开始检测时即创建序号为 0 的日期时间目录");
|
||||
sop.SelectedRecipe=selected;
|
||||
await Round();
|
||||
var expected=new Dictionary<string,byte>{{"装配_检查.png",30},{"步骤3.png",90},{"装配_检查-2.png",120}};
|
||||
|
||||
@ -66,12 +66,47 @@ internal static class Program
|
||||
{
|
||||
AtomicYamlSave();
|
||||
LegacyWorkspace();
|
||||
CompletionImageRoundNumbering();
|
||||
RoundTrip();
|
||||
OverwriteAndRename();
|
||||
RejectedPackages();
|
||||
MissingStandardVideo();
|
||||
}
|
||||
|
||||
/// <summary>用固定时间验证同秒、重启、并发换号以及跨日和跨配方从零计数。</summary>
|
||||
private static void CompletionImageRoundNumbering()
|
||||
{
|
||||
var workspace = new RecipeWorkspace(Path.Combine(Root, "round-numbering"));
|
||||
const string recipe = "轮次检测";
|
||||
var startedAt = new DateTime(2026, 12, 4, 15, 30, 25);
|
||||
var archive = Path.Combine(workspace.DirectoryFor(recipe), "completion-images");
|
||||
Directory.CreateDirectory(Path.Combine(archive, "20261204-153025-123"));
|
||||
Directory.CreateDirectory(Path.Combine(archive, "2026-12-04_notes-99"));
|
||||
Directory.CreateDirectory(Path.Combine(archive, "2026-12-03_15-30-25-99"));
|
||||
|
||||
var first = workspace.CreateCompletionImageRound(recipe, startedAt);
|
||||
Check(first == Path.Combine(archive, "2026-12-04_15-30-25-0") && Directory.Exists(first),
|
||||
"当天首轮使用规定的日期时间格式并从 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 =>
|
||||
{
|
||||
var path = reopened.CreateCompletionImageRound(recipe, startedAt.AddSeconds(i + 1));
|
||||
sameDay[i] = Path.GetFileName(path);
|
||||
});
|
||||
Check(sameDay.Select(name => int.Parse(name[(name.LastIndexOf('-') + 1)..])).Order().SequenceEqual(Enumerable.Range(2, 12)),
|
||||
"同配方并发建目录时,日序号连续且没有重复");
|
||||
|
||||
var tomorrow = startedAt.Date.AddDays(1);
|
||||
Check(Path.GetFileName(reopened.CreateCompletionImageRound(recipe, tomorrow)) == "2026-12-05_00-00-00-0",
|
||||
"跨过本地午夜后新一天从 0 开始");
|
||||
Check(Path.GetFileName(reopened.CreateCompletionImageRound("其他配方", startedAt)) == "2026-12-04_15-30-25-0",
|
||||
"每个配方各自按天编号");
|
||||
}
|
||||
|
||||
/// <summary>旧训练文件即使已损坏,也不应阻塞配方和标准视频迁移。</summary>
|
||||
private static void LegacyWorkspace()
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user