SettingViewModel.System.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Prism.Commands;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using TeamAAS.Communication;
  7. using TeamAAS.Communication.Config;
  8. using TeamAAS.Models;
  9. using TeamAAS.Theme;
  10. namespace TeamAAS.ViewModels
  11. {
  12. /// <summary>设置页 - 通用设置(字体/标题)与全局事件配置(心跳/切换产品)</summary>
  13. public partial class SettingViewModel
  14. {
  15. #region 通用设置
  16. private SystemConfiguration _systemConfig;
  17. public SystemConfiguration SystemConfig
  18. {
  19. get => _systemConfig;
  20. set => SetProperty(ref _systemConfig, value);
  21. }
  22. public DelegateCommand SaveSystemConfigCommand { get; }
  23. public DelegateCommand LoadSystemConfigCommand { get; }
  24. /// <summary>日志详细度下拉源(粗略/详细),供「默认存储/显示详细度」两个下拉共用</summary>
  25. public Array LogVerbosities { get; } = System.Enum.GetValues(typeof(TeamAAS.Logging.LogVerbosity));
  26. private string GetSystemConfigPath() => PathHelper.SystemConfigurationFile;
  27. private void SaveSystemConfig()
  28. {
  29. try
  30. {
  31. App.SystemConfig = _systemConfig;
  32. App.UpdateFontResources();
  33. ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor); // 标题/字体/主题/主色改动立即生效
  34. App.SaveSystemConfig();
  35. }
  36. catch { }
  37. }
  38. private void LoadSystemConfig()
  39. {
  40. try
  41. {
  42. var path = GetSystemConfigPath();
  43. if (!File.Exists(path))
  44. {
  45. SystemConfig = new SystemConfiguration();
  46. App.SystemConfig = SystemConfig;
  47. App.UpdateFontResources();
  48. ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor);
  49. return;
  50. }
  51. var json = File.ReadAllText(path);
  52. SystemConfig = JsonSerializer.Deserialize<SystemConfiguration>(json) ?? new SystemConfiguration();
  53. App.SystemConfig = SystemConfig;
  54. App.UpdateFontResources();
  55. ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor);
  56. }
  57. catch
  58. {
  59. SystemConfig = new SystemConfiguration();
  60. App.SystemConfig = SystemConfig;
  61. App.UpdateFontResources();
  62. ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor);
  63. }
  64. }
  65. #endregion
  66. #region 全局事件配置
  67. private GlobalEventConfig _globalEventConfig;
  68. public GlobalEventConfig GlobalEventConfig
  69. {
  70. get => _globalEventConfig;
  71. set => SetProperty(ref _globalEventConfig, value);
  72. }
  73. public DelegateCommand SaveGlobalEventConfigCommand { get; }
  74. public DelegateCommand LoadGlobalEventConfigCommand { get; }
  75. public DelegateCommand AddHeartbeatCommand { get; }
  76. public DelegateCommand<HeartbeatConfig> RemoveHeartbeatCommand { get; }
  77. public DelegateCommand AddProductSwitchEventCommand { get; }
  78. public DelegateCommand<ProductSwitchEvent> RemoveProductSwitchEventCommand { get; }
  79. private string GetGlobalEventConfigPath() => PathHelper.GlobalEventConfigFile;
  80. private void SaveGlobalEventConfig()
  81. {
  82. try
  83. {
  84. var json = JsonSerializer.Serialize(_globalEventConfig, new JsonSerializerOptions { WriteIndented = true });
  85. File.WriteAllText(GetGlobalEventConfigPath(), json);
  86. // 配置变更后立即刷新心跳监听(新增/删除/修改立即生效)
  87. TeamAAS.FlowEngine.Execution.FlowRunner.Instance.StartHeartbeat();
  88. }
  89. catch { }
  90. }
  91. private void LoadGlobalEventConfig()
  92. {
  93. try
  94. {
  95. var path = GetGlobalEventConfigPath();
  96. if (!File.Exists(path))
  97. {
  98. _globalEventConfig = new GlobalEventConfig();
  99. return;
  100. }
  101. var json = File.ReadAllText(path);
  102. _globalEventConfig = JsonSerializer.Deserialize<GlobalEventConfig>(json) ?? new GlobalEventConfig();
  103. }
  104. catch
  105. {
  106. _globalEventConfig = new GlobalEventConfig();
  107. }
  108. }
  109. private void AddHeartbeat()
  110. {
  111. if (_globalEventConfig == null) _globalEventConfig = new GlobalEventConfig();
  112. _globalEventConfig.Heartbeats.Add(new HeartbeatConfig
  113. {
  114. DeviceName = $"心跳{_globalEventConfig.Heartbeats.Count + 1}",
  115. IntervalMs = 3000,
  116. TimeoutMs = 1000,
  117. Enabled = true
  118. });
  119. }
  120. private void RemoveHeartbeat(HeartbeatConfig hb)
  121. {
  122. if (_globalEventConfig == null || hb == null) return;
  123. _globalEventConfig.Heartbeats.Remove(hb);
  124. }
  125. private void AddProductSwitchEvent()
  126. {
  127. if (_globalEventConfig == null) _globalEventConfig = new GlobalEventConfig();
  128. int nextSeq = _globalEventConfig.ProductSwitchEvents.Count > 0
  129. ? _globalEventConfig.ProductSwitchEvents.Max(x => x.Sequence) + 1
  130. : 1;
  131. _globalEventConfig.ProductSwitchEvents.Add(new ProductSwitchEvent
  132. {
  133. Sequence = nextSeq,
  134. ProductName = $"产品{nextSeq}",
  135. });
  136. }
  137. private void RemoveProductSwitchEvent(ProductSwitchEvent evt)
  138. {
  139. if (_globalEventConfig == null || evt == null) return;
  140. _globalEventConfig.ProductSwitchEvents.Remove(evt);
  141. }
  142. #endregion
  143. }
  144. }