alielie
2025-10-13 e3868d4d61ded0e76fc8fe3fdacdd9e6d9113cf1
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
using HMI.Core.Entity;
using HMI.Core.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace HMI.Core.Manager
{
    /// <summary>
    /// TODO 优化数据源,需要支持sqlite和xml
    /// </summary>
    public class ParameterManager
    {
 
        /// <summary>
        /// 获取通用参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetCommonParameter<T>() where T : IList<ParameterData>
        {
            string path = PathHelper.ParameterDataPath;
            if (!File.Exists(path)) return default(T);
            T datas = XmlHelper.DeserializeFromXml<T>(path);
            return datas;
        }
 
        /// <summary>
        /// 获取通用参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static ParameterData GetCommonParameterByName(string name)
        {
            string path = PathHelper.ParameterDataPath;
            if (!File.Exists(path)) return null;
            List<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(path);
            return datas.FirstOrDefault(d => d.Name == name);
        }
 
        /// <summary>
        /// 保存通用参数
        /// </summary>
        /// <param name="datas"></param>
        /// <exception cref="Exception"></exception>
        public static void SaveCommonParameter(IList<ParameterData> datas)
        {
            if (datas == null)
            {
                throw new Exception("不能为null");
            }
            string path = PathHelper.ParameterDataPath;
            XmlHelper.SerializeToXml(path, datas);
        }
 
        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;
        }
 
        public static bool UpdateCommonParameter(string name, object value, out string errMsg)
        {
            errMsg = string.Empty;
            try
            {
                string path = PathHelper.ParameterDataPath;
                if (!File.Exists(path))
                {
                    errMsg = "配置文件不存在";
                    return false;
                }
                List<ParameterData> datas = XmlHelper.DeserializeFromXml<List<ParameterData>>(path);
                ParameterData data = datas.FirstOrDefault(d => d.Name == name);
                if (data != null)
                {
                    data.Value = value.ToString();
                    XmlHelper.SerializeToXml(path, datas);
                    return true;
                }
                string type = "String";
                if (value is bool)
                {
                    type = "Bool";
                }
                else if (value is int)
                {
                    type = "Int";
                }
                else if (value is double)
                {
                    type = "Double";
                }
 
                data = new ParameterData
                {
                    Name = name,
                    Type = type,
                    Value = value.ToString()
                };
                datas.Add(data);
                XmlHelper.SerializeToXml(path, datas);
                return true;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
        }
    }
}