| | |
| | | |
| | | namespace Bro.Common.Helper |
| | | { |
| | | public class SettingHelper |
| | | public static 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 string ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE); |
| | | //static JObject data = null; |
| | | //static JObject Data |
| | | //{ |
| | | // get |
| | | // { |
| | | // 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 SettingInfo SettingInfo = null; |
| | | static string SettingPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE); |
| | | |
| | | public static JObject GetSettingData() |
| | | static SettingHelper() |
| | | { |
| | | JObject settingData = null; |
| | | if (File.Exists(ConfigPath)) |
| | | if (File.Exists(SettingPath)) |
| | | { |
| | | using (StreamReader reader = new StreamReader(ConfigPath, System.Text.Encoding.UTF8)) |
| | | using (StreamReader reader = new StreamReader(SettingPath, System.Text.Encoding.UTF8)) |
| | | { |
| | | string dataStr = reader.ReadToEnd(); |
| | | settingData = JsonConvert.DeserializeObject<JObject>(dataStr); |
| | | SettingInfo = JsonConvert.DeserializeObject<SettingInfo>(dataStr); |
| | | } |
| | | } |
| | | return settingData; |
| | | } |
| | | |
| | | public static List<string> GetProductionCodes() |
| | | { |
| | | List<string> codes = new List<string>(); |
| | | var Data = GetSettingData(); |
| | | if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES)) |
| | | if (SettingInfo.ProductionCodes.Count == 0) |
| | | { |
| | | codes = Data.Value<JArray>(PROPERTY_PRODUCTIONCODES).ToList().ConvertAll(u => u.ToString()); |
| | | SettingInfo.ProductionCodes.Add("Default"); |
| | | } |
| | | |
| | | if (codes.Count == 0) |
| | | if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProduction)) |
| | | { |
| | | codes.Add("Default"); |
| | | if (SettingInfo.ProductionCodes.Contains(SettingInfo.DefaultProduction)) |
| | | { |
| | | SettingInfo.ProductionCodes.Remove(SettingInfo.DefaultProduction); |
| | | SettingInfo.ProductionCodes.Insert(0, SettingInfo.DefaultProduction); |
| | | } |
| | | } |
| | | |
| | | return codes; |
| | | return SettingInfo.ProductionCodes; |
| | | } |
| | | |
| | | public static List<string> GetProcessCodes() |
| | | { |
| | | List<string> codes = new List<string>(); |
| | | var Data = GetSettingData(); |
| | | if (Data != null && Data.ContainsKey(PROPERTY_PROCESSCODES)) |
| | | if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProcess)) |
| | | { |
| | | codes = Data.Value<JArray>(PROPERTY_PROCESSCODES).ToList().ConvertAll(u => u.ToString()); |
| | | if (SettingInfo.ProcessCodes.Contains(SettingInfo.DefaultProcess)) |
| | | { |
| | | SettingInfo.ProcessCodes.Remove(SettingInfo.DefaultProcess); |
| | | SettingInfo.ProcessCodes.Insert(0, SettingInfo.DefaultProcess); |
| | | } |
| | | } |
| | | return codes; |
| | | |
| | | return SettingInfo.ProcessCodes; |
| | | } |
| | | |
| | | public static void AddNewProductionCode(string code) |
| | | { |
| | | var dataTemp = GetSettingData(); |
| | | |
| | | if (!dataTemp.ContainsKey(PROPERTY_PRODUCTIONCODES)) |
| | | if (!SettingInfo.ProductionCodes.Contains(code)) |
| | | { |
| | | dataTemp.Add(PROPERTY_PRODUCTIONCODES, new JArray()); |
| | | SettingInfo.ProductionCodes.Add(code); |
| | | } |
| | | |
| | | if (dataTemp != null && dataTemp.ContainsKey(PROPERTY_PRODUCTIONCODES)) |
| | | { |
| | | List<string> codes = GetProductionCodes(); |
| | | if (!codes.Contains(code)) |
| | | { |
| | | codes.Add(code); |
| | | } |
| | | dataTemp[PROPERTY_PRODUCTIONCODES] = new JArray(codes); |
| | | SaveSettingInfo(); |
| | | } |
| | | |
| | | string newDataStr = JsonConvert.SerializeObject(dataTemp, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); |
| | | using (StreamWriter writer = new StreamWriter(ConfigPath, false, System.Text.Encoding.UTF8)) |
| | | 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(); |
| | |
| | | } |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | |
| | | public static string GetConfigFilePath() |
| | | { |
| | | string path = ""; |
| | | var Data = GetSettingData(); |
| | | if (Data != null && Data.ContainsKey(PROPERTY_CONFIGPATH)) |
| | | { |
| | | path = Data.Value<string>(PROPERTY_CONFIGPATH); |
| | | } |
| | | public class SettingInfo |
| | | { |
| | | public List<string> ProcessCodes { get; set; } = new List<string>(); |
| | | public List<string> ProductionCodes { get; set; } = new List<string>(); |
| | | |
| | | return path; |
| | | } |
| | | public string DefaultProcess { get; set; } |
| | | |
| | | public static string GetProgramDescription() |
| | | { |
| | | string desc = "伯肯森自动化技术有限公司"; |
| | | var Data = GetSettingData(); |
| | | if (Data != null && Data.ContainsKey(PROPERTY_DESCRIPTION)) |
| | | { |
| | | desc = Data.Value<string>(PROPERTY_DESCRIPTION); |
| | | } |
| | | public string DefaultProduction { get; set; } |
| | | |
| | | return desc; |
| | | } |
| | | |
| | | public static string GetProgramIcon() |
| | | { |
| | | string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico"); |
| | | |
| | | if (!File.Exists(iconPath)) |
| | | { |
| | | iconPath = ""; |
| | | } |
| | | var Data = GetSettingData(); |
| | | if (Data != null && Data.ContainsKey(PROPERTY_ICONPATH)) |
| | | { |
| | | iconPath = Data.Value<string>(PROPERTY_ICONPATH); |
| | | } |
| | | |
| | | return iconPath; |
| | | } |
| | | public string ConfigPath { get; set; } |
| | | public string Description { get; set; } |
| | | public string IconPath { get; set; } |
| | | } |
| | | } |