ConfigHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using SWRIS.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Threading;
  10. namespace SWRIS.Core
  11. {
  12. public class ConfigHelper
  13. {
  14. public static readonly string AesKey = "mhSnwPLLL5T6ITmLJQogp3et5U2gYw5wiLtdZaZajRU="; // 32字节的Base64字符串
  15. public static readonly string AesIV = "Z/stNJl+kA7G02oBlpeERA=="; // 16字节的Base64字符串
  16. private static readonly string filePath = AppDomain.CurrentDomain.BaseDirectory + "Config/config.json";
  17. private static readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
  18. private const int LockTimeout = 5000; // 5秒超时
  19. public static ConfigModel Read()
  20. {
  21. try
  22. {
  23. if (!_lock.TryEnterReadLock(LockTimeout))
  24. {
  25. LogHelper.Error("获取读取锁超时!");
  26. return null;
  27. }
  28. try
  29. {
  30. if (File.Exists(filePath))
  31. {
  32. using (StreamReader r = new StreamReader(filePath))
  33. {
  34. var json = r.ReadToEnd();
  35. var config = JsonConvert.DeserializeObject<ConfigModel>(json);
  36. if (config.Calibration.Tcp == null)
  37. {
  38. config.Calibration.Tcp = new TcpSettings();
  39. }
  40. if (config.Calibration.Serial == null)
  41. {
  42. config.Calibration.Serial = new SerialSettings();
  43. }
  44. if (config.Calibration.Limits == null)
  45. {
  46. config.Calibration.Limits = new List<LimitData> { new LimitData(), new LimitData() };
  47. }
  48. else
  49. {
  50. // 补充缺少的元素
  51. while (config.Calibration.Limits.Count < 2)
  52. {
  53. config.Calibration.Limits.Add(new LimitData());
  54. }
  55. }
  56. return config;
  57. }
  58. }
  59. return null;
  60. }
  61. finally
  62. {
  63. _lock.ExitReadLock();
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. LogHelper.Error("读取配置文件失败!", ex);
  69. return null;
  70. }
  71. }
  72. public static bool Set(ConfigModel config)
  73. {
  74. try
  75. {
  76. if (!_lock.TryEnterWriteLock(LockTimeout))
  77. {
  78. LogHelper.Error("获取写入锁超时!");
  79. return false;
  80. }
  81. try
  82. {
  83. string jsonData = JsonConvert.SerializeObject(config, Formatting.Indented);
  84. File.WriteAllText(filePath, jsonData);
  85. return true;
  86. }
  87. finally
  88. {
  89. _lock.ExitWriteLock();
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. LogHelper.Error("保存配置文件失败!", ex);
  95. return false;
  96. }
  97. }
  98. [Obfuscation(Feature = "virtualization", Exclude = false)]
  99. public static bool SetEquipment(EquipmentModel equipmentData)
  100. {
  101. // 参数检查
  102. if (equipmentData == null || equipmentData.Parameter == null)
  103. {
  104. return false;
  105. }
  106. try
  107. {
  108. // 获取写入锁
  109. if (!_lock.TryEnterWriteLock(LockTimeout))
  110. {
  111. LogHelper.Error("获取写入锁超时!");
  112. return false;
  113. }
  114. try
  115. {
  116. // 检查文件是否存在
  117. if (!File.Exists(filePath))
  118. {
  119. return false;
  120. }
  121. // 读取JSON文件
  122. JObject jsonObject;
  123. using (StreamReader reader = File.OpenText(filePath))
  124. using (JsonTextReader jsonTextReader = new JsonTextReader(reader))
  125. {
  126. jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
  127. }
  128. // 获取设备数组
  129. if (!(jsonObject["Equipments"] is JArray equipments)) return false;
  130. // 查找要修改的设备(以 RopeNumber 为查询条件)
  131. var targetEquipment = equipments.FirstOrDefault(c =>
  132. c["RopeNumber"] != null &&
  133. c["RopeNumber"].ToObject<int>() == equipmentData.RopeNumber);
  134. if (targetEquipment == null) return false;
  135. // 关键修改点:先移除旧设备,再添加新设备
  136. equipments.Remove(targetEquipment);
  137. equipments.Add(JObject.FromObject(equipmentData));
  138. string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  139. File.WriteAllText(filePath, output);
  140. return true;
  141. }
  142. finally
  143. {
  144. _lock.ExitWriteLock();
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. LogHelper.Error("保存配置文件失败!", ex);
  150. return false;
  151. }
  152. }
  153. }
  154. }