SystemConfigManager.cs 15 KB

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