SystemConfigManager.cs 16 KB

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