alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using LabViewHelper;
using NationalInstruments.LabVIEW.Interop;
using OpenTap;
using OpenTap.Addin.Annotation;
using OpenTap.Addin.Util;
using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using ViHelper;
 
namespace AddInPlugin
{
    public class LvlibpData
    {
        public string path { get; set; }
        public DateTime lastUpdateTime { get; set; }
 
        public string[] names { get; private set; }
 
        private ConcurrentDictionary<string, ViVariable[]> inCache = new ConcurrentDictionary<string, ViVariable[]>();
        private ConcurrentDictionary<string, ViVariable[]> outCache = new ConcurrentDictionary<string, ViVariable[]>();
 
        public LvlibpData(DateTime lastUpdateTime, string[] names)
        {
            this.lastUpdateTime = lastUpdateTime;
            this.names = names;
        }
 
        public void GetIO(string viName, out ViVariable[] inVars, out ViVariable[] outVars)
        {
            if (!inCache.ContainsKey(viName) || !outCache.ContainsKey(viName))
            {
                LabViHelper.GetIOTypeByLvlibp(path, viName, out inVars, out outVars);
                inCache[viName] = inVars;
                outCache[viName] = outVars;
            }
            else
            {
                inVars = inCache[viName];
                outVars = outCache[viName];
            }
        }
    }
 
    public class LvlibpLoader
    {
        static readonly ConcurrentDictionary<string, LvlibpData> Cache = new ConcurrentDictionary<string, LvlibpData>();
 
        public static void PreLoad()
        {
            try
            {
                LvlibpLoader.GetLibpNames("test.lvlibp", out _);
            }
            catch
            {
 
            }
        }
 
        private static LvlibpData GetCache(string path)
        {
            path = path.ToLower();
            //if (!File.Exists(path))
            //{
            //    return null;
            //}
            DateTime updateTime = File.GetLastWriteTime(path);
 
            if (!Cache.ContainsKey(path) || Cache[path].lastUpdateTime != updateTime)
            {
                LabViewRunner.GetLibpNames(new LVPath(path), out var names, out ErrorCluster error);
                Cache[path] = new LvlibpData(updateTime, names) { path = path };
            }
            return Cache[path];
        }
 
        public static void GetLibpNames(string path, out string[] names)
        {
            var cache = GetCache(path);
            names = cache?.names;
        }
 
        public static void GetViIO(string path, string viName, out ViVariable[] inVars, out ViVariable[] outVars)
        {
            inVars = new ViVariable[0];
            outVars = new ViVariable[0];
            var cache = GetCache(path);
            cache?.GetIO(viName, out inVars, out outVars);
        }
    }
 
    [Prototype]
    [Display("LvlibpStep", Description: "ÔËÐÐlvlibp")]
 
    public class LvlibpStep : VariableTestStep
    {
        #region Settings
        private string filePath;
 
        [Obsolete]
        // [Display("·¾¶", Order: 0.0, Description: "¾ø¶Ô·¾¶.")]
        [FilePath(FilePathAttribute.BehaviorChoice.Open, fileExtension: "Labview|*.lvlibp")]
        public string FilePath
        {
            get => filePath;
            set
            {
                Set(ref filePath, value);
            }
        }
 
        private LocationString fileLocation = new LocationString();
        [Display("·¾¶", Order: 0.0, Description: "¾ø¶Ô·¾¶.")]
        [FilePath(FilePathAttribute.BehaviorChoice.Open, fileExtension: "Labview|*.lvlibp")]
        public LocationString FileLocation
        {
            get => fileLocation;
            set
            {
                Set(ref fileLocation, value);
            }
        }
 
        protected override void OnRootChanged(TestPlan old, TestPlan plan)
        {
            try
            {
                if (!string.IsNullOrEmpty(FilePath))
                {
                    FileLocation.AbsoluteLocation = FilePath;
                    FilePath = null;
                }
                FileLocation.Refresh(Root.Path);
                FileLocation.PropertyChanged += FileLocation_PropertyChanged;
                if (!FileLocation.GetLocation(Root.Path, out string path))
                {
                    return;
                }
                //LabViewRunner.GetIO(new LVPath(ViPath), 0, out Cluster[] inputs, out Cluster[] outputs, out type[] iTypes, out type[] oTypes, out var viError);
                //LabViewRunner.GetLibpNames(new LVPath(path), out string[] names, out ErrorCluster error);
                LvlibpLoader.GetLibpNames(path, out string[] names);
 
                NameDatasource = new ObservableCollection<string>(names);
                LoadVi();
                LoadException = null;
            }
            catch (Exception ex)
            {
                LoadException = ex;
                //Log.Error("ViStep load vi error. {0}", ex);
            }
        }
 
        private ObservableCollection<string> nameDatasource;
        [XmlIgnore]
        public ObservableCollection<string> NameDatasource
        {
            get => nameDatasource;
            set
            {
                Set(ref nameDatasource, value);
            }
        }
 
        private string viName;
 
        [Display("VI", Order: 0.0, Description: "VIÃû³Æ.")]
        [DynamicSelect(nameof(NameDatasource))]
        public string ViName
        {
            get => viName;
            set
            {
                Set(ref viName, value);
                LoadVi();
            }
        }
 
 
        private bool showWindow;
 
        [Display("ÏÔʾ´°¿Ú", Order: 0.0)]
        public bool ShowWindow
        {
            get => showWindow;
            set
            {
                Set(ref showWindow, value);
            }
        }
 
        #endregion
 
        private Exception loadException;
 
        [XmlIgnore]
        public Exception LoadException
        {
            get => loadException;
            set
            {
                Set(ref loadException, value);
            }
        }
 
        public LvlibpStep()
        {
            // ToDo: Set default values for properties / settings.
            //Rules.Add(() => !string.IsNullOrWhiteSpace(FilePath), "·¾¶²»ÄÜΪ¿Õ", FilePath);
            Rules.Add(() => FileLocation.GetLocation(Root.Path, out _), "Îļþ¼ÓÔØÊ§°Ü", nameof(FileLocation));
            Rules.Add(() => LoadException == null, () => $"VI¼ÓÔØÊ§°Ü:{LoadException}", nameof(FileLocation));
        }
 
        private void FileLocation_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Load();
        }
 
        private void ConcatVariable(MethodVariable mv, MethodVariable[] source)
        {
            MethodVariables.Add(mv);
            var preview = source?.FirstOrDefault(v => v.Name == mv.Name && !string.IsNullOrEmpty(v.ValueStr));
            if (preview != null)
            {
                mv.ValueStr = preview.ValueStr;
            }
        }
 
        private void LoadVi()
        {
            try
            {
                if (!FileLocation.GetLocation(Root?.Path, out string path))
                {
                    return;
                }
                //LabViewRunner.GetIO(new LVPath(ViPath), 0, out Cluster[] inputs, out Cluster[] outputs, out type[] iTypes, out type[] oTypes, out var viError);
                //LabViHelper.GetIOTypeByLvlibp(path, ViName, out var inVariables, out var outVariables);
 
                LvlibpLoader.GetViIO(path, viName, out var inVariables, out var outVariables);
 
                var last = MethodVariables?.ToArray();
                MethodVariables.Clear();
                foreach (var inv in inVariables)
                {
                    var newOne = new MethodVariable
                    {
                        Name = inv.Name,
                        Type = inv.Type,
                        TypeName = inv.Type.ToString(),
                        Direction = VariableDirection.In,
                        ValueStr = inv.Type == typeof(string) ? $"\"{inv.Data?.ToString()}\"" : inv.Data?.ToString()
                    };
                    ConcatVariable(newOne, last);
                }
 
                foreach (var onv in outVariables)
                {
                    var newOne = new MethodVariable
                    {
                        Name = onv.Name,
                        Type = onv.Type,
                        TypeName = onv.Type.ToString(),
                        Direction = VariableDirection.Out
                    };
                    ConcatVariable(newOne, last);
                }
                LoadException = null;
            }
            catch (Exception ex)
            {
                LoadException = ex;
                //Log.Error("ViStep load vi error. {0}", ex);
            }
        }
 
        private void Load()
        {
            try
            {
                if (!FileLocation.GetLocation(Root?.Path, out string path))
                {
                    return;
                }
                //LabViewRunner.GetIO(new LVPath(ViPath), 0, out Cluster[] inputs, out Cluster[] outputs, out type[] iTypes, out type[] oTypes, out var viError);
 
                LabViewRunner.GetLibpNames(new LVPath(path), out string[] names, out var outVariables);
                NameDatasource = new ObservableCollection<string>(names);
 
                LoadException = null;
            }
            catch (Exception ex)
            {
                LoadException = ex;
                //Log.Error("ViStep load vi error. {0}", ex);
            }
        }
 
        public override void PrePlanRun()
        {
            // ToDo: Optionally add any setup code this step needs to run before the testplan starts
            base.PrePlanRun();
        }
 
        /// <summary>
        /// ×ª»»²ÎÊý
        /// </summary>
        private void ResolveVariables()
        {
            foreach (var m in MethodVariables)
            {
                m.Value = PlanRun.Resolve(this, m.ValueStr, m.Type);
            }
        }
 
        /// <summary>
        /// ÔËÐк¯Êý
        /// </summary>
        private void RunMethod()
        {
            if (!FileLocation.GetLocation(Root?.Path, out string path))
            {
                return;
            }
            ResolveVariables();
 
            var ins = MethodVariables.Where(m => m.Direction == VariableDirection.In && m.Value != null)
                    .Select(m => new LabViewHelper.Cluster { name = m.Name, variant__32Data = new LVVariant(m.Value) }).ToArray();
 
 
            LabViewRunner.RunLibpByName2(new LVPath(path), ViName, 0, ShowWindow, ins, out var outs, out ErrorCluster err);
            if (err.status)
            {
                Verdict = Verdict.Error;
                return;
            }
 
            foreach (var ov in outs)
            {
                foreach (var m in MethodVariables)
                {
                    if (ov.name == m.Name)
                    {
                        m.Value = ov.variant__32Data.value;
                        break;
                    }
                }
            }
        }
 
        protected override void RunDetail()
        {
            try
            {
                // ToDo: Add test case code here
                //RunChildSteps(); //If step has child steps.
                RunMethod();
 
                UpdateVariableToRuntime();
                this.Verdict = Verdict.Pass;
            }
            catch (Exception ex)
            {
                Log.Error("ViStep Run error. {0}", ex);
                this.Verdict = Verdict.Error;
            }
        }
 
        public override void PostPlanRun()
        {
            // ToDo: Optionally add any cleanup code this step needs to run after the entire testplan has finished
            base.PostPlanRun();
        }
    }
}