aisop/tests/CameraSettings.Smoke/Program.cs

47 lines
2.6 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\":{\"ExposureTimeUs\":1000}}");
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}).ExposureTimeUs!=1000)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("保存相机参数改写了录像目录");
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}));
}