SystemConfigManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Xml;
  10. using Aitex.Common.Util;
  11. using Aitex.Core.RT.ConfigCenter;
  12. using Aitex.Core.RT.DataCenter;
  13. using Aitex.Core.RT.Event;
  14. using Aitex.Core.RT.Log;
  15. using Aitex.Core.RT.OperationCenter;
  16. using Aitex.Core.RT.SCCore;
  17. using Aitex.Core.Util;
  18. using MECF.Framework.Common.Equipment;
  19. namespace MECF.Framework.Common.SCCore
  20. {
  21. public class SystemConfigManager : Singleton<SystemConfigManager>, ISCManager
  22. {
  23. private Dictionary<string, SCConfigItem> _items = new Dictionary<string, SCConfigItem>();
  24. private object _itemLocker = new object();
  25. private string _scConfigFile ;
  26. private string _scDataFile = PathManager.GetCfgDir() + "_sc.data";
  27. private string _scDataBackupFile = PathManager.GetCfgDir() + "_sc.data.bak";
  28. private string _scDataErrorFile = PathManager.GetCfgDir() + "_sc.data.err.";
  29. public void Initialize(string scConfigPathName)
  30. {
  31. _scConfigFile = scConfigPathName;
  32. BuildItems(_scConfigFile);
  33. BackupAndRecoverDataFile();
  34. CustomData();
  35. GenerateDataFile();
  36. foreach (var item in _items)
  37. {
  38. CONFIG.Subscribe("", item.Key, ()=>item.Value.Value);
  39. }
  40. OP.Subscribe("System.SetConfig", InvokeSetConfig);
  41. SC.Manager = this;
  42. }
  43. private bool InvokeSetConfig(string cmd, object[] parameters)
  44. {
  45. string key = (string) parameters[0];
  46. object old = GetConfigValueAsObject(key);
  47. if ((string)parameters[0] == "PMB.Chiller.ChillerSameWithPMA" && (string)parameters[1] == "true")
  48. {
  49. if (_items["PMA.Chiller.EnableChiller"].Value.ToString() == "False") return true;
  50. }
  51. if (InitializeItemValue(_items[(string)parameters[0]], (string)parameters[1]))
  52. {
  53. GenerateDataFile();
  54. LOG.Write(eEvent.EV_SYSTEM_CONFIG, ModuleName.System, string.Format("SC {0} value changed from {1} to {2}", key, old, parameters[1]));
  55. }
  56. if ((string)parameters[0] == "PMA.Chiller.EnableChiller" && (string)parameters[1] == "false")
  57. {
  58. if (_items["PMB.Chiller.ChillerSameWithPMA"].Value.ToString() == "False") return true;
  59. if (InitializeItemValue(_items["PMB.Chiller.ChillerSameWithPMA"], "False"))
  60. {
  61. GenerateDataFile();
  62. LOG.Write(eEvent.EV_SYSTEM_CONFIG, ModuleName.System, string.Format("SC {0} value changed from {1} to {2}", "PMB.Chiller.ChillerSameWithPMA", "true", "false"));
  63. }
  64. }
  65. if ((string)parameters[0] == "PMB.Chiller.EnableChiller" && (string)parameters[1] == "false")
  66. {
  67. if (_items["PMB.Chiller.ChillerSameWithPMA"].Value.ToString() == "False") return true;
  68. if (InitializeItemValue(_items["PMB.Chiller.EnableChiller"], "true"))
  69. {
  70. GenerateDataFile();
  71. LOG.Write(eEvent.EV_SYSTEM_CONFIG, ModuleName.System, string.Format("SC {0} value changed from {1} to {2}", "PMB.Chiller.EnableChiller", "false", "true"));
  72. }
  73. }
  74. if ((string)parameters[0] == "PMB.Chiller.ChillerSameWithPMA" && (string)parameters[1] == "true")
  75. {
  76. if (_items["PMB.Chiller.EnableChiller"].Value.ToString() == "True") return true;
  77. if (InitializeItemValue(_items["PMB.Chiller.EnableChiller"], "true"))
  78. {
  79. GenerateDataFile();
  80. LOG.Write(eEvent.EV_SYSTEM_CONFIG, ModuleName.System, string.Format("SC {0} value changed from {1} to {2}", "PMB.Chiller.EnableChiller", "false", "true"));
  81. }
  82. }
  83. return true;
  84. }
  85. public void Terminate()
  86. {
  87. }
  88. public string GetFileContent()
  89. {
  90. if (!File.Exists(_scConfigFile))
  91. return "";
  92. StringBuilder s = new StringBuilder();
  93. try
  94. {
  95. using (StreamReader sr = new StreamReader(_scConfigFile))
  96. {
  97. while (!sr.EndOfStream)
  98. {
  99. s.Append(sr.ReadLine());
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. LOG.WriteExeption(ex);
  106. return "";
  107. }
  108. return s.ToString();
  109. }
  110. private void BuildItems(string xmlFile)
  111. {
  112. XmlDocument xml = new XmlDocument();
  113. try
  114. {
  115. var _sc_stream = File.Open(xmlFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
  116. xml.Load(_sc_stream);
  117. XmlNodeList nodeConfigs = xml.SelectNodes("root/configs");
  118. foreach (XmlElement nodeConfig in nodeConfigs)
  119. {
  120. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. LOG.WriteExeption(ex);
  126. }
  127. }
  128. private void BuildPathConfigs(string parentPath, XmlElement configElement)
  129. {
  130. XmlNodeList nodeConfigsList = configElement.SelectNodes("configs");
  131. foreach (XmlElement nodeConfig in nodeConfigsList)
  132. {
  133. if (string.IsNullOrEmpty(parentPath))
  134. {
  135. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  136. }
  137. else
  138. {
  139. BuildPathConfigs(parentPath + "." + nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  140. }
  141. }
  142. XmlNodeList nodeConfigs = configElement.SelectNodes("config");
  143. foreach (XmlElement nodeConfig in nodeConfigs)
  144. {
  145. SCConfigItem item = new SCConfigItem()
  146. {
  147. Default = nodeConfig.GetAttribute("default"),
  148. Name = nodeConfig.GetAttribute("name"),
  149. Description = nodeConfig.GetAttribute("description"),
  150. Max = nodeConfig.GetAttribute("max"),
  151. Min = nodeConfig.GetAttribute("min"),
  152. Parameter = nodeConfig.GetAttribute("paramter"),
  153. Path = parentPath,
  154. Tag = nodeConfig.GetAttribute("tag"),
  155. Type = nodeConfig.GetAttribute("type"),
  156. Unit = nodeConfig.GetAttribute("unit"),
  157. };
  158. InitializeItemValue(item, item.Default);
  159. if (_items.ContainsKey(item.PathName))
  160. {
  161. //LOG.Error("Duplicated SC item, "+ item.PathName);
  162. }
  163. _items[item.PathName] = item;
  164. }
  165. }
  166. private void BackupAndRecoverDataFile()
  167. {
  168. try
  169. {
  170. if (File.Exists(_scDataFile) && IsXmlFileLoadable(_scDataFile))
  171. {
  172. File.Copy(_scDataFile, _scDataBackupFile, true);
  173. }
  174. else if (File.Exists(_scDataBackupFile) && IsXmlFileLoadable(_scDataBackupFile))
  175. {
  176. if (File.Exists(_scDataFile))
  177. {
  178. File.Copy(_scDataFile, _scDataErrorFile + DateTime.Now.ToString("yyyyMMdd_HHmmss"), true);
  179. }
  180. File.Copy(_scDataBackupFile, _scDataFile, true);
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. LOG.WriteExeption(ex);
  186. }
  187. }
  188. private void CustomData()
  189. {
  190. Dictionary<string, string> values = new Dictionary<string, string>();
  191. try
  192. {
  193. if (File.Exists(_scDataFile))
  194. {
  195. XmlDocument xmlData = new XmlDocument();
  196. var _sc_stream = File.Open(_scDataFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
  197. xmlData.Load(_sc_stream);
  198. XmlNodeList scdatas = xmlData.SelectNodes("root/scdata");
  199. foreach (XmlElement nodedata in scdatas)
  200. {
  201. string name = nodedata.GetAttribute("name");
  202. if (_items.ContainsKey(name))
  203. {
  204. InitializeItemValue(_items[name], nodedata.GetAttribute("value"));
  205. }
  206. }
  207. _sc_stream.Close();
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. LOG.WriteExeption(ex);
  213. }
  214. }
  215. private void GenerateDataFile()
  216. {
  217. try
  218. {
  219. XmlDocument xml = new XmlDocument();
  220. xml.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
  221. XmlElement nodeRoot = xml.SelectSingleNode("root") as XmlElement;
  222. foreach (var scConfigItem in _items)
  223. {
  224. XmlElement node = xml.CreateElement("scdata");
  225. node.SetAttribute("name", scConfigItem.Key);
  226. node.SetAttribute("value", scConfigItem.Value.Value.ToString());
  227. nodeRoot.AppendChild(node);
  228. }
  229. if (File.Exists(_scDataFile) && IsXmlFileLoadable(_scDataFile))
  230. {
  231. File.Copy(_scDataFile, _scDataBackupFile, true);
  232. //File.Delete(_scDataFile);
  233. }
  234. using (FileStream fsFileStream = new FileStream(_scDataFile,
  235. FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, FileOptions.WriteThrough))
  236. {
  237. fsFileStream.SetLength(0);
  238. XmlWriterSettings settings = new XmlWriterSettings();
  239. settings.Indent = true;
  240. settings.OmitXmlDeclaration = false;
  241. using (XmlWriter xmlWrite = XmlWriter.Create(fsFileStream, settings))
  242. {
  243. xml.Save(xmlWrite);
  244. }
  245. }
  246. }
  247. catch (Exception ex)
  248. {
  249. LOG.WriteExeption(ex);
  250. }
  251. }
  252. private bool IsXmlFileLoadable(string file)
  253. {
  254. try
  255. {
  256. XmlDocument xml = new XmlDocument();
  257. xml.Load(file);
  258. }
  259. catch (Exception ex)
  260. {
  261. LOG.WriteExeption(ex);
  262. return false;
  263. }
  264. return true;
  265. }
  266. public SCConfigItem GetConfigItem(string name)
  267. {
  268. //Debug.Assert(_items.ContainsKey(name), "can not find sc name, "+name);
  269. if (!_items.ContainsKey(name))
  270. {
  271. return null;
  272. }
  273. return _items[name];
  274. }
  275. public bool ContainsItem(string name)
  276. {
  277. return _items.ContainsKey(name);
  278. }
  279. public object GetConfigValueAsObject(string name)
  280. {
  281. SCConfigItem item = GetConfigItem(name);
  282. if(item == null)
  283. {
  284. return null;
  285. }
  286. switch (item.Type)
  287. {
  288. case "Bool": return item.BoolValue;
  289. case "Integer": return item.IntValue;
  290. case "Double": return item.DoubleValue;
  291. case "String": return item.StringValue;
  292. }
  293. return null;
  294. }
  295. public T GetValue<T>(string name) where T : struct
  296. {
  297. try
  298. {
  299. if (typeof(T) == typeof(bool))
  300. return (T)(object)_items[name].BoolValue;
  301. if (typeof(T) == typeof(int))
  302. return (T)(object)_items[name].IntValue;
  303. if (typeof(T) == typeof(double))
  304. return (T)(object)_items[name].DoubleValue;
  305. }
  306. catch (KeyNotFoundException)
  307. {
  308. MessageBox.Show($"Can not find system config item {name}");
  309. return default(T);
  310. }
  311. catch (Exception)
  312. {
  313. MessageBox.Show($"Can not get valid system config item value {name}");
  314. return default(T);
  315. }
  316. Debug.Assert(false, "unsupported type");
  317. return default(T);
  318. }
  319. public string GetStringValue(string name)
  320. {
  321. if (!_items.ContainsKey(name))
  322. return null;
  323. return _items[name].StringValue;
  324. }
  325. public List<SCConfigItem> GetItemList()
  326. {
  327. return _items.Values.ToList();
  328. }
  329. public void SetItemValueFromString(string name, string value)
  330. {
  331. if (InitializeItemValue(_items[name], value))
  332. {
  333. GenerateDataFile();
  334. }
  335. }
  336. private bool InitializeItemValue(SCConfigItem item, string value)
  337. {
  338. bool changed = false;
  339. switch (item.Type)
  340. {
  341. case "Bool":
  342. bool boolValue;
  343. if (bool.TryParse(value, out boolValue) && boolValue != item.BoolValue)
  344. {
  345. item.BoolValue = boolValue;
  346. changed = true;
  347. }
  348. break;
  349. case "Integer":
  350. int intValue;
  351. if (int.TryParse(value, out intValue) && intValue != item.IntValue)
  352. {
  353. int.TryParse(item.Min, out int min);
  354. int.TryParse(item.Max, out int max);
  355. if (intValue <min || intValue > max)
  356. {
  357. LOG.Write(eEvent.WARN_SYSTEM_CONFIG, ModuleName.System, $"SC {item.PathName} value {intValue} out of setting range ({item.Min}, {item.Max})");
  358. break;
  359. }
  360. item.IntValue = intValue;
  361. changed = true;
  362. }
  363. break;
  364. case "Double":
  365. double doubleValue;
  366. if (double.TryParse(value, out doubleValue) && Math.Abs(doubleValue - item.DoubleValue) > 0.0001)
  367. {
  368. double.TryParse(item.Min, out double min);
  369. double.TryParse(item.Max, out double max);
  370. if (doubleValue < min || doubleValue > max)
  371. {
  372. LOG.Write(eEvent.WARN_SYSTEM_CONFIG, ModuleName.System, $"SC {item.PathName} value {doubleValue} out of setting range ({item.Min}, {item.Max})");
  373. break;
  374. }
  375. item.DoubleValue = doubleValue;
  376. changed = true;
  377. }
  378. break;
  379. case "String":
  380. if (value != item.StringValue)
  381. {
  382. item.StringValue = value;
  383. changed = true;
  384. }
  385. break;
  386. }
  387. return changed;
  388. }
  389. public void SetItemValue(string name, object value)
  390. {
  391. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  392. if (!_items.ContainsKey(name))
  393. {
  394. return;
  395. }
  396. bool changed = false;
  397. switch (_items[name].Type)
  398. {
  399. case "Bool":
  400. bool boolValue = (bool)value;
  401. if (boolValue != _items[name].BoolValue)
  402. {
  403. _items[name].BoolValue = boolValue;
  404. changed = true;
  405. }
  406. break;
  407. case "Integer":
  408. int intValue = (int)value;
  409. if (intValue != _items[name].IntValue)
  410. {
  411. _items[name].IntValue = intValue;
  412. changed = true;
  413. }
  414. break;
  415. case "Double":
  416. double doubleValue = (double)value;
  417. if (Math.Abs(doubleValue - _items[name].DoubleValue) > 0.0001)
  418. {
  419. _items[name].DoubleValue = doubleValue;
  420. changed = true;
  421. }
  422. break;
  423. case "String":
  424. string stringValue = (string)value;
  425. if (stringValue != _items[name].StringValue)
  426. {
  427. _items[name].StringValue = stringValue;
  428. changed = true;
  429. }
  430. break;
  431. }
  432. if (changed)
  433. {
  434. GenerateDataFile();
  435. }
  436. }
  437. public void SetItemValueStringFormat(string name, string value)
  438. {
  439. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  440. if (!_items.ContainsKey(name))
  441. {
  442. return;
  443. }
  444. bool changed = false;
  445. switch (_items[name].Type)
  446. {
  447. case "Bool":
  448. bool boolValue = Convert.ToBoolean(value);
  449. if (boolValue != _items[name].BoolValue)
  450. {
  451. _items[name].BoolValue = boolValue;
  452. changed = true;
  453. }
  454. break;
  455. case "Integer":
  456. int intValue = Convert.ToInt32(value);
  457. if (intValue != _items[name].IntValue)
  458. {
  459. _items[name].IntValue = intValue;
  460. changed = true;
  461. }
  462. break;
  463. case "Double":
  464. double doubleValue = Convert.ToDouble(value);
  465. if (Math.Abs(doubleValue - _items[name].DoubleValue) > 0.0001)
  466. {
  467. _items[name].DoubleValue = doubleValue;
  468. changed = true;
  469. }
  470. break;
  471. case "String":
  472. string stringValue = (string)value;
  473. if (stringValue != _items[name].StringValue)
  474. {
  475. _items[name].StringValue = stringValue;
  476. changed = true;
  477. }
  478. break;
  479. }
  480. if (changed)
  481. {
  482. GenerateDataFile();
  483. }
  484. }
  485. public void SetItemValue(string name, bool value)
  486. {
  487. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  488. Debug.Assert(_items[name].Type=="Bool", "sc type not bool, defined as" + _items[name].Type);
  489. if (value != _items[name].BoolValue)
  490. {
  491. _items[name].BoolValue = value;
  492. GenerateDataFile();
  493. }
  494. }
  495. public void SetItemValue(string name, int value)
  496. {
  497. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  498. Debug.Assert(_items[name].Type == "Integer", "sc type not bool, defined as" + _items[name].Type);
  499. if (value != _items[name].IntValue)
  500. {
  501. _items[name].IntValue = value;
  502. GenerateDataFile();
  503. }
  504. }
  505. public void SetItemValue(string name, double value)
  506. {
  507. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  508. Debug.Assert(_items[name].Type == "Double", "sc type not bool, defined as" + _items[name].Type);
  509. if (Math.Abs(value - _items[name].DoubleValue) > 0.0001)
  510. {
  511. _items[name].DoubleValue = value;
  512. GenerateDataFile();
  513. }
  514. }
  515. public void SetItemValue(string name, string value)
  516. {
  517. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  518. if (value != _items[name].StringValue)
  519. {
  520. _items[name].StringValue = value;
  521. GenerateDataFile();
  522. }
  523. }
  524. /// <summary>
  525. /// 获取配置前置
  526. /// </summary>
  527. /// <returns></returns>
  528. public string GetConfigPreContent(string cellName)
  529. {
  530. if (cellName.StartsWith("Buffer"))
  531. {
  532. return $"Buffer";
  533. }
  534. else if (cellName.StartsWith("Loader"))
  535. {
  536. return "Loader1";
  537. }
  538. else if (cellName.StartsWith("Rinse"))
  539. {
  540. return $"QDR";
  541. }
  542. else if (cellName.StartsWith("Metal"))
  543. {
  544. return $"Metal";
  545. }
  546. else if (cellName.StartsWith("Prewet"))
  547. {
  548. return "Prewet";
  549. }
  550. else if (cellName.StartsWith("Dryer"))
  551. {
  552. return "Dryer";
  553. }
  554. else
  555. {
  556. return "";
  557. }
  558. }
  559. }
  560. }