领胜LDS 键盘AOI检测项目
wells.liu
2020-07-02 578014f827b6871833cbfa6e781e05d1f9397995
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace Bro.Common.Helper
{
    public class SettingHelper
    {
        const string SETTINGFILE = "Setting.json";
        const string PROPERTY_PRODUCTIONCODES = "ProductionCodes";
        const string PROPERTY_CONFIGPATH = "ConfigPath";
        const string PROPERTY_PROCESSCODES = "ProcessCodes";
        const string PROPERTY_DESCRIPTION = "Description";
        const string PROPERTY_ICONPATH = "IconPath";
 
        static JObject data = null;
        static JObject Data
        {
            get
            {
                string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
                if (File.Exists(configPath))
                {
                    using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8))
                    {
                        string dataStr = reader.ReadToEnd();
                        data = JsonConvert.DeserializeObject<JObject>(dataStr);
                    }
                }
 
                return data;
            }
        }
 
        public static List<string> GetProductionCodes()
        {
            List<string> codes = new List<string>();
 
            if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES))
            {
                codes = Data.Value<JArray>(PROPERTY_PRODUCTIONCODES).ToList().ConvertAll(u => u.ToString());
            }
 
            if (codes.Count == 0)
            {
                codes.Add("Default");
            }
 
            return codes;
        }
 
        public static List<string> GetProcessCodes()
        {
            List<string> codes = new List<string>();
 
            if (Data != null && Data.ContainsKey(PROPERTY_PROCESSCODES))
            {
                codes = Data.Value<JArray>(PROPERTY_PROCESSCODES).ToList().ConvertAll(u => u.ToString());
            }
            return codes;
        }
 
        public static List<string> AddNewProductionCode(string code)
        {
            List<string> codes = GetProcessCodes();
            if (!codes.Contains(code))
            {
                codes.Add(code);
            }
            if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES))
            {
                Data[PROPERTY_PRODUCTIONCODES] = new JArray(codes);
                string newDataStr = JsonConvert.SerializeObject(Data, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
                using (StreamWriter writer = new StreamWriter(GetSettingFilePath(), false, System.Text.Encoding.UTF8))
                {
                    writer.Write(newDataStr);
                    writer.Flush();
                    writer.Close();
                }
            }
 
            return GetProcessCodes();
        }
 
        public static string GetConfigFilePath()
        {
            string path = "";
 
            if (Data != null && Data.ContainsKey(PROPERTY_CONFIGPATH))
            {
                path = Data.Value<string>(PROPERTY_CONFIGPATH);
            }
 
            return path;
        }
 
        public static string GetSettingFilePath()
        {
            string path = "";
 
            if (Data != null && Data.ContainsKey(SETTINGFILE))
            {
                path = Data.Value<string>(SETTINGFILE);
            }
 
            return path;
        }
 
        public static string GetProgramDescription()
        {
            string desc = "伯肯森自动化技术有限公司";
 
            if (Data != null && Data.ContainsKey(PROPERTY_DESCRIPTION))
            {
                desc = Data.Value<string>(PROPERTY_DESCRIPTION);
            }
 
            return desc;
        }
 
        public static string GetProgramIcon()
        {
            string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
 
            if (!File.Exists(iconPath))
            {
                iconPath = "";
            }
 
            if (Data != null && Data.ContainsKey(PROPERTY_ICONPATH))
            {
                iconPath = Data.Value<string>(PROPERTY_ICONPATH);
            }
 
            return iconPath;
        }
    }
}