using Aitex.Common.Util; using Aitex.Core.RT.Log; using Aitex.Core.Util; using MECF.Framework.Common.ToolLayout; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.Persistent.Reservoirs { public class ReplenPersistentManager : Singleton { #region 内部变量 private Dictionary> _persistentValueDic = new Dictionary>(); private Dictionary> _persistentValuePathDic = new Dictionary>(); private int _replenNum = 0; #endregion /// /// 初始化 /// public void Initialize() { try { List installedModules = ReservoirItemManager.Instance.InstalledModules; foreach (string module in installedModules) { ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(module); if (reservoirItem.ChemReplenType == "" || reservoirItem.ChemReplenPumps == 0) { continue; } else { _persistentValueDic[module] = new Dictionary(); _persistentValuePathDic[module] = new Dictionary(); _replenNum = reservoirItem.ChemReplenPumps; string foldStr = $"{PathManager.GetCfgDir()}Persistent\\Reservoirs\\{module}DosingSystem"; if (!Directory.Exists(foldStr)) { Directory.CreateDirectory(foldStr); } for (int i = 0; i < _replenNum; i++) { string replenName = "Replen"+(i+1).ToString(); string str = $"{foldStr}\\{replenName}Persistent.xml"; _persistentValuePathDic[module][replenName] = str; if (File.Exists(str)) { ReplenPersistentValue replenPersistentValue = CustomXmlSerializer.Deserialize(new FileInfo(str)); if (replenPersistentValue != null) { _persistentValueDic[module][replenName] = replenPersistentValue; } } else { ReplenPersistentValue persistentValue = new ReplenPersistentValue(); persistentValue.ReplenPumpFactor = 1; persistentValue.ReplenName = replenName; persistentValue.AutoDosingStartAmpHour = 0; persistentValue.AutoDosingStartTime = DateTime.MinValue; persistentValue.IsDosingRunning = false; persistentValue.CurrentDosingVolume = 0; persistentValue.TargetDosingVolume = 0; persistentValue.RemainDosingVolume = 0; persistentValue.OperatingMode = "Manual"; _persistentValueDic[module][replenName] = persistentValue; UpdatePersistentValue(module, replenName); } } } } } catch (Exception ex) { LOG.WriteLog(eEvent.ERR_RESERVOIR, "System", "Load ReplenPersistent xml exception"); } } /// /// 获取Replen Persistent数值 /// /// public ReplenPersistentValue GetReplenPersistentValue(string module, string replenName) { if (_persistentValueDic.ContainsKey(module) && _persistentValueDic[module].ContainsKey(replenName)) { return _persistentValueDic[module][replenName]; } else { return null; } } /// /// 更新PersistentValue /// /// public void UpdatePersistentValue(string module, string replenName) { if (_persistentValueDic.ContainsKey(module) && _persistentValueDic[module].ContainsKey(replenName)) { try { CustomXmlSerializer.Serialize(_persistentValueDic[module][replenName], _persistentValuePathDic[module][replenName]); } catch (Exception ex) { LOG.WriteLog(eEvent.ERR_RESERVOIR, module, "Update ReplenPersistent xml file excepetion"); } } else { LOG.WriteLog(eEvent.ERR_RESERVOIR, module, "Update ReplenPersistent xml file excepetion"); } } } }