VIDManager2.cs 43 KB

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