VIDManager2.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. ExportSvid(svid.VIDList, true, true);
  245. //ECID
  246. var ecid = new VIDGenerator2("ECID", $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_ECID.xml");
  247. ecid.Initialize();
  248. ecid.GenerateId(SystemConfigManager.Instance.VidConfigList);
  249. ExportEcid(ecid.VIDList);
  250. //CEID
  251. List<VIDItem> ceids = new List<VIDItem>();
  252. foreach (var eventItem in Singleton<EventManager>.Instance.VidEventList)
  253. {
  254. if (UniversalEvents.UniversalEventsDictionary.ContainsKey(eventItem.Name))
  255. {
  256. ceids.Add(UniversalEvents.UniversalEventsDictionary[eventItem.Name]);
  257. }
  258. else if (enableGem300Events && Gem300Events.Gem300EventsDictionary.ContainsKey(eventItem.Name))
  259. {
  260. ceids.Add(Gem300Events.Gem300EventsDictionary[eventItem.Name]);
  261. }
  262. }
  263. ceids = ceids.OrderBy(x => x.Index).ToList();
  264. CustomXmlSerializer.Serialize(ceids, $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_CEID.xml");
  265. ExportCeid(ceids);
  266. //ALID
  267. var alid = new VIDGenerator2("ALID", $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_ALID.xml");
  268. alid.Initialize();
  269. alid.GenerateId(Singleton<EventManager>.Instance.VidAlarmList);
  270. ExportAlid(alid.VIDList);
  271. //DVID, to be designed
  272. Dictionary<string, VIDItem> dvids = new Dictionary<string, VIDItem>();
  273. foreach (var eventItem in ceids)
  274. {
  275. if (eventItem.LinkableVid == null)
  276. continue;
  277. foreach (var LinkableVid in eventItem.LinkableVid)
  278. {
  279. string dvidName = ((DataVariables.DataName)LinkableVid).ToString();
  280. if (DataVariables.DataVariablesDictionary.ContainsKey(dvidName))
  281. dvids[dvidName] = DataVariables.DataVariablesDictionary[dvidName];
  282. }
  283. }
  284. var dvidList = dvids.Values.ToList().OrderBy(x => x.Index).ToList();
  285. CustomXmlSerializer.Serialize(dvidList, $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\_DVID.xml");
  286. ExportDvid(dvidList);
  287. //ReGenerate GemModel.Xml
  288. if (needReGenerateGemModelXml)
  289. ReGenerateGemModelXml(svid.VIDList, ecid.VIDList, dvidList, ceids, alid.VIDList, equipName);
  290. }
  291. private List<VIDItem> OriginalSvids = new List<VIDItem>()
  292. {
  293. new VIDItem(){Name = "AlarmsEnabled",Index = 1,DataType = "List"},
  294. new VIDItem(){Name = "AlarmsSet",Index = 2,DataType = "List"},
  295. new VIDItem(){Name = "Clock",Index = 3,DataType = "Ascii"},
  296. new VIDItem(){Name = "ControlState",Index = 4,DataType = "U4"},
  297. new VIDItem(){Name = "EventsEnabled",Index = 5,DataType = "List"},
  298. new VIDItem(){Name = "PPExecName",Index = 6,DataType = "Ascii"},
  299. new VIDItem(){Name = "PreviousProcessState",Index = 7,DataType = "U1"},
  300. new VIDItem(){Name = "ProcessState",Index = 8,DataType = "U1"},
  301. new VIDItem(){Name = "SpoolCountActual",Index = 9,DataType = "U4"},
  302. new VIDItem(){Name = "SpoolCountTotal",Index = 10,DataType = "U4"},
  303. new VIDItem(){Name = "SpoolFullTime",Index = 11,DataType = "Ascii"},
  304. new VIDItem(){Name = "SpoolStartTime",Index = 12,DataType = "Ascii"},
  305. new VIDItem(){Name = "SpoolState",Index = 13,DataType = "Ascii"},
  306. new VIDItem(){Name = "SpoolSubstate",Index = 14,DataType = "Ascii"},
  307. };
  308. private List<VIDItem> OriginalEcids = new List<VIDItem>()
  309. {
  310. new VIDItem(){Name = "EstablishCommunicationsTimeout",Index = 2000,DataType = "U2",Description = "2"},
  311. new VIDItem(){Name = "MaxSpoolTransmit",Index = 2001,DataType = "U4",Description = "100"},
  312. new VIDItem(){Name = "OverWriteSpool",Index = 2003,DataType = "Boolean",Description = "FALSE"},
  313. new VIDItem(){Name = "MaxSpoolCapacity",Index = 2005,DataType = "U4",Description = "100"},
  314. new VIDItem(){Name = "SpoolEnabled",Index = 2006,DataType = "Boolean",Description = "False"},
  315. new VIDItem(){Name = "TimeFormat",Index = 2007,DataType = "U1",Description = "0"},
  316. };
  317. private void ExportAlid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  318. {
  319. var lists = dataList.OrderBy(x => x.ModuleIndex).ThenBy(x => x.Name).ToList();
  320. bool? result = defaultPath;
  321. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  322. if (!defaultPath)
  323. {
  324. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  325. dlg.DefaultExt = ".xlsx"; // Default file extension
  326. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  327. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  328. result = dlg.ShowDialog();// Show open file dialog box
  329. savePath = dlg.FileName;
  330. }
  331. if (result == true)
  332. {
  333. System.Data.DataSet ds = new System.Data.DataSet();
  334. ds.Tables.Add(new System.Data.DataTable("ALID(Alarm ID)"));
  335. ds.Tables[0].Columns.Add("ALID");
  336. ds.Tables[0].Columns.Add("Name");
  337. ds.Tables[0].Columns.Add("Description");
  338. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  339. foreach (var item in lists)
  340. {
  341. var row = ds.Tables[0].NewRow();
  342. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  343. IdDictionary[item.ModuleIndex] = 0;
  344. row[0] = item.Index;
  345. row[1] = item.Name;
  346. if (string.IsNullOrEmpty(item.Description))
  347. {
  348. var arr = item.Name.Split('.');
  349. for (int i = 0; i < arr.Length; i++)
  350. row[2] += $"{arr[i]} ";
  351. }
  352. else
  353. {
  354. row[2] = item.Description;
  355. }
  356. ds.Tables[0].Rows.Add(row);
  357. }
  358. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  359. {
  360. LOG.Write($"Export failed, {reason}");
  361. return;
  362. }
  363. LOG.Write($"Export succeed, file save as {savePath}");
  364. }
  365. }
  366. private void ExportCeid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  367. {
  368. var lists = dataList.OrderBy(x => x.Index).ToList();
  369. bool? result = defaultPath;
  370. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  371. if (!defaultPath)
  372. {
  373. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  374. dlg.DefaultExt = ".xlsx"; // Default file extension
  375. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  376. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  377. result = dlg.ShowDialog();// Show open file dialog box
  378. savePath = dlg.FileName;
  379. }
  380. if (result == true)
  381. {
  382. System.Data.DataSet ds = new System.Data.DataSet();
  383. ds.Tables.Add(new System.Data.DataTable("CEID(Collection Events)"));
  384. ds.Tables[0].Columns.Add("CEID");
  385. ds.Tables[0].Columns.Add("Name");
  386. ds.Tables[0].Columns.Add("LinkableVID");
  387. ds.Tables[0].Columns.Add("Description");
  388. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  389. foreach (var item in lists)
  390. {
  391. var row = ds.Tables[0].NewRow();
  392. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  393. IdDictionary[item.ModuleIndex] = 0;
  394. row[0] = item.Index;
  395. row[1] = item.Name;
  396. if (item.LinkableVid != null)
  397. {
  398. string LinkableVidDescription = String.Empty;
  399. for (int i = 0; i < item.LinkableVid.Length; i++)
  400. {
  401. LinkableVidDescription += $"{(DataVariables.DataName)item.LinkableVid[i]} = {item.LinkableVid[i]} \r\n";
  402. }
  403. row[2] = LinkableVidDescription;
  404. }
  405. if (string.IsNullOrEmpty(item.Description))
  406. {
  407. var arr = item.Name.Split('.');
  408. for (int i = 0; i < arr.Length; i++)
  409. row[3] += $"{arr[i]} ";
  410. }
  411. else
  412. {
  413. row[3] = item.Description;
  414. }
  415. ds.Tables[0].Rows.Add(row);
  416. }
  417. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  418. {
  419. LOG.Write($"Export failed, {reason}");
  420. return;
  421. }
  422. LOG.Write($"Export succeed, file save as {savePath}");
  423. }
  424. }
  425. private void ExportEcid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  426. {
  427. var lists = dataList.OrderBy(x => x.Index).ToList();
  428. bool? result = defaultPath;
  429. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  430. if (!defaultPath)
  431. {
  432. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  433. dlg.DefaultExt = ".xlsx"; // Default file extension
  434. dlg.FileName = $"Equipment_VIDs_{DateTime.Now:yyyyMMdd_HHmmss}";
  435. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  436. result = dlg.ShowDialog();// Show open file dialog box
  437. savePath = dlg.FileName;
  438. }
  439. if (result == true)
  440. {
  441. System.Data.DataSet ds = new System.Data.DataSet();
  442. ds.Tables.Add(new System.Data.DataTable("ECID(Equipment Constant)"));
  443. ds.Tables[0].Columns.Add("ECID");
  444. ds.Tables[0].Columns.Add("Name");
  445. ds.Tables[0].Columns.Add("Format");
  446. ds.Tables[0].Columns.Add("Description");
  447. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  448. foreach (var item in lists)
  449. {
  450. var row = ds.Tables[0].NewRow();
  451. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  452. IdDictionary[item.ModuleIndex] = 0;
  453. row[0] = item.Index;
  454. row[1] = item.Name;
  455. row[2] = VIDItemType2GemDataType(item.DataType);
  456. if (string.IsNullOrEmpty(item.Description))
  457. {
  458. var arr = item.Name.Split('.');
  459. for (int i = 0; i < arr.Length; i++)
  460. row[3] += $"{arr[i]} ";
  461. }
  462. else
  463. {
  464. row[3] = item.Description;
  465. }
  466. ds.Tables[0].Rows.Add(row);
  467. }
  468. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  469. {
  470. LOG.Write($"Export failed, {reason}");
  471. return;
  472. }
  473. LOG.Write($"Export succeed, file save as {savePath}");
  474. }
  475. }
  476. private void ExportSvid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  477. {
  478. var lists = dataList.OrderBy(x => x.Index).ToList();
  479. bool? result = defaultPath;
  480. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  481. if (!defaultPath)
  482. {
  483. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  484. dlg.DefaultExt = ".xlsx"; // Default file extension
  485. dlg.FileName = $"Chamber_Status_Variable_{DateTime.Now:yyyyMMdd_HHmmss}";
  486. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  487. result = dlg.ShowDialog();// Show open file dialog box
  488. savePath = dlg.FileName;
  489. }
  490. if (result == true)
  491. {
  492. System.Data.DataSet ds = new System.Data.DataSet();
  493. ds.Tables.Add(new System.Data.DataTable("SVID(Status Variable)"));
  494. ds.Tables[0].Columns.Add("SVID");
  495. ds.Tables[0].Columns.Add("Name");
  496. ds.Tables[0].Columns.Add("Format");
  497. ds.Tables[0].Columns.Add("Description");
  498. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  499. foreach (var item in lists)
  500. {
  501. var row = ds.Tables[0].NewRow();
  502. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  503. IdDictionary[item.ModuleIndex] = 0;
  504. row[0] = item.Index;
  505. row[1] = item.Name;
  506. row[2] = VIDItemType2GemDataType(item.DataType);
  507. if (string.IsNullOrEmpty(item.Description))
  508. {
  509. var arr = item.Name.Split('.');
  510. for (int i = 0; i < arr.Length; i++)
  511. row[3] += $"{arr[i]} ";
  512. }
  513. else
  514. {
  515. row[3] = item.Description;
  516. }
  517. ds.Tables[0].Rows.Add(row);
  518. }
  519. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  520. {
  521. LOG.Write($"Export failed, {reason}");
  522. return;
  523. }
  524. LOG.Write($"Export succeed, file save as {savePath}");
  525. }
  526. }
  527. private void ExportDvid(List<VIDItem> dataList, bool defaultPath = true, bool createNewFile = false)
  528. {
  529. var lists = dataList.OrderBy(x => x.Index).ToList();
  530. bool? result = defaultPath;
  531. string savePath = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}VIDs\\Equipment_VIDs_{DateTime.Now:yyyyMMdd}.xlsx";
  532. if (!defaultPath)
  533. {
  534. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  535. dlg.DefaultExt = ".xlsx"; // Default file extension
  536. dlg.FileName = $"Data_Variable_{DateTime.Now:yyyyMMdd_HHmmss}";
  537. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  538. result = dlg.ShowDialog();// Show open file dialog box
  539. savePath = dlg.FileName;
  540. }
  541. if (result == true)
  542. {
  543. System.Data.DataSet ds = new System.Data.DataSet();
  544. ds.Tables.Add(new System.Data.DataTable("DVID(Data Variable)"));
  545. ds.Tables[0].Columns.Add("DVID");
  546. ds.Tables[0].Columns.Add("Name");
  547. ds.Tables[0].Columns.Add("Format");
  548. ds.Tables[0].Columns.Add("Description");
  549. Dictionary<int, int> IdDictionary = new Dictionary<int, int>();
  550. foreach (var item in lists)
  551. {
  552. var row = ds.Tables[0].NewRow();
  553. if (!IdDictionary.ContainsKey(item.ModuleIndex))
  554. IdDictionary[item.ModuleIndex] = 0;
  555. row[0] = item.Index;
  556. row[1] = item.Name;
  557. row[2] = VIDItemType2GemDataType(item.DataType);
  558. if (string.IsNullOrEmpty(item.Description))
  559. {
  560. var arr = item.Name.Split('.');
  561. for (int i = 0; i < arr.Length; i++)
  562. row[3] += $"{arr[i]} ";
  563. }
  564. else
  565. {
  566. row[3] = item.Description;
  567. }
  568. ds.Tables[0].Rows.Add(row);
  569. }
  570. if (!ExcelHelper.ExportToExcel(savePath, ds, out string reason, createNewFile))
  571. {
  572. LOG.Write($"Export failed, {reason}");
  573. return;
  574. }
  575. LOG.Write($"Export succeed, file save as {savePath}");
  576. }
  577. }
  578. private void ReGenerateGemModelXml(List<VIDItem> Svids, List<VIDItem> Ecids, List<VIDItem> Dvids, List<VIDItem> Ceids, List<VIDItem> Alids, string equipName)
  579. {
  580. XmlDocument xml = new XmlDocument();
  581. string _defaultPathFile = $"{PathManager.GetCfgDir().Replace("\\bin\\Debug", "")}{equipName}GemModel.xml";
  582. try
  583. {
  584. xml.Load(_defaultPathFile);
  585. if (Svids != null)
  586. {
  587. XmlNode itemNodes = xml.SelectSingleNode("Equipment/StatusVariables");
  588. itemNodes.RemoveAll();
  589. foreach (var svid in OriginalSvids)
  590. {
  591. XmlElement subNode = xml.CreateElement("SVID");
  592. subNode.SetAttribute("id", svid.Index.ToString());
  593. subNode.SetAttribute("valueType", svid.DataType);
  594. subNode.SetAttribute("logicalName", svid.Name);
  595. subNode.SetAttribute("value", "");
  596. subNode.SetAttribute("eventTrigger", "");
  597. subNode.SetAttribute("units", "");
  598. subNode.SetAttribute("description", svid.Description);
  599. subNode.SetAttribute("isArray", "false");
  600. itemNodes.AppendChild(subNode);
  601. }
  602. foreach (var svid in Svids)
  603. {
  604. XmlElement subNode = xml.CreateElement("SVID");
  605. subNode.SetAttribute("id", svid.Index.ToString());
  606. subNode.SetAttribute("valueType", VIDItemType2GemDataType(svid.DataType));
  607. subNode.SetAttribute("logicalName", svid.Name);
  608. subNode.SetAttribute("value", "");
  609. subNode.SetAttribute("eventTrigger", "");
  610. subNode.SetAttribute("units", "");
  611. subNode.SetAttribute("description", svid.Description);
  612. subNode.SetAttribute("isArray", "false");
  613. itemNodes.AppendChild(subNode);
  614. }
  615. }
  616. if (Ecids != null)
  617. {
  618. XmlNode itemNodes = xml.SelectSingleNode("Equipment/EquipmentConstants");
  619. itemNodes.RemoveAll();
  620. foreach (var ecid in OriginalEcids)
  621. {
  622. XmlElement subNode = xml.CreateElement("ECID");
  623. subNode.SetAttribute("id", ecid.Index.ToString());
  624. subNode.SetAttribute("valueType", ecid.DataType);
  625. subNode.SetAttribute("logicalName", ecid.Name);
  626. subNode.SetAttribute("value", ecid.Description);
  627. subNode.SetAttribute("min", "0");
  628. subNode.SetAttribute("max", "100");
  629. subNode.SetAttribute("eventTrigger", "");
  630. subNode.SetAttribute("units", "");
  631. subNode.SetAttribute("description", "");
  632. subNode.SetAttribute("isArray", "false");
  633. itemNodes.AppendChild(subNode);
  634. }
  635. foreach (var ecid in Ecids)
  636. {
  637. XmlElement subNode = xml.CreateElement("ECID");
  638. subNode.SetAttribute("id", ecid.Index.ToString());
  639. subNode.SetAttribute("valueType", VIDItemType2GemDataType(ecid.DataType));
  640. subNode.SetAttribute("logicalName", ecid.Name);
  641. subNode.SetAttribute("value", "");
  642. subNode.SetAttribute("min", "");
  643. subNode.SetAttribute("max", "");
  644. subNode.SetAttribute("eventTrigger", "");
  645. subNode.SetAttribute("units", "");
  646. subNode.SetAttribute("description", ecid.Description);
  647. subNode.SetAttribute("isArray", "false");
  648. itemNodes.AppendChild(subNode);
  649. }
  650. }
  651. if (Dvids != null)
  652. {
  653. XmlNode itemNodes = xml.SelectSingleNode("Equipment/DataVariables");
  654. itemNodes.RemoveAll();
  655. foreach (var dvids in Dvids)
  656. {
  657. if (dvids.Index < 500)
  658. continue;
  659. XmlElement subNode = xml.CreateElement("DVID");
  660. subNode.SetAttribute("id", dvids.Index.ToString());
  661. subNode.SetAttribute("valueType", VIDItemType2GemDataType(dvids.DataType));
  662. subNode.SetAttribute("logicalName", dvids.Name);
  663. subNode.SetAttribute("value", "");
  664. subNode.SetAttribute("eventTrigger", "");
  665. subNode.SetAttribute("description", dvids.Description);
  666. subNode.SetAttribute("isArray", "false");
  667. itemNodes.AppendChild(subNode);
  668. }
  669. }
  670. if (Ceids != null)
  671. {
  672. int reportID = 0;
  673. Dictionary<string, int> reportDictionary = new Dictionary<string, int>();
  674. XmlNode RPTIDNodes = xml.SelectSingleNode("Equipment/DataCollections/RPTIDs");
  675. RPTIDNodes.RemoveAll();
  676. foreach (var ceid in Ceids)
  677. {
  678. XmlElement RPTID = xml.CreateElement("RPTID");
  679. if (ceid.LinkableVid != null)
  680. {
  681. string reportKey = string.Join(",", ceid.LinkableVid);
  682. if (!reportDictionary.ContainsKey(reportKey))
  683. {
  684. reportID += 1;
  685. reportDictionary[reportKey] = reportID;
  686. RPTID.SetAttribute("id", reportID.ToString());
  687. RPTID.SetAttribute("logicalName", $"DefaultDefinedReport_{reportID}");
  688. foreach (var vid in ceid.LinkableVid)
  689. {
  690. XmlElement ReportVariable = xml.CreateElement("ReportVariable");
  691. ReportVariable.SetAttribute("id", vid.ToString());
  692. ReportVariable.SetAttribute("varType", vid < 500 ? "StatusVariable" : "DataVariable");
  693. ReportVariable.SetAttribute("logicalName", ((DataVariables.DataName)vid).ToString());
  694. RPTID.AppendChild(ReportVariable);
  695. }
  696. RPTIDNodes.AppendChild(RPTID);
  697. }
  698. }
  699. }
  700. XmlNode CEIDNodes = xml.SelectSingleNode("Equipment/DataCollections/CEIDs");
  701. CEIDNodes.RemoveAll();
  702. foreach (var ceid in Ceids)
  703. {
  704. XmlElement CEID = xml.CreateElement("CEID");
  705. CEID.SetAttribute("id", ceid.Index.ToString());
  706. CEID.SetAttribute("logicalName", ceid.Name);
  707. CEID.SetAttribute("description", ceid.Description);
  708. CEID.SetAttribute("enabled", "true");
  709. if (ceid.LinkableVid != null)
  710. {
  711. string reportKey = string.Join(",", ceid.LinkableVid);
  712. XmlElement RPTID = xml.CreateElement("RPTID");
  713. RPTID.SetAttribute("id", reportDictionary[reportKey].ToString());
  714. RPTID.SetAttribute("logicalName", $"DefaultDefinedReport_{reportDictionary[reportKey]}");
  715. foreach (var vid in ceid.LinkableVid)
  716. {
  717. XmlElement ReportVariable = xml.CreateElement("ReportVariable");
  718. ReportVariable.SetAttribute("id", vid.ToString());
  719. ReportVariable.SetAttribute("varType", vid < 500 ? "StatusVariable" : "DataVariable");
  720. ReportVariable.SetAttribute("logicalName", ((DataVariables.DataName)vid).ToString());
  721. RPTID.AppendChild(ReportVariable);
  722. }
  723. CEID.AppendChild(RPTID);
  724. }
  725. CEIDNodes.AppendChild(CEID);
  726. }
  727. }
  728. if (Alids != null)
  729. {
  730. XmlNode itemNodes = xml.SelectSingleNode("Equipment/Alarms");
  731. itemNodes.RemoveAll();
  732. foreach (var alid in Alids)
  733. {
  734. XmlElement subNode = xml.CreateElement("ALID");
  735. subNode.SetAttribute("id", alid.Index.ToString());
  736. subNode.SetAttribute("logicalName", alid.Name);
  737. subNode.SetAttribute("description", alid.Description);
  738. subNode.SetAttribute("category", "EquipmentStatusWarning");
  739. subNode.SetAttribute("enabled", "false");
  740. subNode.SetAttribute("eventSet", "1" + alid.Index);
  741. subNode.SetAttribute("eventClear", "2" + alid.Index);
  742. itemNodes.AppendChild(subNode);
  743. }
  744. }
  745. xml.Save(_defaultPathFile);
  746. }
  747. catch (Exception ex)
  748. {
  749. LOG.Write(ex);
  750. }
  751. }
  752. string VIDItemType2GemDataType(string VIDItemType)
  753. {
  754. switch (VIDItemType.Replace("System.", ""))
  755. {
  756. case "List":
  757. return "List";
  758. case "I1":
  759. return "I1";
  760. case "I4":
  761. return "I4";
  762. case "U1":
  763. return "U1";
  764. case "U2":
  765. return "U2";
  766. case "U4":
  767. return "U4";
  768. case "Int":
  769. case "Integer":
  770. return "I4";
  771. case "Bool":
  772. case "Boolean":
  773. return "Boolean";
  774. case "Float":
  775. case "Single":
  776. case "Double":
  777. case "F8":
  778. return "F8";
  779. case "Binary":
  780. return "Binary";
  781. default:
  782. return "Ascii";
  783. }
  784. }
  785. }
  786. }