using OrpaonEMS.App.Models; using OrpaonEMS.App.Services; using OrpaonEMS.Core; using Prism.Commands; using System; using System.Collections.ObjectModel; using System.Linq; namespace OrpaonEMS.App.ViewModels { public class ControlConfigViewModel : NavigationViewModel { public ControlConfigViewModel(YuePuRunModelService yuePuRunModelService, ConfigDataService configDataService) { YuePuRunModelService = yuePuRunModelService; ConfigDataService = configDataService; // 从配置文件加载参数 if (ConfigDataService.ControlConfigValue != null) { SolarToEsAsFullSoc = ConfigDataService.ControlConfigValue.SolarToEsAsFullSoc; Master_ToSlaveByMasterSoc = ConfigDataService.ControlConfigValue.Master_ToSlaveByMasterSoc; Master_ToSlaveBySlaveSoc = ConfigDataService.ControlConfigValue.Master_ToSlaveBySlaveSoc; Master_SolarToSlaveEsFullByMasterSoc = ConfigDataService.ControlConfigValue.Master_SolarToSlaveEsFullByMasterSoc; Slave_ToMasterBySlaveSoc = ConfigDataService.ControlConfigValue.Slave_ToMasterBySlaveSoc; Slave_ToMasterByMasterSoc = ConfigDataService.ControlConfigValue.Slave_ToMasterByMasterSoc; Slave_SolarToMasterEsFullBySlaverSoc = ConfigDataService.ControlConfigValue.Slave_SolarToMasterEsFullBySlaverSoc; NightMaster_ToMasterFullSoc = ConfigDataService.ControlConfigValue.NightMaster_ToMasterFullSoc; NightSlave_ToSlaveFullSoc = ConfigDataService.ControlConfigValue.NightSlave_ToSlaveFullSoc; // 加载放电模式列表与已选项 var list = ConfigDataService.GetDisChargeModel(); DisChargeModels = new ObservableCollection(list); var sel = ConfigDataService.ControlConfigValue.SelectedDisChargeModel; if (DisChargeModels != null && DisChargeModels.Count > 0) { // 若配置中无效则回落到第一个项 if (DisChargeModels.Any(m => m.Model == sel)) SelectedDisChargeModelId = sel; else SelectedDisChargeModelId = DisChargeModels[0].Model; } else { SelectedDisChargeModelId = 1; } // 与旧逻辑保持同步(可选) if (System.Enum.IsDefined(typeof(DisChargeType), SelectedDisChargeModelId)) SelectedDisChargeType = (DisChargeType)SelectedDisChargeModelId; } // 同步到运行模式服务 YuePuRunModelService.SolarToEsAsFullSoc = SolarToEsAsFullSoc; YuePuRunModelService.Master_ToSlaveByMasterSoc = Master_ToSlaveByMasterSoc; YuePuRunModelService.Master_ToSlaveBySlaveSoc = Master_ToSlaveBySlaveSoc; YuePuRunModelService.Master_SolarToSlaveEsFullByMasterSoc = Master_SolarToSlaveEsFullByMasterSoc; YuePuRunModelService.Slave_ToMasterBySlaveSoc = Slave_ToMasterBySlaveSoc; YuePuRunModelService.Slave_ToMasterByMasterSoc = Slave_ToMasterByMasterSoc; YuePuRunModelService.Slave_SolarToMasterEsFullBySlaverSoc = Slave_SolarToMasterEsFullBySlaverSoc; YuePuRunModelService.NightMaster_ToMasterFullSoc = NightMaster_ToMasterFullSoc; YuePuRunModelService.NightSlave_ToSlaveFullSoc = NightSlave_ToSlaveFullSoc; //SelectedDisChargeType= } private DelegateCommand _BtnConfigSave; /// /// 运行模式操作 /// public DelegateCommand BtnConfigSave { set { _BtnConfigSave = value; } get { if (_BtnConfigSave == null) { _BtnConfigSave = new DelegateCommand((par) => BtnConfigSaveCall(par)); } return _BtnConfigSave; } } private DelegateCommand _BtnCloseSys; /// /// 关闭系统 /// public DelegateCommand BtnCloseSys { set { _BtnCloseSys = value; } get { if (_BtnCloseSys == null) { _BtnCloseSys = new DelegateCommand(() => BtnCloseSysCall()); } return _BtnCloseSys; } } /// /// 关闭系统的执行方法 /// private void BtnCloseSysCall() { try { // 停止后台监听线程(例如削峰填谷时间监听) if (ConfigDataService != null) { ConfigDataService.ThreadEnable = false; } // 关闭运行服务中的状态循环与通信连接 YuePuRunModelService?.CloseSystem(); } catch (System.Exception ex) { System.Windows.MessageBox.Show($"关闭系统时发生异常: {ex.Message}", "错误", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } finally { try { // 退出应用程序 System.Windows.Application.Current.Shutdown(); } catch { System.Environment.Exit(0); } } } private void BtnConfigSaveCall(string par) { try { // 同步到运行模式服务 YuePuRunModelService.SolarToEsAsFullSoc = SolarToEsAsFullSoc; YuePuRunModelService.Master_ToSlaveBySlaveSoc = Master_ToSlaveBySlaveSoc; YuePuRunModelService.Master_SolarToSlaveEsFullByMasterSoc = Master_SolarToSlaveEsFullByMasterSoc; YuePuRunModelService.Slave_ToMasterBySlaveSoc = Slave_ToMasterBySlaveSoc; YuePuRunModelService.Slave_ToMasterByMasterSoc = Slave_ToMasterByMasterSoc; YuePuRunModelService.Slave_SolarToMasterEsFullBySlaverSoc = Slave_SolarToMasterEsFullBySlaverSoc; YuePuRunModelService.NightMaster_ToMasterFullSoc = NightMaster_ToMasterFullSoc; YuePuRunModelService.NightSlave_ToSlaveFullSoc = NightSlave_ToSlaveFullSoc; // 保存到配置文件 if (ConfigDataService.ControlConfigValue != null) { ConfigDataService.ControlConfigValue.SolarToEsAsFullSoc = SolarToEsAsFullSoc; ConfigDataService.ControlConfigValue.Master_ToSlaveByMasterSoc = Master_ToSlaveByMasterSoc; ConfigDataService.ControlConfigValue.Master_ToSlaveBySlaveSoc = Master_ToSlaveBySlaveSoc; ConfigDataService.ControlConfigValue.Master_SolarToSlaveEsFullByMasterSoc = Master_SolarToSlaveEsFullByMasterSoc; ConfigDataService.ControlConfigValue.Slave_ToMasterBySlaveSoc = Slave_ToMasterBySlaveSoc; ConfigDataService.ControlConfigValue.Slave_ToMasterByMasterSoc = Slave_ToMasterByMasterSoc; ConfigDataService.ControlConfigValue.Slave_SolarToMasterEsFullBySlaverSoc = Slave_SolarToMasterEsFullBySlaverSoc; ConfigDataService.ControlConfigValue.NightMaster_ToMasterFullSoc = NightMaster_ToMasterFullSoc; ConfigDataService.ControlConfigValue.NightSlave_ToSlaveFullSoc = NightSlave_ToSlaveFullSoc; // 保存选择的放电模式(以 Model Id 为准) ConfigDataService.ControlConfigValue.SelectedDisChargeModel = SelectedDisChargeModelId; // 同步旧字段 if (System.Enum.IsDefined(typeof(DisChargeType), SelectedDisChargeModelId)) SelectedDisChargeType = (DisChargeType)SelectedDisChargeModelId; // 调用保存方法 bool saveResult = ConfigDataService.SaveControlConfigValue(); if (saveResult) { System.Windows.MessageBox.Show("控制参数配置保存成功!", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information); } else { System.Windows.MessageBox.Show("控制参数配置保存失败,请查看日志!", "错误", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } } else { System.Windows.MessageBox.Show("控制参数配置对象为空!", "错误", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } } catch (System.Exception ex) { System.Windows.MessageBox.Show($"保存控制参数配置时发生异常: {ex.Message}", "错误", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } } private double _SolarToEsAsFullSoc = 97; /// /// 光伏给储能充电 作为满的比值 /// 90-97 /// public double SolarToEsAsFullSoc { get { return _SolarToEsAsFullSoc; } set { _SolarToEsAsFullSoc = value; RaisePropertyChanged(); } } private double _Master_ToSlaveByMasterSoc = 5; /// /// Master模式,切换到Slave模式时MasterSOC的阀值 /// 两个同时考虑 /// public double Master_ToSlaveByMasterSoc { get { return _Master_ToSlaveByMasterSoc; } set { _Master_ToSlaveByMasterSoc = value; RaisePropertyChanged(); } } private double _Master_ToSlaveBySlaveSoc = 15; /// /// Master模式,切换到Slave模式时Slave SOC的阀值 /// 两个同时考虑 /// public double Master_ToSlaveBySlaveSoc { get { return _Master_ToSlaveBySlaveSoc; } set { _Master_ToSlaveBySlaveSoc = value; RaisePropertyChanged(); } } private double _Master_SolarToSlaveEsFullByMasterSoc = 95; /// /// Master模式,光伏给从储能充满了,是否切换到Slave模式,但是此时需要判断主储能MasterSOC是否满了(主储能也满的话,也无法接收光伏的电),否则不切换 /// 主要考虑尽可能的不浪费光伏的电,主从储能只要有余量就要接受光伏的电,也要防止频繁的切换 /// < SolarToEsAsFullSoc /// public double Master_SolarToSlaveEsFullByMasterSoc { get { return _Master_SolarToSlaveEsFullByMasterSoc; } set { _Master_SolarToSlaveEsFullByMasterSoc = value; RaisePropertyChanged(); } } private double _Slave_ToMasterBySlaveSoc = 5; /// /// Slave模式,切换到Master模式时SOC的阀值 /// 两个同时考虑 /// public double Slave_ToMasterBySlaveSoc { get { return _Slave_ToMasterBySlaveSoc; } set { _Slave_ToMasterBySlaveSoc = value; RaisePropertyChanged(); } } private double _Slave_ToMasterByMasterSoc = 15; /// /// Slave模式,切换到Master模式时SOC的阀值 /// 两个同时考虑 /// public double Slave_ToMasterByMasterSoc { get { return _Slave_ToMasterByMasterSoc; } set { _Slave_ToMasterByMasterSoc = value; RaisePropertyChanged(); } } private double _Slave_SolarToMasterEsFullBySlaverSoc = 95; /// /// Slave模式,光伏给主储能充满了,是否切换到Master模式,但是此时需要判断从储能SOC是否满了(从储能也满的话,也无法接收光伏的电),否则不切换 /// 主要考虑尽可能的不浪费光伏的电,主从储能只要有余量就要接受光伏的电,也要防止频繁的切换 /// < SolarToEsAsFullSoc /// public double Slave_SolarToMasterEsFullBySlaverSoc { get { return _Slave_SolarToMasterEsFullBySlaverSoc; } set { _Slave_SolarToMasterEsFullBySlaverSoc = value; RaisePropertyChanged(); } } private double _NightMaster_ToMasterFullSoc = 98; /// /// 晚上,主储能充满的标志,也是切换到从储能的控制标志 /// public double NightMaster_ToMasterFullSoc { get { return _NightMaster_ToMasterFullSoc; } set { _NightMaster_ToMasterFullSoc = value; RaisePropertyChanged(); } } private double _NightSlave_ToSlaveFullSoc = 98; /// /// 晚上,从储能充满的标志,也是切换到主储能的控制标志 /// public double NightSlave_ToSlaveFullSoc { get { return _NightSlave_ToSlaveFullSoc; } set { _NightSlave_ToSlaveFullSoc = value; RaisePropertyChanged(); } } public YuePuRunModelService YuePuRunModelService { get; } /// /// 配置数据服务 /// public ConfigDataService ConfigDataService { get; } /// /// 选中的放电模式 /// private DisChargeType _SelectedDisChargeType; public DisChargeType SelectedDisChargeType { get { return _SelectedDisChargeType; } set { _SelectedDisChargeType = value; RaisePropertyChanged(); } } /// /// 放电模式列表 /// private ObservableCollection _DisChargeModels; public ObservableCollection DisChargeModels { get { return _DisChargeModels; } set { _DisChargeModels = value; RaisePropertyChanged(); } } /// /// 选中的放电模式(列表项的 Model Id) /// private int _SelectedDisChargeModelId; public int SelectedDisChargeModelId { get { return _SelectedDisChargeModelId; } set { _SelectedDisChargeModelId = value; // 同步枚举值(若兼容) if (System.Enum.IsDefined(typeof(DisChargeType), _SelectedDisChargeModelId)) SelectedDisChargeType = (DisChargeType)_SelectedDisChargeModelId; RaisePropertyChanged(); } } } }