90 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Check.Main.Camera;
 | ||
| using Check.Main.Common;
 | ||
| using Check.Main.Infer;
 | ||
| using System;
 | ||
| using System.Collections.Generic;
 | ||
| using System.ComponentModel;
 | ||
| using System.Data;
 | ||
| using System.Drawing;
 | ||
| using System.Globalization;
 | ||
| using System.Linq;
 | ||
| using System.Text;
 | ||
| using System.Threading.Tasks;
 | ||
| using System.Windows.Forms;
 | ||
| using WeifenLuo.WinFormsUI.Docking;
 | ||
| 
 | ||
| namespace Check.Main.UI
 | ||
| {
 | ||
|     public partial class FormStatistics : DockContent
 | ||
|     {
 | ||
|         private readonly StatisticsData _statisticsData;
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 将私有字段通过公共属性暴露,以便外部(如FormMain)访问。
 | ||
|         /// </summary>
 | ||
|         public StatisticsData CurrentStatistics => _statisticsData;
 | ||
|         public FormStatistics()
 | ||
|         {
 | ||
|             InitializeComponent();
 | ||
|             _statisticsData = new StatisticsData();
 | ||
| 
 | ||
|             // 订阅CameraManager的检测完成事件
 | ||
|             DetectionCoordinator.OnDetectionCompleted += CameraManager_OnDetectionCompleted;
 | ||
|             // 初始化UI显示
 | ||
|             UpdateUI();
 | ||
|         }
 | ||
|         // 当CameraManager发布检测结果时,此方法被调用
 | ||
|         private void CameraManager_OnDetectionCompleted(object sender, DetectionResultEventArgs e)
 | ||
|         {
 | ||
|             // 更新统计数据
 | ||
|             _statisticsData.UpdateWithResult(e.IsOK);
 | ||
| 
 | ||
|             // 在UI线程上更新界面显示
 | ||
|             UpdateUI();
 | ||
|         }
 | ||
|         // 更新所有标签的文本
 | ||
|         private void UpdateUI()
 | ||
|         {
 | ||
|             // 使用Invoke确保线程安全
 | ||
|             if (this.InvokeRequired)
 | ||
|             {
 | ||
|                 this.Invoke(new Action(UpdateUI));
 | ||
|                 return;
 | ||
|             }
 | ||
| 
 | ||
|             txtOKNum.Text = _statisticsData.GoodCount.ToString();
 | ||
|             txtNGNum.Text = _statisticsData.NgCount.ToString();
 | ||
|             txtTotal.Text = _statisticsData.TotalCount.ToString();
 | ||
|             // 将良率格式化为百分比
 | ||
|             txtYieldRate.Text = _statisticsData.YieldRate.ToString("P2", CultureInfo.InvariantCulture);
 | ||
| 
 | ||
|             // 根据良率改变颜色以示提醒
 | ||
|             if (_statisticsData.YieldRate < 0.9 && _statisticsData.TotalCount > 10)
 | ||
|             {
 | ||
|                 txtYieldRate.ForeColor = Color.Red;
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 txtYieldRate.ForeColor = Color.ForestGreen;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         private void toolStripButtonRest_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             StatisticsExporter.ExportToExcel(CurrentStatistics, "Reset");
 | ||
| 
 | ||
|             _statisticsData.Reset();
 | ||
|             // 同时重置产品ID计数器
 | ||
|             DetectionCoordinator.ResetAllCounters();
 | ||
|             UpdateUI();
 | ||
|             ThreadSafeLogger.Log("统计数据已重置。");
 | ||
|         }
 | ||
|         // 窗体关闭时,取消事件订阅,防止内存泄漏
 | ||
|         protected override void OnFormClosing(FormClosingEventArgs e)
 | ||
|         {
 | ||
|             DetectionCoordinator.OnDetectionCompleted -= CameraManager_OnDetectionCompleted;
 | ||
|             base.OnFormClosing(e);
 | ||
|         }
 | ||
|     }
 | ||
| }
 |