using NationalInstruments.TestStand.Interop.API;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace HMI.Simple.Util
{
public class ParameterUtil
{
///
/// 根据路径和参数名获取参数值
///
/// 路径
/// 参数名
/// 参数值
public static string GetCommonParameterValue(string path, string name)
{
if (!File.Exists(path)) return null;
List datas = XmlHelper.DeserializeFromXml>(path);
ParameterData data = datas.FirstOrDefault(d => d.Name == name);
if (data == null) return null;
return data.Value;
}
///
/// 获取HMI里配置的参数
///
/// 参数名
/// 参数值
public static string GetCommonParameterValue(string name, out string errMsg)
{
errMsg = string.Empty;
try
{
string value = RegUtil.GetLocaltion();
string path = Path.Combine(value, "Config", "Parameter", "CommonParameter.data");
if (!File.Exists(path))
{
errMsg = "未找到配置文件";
return null;
}
List datas = XmlHelper.DeserializeFromXml>(path);
ParameterData data = datas.FirstOrDefault(d => d.Name == name);
if (data == null)
{
errMsg = "未找到参数,请检查配置";
return null;
}
return data.Value;
}
catch (Exception ex)
{
errMsg = ex.Message;
return null;
}
}
private static void WriteParameterData(SequenceContext context, ParameterData data)
{
string prefix = "SystemSetting";
if (data.IsArray)
{
PropertyValueTypes type;
if (data.Type == "Bool")
{
type = PropertyValueTypes.PropValType_Boolean;
}
else if (data.Type == "Int")
{
type = PropertyValueTypes.PropValType_Number;
}
else if (data.Type == "Double")
{
type = PropertyValueTypes.PropValType_Number;
}
else
{
type = PropertyValueTypes.PropValType_String;
}
string[] arr = data.Value?.Split(',') ?? new string[0];
if (arr.Length > 0)
{
context.StationGlobals.DeleteSubProperty($"{prefix}.{data.Name}", 2);
context.StationGlobals.NewSubProperty($"{prefix}.{data.Name}", type, true, null, 1);
context.StationGlobals.GetPropertyObject($"{prefix}.{data.Name}", 0).SetNumElements(arr.Length);
for (int i = 0; i < arr.Length; i++)
{
if (type == PropertyValueTypes.PropValType_String)
{
context.StationGlobals.SetValString($"{prefix}.{data.Name}[{i}]", 0, arr[i]);
}
if (data.Type == "Int")
{
context.StationGlobals.SetValNumber($"{prefix}.{data.Name}[{i}]", 0, int.Parse(arr[i]));
}
if (data.Type == "Double")
{
context.StationGlobals.SetValNumber($"{prefix}.{data.Name}[{i}]", 0, double.Parse(arr[i]));
}
if (data.Type == "Bool")
{
context.StationGlobals.SetValBoolean($"{prefix}.{data.Name}[{i}]", 0, bool.Parse(arr[i]));
}
}
}
}
else
{
if (data.Type == "Bool")
{
context.StationGlobals.SetValBoolean($"{prefix}.{data.Name}", 1, bool.Parse(data.Value));
}
else if (data.Type == "Int")
{
context.StationGlobals.SetValNumber($"{prefix}.{data.Name}", 1, int.Parse(data.Value));
}
else if (data.Type == "Double")
{
context.StationGlobals.SetValNumber($"{prefix}.{data.Name}", 1, double.Parse(data.Value));
}
else
{
context.StationGlobals.SetValString($"{prefix}.{data.Name}", 1, data.Value);
}
}
}
///
/// 将参数写进Engine的StationGlobals.StationState
///
///
///
public static bool WriteConfigToContext(object instance, out string errMsg)
{
errMsg = string.Empty;
try
{
string value = RegUtil.GetLocaltion();
string path = Path.Combine(value, "Config", "Parameter", "CommonParameter.data");
return WriteConfigToContext(path, instance, out errMsg);
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
///
/// 将参数写进Engine的StationGlobals.StationState
///
/// 绝对路径
/// TestStand上下文
/// 异常信息
/// 是否成功
public static bool WriteConfigToContext(string xmlPath, object instance, out string errMsg)
{
errMsg = string.Empty;
try
{
SequenceContext context = (SequenceContext)instance;
if (string.IsNullOrEmpty(xmlPath))
{
throw new Exception("配置文件路径不能为空");
}
if (!File.Exists(xmlPath))
{
throw new Exception("未找到配置文件");
}
List datas = XmlHelper.DeserializeFromXml>(xmlPath);
foreach (var data in datas)
{
WriteParameterData(context, data);
}
return true;
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
///
/// 将参数写进Engine的StationGlobals.StationState
///
///
///
public static bool WriteConfigToEngineByName(string xmlPath, object instance, string name, out string errMsg)
{
errMsg = string.Empty;
try
{
SequenceContext context = (SequenceContext)instance;
if (string.IsNullOrEmpty(xmlPath))
{
throw new Exception("配置文件路径不能为空");
}
if (!File.Exists(xmlPath))
{
throw new Exception("未找到配置文件");
}
List datas = XmlHelper.DeserializeFromXml>(xmlPath);
ParameterData data = datas.FirstOrDefault(d => d.Name == name);
if (data == null)
{
throw new Exception("未找到参数,请检查配置");
}
WriteParameterData(context, data);
return true;
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
///
/// 将参数写进Engine的StationGlobals.StationState
///
///
///
public static bool WriteConfigToEngineByName(object instance, string name, out string errMsg)
{
errMsg = string.Empty;
try
{
SequenceContext engine = (SequenceContext)instance;
string value = RegUtil.GetLocaltion();
string path = Path.Combine(value, "Config", "Parameter", "CommonParameter.data");
return WriteConfigToEngineByName(path, instance, name, out errMsg);
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
}
}