SystemConfigManager.cs 18 KB

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