alielie
2025-06-12 c1c3772a11b1f3fbad8846cc714120771d85fa53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using NationalInstruments.TestStand.Interop.API;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace HMI.Simple.Util
{
    public class ParameterUtil
    {
        /// <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;
            List<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(path);
            ParameterData data = datas.FirstOrDefault(d => d.Name == name);
            if (data == null) return null;
            return data.Value;
        }
 
        /// <summary>
        /// 获取HMI里配置的参数
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        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<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)
            {
                errMsg = ex.Message;
                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.SetValInteger64($"{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;
            }
        }
    }
}