ConfigHelper.cs 5.8 KB

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