using NationalInstruments.TestStand.Interop.API; using NationalInstruments.TestStand.Interop.UI.Ax; using System; using System.Diagnostics; namespace Plugin.TsSupport { public class TeststandWrapper : ITestExecutionWrapper { #region TestStand事件 /// /// TS 引擎 /// private Engine engine = null; /// /// 测试序列路径 /// private string testSeqFile = string.Empty; /// /// 测试序列 /// private SequenceFile testSeq = null; /// /// 测试模型序列 /// private SequenceFile modeSeq = null; /// /// 运行实例 /// private Execution execution = null; public SequenceContext context { get; set; } = null; /// /// 事件管理器 /// public AxApplicationMgr AxApplicationMgr { get; private set; } #endregion public event EventHandler UIMessageEvent; public event EventHandler EndExecutionEvent; public TeststandWrapper() { AxApplicationMgr = TestAppManager.GetAxApplicationMgr(); AxApplicationMgr.EndExecution += AxApplicationMgr_EndExecution; AxApplicationMgr.UIMessageEvent += AxApplicationMgr_UIMessageEvent; //AxApplicationMgr.ExitApplication += new EventHandler(this.AxApplicationMgr_ExitApplication); //AxApplicationMgr.ExecutionClosed += // new _ApplicationMgrEvents_ExecutionClosedEventHandler(this.AxApplicationMgr_ExecutionClosed); } private void AxApplicationMgr_UIMessageEvent(object sender, _ApplicationMgrEvents_UIMessageEventEvent e) { UIMessageEvent?.Invoke(this, new UIMessageEvent() { Origin = e, EventCode = Convert.ToInt32(e.uiMsg.Event), NumberData = Convert.ToInt16(e.uiMsg.NumericData), Data = e.uiMsg.ActiveXData, StrData = e.uiMsg.StringData }); } private void AxApplicationMgr_EndExecution(object sender, _ApplicationMgrEvents_EndExecutionEvent e) { EndExecutionEvent?.Invoke(this, new ExecutionEndEvent() { Origin = e }); } public object Init() { this.engine = this.AxApplicationMgr.GetEngine(); this.engine.PersistBreakpoints = false; this.AxApplicationMgr.IsEditor = true; this.AxApplicationMgr.LoginOnStart = false; this.AxApplicationMgr.Start(); return this.AxApplicationMgr; } public object StartSequence(string filePath, string modelPath) { this.testSeqFile = filePath; this.modeSeq = this.engine.GetSequenceFileEx(modelPath); 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); return execution; } public T GetFileGlobalValue(string key, int option) { if (typeof(T) == typeof(bool)) { return (T)(object)context?.FileGlobals?.GetValBoolean(key, option); } else if (typeof(T) == typeof(string)) { return (T)(object)context?.FileGlobals?.GetValString(key, option); } else if (typeof(T) == typeof(int)) { return (T)(object)context?.FileGlobals?.GetValNumber(key, option); } else if (typeof(T) == typeof(double)) { return (T)(object)context?.FileGlobals?.GetValNumber(key, option); } else if (typeof(T) == typeof(float)) { return (T)(object)context?.FileGlobals?.GetValNumber(key, option); } return default(T); } public T GetStationGlobalValue(string key, int option) { if (typeof(T) == typeof(bool)) { return (T)(object)engine?.Globals?.GetValBoolean(key, option); } else if (typeof(T) == typeof(string)) { return (T)(object)engine?.Globals?.GetValString(key, option); } else if (typeof(T) == typeof(int)) { return (T)(object)engine?.Globals?.GetValNumber(key, option); } else if (typeof(T) == typeof(double)) { return (T)(object)engine?.Globals?.GetValNumber(key, option); } else if (typeof(T) == typeof(float)) { return (T)(object)engine?.Globals?.GetValNumber(key, option); } return default(T); } public void SetFileGlobalValue(string key, int option, T value) { if (value is bool bVal) { context?.FileGlobals?.SetValBoolean(key, option, bVal); } else if (value is string sVal) { context?.FileGlobals?.SetValString(key, option, sVal); } else if (value is int iVal) { context?.FileGlobals?.SetValNumber(key, option, iVal); } else if (value is double dVal) { context?.FileGlobals?.SetValNumber(key, option, dVal); } else if (value is float fVal) { context?.FileGlobals?.SetValNumber(key, option, fVal); } } public void SetStationGlobalValue(string key, int option, T value, bool inContext = false) { PropertyObject obj = null; if (inContext) { obj = context?.StationGlobals; } else { obj = engine?.Globals; } if (value is bool bVal) { obj?.SetValBoolean(key, option, bVal); } else if (value is string sVal) { obj?.SetValString(key, option, sVal); } else if (value is int iVal) { obj?.SetValNumber(key, option, iVal); } else if (value is double dVal) { obj?.SetValNumber(key, option, dVal); } else if (value is float fVal) { obj?.SetValNumber(key, option, fVal); } //engine?.CommitGlobalsToDisk(promptOnSaveConflicts: false); } public void CommitToDisk(bool option) { engine?.CommitGlobalsToDisk(option); } public bool WaitForExit() { return this.execution?.WaitForEndEx(-1, true, Type.Missing, null) == true; } public bool ReleaseSeqFile() { if (testSeq != null) { return engine?.ReleaseSequenceFileEx(testSeq, 4) == true; } return true; } public void SetContext(object context) { this.context = (SequenceContext)context; } } }