From 96b6869bb20677f9b945f67c014a9b992ee05ac4 Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期三, 28 四月 2021 11:40:01 +0800 Subject: [PATCH] 1. 添加离线测试功能 2. 单键分开测量修改赋值和释放时判断 --- src/Bro.Common.Model/Helper/SettingHelper.cs | 197 ++++++++++++++++++++++++++++++------------------- 1 files changed, 120 insertions(+), 77 deletions(-) diff --git a/src/Bro.Common.Model/Helper/SettingHelper.cs b/src/Bro.Common.Model/Helper/SettingHelper.cs index f481646..f22e684 100644 --- a/src/Bro.Common.Model/Helper/SettingHelper.cs +++ b/src/Bro.Common.Model/Helper/SettingHelper.cs @@ -7,123 +7,166 @@ 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 + public static SettingInfo SettingInfo = null; + static string SettingPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE); + + static SettingHelper() { - get + if (File.Exists(SettingPath)) { - if (File.Exists(ConfigPath)) + using (StreamReader reader = new StreamReader(SettingPath, System.Text.Encoding.UTF8)) { - using (StreamReader reader = new StreamReader(ConfigPath, System.Text.Encoding.UTF8)) - { - string dataStr = reader.ReadToEnd(); - data = JsonConvert.DeserializeObject<JObject>(dataStr); - } + string dataStr = reader.ReadToEnd(); + SettingInfo = JsonConvert.DeserializeObject<SettingInfo>(dataStr); } - return data; } } public static List<string> GetProductionCodes() { - List<string> codes = new List<string>(); - - 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>(); - - 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 List<string> AddNewProductionCode(string code) + public static void AddNewProductionCode(string code) { - List<string> codes = GetProductionCodes(); - if (!codes.Contains(code)) + if (!SettingInfo.ProductionCodes.Contains(code)) { - codes.Add(code); + SettingInfo.ProductionCodes.Add(code); } - var dataTemp = Data; - if (dataTemp != null && dataTemp.ContainsKey(PROPERTY_PRODUCTIONCODES)) - { - dataTemp[PROPERTY_PRODUCTIONCODES] = new JArray(codes); - string newDataStr = JsonConvert.SerializeObject(dataTemp, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); - using (StreamWriter writer = new StreamWriter(ConfigPath, false, System.Text.Encoding.UTF8)) + 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(); } } - return GetProductionCodes(); } + #endregion + } - public static string GetConfigFilePath() - { - string path = ""; + public class SettingInfo + { + public List<string> ProcessCodes { get; set; } = new List<string>(); + public List<string> ProductionCodes { get; set; } = new List<string>(); - if (Data != null && Data.ContainsKey(PROPERTY_CONFIGPATH)) - { - path = Data.Value<string>(PROPERTY_CONFIGPATH); - } + public string DefaultProcess { get; set; } - return path; - } + public string DefaultProduction { get; set; } - 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; - } + public string ConfigPath { get; set; } + public string Description { get; set; } + public string IconPath { get; set; } } } -- Gitblit v1.8.0