using AddInPlugin.Util;
using LabViewHelper;
using NationalInstruments.LabVIEW.Interop;
using OpenTap;
using OpenTap.Addin.Annotation;
using System;
using System.Linq;
using ViHelper;
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)
{
viPath = value;
OnPropertyChanged(nameof(viPath));
LoadVi();
}
}
}
#endregion
static ViStep()
{
try
{
LabViewRunner.GetIO(new LVPath(), 0, out Cluster[] inputs, out Cluster[] outputs, out type[] iTypes, out type[] oTypes, out var viError);
}
catch (Exception ex)
{
}
}
public ViStep()
{
// ToDo: Set default values for properties / settings.
Rules.Add(() => !string.IsNullOrWhiteSpace(ViPath), "·¾¶²»ÄÜΪ¿Õ", 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.Data.ToString()
});
}
foreach (var onv in outVariables)
{
MethodVariables.Add(new MethodVariable
{
Name = onv.Name,
Type = onv.Type,
TypeName = onv.Type.ToString(),
Direction = VariableDirection.Out
});
}
}
catch (Exception 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;
}
}
}
}
public override void Run()
{
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;
}
}
private void UpdateVariableToRuntime()
{
//if (IsConstructorMethod)
//{
// var methodVariable = MethodVariables[0];
// PlanRun.UpdateRuntimeVariable(methodVariable.ValueStr, instance);
//}
foreach (var mv in MethodVariables)
{
if (mv.Direction == VariableDirection.Out)
{
PlanRun.Update(this, mv.ValueStr, mv.Value);
}
}
}
public override void PostPlanRun()
{
// ToDo: Optionally add any cleanup code this step needs to run after the entire testplan has finished
base.PostPlanRun();
}
}
}