using CoreUnit.Common; using Newtonsoft.Json.Linq; using System; using System.Configuration; using System.IO; using System.Linq; namespace DB { public class RecipeExIds { public const string RECIPE_DIR = "_config_recipe"; public const string SETTING_DIR = "_config_setting"; public const string TEACHING_EDITOR_RECIPE = "TeachingEditor.json"; public const string SYSTEM_PARAMETER = "SystemParameterILSOS.json"; public const string CAPTURED_TEACHING = "CapturedTeaching.bmp"; } public class LASARecipeHelper { static string mSystemParameterPath = string.Empty; static string SytemParameterPath { get { if (string.IsNullOrWhiteSpace(mSystemParameterPath)) { mSystemParameterPath = Path.Combine( ConfigurationManager.AppSettings.Get("DATA_PATH"), RecipeExIds.SETTING_DIR, RecipeExIds.SYSTEM_PARAMETER); } return mSystemParameterPath; } } static bool CopyIfNotExist(string path) { if (!File.Exists(path)) { var dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } File.Copy(@"..\..\..\External\LaserScanner\SystemRecipe.json", path); return true; } return false; } public static object GetValue(string ID, object defaultValue) { CopyIfNotExist(SytemParameterPath); var recipeStr = File.ReadAllText(SytemParameterPath); try { if (!string.IsNullOrWhiteSpace(recipeStr)) { var jproperties = JObject.Parse(recipeStr); return jproperties.FirstOrDefault(ID); } } catch (Exception ex) { var message = ex.Message; } return defaultValue; } public static void SaveValues(string ID, object value) { CopyIfNotExist(SytemParameterPath); try { var recipeStr = File.ReadAllText(SytemParameterPath); if (!string.IsNullOrWhiteSpace(recipeStr)) { var jproperties = JObject.Parse(recipeStr); var firstToken = jproperties.FindTokens(ID); if (firstToken.Count() == 0) { jproperties.Add(ID, value.ToString()); File.WriteAllText(SytemParameterPath, jproperties.ToString()); } else { jproperties.Remove(ID); jproperties.Add(ID, value.ToString()); File.WriteAllText(SytemParameterPath, jproperties.ToString()); } } else { var newProperty = new JObject { new JProperty(ID, value) }; File.WriteAllText(SytemParameterPath, newProperty.ToString()); } } catch (Exception ex) { var message = ex.Message; } } } }