alielie
2025-10-10 15bbd62b0b24b72c717ae91ab45348a2083ad04e
SimpleUtil/HMI.Simple.Util/ParameterUtil.cs
@@ -1,4 +1,4 @@
using Microsoft.Win32;
using NationalInstruments.TestStand.Interop.API;
using System;
using System.Collections.Generic;
using System.IO;
@@ -8,8 +8,12 @@
{
    public class ParameterUtil
    {
        private static string Company = "JingCe";
        private static string Program = "HMI";
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetCommonParameterValue(string path, string name)
        {
            if (!File.Exists(path)) return null;
@@ -29,38 +33,21 @@
            errMsg = string.Empty;
            try
            {
                string registryPath = $@"SOFTWARE\{Company}\{Program}";
                // 尝试打开注册表键
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryPath))
                string value = RegUtil.GetLocaltion();
                string path = Path.Combine(value, "Config", "Parameter", "CommonParameter.data");
                if (!File.Exists(path))
                {
                    if (key == null)
                    {
                        errMsg = "注册表不存在,请先运行一次HMI";
                        return null;
                    }
                    string value = key.GetValue("Location")?.ToString();
                    if (string.IsNullOrEmpty(value))
                    {
                        errMsg = "注册表不存在,请先运行一次HMI";
                        return null;
                    }
                    string path = Path.Combine(value, "Config", "Parameter", "CommonParameter.data");
                    if (!File.Exists(path))
                    {
                        errMsg = "未找到配置文件";
                        return null;
                    }
                    List<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(path);
                    ParameterData data = datas.FirstOrDefault(d => d.Name == name);
                    if (data == null)
                    {
                        errMsg = "未找到参数,请检查配置";
                        return null;
                    }
                    ;
                    return data.Value;
                    errMsg = "未找到配置文件";
                    return null;
                }
                List<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(path);
                ParameterData data = datas.FirstOrDefault(d => d.Name == name);
                if (data == null)
                {
                    errMsg = "未找到参数,请检查配置";
                    return null;
                }
                return data.Value;
            }
            catch (Exception ex)
            {
@@ -68,5 +55,137 @@
                return null;
            }
        }
        private static void WriteParameterData(SequenceContext context, ParameterData data)
        {
            string prefix = "SystemSetting";
            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);
            }
        }
        /// <summary>
        /// 将参数写进Engine的StationGlobals.StationState
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 将参数写进Engine的StationGlobals.StationState
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        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<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(xmlPath);
                foreach (var data in datas)
                {
                    WriteParameterData(context, data);
                }
                return true;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
        }
        /// <summary>
        /// 将参数写进Engine的StationGlobals.StationState
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        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<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(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;
            }
        }
        /// <summary>
        /// 将参数写进Engine的StationGlobals.StationState
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        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;
            }
        }
    }
}