59 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Concurrent;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Threading.Tasks;
 | |
| 
 | |
| namespace Check.Main.Common
 | |
| {
 | |
|     public class ImageData : IDisposable
 | |
|     {
 | |
|         public long ProductId { get; }
 | |
|         public int CameraIndex { get; }
 | |
|         public Bitmap Image { get; }
 | |
| 
 | |
|         public ImageData(long productId, int cameraIndex, Bitmap image)
 | |
|         {
 | |
|             ProductId = productId;
 | |
|             CameraIndex = cameraIndex;
 | |
|             Image = image;
 | |
|         }
 | |
|         public void Dispose() => Image?.Dispose();
 | |
|     }
 | |
| 
 | |
|     // ProductAssembly.cs - 用于组装一个完整的产品
 | |
|     public class ProductAssembly : IDisposable
 | |
|     {
 | |
|         public long ProductId { get; }
 | |
|         private readonly int _expectedCameraCount;
 | |
|         // 使用 ConcurrentDictionary 保证线程安全
 | |
|         private readonly ConcurrentDictionary<int, string> _results = new ConcurrentDictionary<int, string>();
 | |
| 
 | |
|         public ProductAssembly(long productId, int expectedCameraCount)
 | |
|         {
 | |
|             ProductId = productId;
 | |
|             _expectedCameraCount = expectedCameraCount;
 | |
|         }
 | |
| 
 | |
|         // 添加一个相机的检测结果
 | |
|         public bool AddResult(int cameraIndex, string result)
 | |
|         {
 | |
|             _results.TryAdd(cameraIndex, result);
 | |
|             return _results.Count == _expectedCameraCount;
 | |
|         }
 | |
| 
 | |
|         // 获取最终的聚合结果
 | |
|         public string GetFinalResult()
 | |
|         {
 | |
|             // 如果任何一个结果包含 "NG",则最终结果为 "NG"
 | |
|             return _results.Values.Any(r => r.Contains("NG")) ? "NG" : "OK";
 | |
|         }
 | |
| 
 | |
|         public void Dispose()
 | |
|         {
 | |
|             // 目前只持有结果字符串,无需释放资源
 | |
|         }
 | |
|     }
 | |
| }
 |