From 99df058a665ea04065a23e729ec79a2f23f79881 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期二, 20 五月 2025 16:59:43 +0800
Subject: [PATCH] 优化

---
 Plugin.TsExecution/SingleControl.xaml.cs |  303 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 255 insertions(+), 48 deletions(-)

diff --git a/Plugin.TsExecution/SingleControl.xaml.cs b/Plugin.TsExecution/SingleControl.xaml.cs
index 9367bb2..8b0cfff 100644
--- a/Plugin.TsExecution/SingleControl.xaml.cs
+++ b/Plugin.TsExecution/SingleControl.xaml.cs
@@ -1,8 +1,11 @@
 锘縰sing HMI.Core.Util;
+using HMI.Util;
 using NationalInstruments.TestStand.Interop.API;
+using Plugin.TsExecution.Entity;
 using System;
+using System.Collections.ObjectModel;
 using System.ComponentModel;
-using System.Data;
+using System.IO;
 using System.Runtime.CompilerServices;
 using System.Timers;
 using System.Windows;
@@ -35,7 +38,7 @@
         #endregion
         private bool isLoadOK = false;
         private int index;
-        private string counterFilePath;
+        private string CountIniPath;
 
         private string status = "Ready";
 
@@ -45,8 +48,8 @@
             set { Set(ref status, value); }
         }
 
-        private int totalNum;
-        public int TotalNum
+        private double totalNum;
+        public double TotalNum
         {
             get => totalNum;
             private set
@@ -55,15 +58,15 @@
             }
         }
 
-        private int passNum;
-        public int PassNum
+        private double passNum;
+        public double PassNum
         {
             get => passNum;
             private set { Set(ref passNum, value); }
         }
 
-        private int failNum;
-        public int FailNum
+        private double failNum;
+        public double FailNum
         {
             get => failNum;
             private set { Set(ref failNum, value); }
@@ -87,13 +90,6 @@
         public string TestInfo
         {
             get => this.lang("Ts.TestInfo", Barcode, ElapseTime);
-        }
-
-        private string currentStep;
-        public string CurrentStep
-        {
-            get { return currentStep; }
-            set { Set(ref currentStep, value); }
         }
 
         private string barcode = string.Empty;
@@ -126,38 +122,127 @@
             }
         }
 
+        private ObservableCollection<TestStep> _datasource = new ObservableCollection<TestStep>();
+        public ObservableCollection<TestStep> Datasource
+        {
+            get => _datasource;
+            set
+            {
+                Set(ref _datasource, value);
+            }
+        }
+
         private DateTime TimeNow = new DateTime();
         private TimeSpan TimeCount = new TimeSpan();
 
         private Timer refreshTimer;
 
-        private DataTable dt;
-
-        public SingleControl(int Index, int passNum, int failNum)
+        private string currentStep = "绛夊緟鎵爜";
+        public string CurrentStep
         {
+            get => currentStep;
+            set
+            {
+                Set(ref currentStep, value);
+            }
+        }
+
+        private int _probeCount;
+        public int ProbeCount
+        {
+            get => _probeCount;
+            set
+            {
+                Set(ref _probeCount, value);
+            }
+        }
+
+        /// <summary>
+        /// 杩涘害鍊�
+        /// </summary>
+        private int _processValue;
+        public int ProcessValue
+        {
+            get => _processValue;
+            set
+            {
+                Set(ref _processValue, value);
+            }
+        }
+
+        public string ProcessValueLabel
+        {
+            get
+            {
+                if (ProcessValue > 0) return $"{ProcessValue}%";
+                return "";
+            }
+            set
+            {
+            }
+        }
+
+        public void SetSimpleUI(bool flag)
+        {
+            if (flag)
+            {
+                simplePanel.Visibility = Visibility.Visible;
+                detailPanel.Visibility = Visibility.Collapsed;
+            }
+            else
+            {
+                simplePanel.Visibility = Visibility.Collapsed;
+                detailPanel.Visibility = Visibility.Visible;
+            }
+        }
+
+        public int ProbeWarn { get; private set; }
+
+        public SingleControl(int Index, string CountIniPath, int probeWarn)
+        {
+            this.CountIniPath = CountIniPath;
             DataContext = this;
             this.index = Index;
-            PassNum = passNum;
-            FailNum = failNum;
-            dt = new DataTable();
-            dt.Columns.Add(new DataColumn("Name", typeof(string)));
-            dt.Columns.Add(new DataColumn("Unit", typeof(string)));
-            dt.Columns.Add(new DataColumn("Value", typeof(string)));
-            dt.Columns.Add(new DataColumn("LimitDown", typeof(string)));
-            dt.Columns.Add(new DataColumn("LimitUp", typeof(string)));
-            dt.Columns.Add(new DataColumn("Result", typeof(string)));
+            ProbeWarn = probeWarn;
 
             refreshTimer = new Timer()
             {
-                Interval = 1000
+                Interval = 100
             };
             refreshTimer.Elapsed += RefreshTimer_Elapsed;
-
+            ReadProbeCount();
+            ReadCounter();
             InitializeComponent();
+            //if (ProbeWarn > 0)
+            //{
+            //    probeWarnPanel.Visibility = Visibility.Visible;
+            //}
+            //else
+            //{
+            //    probeWarnPanel.Visibility = Visibility.Collapsed;
+            //}
         }
 
         private void RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
         {
+            //Dispatcher.Invoke(() =>
+            //{
+            //    var data = PluginLoader.Instance.Plugins.FirstOrDefault(p => p.Name == "TS");
+            //    if (data?.Content is TsExecution ts)
+            //    {
+            //        if (Status == "Testing")
+            //        {
+            //            ts.UpdatePositionStatus(index, "Passed");
+            //        }
+            //        else
+            //        {
+            //            ts.UpdatePositionStatus(index, "Testing");
+            //        }
+            //    }
+            //});
+
+            //AddTestData("1", "1", "1", "1", "1", "1", "1");
+
             TimeCount = DateTime.Now - TimeNow;
             elapseTime = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
             Set("ElapseTime");
@@ -172,23 +257,30 @@
 
         /// <summary>
         /// 鑾峰彇TS鐨勬瘡姝ユ祴璇曠殑鏁版嵁娣诲姞鍒拌〃鏍�
+        /// 椤圭洰鍚嶇О锛屾祴璇曞弬鏁帮紝娴嬭瘯鍊硷紝涓婇檺锛屼笅闄愶紝鍗曚綅锛岀粨鏋�
         /// </summary>
         /// <param name="TS_Data"></param>
-        public void AddTestData(string step_name, string unit, string TestValue, string LowLimit, string HightLimit, string Status)
+        public void AddTestData(string name, string step_name, string value, string HightLimit, string LowLimit, string unit, string Status)
         {
             try
             {
-                var dr = dt.NewRow();
-                dr[0] = step_name;
-                dr[1] = unit;
-                dr[2] = TestValue;
-                dr[3] = LowLimit;
-                dr[4] = HightLimit;
-                dr[5] = Status;
-                dt.Rows.Add(dr);
-                table.ItemsSource = dt.DefaultView;
+                var data = new TestStep
+                {
+                    Name = name,
+                    Step = step_name,
+                    Value = value,
+                    LimitUp = HightLimit,
+                    LimitDown = LowLimit,
+                    Unit = unit,
+                    Result = Status
+                };
+                Dispatcher.BeginInvoke(new Action(() =>
+                {
+                    Datasource.Add(data);
+                    table.ScrollIntoView(data);
+                }));
             }
-            catch
+            catch (Exception ex)
             {
                 //灞忚斀閿欒
             }
@@ -196,7 +288,6 @@
 
         public void UpdateStatus(string status)
         {
-            Status = status;
             if (status == "Passed")
             {
                 UpdateTestCounter(true);
@@ -205,12 +296,16 @@
             {
                 UpdateTestCounter(false);
             }
+            else if (Status != "Testing" && status == "Testing")
+            {
+                UpdateProbeCount(ProbeCount + 1);
+            }
+            Status = status;
         }
 
         public void IniTestData()
         {
-            dt.Clear();
-            table.ItemsSource = dt.DefaultView;
+            Datasource.Clear();
         }
 
         /// <summary>
@@ -219,9 +314,8 @@
         /// <param name="ProcessVal"></param>
         public void SetTestProcess(int ProcessVal)
         {
-            //this.TestProcess.Value = ProcessVal;
-            //this.TestProcessLabel.Text = ProcessVal.ToString() + "%";
-            //this.TestProcessLabel.BackColor = Color.Transparent;
+            ProcessValue = ProcessVal;
+            Set("ProcessValueLabel");
         }
 
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
@@ -253,16 +347,129 @@
             this.WriteCounter();
         }
 
+        /// <summary>
+        /// 浠巌ni璇诲彇鎺㈤拡浣跨敤娆℃暟
+        /// </summary>
+        private void ReadProbeCount()
+        {
+            try
+            {
+                var num = IniFileHelper.ReadIniData("Socket" + this.index.ToString(), "ProbeCount", "0", this.CountIniPath);
+                this.ProbeCount = int.Parse(num);
+            }
+            catch (Exception ex)
+            {
+                this.ProbeCount = 0;
+            }
+        }
+
+        /// <summary>
+        /// 鏇存柊鎺㈤拡浣跨敤娆℃暟
+        /// </summary>
+        /// <param name="count"></param>
+        private void UpdateProbeCount(int count)
+        {
+            /// 鍐欏埌閰嶇疆鏂囦欢
+            IniFileHelper.WriteIniData("Socket" + this.index.ToString(), "ProbeCount", count.ToString(), this.CountIniPath);
+            ProbeCount = count;
+
+            //if (ProbeWarn <= 0) probeCountAlarm.Visibility = Visibility.Collapsed;
+            //else if (count >= ProbeWarn) probeCountAlarm.Visibility = Visibility.Visible;
+            //else probeCountAlarm.Visibility = Visibility.Collapsed;
+        }
+
         private void WriteCounter()
         {
+            IniFileHelper.WriteIniData("Socket" + this.index.ToString(), "Pass",
+                this.passNum.ToString(), this.CountIniPath);
+            IniFileHelper.WriteIniData("Socket" + this.index.ToString(), "Fail", this.failNum.ToString(),
+                this.CountIniPath);
+        }
 
+        private void ReadCounter()
+        {
+            try
+            {
+                this.PassNum = Convert.ToInt32(IniFileHelper.ReadIniData("Socket" + this.index.ToString(), "Pass", "", CountIniPath));
+                this.FailNum = Convert.ToInt32(IniFileHelper.ReadIniData("Socket" + this.index.ToString(), "Fail", "", CountIniPath));
+            }
+            catch (Exception ex)
+            {
+                //do nothing
+            }
         }
 
         public void ResetCounter()
         {
-            PassNum = 0;
-            FailNum = 0;
+            this.PassNum = 0;
+            this.FailNum = 0;
             this.UpdateTestCounter(null);
         }
+
+        public void WriteToCSV(string path)
+        {
+            try
+            {
+
+                FileInfo fi = new FileInfo(path);
+                if (!fi.Directory.Exists)
+                {
+                    fi.Directory.Create();  //鏂囦欢涓嶅瓨鍦紝鍒涘缓鏂囦欢
+                }
+
+                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
+                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
+                {
+                    string title = "椤圭洰鍚嶇О,娴嬭瘯鍙傛暟,娴嬭瘯鍊�,涓婇檺,涓嬮檺,鍗曚綅,缁撴灉";
+                    sw.WriteLine(title);
+
+                    if (Datasource != null && Datasource.Count > 0)
+                    {
+                        foreach (var data in Datasource)
+                        {
+                            sw.WriteLine($"{data.Name},{data.Step},{data.Value},{data.LimitUp},{data.LimitDown},{data.Unit},{data.Result}");
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                TsExecution.logger.Error("Write CSV File Failed.", ex);
+            }
+        }
+
+        public void ShowPausePanel()
+        {
+            pausePanel.Visibility = Visibility.Visible;
+        }
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            Button btn = sender as Button;
+            switch (btn.Name)
+            {
+                case "clear":
+                    UpdateProbeCount(0);
+                    break;
+                case "cancelPauseBtn":
+                    SequenceFileContext?.StationGlobals.SetValBoolean($"StationState.IsContinue_{index}", 1, false);
+                    pausePanel.Visibility = Visibility.Collapsed;
+                    break;
+                case "continuePauseBtn":
+                    SequenceFileContext?.StationGlobals.SetValBoolean($"StationState.IsContinue_{index}", 1, true);
+                    pausePanel.Visibility = Visibility.Collapsed;
+                    break;
+                default:
+                    break;
+            }
+        }
+
+        private void pausePanel_Loaded(object sender, RoutedEventArgs e)
+        {
+            var can = pausePanel;
+            var modal = visualModal;
+            Canvas.SetLeft(modal, (pausePanel.ActualWidth - visualModal.ActualWidth) / 2);
+            Canvas.SetTop(modal, (pausePanel.ActualHeight - visualModal.ActualHeight) / 2);
+        }
     }
 }

--
Gitblit v1.9.1