using NationalInstruments.TestStand.Interop.API;
|
using NationalInstruments.TestStand.Interop.UI.Ax;
|
using System;
|
using System.Runtime.InteropServices;
|
|
namespace Plugin.TsSupport
|
{
|
public class TeststandWrapper : ITestExecutionWrapper
|
{
|
#region TestStand事件
|
/// <summary>
|
/// TS 引擎
|
/// </summary>
|
private Engine engine = null;
|
|
/// <summary>
|
/// 测试序列路径
|
/// </summary>
|
private string testSeqFile = string.Empty;
|
|
/// <summary>
|
/// 测试序列
|
/// </summary>
|
private SequenceFile testSeq = null;
|
|
/// <summary>
|
/// 测试模型序列
|
/// </summary>
|
private SequenceFile modeSeq = null;
|
|
/// <summary>
|
/// 运行实例
|
/// </summary>
|
private Execution execution = null;
|
|
public SequenceContext context { get; set; } = null;
|
|
/// <summary>
|
/// 事件管理器
|
/// </summary>
|
public AxApplicationMgr AxApplicationMgr { get; private set; }
|
#endregion
|
|
public event EventHandler<UIMessageEvent> UIMessageEvent;
|
public event EventHandler<ExecutionEndEvent> 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;
|
}
|
|
private SequenceFile GetFile(string path)
|
{
|
return null;
|
}
|
|
public object StartSequence(string filePath, string modelPath)
|
{
|
this.testSeqFile = filePath;
|
|
this.modeSeq = this.engine.GetSequenceFileEx(modelPath);
|
this.testSeq = this.engine.GetSequenceFileEx(this.testSeqFile, 107, TypeConflictHandlerTypes.ConflictHandler_UseGlobalType);
|
this.execution = this.engine.NewExecution(this.testSeq, "Test UUTs", this.modeSeq, false, 0, null, null, null);
|
return execution;
|
}
|
|
public T GetFileGlobalValue<T>(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<T>(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<T>(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<T>(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 (this.testSeq != null && this.engine?.ReleaseSequenceFileEx(this.testSeq, 4) == true)
|
{
|
Marshal.ReleaseComObject(this.testSeq);
|
this.testSeq = null;
|
}
|
|
if (this.modeSeq != null && this.engine?.ReleaseSequenceFileEx(this.modeSeq, 4) == true)
|
{
|
Marshal.ReleaseComObject(this.modeSeq);
|
this.modeSeq = null;
|
}
|
this.engine?.UnloadAllModules();
|
return this.testSeq == null && this.modeSeq == null;
|
}
|
|
public void SetContext(object context)
|
{
|
this.context = (SequenceContext)context;
|
}
|
|
public void Dispose()
|
{
|
AxApplicationMgr = null;
|
engine = null;
|
TestAppManager.Dispose();
|
}
|
}
|
}
|