using HMI.Simple.Util;
using OpenTap;
using OpenTap.Addin.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace HMI.TapApi
{
public class ProductUtil
{
private static void WriteParameterData(object instance, ParameterData data)
{
TestPlanRun context = (TestPlanRun)instance;
string name = $"DUTSetting.{data.Name}";
if (data.IsArray)
{
Type type;
if (data.Type == "Bool")
{
type = typeof(bool);
}
else if (data.Type == "Int")
{
type = typeof(int);
}
else if (data.Type == "Double")
{
type = typeof(double);
}
else
{
type = typeof(string);
}
string[] arr = data.Value?.Split(',') ?? new string[0];
if (arr.Length > 0)
{
//RuntimeVariable rv;
//if (data.Type == "Int")
//{
// int[] values = arr.Select(a => int.Parse(a)).ToArray();
// rv = new RuntimeVariable(context, name, values);
//}
//if (data.Type == "Double")
//{
// double[] values = arr.Select(a => double.Parse(a)).ToArray();
// rv = new RuntimeVariable(context, name, values);
//}
//if (data.Type == "Bool")
//{
// bool[] values = arr.Select(a => bool.Parse(a)).ToArray();
// rv = new RuntimeVariable(context, name, values);
//}
//else
//{
// rv = new RuntimeVariable(context, name, arr);
//}
if (data.Type == "Int")
{
int[] values = arr.Select(a => int.Parse(a)).ToArray();
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", values);
}
if (data.Type == "Double")
{
double[] values = arr.Select(a => double.Parse(a)).ToArray();
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", values);
}
if (data.Type == "Bool")
{
bool[] values = arr.Select(a => bool.Parse(a)).ToArray();
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", values);
}
else
{
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", arr);
}
}
}
else
{
if (data.Type == "Bool")
{
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", bool.Parse(data.Value));
}
else if (data.Type == "Int")
{
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", int.Parse(data.Value));
}
else if (data.Type == "Double")
{
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", double.Parse(data.Value));
}
else
{
context.Update(null, $"{ConstNames.FileGlobalsName}.{name}", data.Value);
}
}
}
private static string GetDataFilePath()
{
string path = ParameterUtil.GetCommonParameterValue("ShareConfig", out _);
if (string.IsNullOrEmpty(path))
{
path = Path.Combine(HMI.Simple.Util.RegUtil.GetLocaltion(), "Config");
}
return path;
}
///
/// 根据产品型号写配置
///
/// 产品型号
/// TestStand上下文
/// 异常信息
/// 是否成功
public static bool WriteConfigToContextByModel(string model, object instance, out string errMsg)
{
errMsg = string.Empty;
try
{
string path = Path.Combine(GetDataFilePath(), "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;
}
}
}
}