using Prism.Commands; using System; using System.IO; using System.Linq; using System.Text.Json; using TeamAAS.Communication; using TeamAAS.Communication.Config; using TeamAAS.Models; using TeamAAS.Theme; namespace TeamAAS.ViewModels { /// 设置页 - 通用设置(字体/标题)与全局事件配置(心跳/切换产品) public partial class SettingViewModel { #region 通用设置 private SystemConfiguration _systemConfig; public SystemConfiguration SystemConfig { get => _systemConfig; set => SetProperty(ref _systemConfig, value); } public DelegateCommand SaveSystemConfigCommand { get; } public DelegateCommand LoadSystemConfigCommand { get; } /// 日志详细度下拉源(粗略/详细),供「默认存储/显示详细度」两个下拉共用 public Array LogVerbosities { get; } = System.Enum.GetValues(typeof(TeamAAS.Logging.LogVerbosity)); private string GetSystemConfigPath() => PathHelper.SystemConfigurationFile; private void SaveSystemConfig() { try { App.SystemConfig = _systemConfig; App.UpdateFontResources(); ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor); // 标题/字体/主题/主色改动立即生效 App.SaveSystemConfig(); } catch { } } private void LoadSystemConfig() { try { var path = GetSystemConfigPath(); if (!File.Exists(path)) { SystemConfig = new SystemConfiguration(); App.SystemConfig = SystemConfig; App.UpdateFontResources(); ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor); return; } var json = File.ReadAllText(path); SystemConfig = JsonSerializer.Deserialize(json) ?? new SystemConfiguration(); App.SystemConfig = SystemConfig; App.UpdateFontResources(); ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor); } catch { SystemConfig = new SystemConfiguration(); App.SystemConfig = SystemConfig; App.UpdateFontResources(); ThemeManager.ApplyTheme(App.SystemConfig.Theme, App.SystemConfig.AccentColor); } } #endregion #region 全局事件配置 private GlobalEventConfig _globalEventConfig; public GlobalEventConfig GlobalEventConfig { get => _globalEventConfig; set => SetProperty(ref _globalEventConfig, value); } public DelegateCommand SaveGlobalEventConfigCommand { get; } public DelegateCommand LoadGlobalEventConfigCommand { get; } public DelegateCommand AddHeartbeatCommand { get; } public DelegateCommand RemoveHeartbeatCommand { get; } public DelegateCommand AddProductSwitchEventCommand { get; } public DelegateCommand RemoveProductSwitchEventCommand { get; } private string GetGlobalEventConfigPath() => PathHelper.GlobalEventConfigFile; private void SaveGlobalEventConfig() { try { var json = JsonSerializer.Serialize(_globalEventConfig, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(GetGlobalEventConfigPath(), json); // 配置变更后立即刷新心跳监听(新增/删除/修改立即生效) TeamAAS.FlowEngine.Execution.FlowRunner.Instance.StartHeartbeat(); } catch { } } private void LoadGlobalEventConfig() { try { var path = GetGlobalEventConfigPath(); if (!File.Exists(path)) { _globalEventConfig = new GlobalEventConfig(); return; } var json = File.ReadAllText(path); _globalEventConfig = JsonSerializer.Deserialize(json) ?? new GlobalEventConfig(); } catch { _globalEventConfig = new GlobalEventConfig(); } } private void AddHeartbeat() { if (_globalEventConfig == null) _globalEventConfig = new GlobalEventConfig(); _globalEventConfig.Heartbeats.Add(new HeartbeatConfig { DeviceName = $"心跳{_globalEventConfig.Heartbeats.Count + 1}", IntervalMs = 3000, TimeoutMs = 1000, Enabled = true }); } private void RemoveHeartbeat(HeartbeatConfig hb) { if (_globalEventConfig == null || hb == null) return; _globalEventConfig.Heartbeats.Remove(hb); } private void AddProductSwitchEvent() { if (_globalEventConfig == null) _globalEventConfig = new GlobalEventConfig(); int nextSeq = _globalEventConfig.ProductSwitchEvents.Count > 0 ? _globalEventConfig.ProductSwitchEvents.Max(x => x.Sequence) + 1 : 1; _globalEventConfig.ProductSwitchEvents.Add(new ProductSwitchEvent { Sequence = nextSeq, ProductName = $"产品{nextSeq}", }); } private void RemoveProductSwitchEvent(ProductSwitchEvent evt) { if (_globalEventConfig == null || evt == null) return; _globalEventConfig.ProductSwitchEvents.Remove(evt); } #endregion } }