AppSettingConfig.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Newtonsoft.Json.Converters;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace InitializerModel
  11. {
  12. public static class AppSettingConfig
  13. {
  14. private static AppSettingConfigModel _Instance;
  15. public static AppSettingConfigModel Instance
  16. {
  17. get
  18. {
  19. if (_Instance == null)
  20. {
  21. Reload();
  22. }
  23. return _Instance;
  24. }
  25. private set => _Instance = value;
  26. }
  27. //private string _FirmwareRoot, _Language, _MES, _MechineCode;
  28. //private bool _IsDisableAuthRequired;
  29. //private string _JohnSenLedComPort;
  30. private static string settingFileName = "config.ini";
  31. //public AppSettingConfig()
  32. //{
  33. // if (!File.Exists(settingFileName))
  34. // {
  35. // LoadDefaultSetting();
  36. // }
  37. // else
  38. // {
  39. // LoadSettingFromDefaultFile();
  40. // }
  41. //}
  42. public static void Reload()
  43. {
  44. //Instance = new AppSettingConfig();
  45. if (!File.Exists(settingFileName))
  46. {
  47. LoadDefaultSetting();
  48. }
  49. else
  50. {
  51. LoadSettingFromDefaultFile();
  52. }
  53. }
  54. public static void Save()
  55. {
  56. //var toSave = new AppSettingConfigModel();
  57. //toSave.FirmwareRoot = _FirmwareRoot;
  58. //toSave.Language = _Language;
  59. //toSave.MES = _MES;
  60. //toSave.MechineCode = _MechineCode;
  61. //toSave.IsDisableAuthRequired = _IsDisableAuthRequired;
  62. File.WriteAllText(
  63. settingFileName,
  64. JsonConvert.SerializeObject(
  65. Instance,
  66. Formatting.Indented,
  67. new StringEnumConverter()
  68. ));
  69. }
  70. private static void LoadSettingFromDefaultFile()
  71. {
  72. try
  73. {
  74. LoadDefaultSetting();
  75. var configStr = File.ReadAllText(settingFileName);
  76. var config = Newtonsoft.Json.JsonConvert.DeserializeObject<AppSettingConfigModel>(configStr);
  77. CompacityChnage(config);
  78. _Instance = config;
  79. //_Instance.FirmwareRoot = config.FirmwareRoot;
  80. //_Language = config.Language;
  81. //_MES = config.MES;
  82. //_MechineCode = config.MechineCode;
  83. //_IsDisableAuthRequired = config.IsDisableAuthRequired;
  84. //_JohnSenLedComPort = config.JohnSenLedComPort;
  85. }
  86. catch
  87. {
  88. }
  89. }
  90. private static void LoadDefaultSetting()
  91. {
  92. _Instance = new AppSettingConfigModel()
  93. {
  94. FirmwareRoot = "./Firmware",
  95. Language = "zh-chs",
  96. MES = "sajet2",
  97. MechineCode = "testMechineCode"
  98. };
  99. }
  100. private static void CompacityChnage(AppSettingConfigModel config)
  101. {
  102. if (config == null)
  103. {
  104. return;
  105. }
  106. if (config.Language.ToLower() == "zh-chs")
  107. {
  108. config.Language = "zh-CHS";
  109. }
  110. if (config.Language.ToLower() == "en-us")
  111. {
  112. config.Language = "en-US";
  113. }
  114. }
  115. }
  116. }