SystemConfigManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. EV.PostInfoLog("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. EV.PostInfoLog("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. EV.PostInfoLog("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. EV.PostInfoLog("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.Write(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. xml.Load(xmlFile);
  116. XmlNodeList nodeConfigs = xml.SelectNodes("root/configs");
  117. foreach (XmlElement nodeConfig in nodeConfigs)
  118. {
  119. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. LOG.Write(ex);
  125. }
  126. }
  127. private void BuildPathConfigs(string parentPath, XmlElement configElement)
  128. {
  129. XmlNodeList nodeConfigsList = configElement.SelectNodes("configs");
  130. foreach (XmlElement nodeConfig in nodeConfigsList)
  131. {
  132. if (string.IsNullOrEmpty(parentPath))
  133. {
  134. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  135. }
  136. else
  137. {
  138. BuildPathConfigs(parentPath + "." + nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  139. }
  140. }
  141. XmlNodeList nodeConfigs = configElement.SelectNodes("config");
  142. foreach (XmlElement nodeConfig in nodeConfigs)
  143. {
  144. SCConfigItem item = new SCConfigItem()
  145. {
  146. Default = nodeConfig.GetAttribute("default"),
  147. Name = nodeConfig.GetAttribute("name"),
  148. Description = nodeConfig.GetAttribute("description"),
  149. Max = nodeConfig.GetAttribute("max"),
  150. Min = nodeConfig.GetAttribute("min"),
  151. Parameter = nodeConfig.GetAttribute("paramter"),
  152. Path = parentPath,
  153. Tag = nodeConfig.GetAttribute("tag"),
  154. Type = nodeConfig.GetAttribute("type"),
  155. Unit = nodeConfig.GetAttribute("unit"),
  156. };
  157. InitializeItemValue(item, item.Default);
  158. if (_items.ContainsKey(item.PathName))
  159. {
  160. LOG.Error("Duplicated SC item, "+ item.PathName);
  161. }
  162. _items[item.PathName] = item;
  163. }
  164. }
  165. private void BackupAndRecoverDataFile()
  166. {
  167. try
  168. {
  169. if (File.Exists(_scDataFile) && IsXmlFileLoadable(_scDataFile))
  170. {
  171. File.Copy(_scDataFile, _scDataBackupFile, true);
  172. }
  173. else if (File.Exists(_scDataBackupFile) && IsXmlFileLoadable(_scDataBackupFile))
  174. {
  175. if (File.Exists(_scDataFile))
  176. {
  177. File.Copy(_scDataFile, _scDataErrorFile + DateTime.Now.ToString("yyyyMMdd_HHmmss"), true);
  178. }
  179. File.Copy(_scDataBackupFile, _scDataFile, true);
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. LOG.Write(ex);
  185. }
  186. }
  187. private void CustomData()
  188. {
  189. Dictionary<string, string> values = new Dictionary<string, string>();
  190. try
  191. {
  192. if (File.Exists(_scDataFile))
  193. {
  194. XmlDocument xmlData = new XmlDocument();
  195. xmlData.Load(_scDataFile);
  196. XmlNodeList scdatas = xmlData.SelectNodes("root/scdata");
  197. foreach (XmlElement nodedata in scdatas)
  198. {
  199. string name = nodedata.GetAttribute("name");
  200. if (_items.ContainsKey(name))
  201. {
  202. InitializeItemValue(_items[name], nodedata.GetAttribute("value"));
  203. }
  204. }
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. LOG.Write(ex);
  210. }
  211. }
  212. private void GenerateDataFile()
  213. {
  214. try
  215. {
  216. XmlDocument xml = new XmlDocument();
  217. xml.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
  218. XmlElement nodeRoot = xml.SelectSingleNode("root") as XmlElement;
  219. foreach (var scConfigItem in _items)
  220. {
  221. XmlElement node = xml.CreateElement("scdata");
  222. node.SetAttribute("name", scConfigItem.Key);
  223. node.SetAttribute("value", scConfigItem.Value.Value.ToString());
  224. nodeRoot.AppendChild(node);
  225. }
  226. if (File.Exists(_scDataFile) && IsXmlFileLoadable(_scDataFile))
  227. {
  228. File.Copy(_scDataFile, _scDataBackupFile, true);
  229. //File.Delete(_scDataFile);
  230. }
  231. using (FileStream fsFileStream = new FileStream(_scDataFile,
  232. FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, FileOptions.WriteThrough))
  233. {
  234. fsFileStream.SetLength(0);
  235. XmlWriterSettings settings = new XmlWriterSettings();
  236. settings.Indent = true;
  237. settings.OmitXmlDeclaration = false;
  238. using (XmlWriter xmlWrite = XmlWriter.Create(fsFileStream, settings))
  239. {
  240. xml.Save(xmlWrite);
  241. }
  242. }
  243. }
  244. catch (Exception ex)
  245. {
  246. LOG.Write(ex);
  247. }
  248. }
  249. private bool IsXmlFileLoadable(string file)
  250. {
  251. try
  252. {
  253. XmlDocument xml = new XmlDocument();
  254. xml.Load(file);
  255. }
  256. catch (Exception ex)
  257. {
  258. LOG.Write(ex);
  259. return false;
  260. }
  261. return true;
  262. }
  263. public SCConfigItem GetConfigItem(string name)
  264. {
  265. //Debug.Assert(_items.ContainsKey(name), "can not find sc name, "+name);
  266. if (!_items.ContainsKey(name))
  267. {
  268. return null;
  269. }
  270. return _items[name];
  271. }
  272. public bool ContainsItem(string name)
  273. {
  274. return _items.ContainsKey(name);
  275. }
  276. public object GetConfigValueAsObject(string name)
  277. {
  278. SCConfigItem item = GetConfigItem(name);
  279. switch (item.Type)
  280. {
  281. case "Bool": return item.BoolValue;
  282. case "Integer": return item.IntValue;
  283. case "Double": return item.DoubleValue;
  284. case "String": return item.StringValue;
  285. }
  286. return null;
  287. }
  288. public T GetValue<T>(string name) where T : struct
  289. {
  290. try
  291. {
  292. if (typeof(T) == typeof(bool))
  293. return (T)(object)_items[name].BoolValue;
  294. if (typeof(T) == typeof(int))
  295. return (T)(object)_items[name].IntValue;
  296. if (typeof(T) == typeof(double))
  297. return (T)(object)_items[name].DoubleValue;
  298. }
  299. catch (KeyNotFoundException)
  300. {
  301. MessageBox.Show($"Can not find system config item {name}");
  302. return default(T);
  303. }
  304. catch (Exception)
  305. {
  306. MessageBox.Show($"Can not get valid system config item value {name}");
  307. return default(T);
  308. }
  309. Debug.Assert(false, "unsupported type");
  310. return default(T);
  311. }
  312. public string GetStringValue(string name)
  313. {
  314. if (!_items.ContainsKey(name))
  315. return null;
  316. return _items[name].StringValue;
  317. }
  318. public List<SCConfigItem> GetItemList()
  319. {
  320. return _items.Values.ToList();
  321. }
  322. public void SetItemValueFromString(string name, string value)
  323. {
  324. if (InitializeItemValue(_items[name], value))
  325. {
  326. GenerateDataFile();
  327. }
  328. }
  329. private bool InitializeItemValue(SCConfigItem item, string value)
  330. {
  331. bool changed = false;
  332. switch (item.Type)
  333. {
  334. case "Bool":
  335. bool boolValue;
  336. if (bool.TryParse(value, out boolValue) && boolValue != item.BoolValue)
  337. {
  338. item.BoolValue = boolValue;
  339. changed = true;
  340. }
  341. break;
  342. case "Integer":
  343. int intValue;
  344. if (int.TryParse(value, out intValue) && intValue != item.IntValue)
  345. {
  346. int.TryParse(item.Min, out int min);
  347. int.TryParse(item.Max, out int max);
  348. if (intValue <min || intValue > max)
  349. {
  350. EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {intValue} out of setting range ({item.Min}, {item.Max})");
  351. break;
  352. }
  353. item.IntValue = intValue;
  354. changed = true;
  355. }
  356. break;
  357. case "Double":
  358. double doubleValue;
  359. if (double.TryParse(value, out doubleValue) && Math.Abs(doubleValue - item.DoubleValue) > 0.0001)
  360. {
  361. double.TryParse(item.Min, out double min);
  362. double.TryParse(item.Max, out double max);
  363. if (doubleValue < min || doubleValue > max)
  364. {
  365. EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {doubleValue} out of setting range ({item.Min}, {item.Max})");
  366. break;
  367. }
  368. item.DoubleValue = doubleValue;
  369. changed = true;
  370. }
  371. break;
  372. case "String":
  373. if (value != item.StringValue)
  374. {
  375. item.StringValue = value;
  376. changed = true;
  377. }
  378. break;
  379. }
  380. return changed;
  381. }
  382. public void SetItemValue(string name, object value)
  383. {
  384. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  385. if (!_items.ContainsKey(name))
  386. {
  387. return;
  388. }
  389. bool changed = false;
  390. switch (_items[name].Type)
  391. {
  392. case "Bool":
  393. bool boolValue = (bool)value;
  394. if (boolValue != _items[name].BoolValue)
  395. {
  396. _items[name].BoolValue = boolValue;
  397. changed = true;
  398. }
  399. break;
  400. case "Integer":
  401. int intValue = (int)value;
  402. if (intValue != _items[name].IntValue)
  403. {
  404. _items[name].IntValue = intValue;
  405. changed = true;
  406. }
  407. break;
  408. case "Double":
  409. double doubleValue = (double)value;
  410. if (Math.Abs(doubleValue - _items[name].DoubleValue) > 0.0001)
  411. {
  412. _items[name].DoubleValue = doubleValue;
  413. changed = true;
  414. }
  415. break;
  416. case "String":
  417. string stringValue = (string)value;
  418. if (stringValue != _items[name].StringValue)
  419. {
  420. _items[name].StringValue = stringValue;
  421. changed = true;
  422. }
  423. break;
  424. }
  425. if (changed)
  426. {
  427. GenerateDataFile();
  428. }
  429. }
  430. public void SetItemValueStringFormat(string name, string value)
  431. {
  432. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  433. if (!_items.ContainsKey(name))
  434. {
  435. return;
  436. }
  437. bool changed = false;
  438. switch (_items[name].Type)
  439. {
  440. case "Bool":
  441. bool boolValue = Convert.ToBoolean(value);
  442. if (boolValue != _items[name].BoolValue)
  443. {
  444. _items[name].BoolValue = boolValue;
  445. changed = true;
  446. }
  447. break;
  448. case "Integer":
  449. int intValue = Convert.ToInt32(value);
  450. if (intValue != _items[name].IntValue)
  451. {
  452. _items[name].IntValue = intValue;
  453. changed = true;
  454. }
  455. break;
  456. case "Double":
  457. double doubleValue = Convert.ToDouble(value);
  458. if (Math.Abs(doubleValue - _items[name].DoubleValue) > 0.0001)
  459. {
  460. _items[name].DoubleValue = doubleValue;
  461. changed = true;
  462. }
  463. break;
  464. case "String":
  465. string stringValue = (string)value;
  466. if (stringValue != _items[name].StringValue)
  467. {
  468. _items[name].StringValue = stringValue;
  469. changed = true;
  470. }
  471. break;
  472. }
  473. if (changed)
  474. {
  475. GenerateDataFile();
  476. }
  477. }
  478. public void SetItemValue(string name, bool value)
  479. {
  480. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  481. Debug.Assert(_items[name].Type=="Bool", "sc type not bool, defined as" + _items[name].Type);
  482. if (value != _items[name].BoolValue)
  483. {
  484. _items[name].BoolValue = value;
  485. GenerateDataFile();
  486. }
  487. }
  488. public void SetItemValue(string name, int value)
  489. {
  490. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  491. Debug.Assert(_items[name].Type == "Integer", "sc type not bool, defined as" + _items[name].Type);
  492. if (value != _items[name].IntValue)
  493. {
  494. _items[name].IntValue = value;
  495. GenerateDataFile();
  496. }
  497. }
  498. public void SetItemValue(string name, double value)
  499. {
  500. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  501. Debug.Assert(_items[name].Type == "Double", "sc type not bool, defined as" + _items[name].Type);
  502. if (Math.Abs(value - _items[name].DoubleValue) > 0.0001)
  503. {
  504. _items[name].DoubleValue = value;
  505. GenerateDataFile();
  506. }
  507. }
  508. public void SetItemValue(string name, string value)
  509. {
  510. Debug.Assert(_items.ContainsKey(name), "can not find sc name, " + name);
  511. if (value != _items[name].StringValue)
  512. {
  513. _items[name].StringValue = value;
  514. GenerateDataFile();
  515. }
  516. }
  517. }
  518. }