using AI.Common.Interface; using CustomizeDiagnosisSDK.Enums; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace AICustomizeSDKDemo.Settings { public enum AIDiagnosisType { CustomizeDiagnosis } public class SettingConfig { public event EventHandler> ValueChangedEvent; public static SettingConfig Instance { get; private set; } public static SettingConfig PreInstance { get; private set; } /// /// 检测模式 /// public EnumDetectMode DetectMode { get; set; } /// /// 检测间隔 /// public int DetectTps { get; set; } /// /// 间隔时间 /// public int IntervalTime { get; set; } /// /// AI开关 /// public bool IsEnableAI { get; set; } /// /// 是否是视频 /// public bool IsVideo { get { var fileExtension = Path.GetExtension(UltrasoundImageSource); if (fileExtension == ".mp4" || fileExtension == ".avi") { return true; } else { return false; } } } /// /// 超声图像来源 /// public string UltrasoundImageSource { get; set; } /// /// 是否异步 /// public bool IsAsync { get; set; } public AIDiagnosisType AIDiagnosisType { get; set; } static SettingConfig() { if (!LoadConfig()) { Instance = new SettingConfig { UltrasoundImageSource = null, IsEnableAI = true, DetectTps = 2, DetectMode = EnumDetectMode.PeriodicIntervals, IntervalTime = 1000, AIDiagnosisType = AIDiagnosisType.CustomizeDiagnosis, IsAsync = false, }; PreInstance = new SettingConfig { UltrasoundImageSource = null, IsEnableAI = true, DetectTps = 2, DetectMode = EnumDetectMode.PeriodicIntervals, IntervalTime = 1000, IsAsync = false, }; } } private static bool LoadConfig() { var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json"); if (!File.Exists(settingFilePath)) { return false; } try { var jsonString = File.ReadAllText(settingFilePath); Instance = SerializeDeserializeHelper.DeserializeJsonResult(jsonString); PreInstance = SerializeDeserializeHelper.DeserializeJsonResult(jsonString); return true; } catch { } return false; } public void RaiseSavedEvent(List changeList) { Instance.ValueChangedEvent?.Invoke(this, changeList); } public async void Save() { var changeList = new List(); if (Instance.UltrasoundImageSource != UltrasoundImageSource) { Instance.UltrasoundImageSource = UltrasoundImageSource; changeList.Add(SettingProperty.UltrasoundImageSource); } if (Instance.IsEnableAI != IsEnableAI) { Instance.IsEnableAI = IsEnableAI; changeList.Add(SettingProperty.IsEnableAI); } if (Instance.DetectTps != DetectTps) { Instance.DetectTps = DetectTps; changeList.Add(SettingProperty.DetectTps); } if (Instance.DetectMode != DetectMode) { Instance.DetectMode = DetectMode; changeList.Add(SettingProperty.DetectMode); } if (Instance.IntervalTime != IntervalTime) { Instance.IntervalTime = IntervalTime; changeList.Add(SettingProperty.IntervalTime); } if (Instance.AIDiagnosisType != AIDiagnosisType) { Instance.AIDiagnosisType = AIDiagnosisType; changeList.Add(SettingProperty.AIDiagnosisType); } if (Instance.IsAsync != IsAsync) { Instance.IsAsync = IsAsync; changeList.Add(SettingProperty.IsAsync); } var settingFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting"); var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json"); var jsonString = SerializeDeserializeHelper.SerializeToJsonString(Instance); if (!Directory.Exists(settingFolderPath)) { Directory.CreateDirectory(settingFolderPath); } File.WriteAllText(settingFilePath, jsonString); if (changeList.Count > 0) { await Task.Run(() => { Instance.ValueChangedEvent?.Invoke(this, changeList); }); } } } }