123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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<List<SettingProperty>> ValueChangedEvent;
- public static SettingConfig Instance { get; private set; }
- public static SettingConfig PreInstance { get; private set; }
- /// <summary>
- /// 检测模式
- /// </summary>
- public EnumDetectMode DetectMode
- {
- get; set;
- }
- /// <summary>
- /// 检测间隔
- /// </summary>
- public int DetectTps
- {
- get; set;
- }
- /// <summary>
- /// 间隔时间
- /// </summary>
- public int IntervalTime
- {
- get; set;
- }
- /// <summary>
- /// AI开关
- /// </summary>
- public bool IsEnableAI
- {
- get;
- set;
- }
- /// <summary>
- /// 是否是视频
- /// </summary>
- public bool IsVideo
- {
- get
- {
- var fileExtension = Path.GetExtension(UltrasoundImageSource);
- if (fileExtension == ".mp4" || fileExtension == ".avi")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- /// <summary>
- /// 超声图像来源
- /// </summary>
- public string UltrasoundImageSource
- {
- get; set;
- }
- /// <summary>
- /// 是否异步
- /// </summary>
- 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<SettingConfig>(jsonString);
- PreInstance = SerializeDeserializeHelper.DeserializeJsonResult<SettingConfig>(jsonString);
- return true;
- }
- catch
- {
- }
- return false;
- }
- public void RaiseSavedEvent(List<SettingProperty> changeList)
- {
- Instance.ValueChangedEvent?.Invoke(this, changeList);
- }
- public async void Save()
- {
- var changeList = new List<SettingProperty>();
- 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);
- });
- }
- }
- }
- }
|