using NationalInstruments.TestStand.Interop.API;
using System;
using System.Collections.Generic;
using System.IO;
namespace HMI.Simple.Util
{
public class ProductUtil
{
private static void WriteParameterData(object instance, ParameterData data)
{
SequenceContext context = (SequenceContext)instance;
if (data.Type == "Bool")
{
context.FileGlobals.SetValBoolean($"DUTSetting.{data.Name}", 1, bool.Parse(data.Value));
}
else if (data.Type == "Int")
{
context.FileGlobals.SetValNumber($"DUTSetting.{data.Name}", 1, int.Parse(data.Value));
}
else if (data.Type == "Double")
{
context.FileGlobals.SetValNumber($"DUTSetting.{data.Name}", 1, double.Parse(data.Value));
}
else
{
context.FileGlobals.SetValString($"DUTSetting.{data.Name}", 1, data.Value);
}
}
///
/// 根据产品型号写配置
///
/// 产品型号
/// TestStand上下文
/// 异常信息
/// 是否成功
public static bool WriteConfigToContextByModel(string model, object instance, out string errMsg)
{
errMsg = string.Empty;
try
{
string dir = RegUtil.GetLocaltion();
string path = Path.Combine(dir, "Config", "Product", "Parameter", $"Parameter_{model}.data");
return WriteConfigToContextByPath(path, instance, out errMsg);
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
///
/// 根据路径写配置
///
/// 绝对路径
/// TestStand上下文
/// 异常信息
/// 是否成功
public static bool WriteConfigToContextByPath(string path, object instance, out string errMsg)
{
errMsg = string.Empty;
try
{
if (!File.Exists(path))
{
throw new Exception($"未找到该配置[{path}],请检查");
}
List datas = XmlHelper.DeserializeFromXml>(path);
foreach (var data in datas)
{
WriteParameterData(instance, data);
}
return true;
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
}
}