alielie
2026-06-04 771a0e7b6b1276087515bbe94de28358cb92963b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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;
        }
 
        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<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;
        }
    }
}