VIDManager2.cs 43 KB

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