| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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
- {
- /// <summary>设置页 - 通用设置(字体/标题)与全局事件配置(心跳/切换产品)</summary>
- 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; }
- /// <summary>日志详细度下拉源(粗略/详细),供「默认存储/显示详细度」两个下拉共用</summary>
- 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<SystemConfiguration>(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<HeartbeatConfig> RemoveHeartbeatCommand { get; }
- public DelegateCommand AddProductSwitchEventCommand { get; }
- public DelegateCommand<ProductSwitchEvent> 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<GlobalEventConfig>(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
- }
- }
|