领胜LDS 键盘AOI检测项目
patrick.xu
2020-12-16 5241a8f6377dfd1618610dd15fd05ed6f51c8ba2
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace Bro.Common.Helper
{
    public static class SettingHelper
    {
        const string SETTINGFILE = "Setting.json";
 
        public static SettingInfo SettingInfo = null;
        static string SettingPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
 
        static SettingHelper()
        {
            if (File.Exists(SettingPath))
            {
                using (StreamReader reader = new StreamReader(SettingPath, System.Text.Encoding.UTF8))
                {
                    string dataStr = reader.ReadToEnd();
                    SettingInfo = JsonConvert.DeserializeObject<SettingInfo>(dataStr);
                }
            }
        }
 
        public static List<string> GetProductionCodes()
        {
            if (SettingInfo.ProductionCodes.Count == 0)
            {
                SettingInfo.ProductionCodes.Add("Default");
            }
 
            if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProduction))
            {
                if (SettingInfo.ProductionCodes.Contains(SettingInfo.DefaultProduction))
                {
                    SettingInfo.ProductionCodes.Remove(SettingInfo.DefaultProduction);
                    SettingInfo.ProductionCodes.Insert(0, SettingInfo.DefaultProduction);
                }
            }
 
            return SettingInfo.ProductionCodes;
        }
 
        public static List<string> GetProcessCodes()
        {
            if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProcess))
            {
                if (SettingInfo.ProcessCodes.Contains(SettingInfo.DefaultProcess))
                {
                    SettingInfo.ProcessCodes.Remove(SettingInfo.DefaultProcess);
                    SettingInfo.ProcessCodes.Insert(0, SettingInfo.DefaultProcess);
                }
            }
 
            return SettingInfo.ProcessCodes;
        }
 
        public static void AddNewProductionCode(string code)
        {
            if (!SettingInfo.ProductionCodes.Contains(code))
            {
                SettingInfo.ProductionCodes.Add(code);
            }
 
            SaveSettingInfo();
        }
 
        public static string GetConfigFilePath()
        {
            return SettingInfo.ConfigPath;
        }
 
        public static string GetProgramDescription()
        {
            if (string.IsNullOrWhiteSpace(SettingInfo.Description))
            {
                SettingInfo.Description = "伯肯森自动化技术有限公司";
            }
 
            return SettingInfo.Description;
        }
 
        public static string GetProgramIcon()
        {
            string iconPath = SettingInfo.IconPath;
            if (string.IsNullOrWhiteSpace(iconPath) || !File.Exists(iconPath))
            {
                iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
            }
 
            if (!File.Exists(iconPath))
            {
                iconPath = "";
            }
            return iconPath;
        }
 
        #region 保存
        public static void SetDefaultProcess(string processCode)
        {
            if (SettingInfo.DefaultProcess != processCode)
            {
                SettingInfo.DefaultProcess = processCode;
                SaveSettingInfo();
            }
        }
 
        public static void SetDefaultProduction(string productionCode)
        {
            if (SettingInfo.DefaultProduction != productionCode)
            {
                SettingInfo.DefaultProduction = productionCode;
                SaveSettingInfo();
            }
        }
 
        public static void SetDefault(string processCode,string productionCode)
        {
            bool isChanged = false;
            if (SettingInfo.DefaultProcess != processCode)
            {
                isChanged = true;
                SettingInfo.DefaultProcess = processCode;
            }
 
            if (SettingInfo.DefaultProduction != productionCode)
            {
                isChanged = true;
                SettingInfo.DefaultProduction = productionCode;
            }
 
            if (isChanged)
            {
                SaveSettingInfo();
            }
        }
 
        static object saveLock = new object();
        private static void SaveSettingInfo()
        {
            string newDataStr = JsonConvert.SerializeObject(SettingInfo);
            lock (saveLock)
            {
                using (StreamWriter writer = new StreamWriter(SettingPath, false, System.Text.Encoding.UTF8))
                {
                    writer.Write(newDataStr);
                    writer.Flush();
                    writer.Close();
                }
            }
        }
        #endregion
    }
 
    public class SettingInfo
    {
        public List<string> ProcessCodes { get; set; } = new List<string>();
        public List<string> ProductionCodes { get; set; } = new List<string>();
 
        public string DefaultProcess { get; set; }
 
        public string DefaultProduction { get; set; }
 
        public string ConfigPath { get; set; }
        public string Description { get; set; }
        public string IconPath { get; set; }
    }
}