alielie
2026-03-02 12e606635c42ea20f4b7c2bdda5204237a4b4c96
Plugin.TsExecution/TsExecution.xaml.cs
@@ -3,15 +3,14 @@
using HMI.Core.Api;
using HMI.Core.Entity;
using HMI.Core.Manager;
using HMI.Core.Setting;
using HMI.Core.Util;
using HMI.MQUtil;
using HMI.UI;
using HMI.UI.Util;
using HMI.Util;
using log4net;
using NationalInstruments.TestStand.Interop.API;
using NationalInstruments.TestStand.Interop.UI.Ax;
using Panuon.WPF.UI;
using Plugin.TsSupport;
using ScottPlot;
using ScottPlot.Plottables;
@@ -31,7 +30,7 @@
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class TsExecution : PluginControl
    public partial class TsExecution : PluginMaskControl
    {
        #region TestStand事件
        /// <summary>
@@ -101,7 +100,18 @@
            set
            {
                Set(ref selectedProduct, value);
                CurrentProductModel = value?.Name;
                SetTestSeqFile(value);
            }
        }
        private string currentProductModel;
        public string CurrentProductModel
        {
            get => currentProductModel;
            set
            {
                Set(ref currentProductModel, value);
            }
        }
@@ -248,6 +258,8 @@
                if (WriteIni("UI_CurrentWorkorder", value))
                {
                    Set(ref currentWorkorder, value);
                    engine?.Globals.SetValString("StationState.WorkOrder", 1, value);
                    engine?.CommitGlobalsToDisk(promptOnSaveConflicts: false);
                }
            }
        }
@@ -277,6 +289,7 @@
            DataContext = this;
            GCTimer = new Timer();
            InitializeComponent();
            InitChart();
            InitTsEngine();
            //初始化TestStand引擎
@@ -338,6 +351,25 @@
            InitProducts();
            CheckSqlHelper();
            base.InitFinish();
            if (ParamSetting.EnableAutoModel)
            {
                selectModelPanel.Visibility = Visibility.Collapsed;
                autoModelPanel.Visibility = Visibility.Visible;
                CurrentProductModel = string.Empty;
            }
            try
            {
                UserInfo user = UserInfo.User;
                CurrentUserCode = user?.Code;
                engine?.Globals.SetValString("StationState.OPID", 1, CurrentUserCode);
                engine?.CommitGlobalsToDisk(promptOnSaveConflicts: false);
            }
            catch (Exception ex)
            {
                log.Warn("Get Login User Failed.", ex);
            }
        }
        private void InitMQ()
@@ -476,23 +508,14 @@
        private void PluginControl_Loaded(object sender, RoutedEventArgs e)
        {
            new MaskWindow((WindowX)Window.GetWindow(this),
                (win) =>
            ShowMask(() =>
                {
                    try
                    {
                        Dispatcher.Invoke(() =>
                        {
                            try
                            {
                                UserInfo user = UserInfo.User;
                                CurrentUserCode = user?.Code;
                            }
                            catch (Exception ex)
                            {
                                log.Warn("Get Login User Failed.", ex);
                            }
                        });
                    //Dispatcher.Invoke(() =>
                    //{
                    //});
                        isLoadOK = true;
                        if (AutoExecuteSeq) StartSequence();
                    }
@@ -517,8 +540,55 @@
                    {
                        if (isLoadOK) Loaded -= PluginControl_Loaded;
                    });
                }
                ).Info(LangUtil.Get("App.UsercontrolIsLoading"));
            }, (ex) =>
            {
                ToastHelper.Error("Loaded Failed.");
                log.Error("Loaded Error.", ex);
            }, null, LangUtil.Get("App.UsercontrolIsLoading"));
            //new MaskWindow((WindowX)Window.GetWindow(this),
            //    (win) =>
            //    {
            //        try
            //        {
            //            Dispatcher.Invoke(() =>
            //            {
            //                try
            //                {
            //                    UserInfo user = UserInfo.User;
            //                    CurrentUserCode = user?.Code;
            //                }
            //                catch (Exception ex)
            //                {
            //                    log.Warn("Get Login User Failed.", ex);
            //                }
            //            });
            //            isLoadOK = true;
            //            if (AutoExecuteSeq) StartSequence();
            //        }
            //        catch (Exception ex)
            //        {
            //            log.Error("TsExecution Loaded Failed.", ex);
            //            MsgBoxHelper.Error(this, $"{LangUtil.Get("Ts.UserControlLoadFailed")}: {ex.Message}");
            //        }
            //        try
            //        {
            //            if (Environment.Version.ToString() != this.AxApplicationMgr.GetEngine().DotNetCLRVersion)
            //                this.GCTimer.Enabled = true;
            //        }
            //        catch (Exception ex)
            //        {
            //            log.Error("TsExecution GCTimer Start Failed", ex);
            //            MsgBoxHelper.Error(this, $"{LangUtil.Get("Ts.LoadFailed")}: {ex.Message}");
            //        }
            //        Dispatcher.Invoke(() =>
            //        {
            //            if (isLoadOK) Loaded -= PluginControl_Loaded;
            //        });
            //    }
            //    ).Info(LangUtil.Get("App.UsercontrolIsLoading"));
        }
        private void RefershUI()
@@ -577,8 +647,8 @@
            pass = Convert.ToInt32(IniFileHelper.ReadIniData("Count", "Pass", "0", CountIniPath));
            fail = Convert.ToInt32(IniFileHelper.ReadIniData("Count", "Fail", "0", CountIniPath));
            var fontSize = Convert.ToDouble(IniFileHelper.ReadIniData("Config", "TableFontSize", "12", CountIniPath));
            this.Resources["tableFontSize"] = fontSize;
            CbFontSize.SelectedItem = fontSize;
            this.Resources["tableFontSize"] = 20d;
            //CbFontSize.SelectedItem = fontSize;
        }
        private string csvDir;
@@ -791,13 +861,11 @@
            {
                value = ParameterManager.GetCommonParameterByName("UI_CurrentWorkorder");
                currentWorkorder = value.Value;
                Set("CurrentWorkorder");
                CurrentWorkorder = value.Value;
            }
            catch
            {
                currentWorkorder = string.Empty;
                Set("CurrentWorkorder");
                CurrentWorkorder = string.Empty;
            }
            try
@@ -814,22 +882,13 @@
            {
            }
            try
            {
                value = ParameterManager.GetCommonParameterByName("TS_DataLogPath");
                dataLogPath = value.Value;
            }
            catch
            {
                dataLogPath = Path.Combine(PathHelper.ConfigFolder, "DataBoard");
            }
            dataLogPath = ParamSetting.DataLogFolder;
            try
            {
                value = ParameterManager.GetCommonParameterByName("TsModelPath");
                tsModelPath = value.Value;
                tsModelPath = value?.Value ?? @"C:\Users\Public\Documents\National Instruments\TestStand 2020 (64-bit)\Components\Models\ParallelModel.seq";
            }
            catch
            {
@@ -861,6 +920,11 @@
                    foreach (var ts in tsSockets)
                    {
                        ts?.SequenceFileContext?.StationGlobals.SetValBoolean($"SystemSetting.{name}", 1, value);
                        if (ts?.SequenceFileContext?.Sequence != null)
                        {
                            ts.SequenceFileContext.Sequence.FailureAction =
                                value ? FailureActions.FailureAction_None : FailureActions.FailureAction_GotoCleanup;
                        }
                    }
                    return true;
                }
@@ -963,10 +1027,41 @@
        private void StartSequence()
        {
            if (!isLoadOK) return;
            if (ParamSetting.EnableAutoModel)
            {
                try
                {
                    var path = ParameterManager.GetCommonParameterByName<string>("CommonMainSeq", "");
                    if (string.IsNullOrEmpty(path))
                    {
                        Dispatcher.Invoke(() =>
                        {
                            MsgBoxHelper.Error(this, $"请先设置CommonMainSeq参数");
                        });
                        return;
                    }
                    this.testSeqFile = path;
                    this.modeSeq = this.engine.GetSequenceFileEx(tsModelPath);
                    this.testSeq = this.engine.GetSequenceFileEx(this.testSeqFile, 2, TypeConflictHandlerTypes.ConflictHandler_UseGlobalType);
                    this.execution = this.engine.NewExecution(this.testSeq, "Test UUTs", this.modeSeq, false, 0, null, null, null);
                    IsTesting = true;
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() =>
                    {
                        log.Error("Start Sequence Failed.", ex);
                        MsgBoxHelper.Error(this, $"{LangUtil.Get("Ts.StartSequenceFail")}: {ex.Message}");
                    });
                }
            }
            else
            {
            //MaskWindow mask = new MaskWindow();
            try
            {
                engine?.Globals.SetValString("StationState.ProductModel", 1, SelectedProduct?.Name);
                    engine?.Globals.SetValString("StationState.ProductModel", 1, CurrentProductModel);
                engine?.CommitGlobalsToDisk(promptOnSaveConflicts: false);
                this.modeSeq = this.engine.GetSequenceFileEx(tsModelPath);
                this.testSeq = this.engine.GetSequenceFileEx(this.testSeqFile, 2, TypeConflictHandlerTypes.ConflictHandler_UseGlobalType);
@@ -986,30 +1081,39 @@
            }
        }
        }
        public void StopSequenceClick()
        {
            new MaskWindow((WindowX)Window.GetWindow(this),
                    (win) =>
                    {
                        try
            ShowMask(() =>
                        {
                            bool stoped = StopSequence();
                            if (stoped)
                            {
                                IsTesting = false;
                            }
                        }
                        finally
                        {
                            Dispatcher.Invoke(() =>
                            {
            }, null, null, LangUtil.Get("App.SequenceIsStopping"));
            //new MaskWindow((WindowX)Window.GetWindow(this),
            //        (win) =>
            //        {
            //            try
            //            {
            //                bool stoped = StopSequence();
            //                if (stoped)
            //                {
            //                    IsTesting = false;
            //                }
            //            }
            //            finally
            //            {
            //                Dispatcher.Invoke(() =>
            //                {
                                win.Close();
                            });
                        }
                    }
                    ).Info(LangUtil.Get("App.SequenceIsStopping"));
            //                    win.Close();
            //                });
            //            }
            //        }
            //        ).Info(LangUtil.Get("App.SequenceIsStopping"));
        }
        /// <summary>
@@ -1070,21 +1174,28 @@
        private void StartClick()
        {
            new MaskWindow(Window.GetWindow(this),
                (win) =>
                {
                    try
            ShowMask(() =>
                    {
                        StartSequence();
                    }
                    finally
                    {
                        win.Dispatcher.Invoke(() =>
                        {
                            win.Close();
                        });
                    }
                }).Info(LangUtil.Get("Ts.SequenceIsStarting"));
            }, null, null, LangUtil.Get("Ts.SequenceIsStarting"));
            //new MaskWindow(Window.GetWindow(this),
            //    (win) =>
            //    {
            //        try
            //        {
            //            StartSequence();
            //        }
            //        catch (Exception ex) {
            //        }
            //        finally
            //        {
            //            win.Dispatcher.Invoke(() =>
            //            {
            //                win.Close();
            //            });
            //        }
            //    }).Info(LangUtil.Get("Ts.SequenceIsStarting"));
        }
        /// <summary>
@@ -1148,7 +1259,7 @@
        {
            var time = DateTime.Now;
            string status = this.tsSockets[index].Status == "Passed" ? "PASS" : "FAIL";
            string csvPath = Path.Combine(csvDir, SelectedProduct?.Name ?? "未知产品", time.ToString("yyyyMMdd"), status, $"{this.tsSockets[index].Barcode}-{time.ToString("HHmmss")}.csv");
            string csvPath = Path.Combine(csvDir, CurrentProductModel ?? "未知产品", time.ToString("yyyyMMdd"), status, $"{this.tsSockets[index].Barcode}-{time.ToString("HHmmss")}.csv");
            this.tsSockets[index].WriteToCSV(csvPath);
        }
@@ -1158,7 +1269,7 @@
            var socket = this.tsSockets[index];
            var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string snr = socket.Barcode;
            string model = SelectedProduct?.Name;
            string model = CurrentProductModel;
            List<string> sqls = new List<string>();
            foreach (var data in socket.Datasource)
@@ -1284,7 +1395,15 @@
                    this.tsSockets[index].ShowPausePanel();
                    break;
                case 10033:
                    ShowAlarm(strData);
                    var alarm = new AlarmText
                    {
                        VirtualId = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                        AlarmInfo = strData
                    };
                    ShowAlarm(alarm);
                    break;
                case 10034:
                    CurrentProductModel = strData;
                    break;
                case 10099:
                    break;
@@ -1293,10 +1412,10 @@
            }
        }
        private void ShowAlarm(string info)
        private void ShowAlarm(AlarmText alarm)
        {
            AlarmShowHelper.ShowAlarm(info);
            mSMQHelper.Send("Alarm", $"ATE测试 {info}");
            AlarmShowHelper.ShowAlarm(alarm);
            mSMQHelper.Send("Alarm", $"ATE测试 {alarm.AlarmInfo}");
        }
        /// <summary>
@@ -1461,5 +1580,23 @@
            {
            }
        }
        private void Label_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //workOrderEditor.Focus();
            //workOrderEditor.IsEditing = true;
            if (RequirePassword() == true)
            {
                var win = new WorkOrderWin
                {
                    WorkOrder = CurrentWorkorder,
                    Owner = Window.GetWindow(this)
                };
                if (win.ShowDialog() == true)
                {
                    CurrentWorkorder = win.WorkOrder;
                }
            }
        }
    }
}