VIDManager2.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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(3, '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(3, '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 = DataVariables.BoatCurrentFilmThickness,Index = 20,DataType = "Ascii"},
  312. new VIDItem(){Name = DataVariables.DryClearCount,Index = 21,DataType = "Ascii"},
  313. new VIDItem(){Name = DataVariables.ProcessRecipeStepName,Index = 22,DataType = "Ascii"},
  314. new VIDItem(){Name = DataVariables.SubRecipeStepName,Index = 23,DataType = "Ascii"},
  315. new VIDItem(){Name = DataVariables.SubRecipeStepNumber,Index = 24,DataType = "Ascii"},
  316. //new VIDItem(){Name = "System.SideDummyAccuThickness",Index = 15,DataType = "List"},
  317. //new VIDItem(){Name = "System.SideDummyUseCount",Index = 16,DataType = "List"},
  318. //new VIDItem(){Name = "System.ExtraDummyAccuThickness",Index = 17,DataType = "List"},
  319. //new VIDItem(){Name = "System.ExtraDummyUseCount",Index = 18,DataType = "List"},
  320. };
  321. private List<VIDItem> OriginalEcids = new List<VIDItem>()
  322. {
  323. new VIDItem(){Name = "EstablishCommunicationsTimeout",Index = 2000,DataType = "U2",Description = "2"},
  324. new VIDItem(){Name = "MaxSpoolTransmit",Index = 2001,DataType = "U4",Description = "100"},
  325. new VIDItem(){Name = "OverWriteSpool",Index = 2003,DataType = "Boolean",Description = "FALSE"},
  326. new VIDItem(){Name = "MaxSpoolCapacity",Index = 2005,DataType = "U4",Description = "100"},
  327. new VIDItem(){Name = "SpoolEnabled",Index = 2006,DataType = "Boolean",Description = "False"},
  328. new VIDItem(){Name = "TimeFormat",Index = 2007,DataType = "U1",Description = "0"},
  329. };
  330. private void ExportAlid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  331. {
  332. var lists = dataList.OrderBy(x => x.ModuleIndex).ThenBy(x => x.Name).ToList();
  333. bool? result = defaultPath;
  334. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  335. if (!defaultPath)
  336. {
  337. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  338. dlg.DefaultExt = ".xlsx"; // Default file extension
  339. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  340. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  341. result = dlg.ShowDialog();// Show open file dialog box
  342. savePath = dlg.FileName;
  343. }
  344. if (result == true)
  345. {
  346. System.Data.DataSet ds = new System.Data.DataSet();
  347. ds.Tables.Add(new System.Data.DataTable("ALID(Alarm ID)"));
  348. ds.Tables[0].Columns.Add("ALID");
  349. ds.Tables[0].Columns.Add("Name");
  350. ds.Tables[0].Columns.Add("Description");
  351. ds.Tables[0].Columns.Add("AlarmSet");
  352. ds.Tables[0].Columns.Add("AlarmClear");
  353. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  354. foreach (var item in lists)
  355. {
  356. var row = ds.Tables[0].NewRow();
  357. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  358. IdDictionary[item.ModuleIndex] = 0;
  359. row[0] = item.Index;
  360. row[1] = item.Name;
  361. if (string.IsNullOrEmpty(item.Description))
  362. {
  363. var arr = item.Name.Split('.');
  364. for (int i = 0; i < arr.Length; i++)
  365. row[2] += $"{arr[i]} ";
  366. }
  367. else
  368. {
  369. row[2] = item.Description;
  370. }
  371. row[3] = $"10{item.ModuleIndex.ToString().PadLeft(3, '0')}01";
  372. row[4] = $"20{item.ModuleIndex.ToString().PadLeft(3, '0')}01";
  373. ds.Tables[0].Rows.Add(row);
  374. }
  375. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  376. {
  377. LOG.Write($"Export failed, {reason}");
  378. return;
  379. }
  380. LOG.Write($"Export succeed, file save as {savePath}");
  381. }
  382. }
  383. private void ExportCeid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  384. {
  385. var lists = dataList.OrderBy(x => x.Index).ToList();
  386. bool? result = defaultPath;
  387. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  388. if (!defaultPath)
  389. {
  390. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  391. dlg.DefaultExt = ".xlsx"; // Default file extension
  392. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  393. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  394. result = dlg.ShowDialog();// Show open file dialog box
  395. savePath = dlg.FileName;
  396. }
  397. if (result == true)
  398. {
  399. System.Data.DataSet ds = new System.Data.DataSet();
  400. ds.Tables.Add(new System.Data.DataTable("CEID(Collection Events)"));
  401. ds.Tables[0].Columns.Add("CEID");
  402. ds.Tables[0].Columns.Add("Name");
  403. ds.Tables[0].Columns.Add("LinkableVID");
  404. ds.Tables[0].Columns.Add("Description");
  405. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  406. foreach (var item in lists)
  407. {
  408. var row = ds.Tables[0].NewRow();
  409. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  410. IdDictionary[item.ModuleIndex] = 0;
  411. row[0] = item.Index;
  412. row[1] = item.Name;
  413. if (item.LinkableVid != null)
  414. {
  415. string LinkableVidDescription = String.Empty;
  416. for (int i = 0; i < item.LinkableVid.Length; i++)
  417. {
  418. LinkableVidDescription += $"{(DataVariables.DataName)item.LinkableVid[i]} = {item.LinkableVid[i]} \r\n";
  419. }
  420. row[2] = LinkableVidDescription;
  421. }
  422. if (string.IsNullOrEmpty(item.Description))
  423. {
  424. var arr = item.Name.Split('.');
  425. for (int i = 0; i < arr.Length; i++)
  426. row[3] += $"{arr[i]} ";
  427. }
  428. else
  429. {
  430. row[3] = item.Description;
  431. }
  432. ds.Tables[0].Rows.Add(row);
  433. }
  434. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  435. {
  436. LOG.Write($"Export failed, {reason}");
  437. return;
  438. }
  439. LOG.Write($"Export succeed, file save as {savePath}");
  440. }
  441. }
  442. private void ExportEcid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  443. {
  444. var lists = dataList.OrderBy(x => x.Index).ToList();
  445. bool? result = defaultPath;
  446. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  447. if (!defaultPath)
  448. {
  449. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  450. dlg.DefaultExt = ".xlsx"; // Default file extension
  451. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  452. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  453. result = dlg.ShowDialog();// Show open file dialog box
  454. savePath = dlg.FileName;
  455. }
  456. if (result == true)
  457. {
  458. System.Data.DataSet ds = new System.Data.DataSet();
  459. ds.Tables.Add(new System.Data.DataTable("ECID(Equipment Constant)"));
  460. ds.Tables[0].Columns.Add("ECID");
  461. ds.Tables[0].Columns.Add("Name");
  462. ds.Tables[0].Columns.Add("Format");
  463. ds.Tables[0].Columns.Add("Description");
  464. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  465. foreach (var item in lists)
  466. {
  467. var row = ds.Tables[0].NewRow();
  468. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  469. IdDictionary[item.ModuleIndex] = 0;
  470. row[0] = item.Index;
  471. row[1] = item.Name;
  472. row[2] = VIDItemType2GemDataType(item.DataType);
  473. if (string.IsNullOrEmpty(item.Description))
  474. {
  475. var arr = item.Name.Split('.');
  476. for (int i = 0; i < arr.Length; i++)
  477. row[3] += $"{arr[i]} ";
  478. }
  479. else
  480. {
  481. row[3] = item.Description;
  482. }
  483. ds.Tables[0].Rows.Add(row);
  484. }
  485. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  486. {
  487. LOG.Write($"Export failed, {reason}");
  488. return;
  489. }
  490. LOG.Write($"Export succeed, file save as {savePath}");
  491. }
  492. }
  493. private void ExportSvid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  494. {
  495. var lists = dataList.OrderBy(x => x.Index).ToList();
  496. bool? result = defaultPath;
  497. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  498. if (!defaultPath)
  499. {
  500. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  501. dlg.DefaultExt = ".xlsx"; // Default file extension
  502. dlg.FileName = $"Chamber_Status_Variable_{DateTime.Now:yyyyMMdd_HHmmss}";
  503. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  504. result = dlg.ShowDialog();// Show open file dialog box
  505. savePath = dlg.FileName;
  506. }
  507. if (result == true)
  508. {
  509. System.Data.DataSet ds = new System.Data.DataSet();
  510. ds.Tables.Add(new System.Data.DataTable("SVID(Status Variable)"));
  511. ds.Tables[0].Columns.Add("SVID");
  512. ds.Tables[0].Columns.Add("Name");
  513. ds.Tables[0].Columns.Add("Format");
  514. ds.Tables[0].Columns.Add("Description");
  515. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  516. foreach (var item in lists)
  517. {
  518. var row = ds.Tables[0].NewRow();
  519. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  520. IdDictionary[item.ModuleIndex] = 0;
  521. row[0] = item.Index;
  522. row[1] = item.Name;
  523. row[2] = VIDItemType2GemDataType(item.DataType);
  524. if (string.IsNullOrEmpty(item.Description))
  525. {
  526. var arr = item.Name.Split('.');
  527. for (int i = 0; i < arr.Length; i++)
  528. row[3] += $"{arr[i]} ";
  529. }
  530. else
  531. {
  532. row[3] = item.Description;
  533. }
  534. ds.Tables[0].Rows.Add(row);
  535. }
  536. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  537. {
  538. LOG.Write($"Export failed, {reason}");
  539. return;
  540. }
  541. LOG.Write($"Export succeed, file save as {savePath}");
  542. }
  543. }
  544. private void ExportDvid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  545. {
  546. var lists = dataList.OrderBy(x => x.Index).ToList();
  547. bool? result = defaultPath;
  548. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  549. if (!defaultPath)
  550. {
  551. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  552. dlg.DefaultExt = ".xlsx"; // Default file extension
  553. dlg.FileName = $"Data_Variable_{DateTime.Now:yyyyMMdd_HHmmss}";
  554. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  555. result = dlg.ShowDialog();// Show open file dialog box
  556. savePath = dlg.FileName;
  557. }
  558. if (result == true)
  559. {
  560. System.Data.DataSet ds = new System.Data.DataSet();
  561. ds.Tables.Add(new System.Data.DataTable("DVID(Data Variable)"));
  562. ds.Tables[0].Columns.Add("DVID");
  563. ds.Tables[0].Columns.Add("Name");
  564. ds.Tables[0].Columns.Add("Format");
  565. ds.Tables[0].Columns.Add("Description");
  566. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  567. foreach (var item in lists)
  568. {
  569. var row = ds.Tables[0].NewRow();
  570. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  571. IdDictionary[item.ModuleIndex] = 0;
  572. row[0] = item.Index;
  573. row[1] = item.Name;
  574. row[2] = VIDItemType2GemDataType(item.DataType);
  575. if (string.IsNullOrEmpty(item.Description))
  576. {
  577. var arr = item.Name.Split('.');
  578. for (int i = 0; i < arr.Length; i++)
  579. row[3] += $"{arr[i]} ";
  580. }
  581. else
  582. {
  583. row[3] = item.Description;
  584. }
  585. ds.Tables[0].Rows.Add(row);
  586. }
  587. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  588. {
  589. LOG.Write($"Export failed, {reason}");
  590. return;
  591. }
  592. LOG.Write($"Export succeed, file save as {savePath}");
  593. }
  594. }
  595. private void ReGenerateGemModelXml(List<VIDItem> Svids, List<VIDItem> Ecids, List<VIDItem> Dvids, List<VIDItem> Ceids, List<VIDItem> Alids, string equipName)
  596. {
  597. XmlDocument xml = new XmlDocument();
  598. string _defaultPathFile = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}{equipName}GemModel.xml";
  599. try
  600. {
  601. xml.Load(_defaultPathFile);
  602. if (Svids != null)
  603. {
  604. XmlNode itemNodes = xml.SelectSingleNode("Equipment/StatusVariables");
  605. itemNodes.RemoveAll();
  606. foreach (var svid in OriginalSvids)
  607. {
  608. XmlElement subNode = xml.CreateElement("SVID");
  609. subNode.SetAttribute("id", svid.Index.ToString());
  610. subNode.SetAttribute("valueType", svid.DataType);
  611. subNode.SetAttribute("logicalName", svid.Name);
  612. subNode.SetAttribute("value", "");
  613. subNode.SetAttribute("eventTrigger", "");
  614. subNode.SetAttribute("units", "");
  615. subNode.SetAttribute("description", svid.Description);
  616. subNode.SetAttribute("isArray", "false");
  617. itemNodes.AppendChild(subNode);
  618. }
  619. foreach (var svid in Svids)
  620. {
  621. XmlElement subNode = xml.CreateElement("SVID");
  622. subNode.SetAttribute("id", svid.Index.ToString());
  623. subNode.SetAttribute("valueType", VIDItemType2GemDataType(svid.DataType));
  624. subNode.SetAttribute("logicalName", svid.Name);
  625. subNode.SetAttribute("value", "");
  626. subNode.SetAttribute("eventTrigger", "");
  627. subNode.SetAttribute("units", "");
  628. subNode.SetAttribute("description", svid.Description);
  629. subNode.SetAttribute("isArray", "false");
  630. itemNodes.AppendChild(subNode);
  631. }
  632. }
  633. if (Ecids != null)
  634. {
  635. XmlNode itemNodes = xml.SelectSingleNode("Equipment/EquipmentConstants");
  636. itemNodes.RemoveAll();
  637. foreach (var ecid in OriginalEcids)
  638. {
  639. XmlElement subNode = xml.CreateElement("ECID");
  640. subNode.SetAttribute("id", ecid.Index.ToString());
  641. subNode.SetAttribute("valueType", ecid.DataType);
  642. subNode.SetAttribute("logicalName", ecid.Name);
  643. subNode.SetAttribute("value", ecid.Description);
  644. subNode.SetAttribute("min", "0");
  645. subNode.SetAttribute("max", "100");
  646. subNode.SetAttribute("eventTrigger", "");
  647. subNode.SetAttribute("units", "");
  648. subNode.SetAttribute("description", "");
  649. subNode.SetAttribute("isArray", "false");
  650. itemNodes.AppendChild(subNode);
  651. }
  652. foreach (var ecid in Ecids)
  653. {
  654. XmlElement subNode = xml.CreateElement("ECID");
  655. subNode.SetAttribute("id", ecid.Index.ToString());
  656. subNode.SetAttribute("valueType", VIDItemType2GemDataType(ecid.DataType));
  657. subNode.SetAttribute("logicalName", ecid.Name);
  658. subNode.SetAttribute("value", "");
  659. subNode.SetAttribute("min", "");
  660. subNode.SetAttribute("max", "");
  661. subNode.SetAttribute("eventTrigger", "");
  662. subNode.SetAttribute("units", "");
  663. subNode.SetAttribute("description", ecid.Description);
  664. subNode.SetAttribute("isArray", "false");
  665. itemNodes.AppendChild(subNode);
  666. }
  667. }
  668. if (Dvids != null)
  669. {
  670. XmlNode itemNodes = xml.SelectSingleNode("Equipment/DataVariables");
  671. itemNodes.RemoveAll();
  672. foreach (var dvids in Dvids)
  673. {
  674. if (dvids.Index < 500)
  675. continue;
  676. XmlElement subNode = xml.CreateElement("DVID");
  677. subNode.SetAttribute("id", dvids.Index.ToString());
  678. subNode.SetAttribute("valueType", VIDItemType2GemDataType(dvids.DataType));
  679. subNode.SetAttribute("logicalName", dvids.Name);
  680. subNode.SetAttribute("value", "");
  681. subNode.SetAttribute("eventTrigger", "");
  682. subNode.SetAttribute("description", dvids.Description);
  683. subNode.SetAttribute("isArray", "false");
  684. itemNodes.AppendChild(subNode);
  685. }
  686. }
  687. var moduleAlarmEventsDictionary = new Dictionary<int, string>();
  688. if (Alids != null)
  689. {
  690. XmlNode itemNodes = xml.SelectSingleNode("Equipment/Alarms");
  691. itemNodes.RemoveAll();
  692. foreach (var alid in Alids)
  693. {
  694. XmlElement subNode = xml.CreateElement("ALID");
  695. subNode.SetAttribute("id", alid.Index.ToString());
  696. subNode.SetAttribute("logicalName", alid.Name);
  697. subNode.SetAttribute("description", alid.Description);
  698. subNode.SetAttribute("category", "EquipmentStatusWarning");
  699. subNode.SetAttribute("enabled", "false");
  700. subNode.SetAttribute("eventSet", $"10{alid.ModuleIndex.ToString().PadLeft(3, '0')}01");
  701. subNode.SetAttribute("eventClear", $"20{alid.ModuleIndex.ToString().PadLeft(3, '0')}01");
  702. itemNodes.AppendChild(subNode);
  703. moduleAlarmEventsDictionary[alid.ModuleIndex] = alid.Module;
  704. }
  705. }
  706. if (Ceids != null)
  707. {
  708. int reportID = 0;
  709. Dictionary<string, int> reportDictionary = new Dictionary<string, int>();
  710. XmlNode RPTIDNodes = xml.SelectSingleNode("Equipment/DataCollections/RPTIDs");
  711. RPTIDNodes.RemoveAll();
  712. foreach (var ceid in Ceids)
  713. {
  714. XmlElement RPTID = xml.CreateElement("RPTID");
  715. if (ceid.LinkableVid != null)
  716. {
  717. string reportKey = string.Join(",", ceid.LinkableVid);
  718. if (!reportDictionary.ContainsKey(reportKey))
  719. {
  720. reportID += 1;
  721. reportDictionary[reportKey] = reportID;
  722. RPTID.SetAttribute("id", reportID.ToString());
  723. RPTID.SetAttribute("logicalName", $"DefaultDefinedReport_{reportID}");
  724. foreach (var vid in ceid.LinkableVid)
  725. {
  726. XmlElement ReportVariable = xml.CreateElement("ReportVariable");
  727. ReportVariable.SetAttribute("id", vid.ToString());
  728. ReportVariable.SetAttribute("varType", vid < 500 ? "StatusVariable" : "DataVariable");
  729. ReportVariable.SetAttribute("logicalName", ((DataVariables.DataName)vid).ToString());
  730. RPTID.AppendChild(ReportVariable);
  731. }
  732. RPTIDNodes.AppendChild(RPTID);
  733. }
  734. }
  735. }
  736. XmlNode CEIDNodes = xml.SelectSingleNode("Equipment/DataCollections/CEIDs");
  737. CEIDNodes.RemoveAll();
  738. foreach (var ceid in Ceids)
  739. {
  740. XmlElement CEID = xml.CreateElement("CEID");
  741. CEID.SetAttribute("id", ceid.Index.ToString());
  742. CEID.SetAttribute("logicalName", ceid.Name);
  743. CEID.SetAttribute("description", ceid.Description);
  744. CEID.SetAttribute("enabled", "true");
  745. if (ceid.LinkableVid != null)
  746. {
  747. string reportKey = string.Join(",", ceid.LinkableVid);
  748. XmlElement RPTID = xml.CreateElement("RPTID");
  749. RPTID.SetAttribute("id", reportDictionary[reportKey].ToString());
  750. RPTID.SetAttribute("logicalName", $"DefaultDefinedReport_{reportDictionary[reportKey]}");
  751. foreach (var vid in ceid.LinkableVid)
  752. {
  753. XmlElement ReportVariable = xml.CreateElement("ReportVariable");
  754. ReportVariable.SetAttribute("id", vid.ToString());
  755. ReportVariable.SetAttribute("varType", vid < 500 ? "StatusVariable" : "DataVariable");
  756. ReportVariable.SetAttribute("logicalName", ((DataVariables.DataName)vid).ToString());
  757. RPTID.AppendChild(ReportVariable);
  758. }
  759. CEID.AppendChild(RPTID);
  760. }
  761. CEIDNodes.AppendChild(CEID);
  762. }
  763. foreach (var alarmEvent in moduleAlarmEventsDictionary)
  764. {
  765. XmlElement EventSet = xml.CreateElement("CEID");
  766. EventSet.SetAttribute("id", $"10{alarmEvent.Key.ToString().PadLeft(3, '0')}01");
  767. EventSet.SetAttribute("logicalName", $"__SYSTEM__10{alarmEvent.Key.ToString().PadLeft(3, '0')}01__ALARMSET");
  768. EventSet.SetAttribute("description", $"Alarm Set : {alarmEvent.Value} Error");
  769. EventSet.SetAttribute("enabled", "true");
  770. CEIDNodes.AppendChild(EventSet);
  771. XmlElement EventClear = xml.CreateElement("CEID");
  772. EventClear.SetAttribute("id", $"20{alarmEvent.Key.ToString().PadLeft(3, '0')}01");
  773. EventClear.SetAttribute("logicalName", $"__SYSTEM__20{alarmEvent.Key.ToString().PadLeft(3, '0')}01__ALARMCLEAR");
  774. EventClear.SetAttribute("description", $"Alarm Clear : {alarmEvent.Value} Error");
  775. EventClear.SetAttribute("enabled", "true");
  776. CEIDNodes.AppendChild(EventClear);
  777. }
  778. }
  779. xml.Save(_defaultPathFile);
  780. }
  781. catch (Exception ex)
  782. {
  783. LOG.Write(ex);
  784. }
  785. }
  786. string VIDItemType2GemDataType(string VIDItemType)
  787. {
  788. switch (VIDItemType.Replace("System.", ""))
  789. {
  790. case "List":
  791. case "FabConnect.SecsGemInterface.Common.SECsDataItem":
  792. return "List";
  793. case "I1":
  794. return "I1";
  795. case "I4":
  796. return "I4";
  797. case "U1":
  798. return "U1";
  799. case "U2":
  800. return "U2";
  801. case "U4":
  802. return "U4";
  803. case "Int":
  804. case "Integer":
  805. return "I4";
  806. case "Bool":
  807. case "Boolean":
  808. return "Boolean";
  809. case "Float":
  810. case "Single":
  811. case "Double":
  812. case "F8":
  813. return "F8";
  814. case "Binary":
  815. return "Binary";
  816. default:
  817. return "Ascii";
  818. }
  819. }
  820. }
  821. }