alielie
2025-05-12 7bf89fd93bc487e8445d6d85bb527251b7371a04
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
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;
        }
    }
}