VIDManager.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.SCCore;
  5. using Aitex.Core.Util;
  6. using Aitex.Core.Utilities;
  7. using MECF.Framework.Common.Equipment;
  8. using MECF.Framework.Common.IOCore;
  9. using MECF.Framework.RT.Core.IoProviders;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Xml;
  17. using System.Xml.Serialization;
  18. using Aitex.Core.RT.Event;
  19. using MECF.Framework.Common.SCCore;
  20. using SciChart.Core.Messaging;
  21. using MECF.Framework.Common.Utilities;
  22. using System.IO;
  23. namespace MECF.Framework.Common.FAServices
  24. {
  25. /// <summary>
  26. /// 所有ID 的命名规则:
  27. ///
  28. /// Module(1-399) + Type(1-80) + Unit(1-99) + Parameter (1-999)
  29. ///
  30. /// 最小 1 01 01 001 = 10101001
  31. ///
  32. /// 最大 399 99 99 999 = 3 999 999 999
  33. ///
  34. /// "PPP" 一部分的就是按参数 module=1, type=1, unit=1, parameter="AAA"
  35. ///
  36. ///
  37. /// </summary>
  38. ///
  39. ///
  40. [Serializable]
  41. [XmlRoot("DataItems")]
  42. public class VIDItem
  43. {
  44. private string name;
  45. [XmlAttribute("Name")]
  46. public string Name
  47. {
  48. get { return name; }
  49. set
  50. {
  51. name = value;
  52. AnalyseName(value);
  53. }
  54. }
  55. [XmlAttribute("Index")]
  56. public long Index { get; set; }
  57. [XmlAttribute("DataType")]
  58. public string DataType { get; set; }
  59. [XmlArray("LinkableVID")]
  60. public int[] LinkableVid { get; set; }
  61. [XmlAttribute("Description")]
  62. public string Description { get; set; }
  63. [XmlAttribute("Module")]
  64. public string Module { get; set; } = "";
  65. [XmlAttribute("Type")]
  66. public string Type { get; set; } = "";
  67. [XmlAttribute("Unit")]
  68. public string Unit { get; set; } = "";
  69. [XmlAttribute("Parameter")]
  70. public string Parameter { get; set; }
  71. private int moduleIndex;
  72. [XmlIgnore]
  73. public int ModuleIndex
  74. {
  75. get { return moduleIndex; }
  76. set
  77. {
  78. moduleIndex = value;
  79. }
  80. }
  81. private int typeIndex;
  82. [XmlIgnore]
  83. public int TypeIndex
  84. {
  85. get { return typeIndex; }
  86. set
  87. {
  88. typeIndex = value;
  89. }
  90. }
  91. private int unitIndex;
  92. [XmlIgnore]
  93. public int UnitIndex
  94. {
  95. get { return unitIndex; }
  96. set
  97. {
  98. unitIndex = value;
  99. }
  100. }
  101. private int parameterIndex;
  102. [XmlIgnore]
  103. public int ParameterIndex
  104. {
  105. get { return parameterIndex; }
  106. set
  107. {
  108. parameterIndex = value;
  109. }
  110. }
  111. void AnalyseName(string name)
  112. {
  113. if (string.IsNullOrEmpty(name))
  114. return;
  115. string[] names = name.Split('.');
  116. Module = ModuleName.System.ToString();
  117. if (names.Length >= 1)
  118. {
  119. Parameter = names[names.Length - 1];
  120. if (names.Length >= 2)
  121. {
  122. Module = names[0];
  123. ModuleIndex = (Enum.TryParse(Module, out ModuleName moduleIndex) ? (int)moduleIndex : 0) + 1;
  124. if (names.Length >= 3)
  125. {
  126. Unit = names[names.Length - 2];
  127. if (names.Length >= 4)
  128. {
  129. for (int j = 1; j < names.Length - 2; j++)
  130. {
  131. Type += names[j];
  132. if (j != names.Length - 3)
  133. Type += ".";
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. public enum VIDTypeEnum
  142. {
  143. SVID,
  144. DVID,
  145. CEID,
  146. ALID,
  147. ECID,
  148. }
  149. public class VIDGenerator
  150. {
  151. public string Type { get; set; }
  152. public string SourceFileName { get; set; }
  153. public List<VIDItem> VIDList = new List<VIDItem>();
  154. public Dictionary<string, int> _moduleIndex = new Dictionary<string, int>();
  155. public Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, long>>>> moduleTypeUnitParamIndex = new Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, long>>>>();
  156. public Dictionary<string, long> _index = new Dictionary<string, long>();
  157. private string _defaultPathFile;
  158. private string _type;
  159. public VIDGenerator(string type, string defaultPathFile)
  160. {
  161. _type = type;
  162. _defaultPathFile = defaultPathFile;
  163. }
  164. public void Initialize()
  165. {
  166. XmlDocument xml = new XmlDocument();
  167. try
  168. {
  169. EnumLoop<ModuleName>.ForEach((item) =>
  170. {
  171. moduleTypeUnitParamIndex[item.ToString()] = new Dictionary<string, Dictionary<string, Dictionary<string, long>>>();
  172. });
  173. xml.Load(_defaultPathFile);
  174. VIDList = CustomXmlSerializer.Deserialize<List<VIDItem>>(xml.OuterXml);
  175. foreach (var VIDItem in VIDList)
  176. {
  177. //_moduleTypeIndex[VIDItem.Type] = VIDItem.TypeIndex;
  178. //_moduleTypeUnitIndex[VIDItem.Unit] = VIDItem.UnitIndex;
  179. //_parameterIndex[VIDItem.Parameter] = VIDItem.ParameterIndex;
  180. if (!moduleTypeUnitParamIndex.ContainsKey(VIDItem.Module))
  181. {
  182. moduleTypeUnitParamIndex[VIDItem.Module] = new Dictionary<string, Dictionary<string, Dictionary<string, long>>>();
  183. }
  184. if (!moduleTypeUnitParamIndex[VIDItem.Module].ContainsKey(VIDItem.Type))
  185. {
  186. moduleTypeUnitParamIndex[VIDItem.Module][VIDItem.Type] = new Dictionary<string, Dictionary<string, long>>();
  187. }
  188. if (!moduleTypeUnitParamIndex[VIDItem.Module][VIDItem.Type].ContainsKey(VIDItem.Unit))
  189. {
  190. moduleTypeUnitParamIndex[VIDItem.Module][VIDItem.Type][VIDItem.Unit] = new Dictionary<string, long>();
  191. }
  192. moduleTypeUnitParamIndex[VIDItem.Module][VIDItem.Type][VIDItem.Unit][VIDItem.Parameter] = VIDItem.Index;
  193. _index[VIDItem.Name] = VIDItem.Index;
  194. }
  195. //XmlNodeList itemNodes = xml.SelectNodes("DataItems/DataItem");
  196. //foreach (var itemNode in itemNodes)
  197. //{
  198. // XmlElement element = itemNode as XmlElement;
  199. // if (element == null)
  200. // continue;
  201. // string name = element.GetAttribute("name").Trim();
  202. // string index = element.GetAttribute("index").Trim();
  203. // if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || (index.Length != 9 && index.Length != 8))
  204. // continue;
  205. // var item = ParseName(name);
  206. // _moduleTypeIndex[$"{item.Item1}.{item.Item2}"] = int.Parse(index.Substring(index.Length - 7, 2));
  207. // _moduleTypeUnitIndex[$"{item.Item1}.{item.Item2}.{item.Item3}"] = int.Parse(index.Substring(index.Length - 5, 2));
  208. // _parameterIndex[$"{item.Item1}.{item.Item2}.{item.Item3}.{item.Item4}"] =
  209. // int.Parse(index.Substring(index.Length - 3, 3));
  210. // if (!_max.ContainsKey($"{item.Item1}"))
  211. // _max[$"{item.Item1}"] = 0;
  212. // _max[$"{item.Item1}"] =
  213. // Math.Max(_moduleTypeIndex[$"{item.Item1}.{item.Item2}"], _max[$"{item.Item1}"]);
  214. // if (!_max.ContainsKey($"{item.Item1}.{item.Item2}"))
  215. // _max[$"{item.Item1}.{item.Item2}"] = 0;
  216. // _max[$"{item.Item1}.{item.Item2}"] =
  217. // Math.Max(_moduleTypeUnitIndex[$"{item.Item1}.{item.Item2}.{item.Item3}"], _max[$"{item.Item1}.{item.Item2}"]);
  218. // if (!_max.ContainsKey($"{item.Item1}.{item.Item2}.{item.Item3}"))
  219. // _max[$"{item.Item1}.{item.Item2}.{item.Item3}"] = 0;
  220. // _max[$"{item.Item1}.{item.Item2}.{item.Item3}"] =
  221. // Math.Max(_parameterIndex[$"{item.Item1}.{item.Item2}.{item.Item3}.{item.Item4}"], _max[$"{item.Item1}.{item.Item2}.{item.Item3}"]);
  222. // _index[name] = int.Parse(index);
  223. //}
  224. }
  225. catch (Exception ex)
  226. {
  227. LOG.Write(ex);
  228. }
  229. EnumLoop<ModuleName>.ForEach((item) =>
  230. {
  231. _moduleIndex[item.ToString()] = ((int)item) + 1;
  232. });
  233. }
  234. public Tuple<string, string, string, string> ParseName(string name)
  235. {
  236. if (string.IsNullOrEmpty(name))
  237. return null;
  238. string module = ModuleName.System.ToString();
  239. string type = "";
  240. string unit = "";
  241. string parameter = "";
  242. string[] names = name.Split('.');
  243. if (names.Length >= 1)
  244. {
  245. parameter = names[names.Length - 1];
  246. if (names.Length >= 2)
  247. {
  248. if (_moduleIndex.ContainsKey(names[0]))
  249. {
  250. module = names[0];
  251. if (names.Length >= 3)
  252. {
  253. unit = names[names.Length - 2];
  254. if (names.Length >= 4)
  255. {
  256. for (int j = 1; j < names.Length - 2; j++)
  257. {
  258. type += names[j];
  259. if (j != names.Length - 3)
  260. type += ".";
  261. }
  262. }
  263. }
  264. }
  265. else
  266. {
  267. //module = module;
  268. unit = names[names.Length - 2];
  269. if (names.Length >= 3)
  270. {
  271. for (int j = 0; j < names.Length - 2; j++)
  272. {
  273. type += names[j];
  274. if (j != names.Length - 3)
  275. type += ".";
  276. }
  277. }
  278. }
  279. }
  280. }
  281. return Tuple.Create(module, type, unit, parameter);
  282. }
  283. public void GenerateId(List<VIDItem> dataList)
  284. {
  285. List<VIDItem> newList = new List<VIDItem>();
  286. foreach (var data in dataList)
  287. {
  288. if (!_index.ContainsKey(data.Name))
  289. newList.Add(data);
  290. }
  291. if (newList.Count > 0)
  292. {
  293. AssignNewId(newList);
  294. VIDList = VIDList.Concat(newList).OrderBy(x => x.Index).ToList();
  295. if (File.Exists(_defaultPathFile))
  296. CustomXmlSerializer.Serialize(VIDList, _defaultPathFile);
  297. }
  298. }
  299. private void AssignNewId(List<VIDItem> dataList)
  300. {
  301. if (dataList.Count == 0)
  302. return;
  303. dataList = dataList.OrderBy(x => x.Name).ToList();
  304. foreach (var data in dataList)
  305. {
  306. data.Name = data.Name;
  307. if (!moduleTypeUnitParamIndex.ContainsKey(data.Module))
  308. {
  309. moduleTypeUnitParamIndex[data.Module] = new Dictionary<string, Dictionary<string, Dictionary<string, long>>>();
  310. moduleTypeUnitParamIndex[data.Module][data.Type] = new Dictionary<string, Dictionary<string, long>>();
  311. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit] = new Dictionary<string, long>();
  312. data.ModuleIndex = moduleTypeUnitParamIndex.Keys.ToList().FindIndex(x => x == data.Module) + 1;
  313. data.TypeIndex = 1;
  314. data.UnitIndex = 1;
  315. data.ParameterIndex = 1;
  316. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter] = long.Parse($"{ data.ModuleIndex}{data.TypeIndex.ToString().PadLeft(2, '0')}{data.UnitIndex.ToString().PadLeft(2, '0')}{ data.ParameterIndex.ToString().PadLeft(3, '0')}");
  317. data.Index = moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter];
  318. continue;
  319. }
  320. if (!moduleTypeUnitParamIndex[data.Module].ContainsKey(data.Type))
  321. {
  322. moduleTypeUnitParamIndex[data.Module][data.Type] = new Dictionary<string, Dictionary<string, long>>();
  323. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit] = new Dictionary<string, long>();
  324. data.ModuleIndex = moduleTypeUnitParamIndex.Keys.ToList().FindIndex(x => x == data.Module) + 1;
  325. data.TypeIndex = moduleTypeUnitParamIndex[data.Module].Count;
  326. data.UnitIndex = 1;
  327. data.ParameterIndex = 1;
  328. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter] = long.Parse($"{ data.ModuleIndex}{data.TypeIndex.ToString().PadLeft(2, '0')}{data.UnitIndex.ToString().PadLeft(2, '0')}{ data.ParameterIndex.ToString().PadLeft(3, '0')}");
  329. data.Index = moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter];
  330. continue;
  331. }
  332. if (!moduleTypeUnitParamIndex[data.Module][data.Type].ContainsKey(data.Unit))
  333. {
  334. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit] = new Dictionary<string, long>();
  335. data.ModuleIndex = moduleTypeUnitParamIndex.Keys.ToList().FindIndex(x => x == data.Module) + 1;
  336. data.TypeIndex = moduleTypeUnitParamIndex[data.Module].Keys.ToList().FindIndex(x => x == data.Type) + 2;
  337. data.UnitIndex = moduleTypeUnitParamIndex[data.Module][data.Type].Count;
  338. data.ParameterIndex = 1;
  339. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter] = long.Parse($"{ data.ModuleIndex}{data.TypeIndex.ToString().PadLeft(2, '0')}{data.UnitIndex.ToString().PadLeft(2, '0')}{ data.ParameterIndex.ToString().PadLeft(3, '0')}");
  340. data.Index = moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter];
  341. continue;
  342. }
  343. moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter] = moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit].Last().Value + 1;
  344. data.Index = moduleTypeUnitParamIndex[data.Module][data.Type][data.Unit][data.Parameter];
  345. }
  346. //XmlDocument xml = new XmlDocument();
  347. //try
  348. //{
  349. // xml.Load(_defaultPathFile);
  350. // XmlNode itemNodes = xml.SelectSingleNode("DataItems");
  351. // itemNodes.RemoveAll();
  352. // Dictionary<int, string> orderedName = _index.OrderBy(o => o.Value).ToDictionary(p => p.Value, o => o.Key);
  353. // foreach (var name in orderedName)
  354. // {
  355. // XmlElement subNode = xml.CreateElement("DataItem");
  356. // subNode.SetAttribute("name", name.Value);
  357. // subNode.SetAttribute("index", name.Key.ToString());
  358. // itemNodes.AppendChild(subNode);
  359. // }
  360. // xml.Save(_defaultPathFile);
  361. //}
  362. //catch (Exception ex)
  363. //{
  364. // LOG.Write(ex);
  365. //}
  366. }
  367. }
  368. public class VIDManager : Singleton<VIDManager>
  369. {
  370. public void Initialize(string equipName, bool enableGem300Events = false, bool needReGenerateGemModelXml = false)
  371. {
  372. if (!System.Diagnostics.Debugger.IsAttached)
  373. return;
  374. //SVID
  375. var svid = new VIDGenerator("SVID", $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_SVID.xml");
  376. svid.Initialize();
  377. svid.GenerateId(Singleton<DataManager>.Instance.VidDataList);
  378. ExportSvid(svid.VIDList, true, true);
  379. //ECID
  380. var ecid = new VIDGenerator("ECID", $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_ECID.xml");
  381. ecid.Initialize();
  382. ecid.GenerateId(SystemConfigManager.Instance.VidConfigList);
  383. ExportEcid(ecid.VIDList);
  384. //CEID
  385. List<VIDItem> ceids = new List<VIDItem>();
  386. foreach (var eventItem in Singleton<EventManager>.Instance.VidEventList)
  387. {
  388. if (UniversalEvents.UniversalEventsDictionary.ContainsKey(eventItem.Name))
  389. {
  390. ceids.Add(UniversalEvents.UniversalEventsDictionary[eventItem.Name]);
  391. }
  392. else if (enableGem300Events && Gem300Events.Gem300EventsDictionary.ContainsKey(eventItem.Name))
  393. {
  394. ceids.Add(Gem300Events.Gem300EventsDictionary[eventItem.Name]);
  395. }
  396. }
  397. ceids = ceids.OrderBy(x => x.Index).ToList();
  398. CustomXmlSerializer.Serialize(ceids, $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_CEID.xml");
  399. ExportCeid(ceids);
  400. //ALID
  401. var alid = new VIDGenerator("ALID", $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_ALID.xml");
  402. alid.Initialize();
  403. alid.GenerateId(Singleton<EventManager>.Instance.VidAlarmList);
  404. ExportAlid(alid.VIDList);
  405. //DVID, to be designed
  406. Dictionary<string, VIDItem> dvids = new Dictionary<string, VIDItem>();
  407. foreach (var eventItem in ceids)
  408. {
  409. if (eventItem.LinkableVid == null)
  410. continue;
  411. foreach (var LinkableVid in eventItem.LinkableVid)
  412. {
  413. string dvidName = ((DataVariables.DataName)LinkableVid).ToString();
  414. if (DataVariables.DataVariablesDictionary.ContainsKey(dvidName))
  415. dvids[dvidName] = DataVariables.DataVariablesDictionary[dvidName];
  416. }
  417. }
  418. var dvidList = dvids.Values.ToList().OrderBy(x => x.Index).ToList();
  419. CustomXmlSerializer.Serialize(dvidList, $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_DVID.xml");
  420. ExportDvid(dvidList);
  421. //ReGenerate GemModel.Xml
  422. if (needReGenerateGemModelXml)
  423. ReGenerateGemModelXml(svid.VIDList, ecid.VIDList, dvidList, ceids, alid.VIDList, equipName);
  424. }
  425. private List<VIDItem> OriginalSvids = new List<VIDItem>()
  426. {
  427. new VIDItem(){Name = "AlarmsEnabled",Index = 1,DataType = "List"},
  428. new VIDItem(){Name = "AlarmsSet",Index = 2,DataType = "List"},
  429. new VIDItem(){Name = "Clock",Index = 3,DataType = "Ascii"},
  430. new VIDItem(){Name = "ControlState",Index = 4,DataType = "U4"},
  431. new VIDItem(){Name = "EventsEnabled",Index = 5,DataType = "List"},
  432. new VIDItem(){Name = "PPExecName",Index = 6,DataType = "Ascii"},
  433. new VIDItem(){Name = "PreviousProcessState",Index = 7,DataType = "U1"},
  434. new VIDItem(){Name = "ProcessState",Index = 8,DataType = "U1"},
  435. new VIDItem(){Name = "SpoolCountActual",Index = 9,DataType = "U4"},
  436. new VIDItem(){Name = "SpoolCountTotal",Index = 10,DataType = "U4"},
  437. new VIDItem(){Name = "SpoolFullTime",Index = 11,DataType = "Ascii"},
  438. new VIDItem(){Name = "SpoolStartTime",Index = 12,DataType = "Ascii"},
  439. new VIDItem(){Name = "SpoolState",Index = 13,DataType = "Ascii"},
  440. new VIDItem(){Name = "SpoolSubstate",Index = 14,DataType = "Ascii"},
  441. };
  442. private List<VIDItem> OriginalEcids = new List<VIDItem>()
  443. {
  444. new VIDItem(){Name = "EstablishCommunicationsTimeout",Index = 2000,DataType = "U2",Description = "2"},
  445. new VIDItem(){Name = "MaxSpoolTransmit",Index = 2001,DataType = "U4",Description = "100"},
  446. new VIDItem(){Name = "OverWriteSpool",Index = 2003,DataType = "Boolean",Description = "FALSE"},
  447. new VIDItem(){Name = "MaxSpoolCapacity",Index = 2005,DataType = "U4",Description = "100"},
  448. new VIDItem(){Name = "SpoolEnabled",Index = 2006,DataType = "Boolean",Description = "False"},
  449. new VIDItem(){Name = "TimeFormat",Index = 2007,DataType = "U1",Description = "0"},
  450. };
  451. private void ExportAlid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  452. {
  453. var lists = dataList.OrderBy(x => x.ModuleIndex).ThenBy(x => x.Name).ToList();
  454. bool? result = defaultPath;
  455. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  456. if (!defaultPath)
  457. {
  458. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  459. dlg.DefaultExt = ".xlsx"; // Default file extension
  460. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  461. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  462. result = dlg.ShowDialog();// Show open file dialog box
  463. savePath = dlg.FileName;
  464. }
  465. if (result == true)
  466. {
  467. System.Data.DataSet ds = new System.Data.DataSet();
  468. ds.Tables.Add(new System.Data.DataTable("ALID(Alarm ID)"));
  469. ds.Tables[0].Columns.Add("ALID");
  470. ds.Tables[0].Columns.Add("Name");
  471. ds.Tables[0].Columns.Add("Description");
  472. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  473. foreach (var item in lists)
  474. {
  475. var row = ds.Tables[0].NewRow();
  476. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  477. IdDictionary[item.ModuleIndex] = 0;
  478. row[0] = item.Index;
  479. row[1] = item.Name;
  480. if (string.IsNullOrEmpty(item.Description))
  481. {
  482. var arr = item.Name.Split('.');
  483. for (int i = 0; i < arr.Length; i++)
  484. row[2] += $"{arr[i]} ";
  485. }
  486. else
  487. {
  488. row[2] = item.Description;
  489. }
  490. ds.Tables[0].Rows.Add(row);
  491. }
  492. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  493. {
  494. LOG.Write($"Export failed, {reason}");
  495. return;
  496. }
  497. LOG.Write($"Export succeed, file save as {savePath}");
  498. }
  499. }
  500. private void ExportCeid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  501. {
  502. var lists = dataList.OrderBy(x => x.Index).ToList();
  503. bool? result = defaultPath;
  504. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  505. if (!defaultPath)
  506. {
  507. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  508. dlg.DefaultExt = ".xlsx"; // Default file extension
  509. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  510. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  511. result = dlg.ShowDialog();// Show open file dialog box
  512. savePath = dlg.FileName;
  513. }
  514. if (result == true)
  515. {
  516. System.Data.DataSet ds = new System.Data.DataSet();
  517. ds.Tables.Add(new System.Data.DataTable("CEID(Collection Events)"));
  518. ds.Tables[0].Columns.Add("CEID");
  519. ds.Tables[0].Columns.Add("Name");
  520. ds.Tables[0].Columns.Add("LinkableVID");
  521. ds.Tables[0].Columns.Add("Description");
  522. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  523. foreach (var item in lists)
  524. {
  525. var row = ds.Tables[0].NewRow();
  526. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  527. IdDictionary[item.ModuleIndex] = 0;
  528. row[0] = item.Index;
  529. row[1] = item.Name;
  530. if (item.LinkableVid != null)
  531. {
  532. string LinkableVidDescription = String.Empty;
  533. for (int i = 0; i < item.LinkableVid.Length; i++)
  534. {
  535. LinkableVidDescription += $"{(DataVariables.DataName)item.LinkableVid[i]} = {item.LinkableVid[i]} \r\n";
  536. }
  537. row[2] = LinkableVidDescription;
  538. }
  539. if (string.IsNullOrEmpty(item.Description))
  540. {
  541. var arr = item.Name.Split('.');
  542. for (int i = 0; i < arr.Length; i++)
  543. row[3] += $"{arr[i]} ";
  544. }
  545. else
  546. {
  547. row[3] = item.Description;
  548. }
  549. ds.Tables[0].Rows.Add(row);
  550. }
  551. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  552. {
  553. LOG.Write($"Export failed, {reason}");
  554. return;
  555. }
  556. LOG.Write($"Export succeed, file save as {savePath}");
  557. }
  558. }
  559. private void ExportEcid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  560. {
  561. var lists = dataList.OrderBy(x => x.Index).ToList();
  562. bool? result = defaultPath;
  563. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  564. if (!defaultPath)
  565. {
  566. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  567. dlg.DefaultExt = ".xlsx"; // Default file extension
  568. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  569. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  570. result = dlg.ShowDialog();// Show open file dialog box
  571. savePath = dlg.FileName;
  572. }
  573. if (result == true)
  574. {
  575. System.Data.DataSet ds = new System.Data.DataSet();
  576. ds.Tables.Add(new System.Data.DataTable("ECID(Equipment Constant)"));
  577. ds.Tables[0].Columns.Add("ECID");
  578. ds.Tables[0].Columns.Add("Name");
  579. ds.Tables[0].Columns.Add("Format");
  580. ds.Tables[0].Columns.Add("Description");
  581. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  582. foreach (var item in lists)
  583. {
  584. var row = ds.Tables[0].NewRow();
  585. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  586. IdDictionary[item.ModuleIndex] = 0;
  587. row[0] = item.Index;
  588. row[1] = item.Name;
  589. row[2] = VIDItemType2GemDataType(item.DataType);
  590. if (string.IsNullOrEmpty(item.Description))
  591. {
  592. var arr = item.Name.Split('.');
  593. for (int i = 0; i < arr.Length; i++)
  594. row[3] += $"{arr[i]} ";
  595. }
  596. else
  597. {
  598. row[3] = item.Description;
  599. }
  600. ds.Tables[0].Rows.Add(row);
  601. }
  602. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  603. {
  604. LOG.Write($"Export failed, {reason}");
  605. return;
  606. }
  607. LOG.Write($"Export succeed, file save as {savePath}");
  608. }
  609. }
  610. private void ExportSvid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  611. {
  612. var lists = dataList.OrderBy(x => x.Index).ToList();
  613. bool? result = defaultPath;
  614. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  615. if (!defaultPath)
  616. {
  617. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  618. dlg.DefaultExt = ".xlsx"; // Default file extension
  619. dlg.FileName = $"Chamber_Status_Variable_{DateTime.Now:yyyyMMdd_HHmmss}";
  620. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  621. result = dlg.ShowDialog();// Show open file dialog box
  622. savePath = dlg.FileName;
  623. }
  624. if (result == true)
  625. {
  626. System.Data.DataSet ds = new System.Data.DataSet();
  627. ds.Tables.Add(new System.Data.DataTable("SVID(Status Variable)"));
  628. ds.Tables[0].Columns.Add("SVID");
  629. ds.Tables[0].Columns.Add("Name");
  630. ds.Tables[0].Columns.Add("Format");
  631. ds.Tables[0].Columns.Add("Description");
  632. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  633. foreach (var item in lists)
  634. {
  635. var row = ds.Tables[0].NewRow();
  636. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  637. IdDictionary[item.ModuleIndex] = 0;
  638. row[0] = item.Index;
  639. row[1] = item.Name;
  640. row[2] = VIDItemType2GemDataType(item.DataType);
  641. if (string.IsNullOrEmpty(item.Description))
  642. {
  643. var arr = item.Name.Split('.');
  644. for (int i = 0; i < arr.Length; i++)
  645. row[3] += $"{arr[i]} ";
  646. }
  647. else
  648. {
  649. row[3] = item.Description;
  650. }
  651. ds.Tables[0].Rows.Add(row);
  652. }
  653. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  654. {
  655. LOG.Write($"Export failed, {reason}");
  656. return;
  657. }
  658. LOG.Write($"Export succeed, file save as {savePath}");
  659. }
  660. }
  661. private void ExportDvid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  662. {
  663. var lists = dataList.OrderBy(x => x.Index).ToList();
  664. bool? result = defaultPath;
  665. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  666. if (!defaultPath)
  667. {
  668. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  669. dlg.DefaultExt = ".xlsx"; // Default file extension
  670. dlg.FileName = $"Data_Variable_{DateTime.Now:yyyyMMdd_HHmmss}";
  671. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  672. result = dlg.ShowDialog();// Show open file dialog box
  673. savePath = dlg.FileName;
  674. }
  675. if (result == true)
  676. {
  677. System.Data.DataSet ds = new System.Data.DataSet();
  678. ds.Tables.Add(new System.Data.DataTable("DVID(Data Variable)"));
  679. ds.Tables[0].Columns.Add("DVID");
  680. ds.Tables[0].Columns.Add("Name");
  681. ds.Tables[0].Columns.Add("Format");
  682. ds.Tables[0].Columns.Add("Description");
  683. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  684. foreach (var item in lists)
  685. {
  686. var row = ds.Tables[0].NewRow();
  687. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  688. IdDictionary[item.ModuleIndex] = 0;
  689. row[0] = item.Index;
  690. row[1] = item.Name;
  691. row[2] = VIDItemType2GemDataType(item.DataType);
  692. if (string.IsNullOrEmpty(item.Description))
  693. {
  694. var arr = item.Name.Split('.');
  695. for (int i = 0; i < arr.Length; i++)
  696. row[3] += $"{arr[i]} ";
  697. }
  698. else
  699. {
  700. row[3] = item.Description;
  701. }
  702. ds.Tables[0].Rows.Add(row);
  703. }
  704. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  705. {
  706. LOG.Write($"Export failed, {reason}");
  707. return;
  708. }
  709. LOG.Write($"Export succeed, file save as {savePath}");
  710. }
  711. }
  712. private void ReGenerateGemModelXml(List<VIDItem> Svids, List<VIDItem> Ecids, List<VIDItem> Dvids, List<VIDItem> Ceids, List<VIDItem> Alids, string equipName)
  713. {
  714. XmlDocument xml = new XmlDocument();
  715. string _defaultPathFile = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}{equipName}GemModel.xml";
  716. try
  717. {
  718. xml.Load(_defaultPathFile);
  719. if (Svids != null)
  720. {
  721. XmlNode itemNodes = xml.SelectSingleNode("Equipment/StatusVariables");
  722. itemNodes.RemoveAll();
  723. foreach (var svid in OriginalSvids)
  724. {
  725. XmlElement subNode = xml.CreateElement("SVID");
  726. subNode.SetAttribute("id", svid.Index.ToString());
  727. subNode.SetAttribute("valueType", svid.DataType);
  728. subNode.SetAttribute("logicalName", svid.Name);
  729. subNode.SetAttribute("value", "");
  730. subNode.SetAttribute("eventTrigger", "");
  731. subNode.SetAttribute("units", "");
  732. subNode.SetAttribute("description", svid.Description);
  733. subNode.SetAttribute("isArray", "false");
  734. itemNodes.AppendChild(subNode);
  735. }
  736. foreach (var svid in Svids)
  737. {
  738. XmlElement subNode = xml.CreateElement("SVID");
  739. subNode.SetAttribute("id", svid.Index.ToString());
  740. subNode.SetAttribute("valueType", VIDItemType2GemDataType(svid.DataType));
  741. subNode.SetAttribute("logicalName", svid.Name);
  742. subNode.SetAttribute("value", "");
  743. subNode.SetAttribute("eventTrigger", "");
  744. subNode.SetAttribute("units", "");
  745. subNode.SetAttribute("description", svid.Description);
  746. subNode.SetAttribute("isArray", "false");
  747. itemNodes.AppendChild(subNode);
  748. }
  749. }
  750. if (Ecids != null)
  751. {
  752. XmlNode itemNodes = xml.SelectSingleNode("Equipment/EquipmentConstants");
  753. itemNodes.RemoveAll();
  754. foreach (var ecid in OriginalEcids)
  755. {
  756. XmlElement subNode = xml.CreateElement("ECID");
  757. subNode.SetAttribute("id", ecid.Index.ToString());
  758. subNode.SetAttribute("valueType", ecid.DataType);
  759. subNode.SetAttribute("logicalName", ecid.Name);
  760. subNode.SetAttribute("value", ecid.Description);
  761. subNode.SetAttribute("min", "0");
  762. subNode.SetAttribute("max", "100");
  763. subNode.SetAttribute("eventTrigger", "");
  764. subNode.SetAttribute("units", "");
  765. subNode.SetAttribute("description", "");
  766. subNode.SetAttribute("isArray", "false");
  767. itemNodes.AppendChild(subNode);
  768. }
  769. foreach (var ecid in Ecids)
  770. {
  771. XmlElement subNode = xml.CreateElement("ECID");
  772. subNode.SetAttribute("id", ecid.Index.ToString());
  773. subNode.SetAttribute("valueType", VIDItemType2GemDataType(ecid.DataType));
  774. subNode.SetAttribute("logicalName", ecid.Name);
  775. subNode.SetAttribute("value", "");
  776. subNode.SetAttribute("min", "");
  777. subNode.SetAttribute("max", "");
  778. subNode.SetAttribute("eventTrigger", "");
  779. subNode.SetAttribute("units", "");
  780. subNode.SetAttribute("description", ecid.Description);
  781. subNode.SetAttribute("isArray", "false");
  782. itemNodes.AppendChild(subNode);
  783. }
  784. }
  785. if (Dvids != null)
  786. {
  787. XmlNode itemNodes = xml.SelectSingleNode("Equipment/DataVariables");
  788. itemNodes.RemoveAll();
  789. foreach (var dvids in Dvids)
  790. {
  791. if (dvids.Index < 500)
  792. continue;
  793. XmlElement subNode = xml.CreateElement("DVID");
  794. subNode.SetAttribute("id", dvids.Index.ToString());
  795. subNode.SetAttribute("valueType", VIDItemType2GemDataType(dvids.DataType));
  796. subNode.SetAttribute("logicalName", dvids.Name);
  797. subNode.SetAttribute("value", "");
  798. subNode.SetAttribute("eventTrigger", "");
  799. subNode.SetAttribute("description", dvids.Description);
  800. subNode.SetAttribute("isArray", "false");
  801. itemNodes.AppendChild(subNode);
  802. }
  803. }
  804. if (Ceids != null)
  805. {
  806. int reportID = 0;
  807. Dictionary<string, int> reportDictionary = new Dictionary<string, int>();
  808. XmlNode RPTIDNodes = xml.SelectSingleNode("Equipment/DataCollections/RPTIDs");
  809. RPTIDNodes.RemoveAll();
  810. foreach (var ceid in Ceids)
  811. {
  812. XmlElement RPTID = xml.CreateElement("RPTID");
  813. if (ceid.LinkableVid != null)
  814. {
  815. string reportKey = string.Join(",", ceid.LinkableVid);
  816. if (!reportDictionary.ContainsKey(reportKey))
  817. {
  818. reportID += 1;
  819. reportDictionary[reportKey] = reportID;
  820. RPTID.SetAttribute("id", reportID.ToString());
  821. RPTID.SetAttribute("logicalName", $"DefaultDefinedReport_{reportID}");
  822. foreach (var vid in ceid.LinkableVid)
  823. {
  824. XmlElement ReportVariable = xml.CreateElement("ReportVariable");
  825. ReportVariable.SetAttribute("id", vid.ToString());
  826. ReportVariable.SetAttribute("varType", vid < 500 ? "StatusVariable" : "DataVariable");
  827. ReportVariable.SetAttribute("logicalName", ((DataVariables.DataName)vid).ToString());
  828. RPTID.AppendChild(ReportVariable);
  829. }
  830. RPTIDNodes.AppendChild(RPTID);
  831. }
  832. }
  833. }
  834. XmlNode CEIDNodes = xml.SelectSingleNode("Equipment/DataCollections/CEIDs");
  835. CEIDNodes.RemoveAll();
  836. foreach (var ceid in Ceids)
  837. {
  838. XmlElement CEID = xml.CreateElement("CEID");
  839. CEID.SetAttribute("id", ceid.Index.ToString());
  840. CEID.SetAttribute("logicalName", ceid.Name);
  841. CEID.SetAttribute("description", ceid.Description);
  842. CEID.SetAttribute("enabled", "true");
  843. if (ceid.LinkableVid != null)
  844. {
  845. string reportKey = string.Join(",", ceid.LinkableVid);
  846. XmlElement RPTID = xml.CreateElement("RPTID");
  847. RPTID.SetAttribute("id", reportDictionary[reportKey].ToString());
  848. RPTID.SetAttribute("logicalName", $"DefaultDefinedReport_{reportDictionary[reportKey]}");
  849. foreach (var vid in ceid.LinkableVid)
  850. {
  851. XmlElement ReportVariable = xml.CreateElement("ReportVariable");
  852. ReportVariable.SetAttribute("id", vid.ToString());
  853. ReportVariable.SetAttribute("varType", vid < 500 ? "StatusVariable" : "DataVariable");
  854. ReportVariable.SetAttribute("logicalName", ((DataVariables.DataName)vid).ToString());
  855. RPTID.AppendChild(ReportVariable);
  856. }
  857. CEID.AppendChild(RPTID);
  858. }
  859. CEIDNodes.AppendChild(CEID);
  860. }
  861. }
  862. if (Alids != null)
  863. {
  864. XmlNode itemNodes = xml.SelectSingleNode("Equipment/Alarms");
  865. itemNodes.RemoveAll();
  866. foreach (var alid in Alids)
  867. {
  868. XmlElement subNode = xml.CreateElement("ALID");
  869. subNode.SetAttribute("id", alid.Index.ToString());
  870. subNode.SetAttribute("logicalName", alid.Name);
  871. subNode.SetAttribute("description", alid.Description);
  872. subNode.SetAttribute("category", "EquipmentStatusWarning");
  873. subNode.SetAttribute("enabled", "false");
  874. subNode.SetAttribute("eventSet", "1" + alid.Index);
  875. subNode.SetAttribute("eventClear", "2" + alid.Index);
  876. itemNodes.AppendChild(subNode);
  877. }
  878. }
  879. xml.Save(_defaultPathFile);
  880. }
  881. catch (Exception ex)
  882. {
  883. LOG.Write(ex);
  884. }
  885. }
  886. string VIDItemType2GemDataType(string VIDItemType)
  887. {
  888. switch (VIDItemType.Replace("System.", ""))
  889. {
  890. case "List":
  891. return "List";
  892. case "I1":
  893. return "I1";
  894. case "I4":
  895. return "I4";
  896. case "U1":
  897. return "U1";
  898. case "U2":
  899. return "U2";
  900. case "U4":
  901. return "U4";
  902. case "Int":
  903. case "Integer":
  904. return "I4";
  905. case "Bool":
  906. case "Boolean":
  907. return "Boolean";
  908. case "Float":
  909. case "Single":
  910. case "Double":
  911. case "F8":
  912. return "F8";
  913. case "Binary":
  914. return "Binary";
  915. default:
  916. return "Ascii";
  917. }
  918. }
  919. }
  920. }