aisop/tests/CameraSettings.Smoke/Program.cs

74 lines
4.3 KiB
C#

using System.Text.Json;
using SopHelper.Models;
using SopHelper.Engine.Camera;
var root=Path.Combine(Path.GetTempPath(),"sophelper-camera-settings-"+Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.Combine(root,"workspace"));
try
{
var legacy=Path.Combine(root,"workspace","camera-settings.json");
var original="{\"directory\":\"/previous/recordings\"}";
File.WriteAllText(legacy,original);
var store=new CameraSettingsStore(root);
var usb=new CameraInfo {Index=0};
if(!store.Load(usb).UsbCapture.IsDefault)throw new Exception("旧录像目录应使用默认相机参数");
Console.WriteLine("PASS: 旧录像目录文件不再阻止读取相机参数");
File.WriteAllText(legacy,"{\"Uvc:0\":{\"UsbCapture\":{\"Width\":640,\"Height\":480}},\"Uvc:2\":{\"UsbCapture\":{\"Fps\":15}}}");
if(store.Load(usb).UsbCapture.Width!=640)throw new Exception("现有设备参数没有迁移");
var settings=new CameraDeviceSettings {UsbCapture=new() {Width=1280,Height=720,Format="MJPG",Fps=30}};
store.Save(usb,settings);
if(store.Load(usb).UsbCapture.Width!=1280||store.Load(new CameraInfo {Index=2}).UsbCapture.Fps!=15)throw new Exception("保存时丢失设备参数");
File.WriteAllText(legacy,original);
if(store.Load(usb).UsbCapture.Width!=1280)throw new Exception("录像目录覆盖了设备参数");
store.Save(usb,settings);
if(File.ReadAllText(legacy)!=original)throw new Exception("保存相机参数改写了录像目录");
File.WriteAllText(store.FilePath,"{\"Uvc:0\":{\"UsbCapture\":{\"Fps\":30},\"ExposureTimeUs\":1000},\"Hikvision:old\":\"unused legacy data\"}");
if(store.Load(usb).UsbCapture.Fps!=30)throw new Exception("旧工业相机参数影响 USB 参数读取");
using(var camera=new SettingsProbe())
{
await store.ApplyAsync(camera,usb);
if(camera.Options?.Fps!=30)throw new Exception("USB 配置没有传递给采集后端");
}
if(Enum.GetValues<CameraKind>().Length!=1||Enum.GetValues<CameraKind>()[0]!=CameraKind.Uvc)throw new Exception("仍暴露非 USB 相机类型");
if(typeof(CameraService).Assembly.GetReferencedAssemblies().Any(a=>a.Name?.Contains("MvCamera",StringComparison.OrdinalIgnoreCase)==true))throw new Exception("仍依赖工业相机 SDK");
Console.WriteLine("PASS: 仅保留 USB 类型与采集配置,旧工业相机参数不会参与配置,运行时无工业 SDK 依赖");
Console.WriteLine("PASS: 旧设备参数可迁移;录像目录与设备参数互不覆盖");
}
finally {Directory.Delete(root,true);}
if(args.Contains("--hardware"))
{
var deviceRoot="/home/wl/sophelper";
var store=new CameraSettingsStore(deviceRoot);
using var camera=new CameraService();
camera.Error+=Console.Error.WriteLine;
var cameras=await camera.EnumerateAsync();
var selected=cameras.FirstOrDefault(c=>c.Kind==CameraKind.Uvc)??throw new Exception("未枚举到 USB 相机");
if(!await store.ConnectAsync(camera,selected))throw new Exception("相机连接失败");
Console.WriteLine(await store.ApplyAsync(camera,selected));
var frames=0;var width=0;var height=0;
camera.FrameArrived+=frame=>{width=frame.Bgr.Width;height=frame.Bgr.Height;Interlocked.Increment(ref frames);};
if(!await camera.StartAsync())throw new Exception("相机采集启动失败");
await Task.Delay(5000);
camera.Stop();
if(frames<10)throw new Exception("相机未持续输出图像,帧数="+frames);
Console.WriteLine(JsonSerializer.Serialize(new {passed=true,camera=selected.Name,frames,width,height}));
}
sealed class SettingsProbe : ICameraService
{
public UvcCaptureOptions? Options;
public bool IsConnected=>true;
public bool IsGrabbing=>false;
public event Action<CameraFrame>? FrameArrived {add{} remove{}}
public event Action<bool>? ConnectionChanged {add{} remove{}}
public event Action<string>? Error {add{} remove{}}
public Task<List<CameraInfo>> EnumerateAsync()=>Task.FromResult(new List<CameraInfo>());
public Task<bool> ConnectAsync(CameraInfo camera)=>Task.FromResult(true);
public Task<string> ConfigureUvcAsync(UvcCaptureOptions options) {Options=options;return Task.FromResult("USB");}
public Task<bool> StartAsync()=>Task.FromResult(true);
public void Stop(){}
public void Disconnect(){}
public void Dispose(){}
}