SettingConfig.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using AI.DiagSystem;
  2. using AIDiagnosis.Common.Enums;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace AIDiagnosisDemo.Settings
  9. {
  10. public enum AIDiagnosisType
  11. {
  12. Normal,
  13. Myocardial,
  14. MiceMyocardial,
  15. RatMyocardial,
  16. ThyroidClassification,
  17. OrganIdentification
  18. }
  19. public class SettingConfig
  20. {
  21. public event EventHandler<List<SettingProperty>> ValueChangedEvent;
  22. public static SettingConfig Instance { get; private set; }
  23. public static SettingConfig PreInstance { get; private set; }
  24. /// <summary>
  25. /// CPU占用
  26. /// </summary>
  27. public int CPUUsed
  28. {
  29. get; set;
  30. }
  31. /// <summary>
  32. /// 图像裁剪
  33. /// </summary>
  34. public bool CropImage
  35. {
  36. get; set;
  37. }
  38. /// <summary>
  39. /// 检测模式
  40. /// </summary>
  41. public EnumDetectMode DetectMode
  42. {
  43. get; set;
  44. }
  45. /// <summary>
  46. /// 检测间隔
  47. /// </summary>
  48. public int DetectTps
  49. {
  50. get; set;
  51. }
  52. /// <summary>
  53. /// 间隔时间
  54. /// </summary>
  55. public int IntervalTime
  56. {
  57. get; set;
  58. }
  59. /// <summary>
  60. /// AI开关
  61. /// </summary>
  62. public bool IsEnableAI
  63. {
  64. get;
  65. set;
  66. }
  67. /// <summary>
  68. /// 模拟占用模式是否开启
  69. /// </summary>
  70. public bool IsSimulation
  71. {
  72. get; set;
  73. }
  74. /// <summary>
  75. /// 是否是视频
  76. /// </summary>
  77. public bool IsVideo
  78. {
  79. get
  80. {
  81. if (UltrasoundImageSourceType == Sourcetype.PictureOrVideoFile)
  82. {
  83. var fileExtension = Path.GetExtension(UltrasoundImageSource);
  84. if (fileExtension == ".mp4" || fileExtension == ".avi")
  85. {
  86. return true;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. else
  94. {
  95. return true;
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// AI性能
  101. /// </summary>
  102. public EnumPerformance Performance
  103. {
  104. get; set;
  105. }
  106. /// <summary>
  107. /// 内存占用
  108. /// </summary>
  109. public int RAMUsed
  110. {
  111. get; set;
  112. }
  113. /// <summary>
  114. /// 超声图像来源
  115. /// </summary>
  116. public string UltrasoundImageSource
  117. {
  118. get; set;
  119. }
  120. /// <summary>
  121. /// 超声图像来源类型
  122. /// </summary>
  123. public Sourcetype UltrasoundImageSourceType
  124. {
  125. get; set;
  126. }
  127. /// <summary>
  128. /// 显示方式
  129. /// </summary>
  130. public EnumDisplayType DisplayType
  131. {
  132. get; set;
  133. }
  134. /// <summary>
  135. /// 是否显示脏器轮廓
  136. /// </summary>
  137. public bool IsShowOrgansContour
  138. {
  139. get; set;
  140. }
  141. /// <summary>
  142. /// 是否播放循环TestImages文件夹内单帧图
  143. /// </summary>
  144. public bool IsCycleImagesPlay
  145. {
  146. get; set;
  147. }
  148. /// <summary>
  149. /// AI计算类型,按理来说应该全部合成到AIDiagnosisSDK,但是有一些AI组并没有合成到AIDiagSystemLib, 有些数据结构名字用途一样但是属于不同的dll,所以目前没合成的单独创建SDK project并引入。
  150. /// 后期AI组会统一合成到AIDiagSystemLib.dll.
  151. /// </summary>
  152. public AIDiagnosisType AIDiagnosisType { get; set; }
  153. /// <summary>
  154. /// 是否对心肌圆形轮廓进行补全
  155. /// </summary>
  156. public bool CompleteMyocardialImg { get; set; }
  157. static SettingConfig()
  158. {
  159. if (!LoadConfig())
  160. {
  161. Instance = new SettingConfig
  162. {
  163. UltrasoundImageSourceType = Sourcetype.PictureOrVideoFile,
  164. UltrasoundImageSource = null,
  165. IsEnableAI = true,
  166. Performance = EnumPerformance.Low,
  167. DetectTps = 2,
  168. CropImage = false,
  169. DetectMode = EnumDetectMode.PeriodicIntervals,
  170. IntervalTime = 1000,
  171. IsShowOrgansContour = true,
  172. DisplayType = EnumDisplayType.Border,
  173. IsSimulation = true,
  174. RAMUsed = 0,
  175. CPUUsed = 0,
  176. IsCycleImagesPlay = false,
  177. AIDiagnosisType = AIDiagnosisType.Normal
  178. };
  179. PreInstance = new SettingConfig
  180. {
  181. UltrasoundImageSourceType = Sourcetype.PictureOrVideoFile,
  182. UltrasoundImageSource = null,
  183. IsEnableAI = true,
  184. Performance = EnumPerformance.Low,
  185. DetectTps = 2,
  186. CropImage = false,
  187. DetectMode = EnumDetectMode.PeriodicIntervals,
  188. IntervalTime = 1000,
  189. IsShowOrgansContour = true,
  190. DisplayType = EnumDisplayType.Border,
  191. IsSimulation = true,
  192. RAMUsed = 0,
  193. CPUUsed = 0,
  194. IsCycleImagesPlay = false,
  195. AIDiagnosisType = AIDiagnosisType.Normal
  196. };
  197. }
  198. }
  199. private static bool LoadConfig()
  200. {
  201. var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json");
  202. if (!File.Exists(settingFilePath))
  203. {
  204. return false;
  205. }
  206. try
  207. {
  208. var jsonString = File.ReadAllText(settingFilePath);
  209. Instance = JsonConvert.DeserializeObject<SettingConfig>(jsonString);
  210. PreInstance = JsonConvert.DeserializeObject<SettingConfig>(jsonString);
  211. return true;
  212. }
  213. catch
  214. {
  215. }
  216. return false;
  217. }
  218. public void RaiseSavedEvent(List<SettingProperty> changeList)
  219. {
  220. Instance.ValueChangedEvent?.Invoke(this, changeList);
  221. }
  222. public async void Save()
  223. {
  224. var changeList = new List<SettingProperty>();
  225. if (Instance.UltrasoundImageSource != UltrasoundImageSource)
  226. {
  227. Instance.UltrasoundImageSource = UltrasoundImageSource;
  228. changeList.Add(SettingProperty.UltrasoundImageSource);
  229. }
  230. if (Instance.UltrasoundImageSourceType != UltrasoundImageSourceType)
  231. {
  232. Instance.UltrasoundImageSourceType = UltrasoundImageSourceType;
  233. changeList.Add(SettingProperty.UltrasoundImageSourceType);
  234. }
  235. if (Instance.IsEnableAI != IsEnableAI)
  236. {
  237. Instance.IsEnableAI = IsEnableAI;
  238. changeList.Add(SettingProperty.IsEnableAI);
  239. }
  240. if (Instance.Performance != Performance)
  241. {
  242. Instance.Performance = Performance;
  243. changeList.Add(SettingProperty.Performance);
  244. }
  245. if (Instance.CropImage != CropImage)
  246. {
  247. Instance.CropImage = CropImage;
  248. changeList.Add(SettingProperty.CropImage);
  249. }
  250. if (Instance.DetectTps != DetectTps)
  251. {
  252. Instance.DetectTps = DetectTps;
  253. changeList.Add(SettingProperty.DetectTps);
  254. }
  255. if (Instance.DetectMode != DetectMode)
  256. {
  257. Instance.DetectMode = DetectMode;
  258. changeList.Add(SettingProperty.DetectMode);
  259. }
  260. if (Instance.IntervalTime != IntervalTime)
  261. {
  262. Instance.IntervalTime = IntervalTime;
  263. changeList.Add(SettingProperty.IntervalTime);
  264. }
  265. if (Instance.RAMUsed != RAMUsed)
  266. {
  267. Instance.RAMUsed = RAMUsed;
  268. changeList.Add(SettingProperty.RAMUsed);
  269. }
  270. if (Instance.CPUUsed != CPUUsed)
  271. {
  272. Instance.CPUUsed = CPUUsed;
  273. changeList.Add(SettingProperty.CPUUsed);
  274. }
  275. if (Instance.IsSimulation != IsSimulation)
  276. {
  277. Instance.IsSimulation = IsSimulation;
  278. changeList.Add(SettingProperty.IsSimulation);
  279. }
  280. if (Instance.DisplayType != DisplayType)
  281. {
  282. Instance.DisplayType = DisplayType;
  283. changeList.Add(SettingProperty.DisplayType);
  284. }
  285. if (Instance.IsShowOrgansContour != IsShowOrgansContour)
  286. {
  287. Instance.IsShowOrgansContour = IsShowOrgansContour;
  288. changeList.Add(SettingProperty.IsShowOrgansContour);
  289. }
  290. if (Instance.AIDiagnosisType != AIDiagnosisType)
  291. {
  292. Instance.AIDiagnosisType = AIDiagnosisType;
  293. changeList.Add(SettingProperty.AIDiagnosisType);
  294. }
  295. if (Instance.CompleteMyocardialImg != CompleteMyocardialImg)
  296. {
  297. Instance.CompleteMyocardialImg = CompleteMyocardialImg;
  298. changeList.Add(SettingProperty.AIDiagnosisType);
  299. }
  300. var settingFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting");
  301. var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json");
  302. var jsonString = JsonConvert.SerializeObject(Instance);
  303. if (!Directory.Exists(settingFolderPath))
  304. {
  305. Directory.CreateDirectory(settingFolderPath);
  306. }
  307. File.WriteAllText(settingFilePath, jsonString);
  308. if (changeList.Count > 0)
  309. {
  310. await Task.Run(() =>
  311. {
  312. Instance.ValueChangedEvent?.Invoke(this, changeList);
  313. });
  314. }
  315. }
  316. }
  317. }