SettingConfig.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using AI.Common.Interface;
  2. using CustomizeDiagnosisSDK.Enums;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. namespace AICustomizeSDKDemo.Settings
  8. {
  9. public enum AIDiagnosisType
  10. {
  11. CustomizeDiagnosis
  12. }
  13. public class SettingConfig
  14. {
  15. public event EventHandler<List<SettingProperty>> ValueChangedEvent;
  16. public static SettingConfig Instance { get; private set; }
  17. public static SettingConfig PreInstance { get; private set; }
  18. /// <summary>
  19. /// 检测模式
  20. /// </summary>
  21. public EnumDetectMode DetectMode
  22. {
  23. get; set;
  24. }
  25. /// <summary>
  26. /// 检测间隔
  27. /// </summary>
  28. public int DetectTps
  29. {
  30. get; set;
  31. }
  32. /// <summary>
  33. /// 间隔时间
  34. /// </summary>
  35. public int IntervalTime
  36. {
  37. get; set;
  38. }
  39. /// <summary>
  40. /// AI开关
  41. /// </summary>
  42. public bool IsEnableAI
  43. {
  44. get;
  45. set;
  46. }
  47. /// <summary>
  48. /// 是否是视频
  49. /// </summary>
  50. public bool IsVideo
  51. {
  52. get
  53. {
  54. var fileExtension = Path.GetExtension(UltrasoundImageSource);
  55. if (fileExtension == ".mp4" || fileExtension == ".avi")
  56. {
  57. return true;
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// 超声图像来源
  67. /// </summary>
  68. public string UltrasoundImageSource
  69. {
  70. get; set;
  71. }
  72. /// <summary>
  73. /// 是否异步
  74. /// </summary>
  75. public bool IsAsync { get; set; }
  76. public AIDiagnosisType AIDiagnosisType { get; set; }
  77. static SettingConfig()
  78. {
  79. if (!LoadConfig())
  80. {
  81. Instance = new SettingConfig
  82. {
  83. UltrasoundImageSource = null,
  84. IsEnableAI = true,
  85. DetectTps = 2,
  86. DetectMode = EnumDetectMode.PeriodicIntervals,
  87. IntervalTime = 1000,
  88. AIDiagnosisType = AIDiagnosisType.CustomizeDiagnosis,
  89. IsAsync = false,
  90. };
  91. PreInstance = new SettingConfig
  92. {
  93. UltrasoundImageSource = null,
  94. IsEnableAI = true,
  95. DetectTps = 2,
  96. DetectMode = EnumDetectMode.PeriodicIntervals,
  97. IntervalTime = 1000,
  98. IsAsync = false,
  99. };
  100. }
  101. }
  102. private static bool LoadConfig()
  103. {
  104. var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json");
  105. if (!File.Exists(settingFilePath))
  106. {
  107. return false;
  108. }
  109. try
  110. {
  111. var jsonString = File.ReadAllText(settingFilePath);
  112. Instance = SerializeDeserializeHelper.DeserializeJsonResult<SettingConfig>(jsonString);
  113. PreInstance = SerializeDeserializeHelper.DeserializeJsonResult<SettingConfig>(jsonString);
  114. return true;
  115. }
  116. catch
  117. {
  118. }
  119. return false;
  120. }
  121. public void RaiseSavedEvent(List<SettingProperty> changeList)
  122. {
  123. Instance.ValueChangedEvent?.Invoke(this, changeList);
  124. }
  125. public async void Save()
  126. {
  127. var changeList = new List<SettingProperty>();
  128. if (Instance.UltrasoundImageSource != UltrasoundImageSource)
  129. {
  130. Instance.UltrasoundImageSource = UltrasoundImageSource;
  131. changeList.Add(SettingProperty.UltrasoundImageSource);
  132. }
  133. if (Instance.IsEnableAI != IsEnableAI)
  134. {
  135. Instance.IsEnableAI = IsEnableAI;
  136. changeList.Add(SettingProperty.IsEnableAI);
  137. }
  138. if (Instance.DetectTps != DetectTps)
  139. {
  140. Instance.DetectTps = DetectTps;
  141. changeList.Add(SettingProperty.DetectTps);
  142. }
  143. if (Instance.DetectMode != DetectMode)
  144. {
  145. Instance.DetectMode = DetectMode;
  146. changeList.Add(SettingProperty.DetectMode);
  147. }
  148. if (Instance.IntervalTime != IntervalTime)
  149. {
  150. Instance.IntervalTime = IntervalTime;
  151. changeList.Add(SettingProperty.IntervalTime);
  152. }
  153. if (Instance.AIDiagnosisType != AIDiagnosisType)
  154. {
  155. Instance.AIDiagnosisType = AIDiagnosisType;
  156. changeList.Add(SettingProperty.AIDiagnosisType);
  157. }
  158. if (Instance.IsAsync != IsAsync)
  159. {
  160. Instance.IsAsync = IsAsync;
  161. changeList.Add(SettingProperty.IsAsync);
  162. }
  163. var settingFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting");
  164. var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json");
  165. var jsonString = SerializeDeserializeHelper.SerializeToJsonString(Instance);
  166. if (!Directory.Exists(settingFolderPath))
  167. {
  168. Directory.CreateDirectory(settingFolderPath);
  169. }
  170. File.WriteAllText(settingFilePath, jsonString);
  171. if (changeList.Count > 0)
  172. {
  173. await Task.Run(() =>
  174. {
  175. Instance.ValueChangedEvent?.Invoke(this, changeList);
  176. });
  177. }
  178. }
  179. }
  180. }