using OpenTap.Addin.Util;
using LabViewHelper;
using NationalInstruments.LabVIEW.Interop;
using OpenTap;
using OpenTap.Addin.Annotation;
using System;
using System.Linq;
using ViHelper;
using System.Xml.Serialization;
namespace AddInPlugin
{
[Prototype]
[Display("ViStep", Description: "ÔËÐÐVI")]
public class ViStep : VariableTestStep
{
#region Settings
private string viPath;
[Display("·¾¶", Order: 0.0, Description: "Dll¾ø¶Ô·¾¶.")]
[FilePath(FilePathAttribute.BehaviorChoice.Open)]
public string ViPath
{
get => viPath;
set
{
if (viPath != value)
{
Set(ref viPath, value);
LoadVi();
}
}
}
#endregion
static ViStep()
{
//Init();
}
public static void Init()
{
//try
//{
// LabViewRunner.GetIO(new LVPath(), 0, out Cluster[] inputs, out Cluster[] outputs, out type[] iTypes, out type[] oTypes, out var viError);
//}
//catch (Exception ex)
//{
//}
}
private Exception loadException;
[XmlIgnore]
public Exception LoadException
{
get => loadException;
set
{
Set(ref loadException, value);
}
}
public ViStep()
{
// ToDo: Set default values for properties / settings.
Rules.Add(() => !string.IsNullOrWhiteSpace(ViPath), "·¾¶²»ÄÜΪ¿Õ", ViPath);
Rules.Add(() => LoadException == null, () => $"VI¼ÓÔØÊ§°Ü:{LoadException}", ViPath);
}
private void LoadVi()
{
try
{
if (string.IsNullOrEmpty(ViPath))
{
return;
}
//LabViewRunner.GetIO(new LVPath(ViPath), 0, out Cluster[] inputs, out Cluster[] outputs, out type[] iTypes, out type[] oTypes, out var viError);
LabViHelper.GetIOType(ViPath, out var inVariables, out var outVariables);
MethodVariables.Clear();
foreach (var inv in inVariables)
{
MethodVariables.Add(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()
});
}
foreach (var onv in outVariables)
{
MethodVariables.Add(new MethodVariable
{
Name = onv.Name,
Type = onv.Type,
TypeName = onv.Type.ToString(),
Direction = VariableDirection.Out
});
}
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();
}
///
/// ת»»²ÎÊý
///
private void ResolveVariables()
{
foreach (var m in MethodVariables)
{
m.Value = PlanRun.Resolve(this, m.ValueStr);
}
}
///
/// ÔËÐк¯Êý
///
private void RunMethod()
{
ResolveVariables();
var ins = MethodVariables.Where(m => m.Direction == VariableDirection.In)
.Select(m => new Cluster { name = m.Name, variant__32Data = new LVVariant(m.Value) }).ToArray();
LabViewRunner.ViRunner(new LVPath(this.ViPath), 0, ins, out var outs);
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();
}
}
}