RecipeFileManager.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.Util;
  5. using Aitex.Core.Utilities;
  6. using Aitex.Core.WCF;
  7. using MECF.Framework.Common.Equipment;
  8. using MECF.Framework.Common.Properties;
  9. using MECF.Framework.Common.RecipeCenter;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Xml;
  16. using System.Xml.Schema;
  17. using Venus_Core;
  18. namespace Aitex.Core.RT.RecipeCenter
  19. {
  20. public class RecipeFileManager : Singleton<RecipeFileManager>
  21. {
  22. //sequence文件 统一放在 Recipes/Sequence 文件夹下面
  23. public const string SequenceFolder = "Sequence";
  24. public const string SourceModule = "Recipe";
  25. private bool _recipeIsValid;
  26. private List<string> _validationErrors = new List<string>();
  27. private List<string> _validationWarnings = new List<string>();
  28. IRecipeFileContext _rcpContext;
  29. private ISequenceFileContext _seqContext;
  30. public void Initialize(IRecipeFileContext context)
  31. {
  32. Initialize(context, null, true);
  33. }
  34. public void Initialize(IRecipeFileContext context, bool enableService)
  35. {
  36. Initialize(context, null, enableService);
  37. }
  38. public void Initialize(IRecipeFileContext rcpContext, ISequenceFileContext seqContext, bool enableService)
  39. {
  40. _rcpContext = rcpContext == null ? new DefaultRecipeFileContext() : rcpContext;
  41. _seqContext = seqContext == null ? new DefaultSequenceFileContext() : seqContext;
  42. CultureSupported.UpdateCoreCultureResource(CultureSupported.English);
  43. //if (enableService)
  44. //{
  45. // Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  46. // {
  47. // typeof(RecipeService)
  48. // });
  49. //}
  50. var dir = string.Format("{0}{1}\\", PathManager.GetRecipeDir(), SequenceFolder);
  51. DirectoryInfo di = new DirectoryInfo(dir);
  52. if (!di.Exists)
  53. {
  54. di.Create();
  55. }
  56. }
  57. private void ValidationEventHandler(object sender, ValidationEventArgs e)
  58. {
  59. switch (e.Severity)
  60. {
  61. case XmlSeverityType.Error:
  62. _validationErrors.Add(e.Message);
  63. _recipeIsValid = false;
  64. break;
  65. case XmlSeverityType.Warning:
  66. _validationWarnings.Add(e.Message);
  67. break;
  68. }
  69. }
  70. /// <summary>
  71. /// XML schema checking
  72. /// </summary>
  73. /// <param name="chamId"></param>
  74. /// <param name="recipeName"></param>
  75. /// <param name="recipeContent"></param>
  76. /// <param name="reason"></param>
  77. /// <returns></returns>
  78. public bool ValidateRecipe(string chamberId, string recipeName, string recipeContent, out List<string> reason)
  79. {
  80. try
  81. {
  82. XmlDocument document = new XmlDocument();
  83. document.LoadXml(recipeContent);
  84. MemoryStream schemaStream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(GetRecipeSchema(chamberId)));
  85. XmlReader xmlSchemaReader = XmlReader.Create(schemaStream);
  86. XmlSchema schema1 = XmlSchema.Read(xmlSchemaReader, ValidationEventHandler);
  87. document.Schemas.Add(schema1);
  88. document.LoadXml(recipeContent);
  89. ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
  90. _recipeIsValid = true;
  91. _validationErrors = new List<string>();
  92. _validationWarnings = new List<string>();
  93. // Validates recipe.
  94. document.Validate(eventHandler);
  95. }
  96. catch (Exception ex)
  97. {
  98. LOG.WriteExeption(ex);
  99. _recipeIsValid = false;
  100. }
  101. if (!_recipeIsValid && _validationErrors.Count == 0)
  102. {
  103. _validationErrors.Add(Resources.RecipeFileManager_ValidateRecipe_XMLSchemaValidateFailed);
  104. }
  105. reason = _validationErrors;
  106. return _recipeIsValid;
  107. }
  108. /// <summary>
  109. /// Check recipe content
  110. /// </summary>
  111. /// <param name="chamId"></param>
  112. /// <param name="recipeContent"></param>
  113. /// <param name="reasons"></param>
  114. /// <returns></returns>
  115. public bool CheckRecipe(string chamberId, string recipeContent, out List<string> reasons)
  116. {
  117. reasons = new List<string>();
  118. //add recipe content validation here
  119. try
  120. {
  121. var xmlFormat = new XmlDocument();
  122. xmlFormat.LoadXml(GetRecipeFormatXml(chamberId));
  123. var mfcDic = new Dictionary<string, double>(); //name + scale
  124. var pcDic = new Dictionary<string, double>(); //name + scale
  125. foreach (XmlElement mfc in xmlFormat.SelectNodes("/TableRecipeFormat/Catalog/Group/Step[@DeviceType='MFC']"))
  126. {
  127. mfcDic.Add(mfc.Attributes["ControlName"].Value, Convert.ToDouble(mfc.Attributes["Max"].Value));
  128. }
  129. foreach (XmlElement pc in xmlFormat.SelectNodes("/TableRecipeFormat/Catalog/Group/Step[@DeviceType='PC']"))
  130. {
  131. pcDic.Add(pc.Attributes["ControlName"].Value, Convert.ToDouble(pc.Attributes["Max"].Value));
  132. }
  133. //var spindleMaxSpeed = Convert.ToDouble(xmlFormat.SelectSingleNode("/TableRecipeFormat/Catalog/Group/Step[@ControlName='Spindle.Speed']").Attributes["Max"].Value);
  134. var xmlRecipe = new XmlDocument();
  135. xmlRecipe.LoadXml(recipeContent);
  136. //read in all recipe items
  137. Dictionary<int, Dictionary<string, string>> recipeItems = new Dictionary<int, Dictionary<string, string>>();
  138. var stepElements = xmlRecipe.SelectNodes("/TableRecipeData/Step");
  139. for (int stepNo = 0; stepNo < stepElements.Count; stepNo++)
  140. {
  141. var stepElement = stepElements[stepNo] as XmlElement;
  142. var step = new Dictionary<string, string>();
  143. recipeItems.Add(stepNo, step);
  144. foreach (XmlAttribute att1 in stepElement.Attributes)
  145. {
  146. step.Add(att1.Name, att1.Value);
  147. }
  148. foreach (XmlElement subNd1 in stepElement.ChildNodes)
  149. {
  150. foreach (XmlAttribute att2 in subNd1.Attributes)
  151. {
  152. step.Add(att2.Name, att2.Value);
  153. }
  154. foreach (XmlElement subNd2 in subNd1.ChildNodes)
  155. {
  156. foreach (XmlAttribute att3 in subNd2.Attributes)
  157. {
  158. step.Add(att3.Name, att3.Value);
  159. }
  160. foreach (XmlElement subNd3 in subNd2.ChildNodes)
  161. {
  162. foreach (XmlAttribute att4 in subNd3.Attributes)
  163. {
  164. step.Add(att4.Name, att4.Value);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. #region check loop control
  171. for (int j = 0; j < recipeItems.Count; j++)
  172. {
  173. var loopStr = recipeItems[j]["Loop"];
  174. bool isLoopStart = Regex.IsMatch(loopStr, @"^Loop\x20\d+$");
  175. bool isLoopEnd = Regex.IsMatch(loopStr, @"^Loop End$");
  176. bool isNullOrEmpty = string.IsNullOrWhiteSpace(loopStr);
  177. if (!isLoopEnd && !isLoopStart && !isNullOrEmpty)
  178. {
  179. string reason = string.Format("Value '{0}' not valid", loopStr);
  180. reasons.Add(string.Format("第{0}步,{1}。", j + 1, reason));
  181. }
  182. if (isLoopEnd)
  183. {
  184. string reason = "Loop Start 缺失";
  185. reasons.Add(string.Format("第{0}步,{1}。", j + 1, reason));
  186. }
  187. else if (isLoopStart)
  188. {
  189. for (int k = j + 1; k < recipeItems.Count; k++)
  190. {
  191. var loopStr2 = recipeItems[k]["Loop"];
  192. bool isCurStepLoopStart = Regex.IsMatch(loopStr2, @"^Loop\x20\d+$");
  193. bool isCurStepLoopEnd = Regex.IsMatch(loopStr2, @"^Loop End$");
  194. isNullOrEmpty = string.IsNullOrWhiteSpace(loopStr2);
  195. if (!isCurStepLoopEnd && !isCurStepLoopStart && !isNullOrEmpty)
  196. {
  197. string reason = string.Format("Value '{0}' not valid", loopStr2);
  198. reasons.Add(string.Format("第{0}步,{1}。", k + 1, reason));
  199. }
  200. else if (isCurStepLoopStart)
  201. {
  202. string reason = "前面循环没有结束,不能设置新的Loop Start标志";
  203. reasons.Add(string.Format("第{0}步,{1}。", k + 1, reason));
  204. }
  205. else if (isCurStepLoopEnd)
  206. {
  207. j = k;
  208. break;
  209. }
  210. if (k == recipeItems.Count - 1)
  211. {
  212. j = k;
  213. string reason = "Loop End 缺失";
  214. reasons.Add(string.Format("第{0}步,{1}。", k + 1, reason));
  215. }
  216. }
  217. }
  218. }
  219. #endregion
  220. //check mfc range
  221. for (int stepNo = 0; stepNo < recipeItems.Count; stepNo++)
  222. {
  223. foreach (var mfcName in mfcDic.Keys)
  224. {
  225. if (recipeItems[stepNo].ContainsKey(mfcName))
  226. {
  227. var mfcSetpoint = Convert.ToDouble(recipeItems[stepNo][mfcName]);
  228. if (mfcSetpoint < 0 || mfcSetpoint > mfcDic[mfcName])
  229. {
  230. reasons.Add(string.Format("第{0}步,{1}设定{2},超出 0~{3}sccm范围。", stepNo + 1, mfcName, mfcSetpoint, mfcDic[mfcName]));
  231. }
  232. }
  233. }
  234. //check pc range
  235. foreach (var pcName in pcDic.Keys)
  236. {
  237. if (recipeItems[stepNo].ContainsKey(pcName))
  238. {
  239. var pcSetpoint = Convert.ToDouble(recipeItems[stepNo][pcName]);
  240. if (pcSetpoint < 0 || pcSetpoint > pcDic[pcName])
  241. {
  242. reasons.Add(string.Format("第{0}步,{1}设定{2},超出 0~{3}mbar范围。", stepNo + 1, pcName, pcSetpoint, pcDic[pcName]));
  243. }
  244. }
  245. }
  246. }
  247. #region recipe parameter validation
  248. //reading predefined varaible
  249. var recipePredfine = new Dictionary<string, string>();
  250. foreach (XmlElement nd in xmlFormat.SelectNodes("/TableRecipeFormat/Validation/Predefine/Item"))
  251. {
  252. recipePredfine.Add(nd.Attributes["VarName"].Value, nd.Attributes["Value"].Value);
  253. }
  254. #endregion
  255. }
  256. catch (Exception ex)
  257. {
  258. reasons.Add(Resources.RecipeFileManager_CheckRecipe_RecipeValidationFailed + ex.Message);
  259. LOG.WriteExeption(ex);
  260. return false;
  261. }
  262. return reasons.Count == 0;
  263. }
  264. /// <summary>
  265. /// This method will be invoked by two places:
  266. /// (1) Load a recipe from server to GUI for editing (do not need validation when loading, do validation when saving);
  267. /// (2) Load a recipe from recipe engine to run process(always do a validation before run recipe);
  268. /// </summary>
  269. /// <param name="recipeName"></param>
  270. /// <param name="needValidation">indicate whether a recipe format validation is needed or not</param>
  271. /// <returns></returns>
  272. public string LoadRecipe(string chamberId, string recipeName, bool needValidation)
  273. {
  274. string rcp = string.Empty;
  275. try
  276. {
  277. using (StreamReader fs = new StreamReader(GenerateRecipeFilePath(chamberId, recipeName)))
  278. {
  279. rcp = fs.ReadToEnd();
  280. fs.Close();
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
  286. rcp = string.Empty;
  287. }
  288. return rcp;
  289. }
  290. public string LoadEAPRecipe(string chamberId, string recipeName, bool needValidation)
  291. {
  292. string rcp = string.Empty;
  293. try
  294. {
  295. using (StreamReader fs = new StreamReader(GenerateEAPRecipeFilePath(chamberId, recipeName)))
  296. {
  297. rcp = fs.ReadToEnd();
  298. fs.Close();
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
  304. rcp = string.Empty;
  305. }
  306. return rcp;
  307. }
  308. public string LoadRecipeByPath(string path)
  309. {
  310. string rcp = string.Empty;
  311. try
  312. {
  313. using (StreamReader fs = new StreamReader(path))
  314. {
  315. rcp = fs.ReadToEnd();
  316. fs.Close();
  317. }
  318. }
  319. catch
  320. {
  321. // LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
  322. rcp = string.Empty;
  323. }
  324. return rcp;
  325. }
  326. /// <summary>
  327. /// This method will be invoked by two places:
  328. /// (1) Load a recipe from server to GUI for editing (do not need validation when loading, do validation when saving);
  329. /// (2) Load a recipe from recipe engine to run process(always do a validation before run recipe);
  330. /// </summary>
  331. /// <param name="recipeName"></param>
  332. /// <param name="needValidation">indicate whether a recipe format validation is needed or not</param>
  333. /// <returns></returns>
  334. public string LoadRecipe(string chamberId, string recipeName, bool needValidation,string type)
  335. {
  336. string rcp = string.Empty;
  337. try
  338. {
  339. using (StreamReader fs = new StreamReader(GenerateRecipeFilePath(chamberId,type, recipeName)))
  340. {
  341. rcp = fs.ReadToEnd();
  342. fs.Close();
  343. }
  344. }
  345. catch (Exception ex)
  346. {
  347. LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
  348. rcp = string.Empty;
  349. }
  350. return rcp;
  351. }
  352. /// <summary>
  353. /// Get recipe list
  354. /// </summary>
  355. /// <param name="chamId"></param>
  356. /// <param name="includingUsedRecipe"></param>
  357. /// <returns></returns>
  358. public IEnumerable<string> GetRecipes(string chamberId, bool includingUsedRecipe)
  359. {
  360. return _rcpContext.GetRecipes(chamberId, includingUsedRecipe);
  361. }
  362. /// <summary>
  363. /// Get recipe list in xml format
  364. /// </summary>
  365. /// <param name="chamId"></param>
  366. /// <param name="includingUsedRecipe"></param>
  367. /// <returns></returns>
  368. public string GetXmlRecipeList(string chamberId, bool includingUsedRecipe)
  369. {
  370. XmlDocument doc = new XmlDocument();
  371. var baseFolderPath = getRecipeDirPath(chamberId);
  372. DirectoryInfo curFolderInfo = new DirectoryInfo(baseFolderPath);
  373. doc.AppendChild(GenerateRecipeList(chamberId, curFolderInfo, doc, includingUsedRecipe));
  374. return doc.OuterXml;
  375. }
  376. public void SaveRecipeHistory(string chamberId, string recipeName, string recipeContent, bool needSaveAs = true)
  377. {
  378. try
  379. {
  380. if (!string.IsNullOrEmpty(recipeName) && needSaveAs)
  381. {
  382. string newRecipeName = string.Format("HistoryRecipe\\{0}\\{1}", DateTime.Now.ToString("yyyyMM"), recipeName);
  383. SaveRecipe(chamberId, newRecipeName, recipeContent, true, false);
  384. //LOG.Write(string.Format("{0}通知TM保存工艺程序{1}", chamberId, recipeName));
  385. }
  386. }
  387. catch (Exception ex)
  388. {
  389. LOG.WriteExeption(string.Format("保存{0}工艺程序{1}发生错误", chamberId, recipeName), ex);
  390. }
  391. }
  392. /// <summary>
  393. /// generate recipe list information in current directory
  394. /// </summary>
  395. /// <param name="chamId"></param>
  396. /// <param name="currentDir"></param>
  397. /// <param name="doc"></param>
  398. /// <returns></returns>
  399. XmlElement GenerateRecipeList(string chamberId, DirectoryInfo currentDir, XmlDocument doc, bool includingUsedRecipe)
  400. {
  401. int trimLength = getRecipeDirPath(chamberId).Length;
  402. XmlElement folderEle = doc.CreateElement("Folder");
  403. folderEle.SetAttribute("Name", currentDir.FullName.Substring(trimLength));
  404. DirectoryInfo[] dirInfos = currentDir.GetDirectories();
  405. foreach (DirectoryInfo dirInfo in dirInfos)
  406. {
  407. if (!includingUsedRecipe && dirInfo.Name == "HistoryRecipe")
  408. continue;
  409. folderEle.AppendChild(GenerateRecipeList(chamberId, dirInfo, doc, includingUsedRecipe));
  410. }
  411. FileInfo[] fileInfos = currentDir.GetFiles("*.rcp");
  412. foreach (FileInfo fileInfo in fileInfos)
  413. {
  414. XmlElement fileNd = doc.CreateElement("File");
  415. string fileStr = fileInfo.FullName.Substring(trimLength).TrimStart(new char[] { '\\' }); ;
  416. fileStr = fileStr.Substring(0, fileStr.LastIndexOf("."));
  417. fileNd.SetAttribute("Name", fileStr);
  418. folderEle.AppendChild(fileNd);
  419. }
  420. return folderEle;
  421. }
  422. /// <summary>
  423. /// Delete a recipe by recipe name
  424. /// </summary>
  425. /// <param name="chamId"></param>
  426. /// <param name="recipeName"></param>
  427. /// <returns></returns>
  428. public bool DeleteRecipe(string chamberId, string recipeName)
  429. {
  430. try
  431. {
  432. var path = GenerateRecipeFilePath(chamberId, recipeName);
  433. if (!_rcpContext.EnableEdit(path))
  434. return false;
  435. File.Delete(path);
  436. InfoDialog(string.Format(Resources.RecipeFileManager_DeleteRecipe_RecipeFile0DeleteSucceeded, recipeName));
  437. }
  438. catch (Exception ex)
  439. {
  440. LOG.WriteExeption("删除recipe file 出错", ex);
  441. WarningDialog(string.Format(Resources.RecipeFileManager_DeleteRecipe_RecipeFile0DeleteFailed, recipeName));
  442. return false;
  443. }
  444. return true;
  445. }
  446. /// <summary>
  447. /// Rename recipe
  448. /// </summary>
  449. /// <param name="chamId"></param>
  450. /// <param name="oldName"></param>
  451. /// <param name="newName"></param>
  452. /// <returns></returns>
  453. public bool RenameRecipe(string chamId, string oldName, string newName)
  454. {
  455. try
  456. {
  457. var path = GenerateRecipeFilePath(chamId, newName);
  458. if (!_rcpContext.EnableEdit(path))
  459. return false;
  460. if (File.Exists(path))
  461. {
  462. WarningDialog(string.Format(Resources.RecipeFileManager_RenameRecipe_RecipeFile0FileExisted, oldName));
  463. return false;
  464. }
  465. else
  466. {
  467. File.Move(GenerateRecipeFilePath(chamId, oldName), GenerateRecipeFilePath(chamId, newName));
  468. InfoDialog(string.Format(Resources.RecipeFileManager_RenameRecipe_RecipeFile0Renamed, oldName, newName));
  469. }
  470. }
  471. catch (Exception ex)
  472. {
  473. LOG.WriteExeption("重命名recipe file 出错", ex);
  474. WarningDialog(string.Format(Resources.RecipeFileManager_RenameRecipe_RecipeFile0RenameFailed, oldName, newName));
  475. return false;
  476. }
  477. return true;
  478. }
  479. //private void EventInfo(string message)
  480. //{
  481. // _rcpContext.PostInfoEvent(message);
  482. //}
  483. //private void EventWarning(string message)
  484. //{
  485. // _rcpContext.PostWarningEvent(message);
  486. //}
  487. //private void EventAlarm(string message)
  488. //{
  489. // _rcpContext.PostAlarmEvent(message);
  490. //}
  491. private void InfoDialog(string message)
  492. {
  493. _rcpContext.PostInfoDialogMessage(message);
  494. }
  495. private void WarningDialog(string message)
  496. {
  497. _rcpContext.PostWarningDialogMessage(message);
  498. }
  499. //private void AlarmDialog(string message)
  500. //{
  501. // _rcpContext.PostAlarmDialogMessage(message);
  502. //}
  503. private void EventDialog(string message, List<string> reason)
  504. {
  505. string msg = message;
  506. foreach (var r in reason)
  507. {
  508. msg += "\r\n" + r;
  509. }
  510. _rcpContext.PostDialogEvent(msg);
  511. }
  512. private string GenerateEAPRecipeFilePath(string chamId, string recipeName)
  513. {
  514. return getEAPRecipeDirPath(chamId) + recipeName + ".rcp";
  515. }
  516. /// <summary>
  517. /// get recipe's file path
  518. /// </summary>
  519. /// <param name="recipeName"></param>
  520. /// <returns></returns>
  521. private string GenerateRecipeFilePath(string chamId, string recipeName)
  522. {
  523. return getRecipeDirPath(chamId) + recipeName + ".rcp";
  524. }
  525. public string GenerateRecipeFilePath(string chamId, string type,string recipeName)
  526. {
  527. return getRecipeDirPath(chamId) +type+"\\"+ recipeName + ".rcp";
  528. }
  529. private string GenerateSequenceFilePath(string chamId, string recipeName)
  530. {
  531. return getRecipeDirPath(chamId) + recipeName + ".seq";
  532. }
  533. /// <summary>
  534. /// get recipe's dir path
  535. /// </summary>
  536. /// <param name="recipeName"></param>
  537. /// <returns></returns>
  538. private string getRecipeDirPath(string chamId)
  539. {
  540. var dir = string.Format("{0}{1}\\", PathManager.GetRecipeDir(), chamId);
  541. DirectoryInfo di = new DirectoryInfo(dir);
  542. if (!di.Exists) di.Create();
  543. return dir;
  544. }
  545. private string getEAPRecipeDirPath(string chamId)
  546. {
  547. var dir = string.Format("{0}{1}\\Process_EAP\\", PathManager.GetRecipeDir(), chamId);
  548. DirectoryInfo di = new DirectoryInfo(dir);
  549. if (!di.Exists) di.Create();
  550. return dir;
  551. }
  552. /// <summary>
  553. /// delete a recipe folder
  554. /// </summary>
  555. /// <param name="chamId"></param>
  556. /// <param name="folderName"></param>
  557. /// <returns></returns>
  558. public bool DeleteFolder(string chamId, string folderName)
  559. {
  560. try
  561. {
  562. Directory.Delete(getRecipeDirPath(chamId) + folderName, true);
  563. InfoDialog(string.Format(Resources.RecipeFileManager_DeleteFolder_RecipeFolder0DeleteSucceeded, folderName));
  564. }
  565. catch (Exception ex)
  566. {
  567. LOG.WriteExeption("删除recipe folder 出错", ex);
  568. WarningDialog(string.Format("recipe folder {0} delete failed", folderName));
  569. return false;
  570. }
  571. return true;
  572. }
  573. /// <summary>
  574. /// save as recipe content
  575. /// </summary>
  576. /// <param name="chamId"></param>
  577. /// <param name="recipeName"></param>
  578. /// <param name="recipeContent"></param>
  579. /// <returns></returns>
  580. public bool SaveAsRecipe(string chamId, string recipeName, string recipeContent)
  581. {
  582. var path = GenerateRecipeFilePath(chamId, recipeName);
  583. //if (File.Exists(path))
  584. //{
  585. // WarningDialog(string.Format(Resources.RecipeFileManager_SaveAsRecipe_RecipeFile0savefailed, recipeName));
  586. // return false;
  587. //}
  588. return SaveRecipe(chamId, recipeName, recipeContent, true, true);
  589. }
  590. public bool SaveAsRecipe(string chamId,string type, string recipeName, string recipeContent)
  591. {
  592. var path = GenerateRecipeFilePath(chamId, type, recipeName);
  593. if (File.Exists(path))
  594. {
  595. LOG.Write(eEvent.WARN_SYSTEM_CONFIG, ModuleName.System, $"{recipeName}文件已存在,创建recipe {recipeName}失败");
  596. //WarningDialog(string.Format(Resources.RecipeFileManager_SaveAsRecipe_RecipeFile0savefailed, recipeName));
  597. return false;
  598. }
  599. return SaveRecipe(chamId,type, recipeName, recipeContent, true, true);
  600. }
  601. /// <summary>
  602. /// save recipe content
  603. /// </summary>
  604. /// <param name="chamId"></param>
  605. /// <param name="recipeName"></param>
  606. /// <param name="recipeContent"></param>
  607. /// <returns></returns>
  608. public bool SaveRecipe(string chamId, string recipeName, string recipeContent, bool clearBarcode, bool notifyUI)
  609. {
  610. bool ret = true;
  611. try
  612. {
  613. var path = GenerateRecipeFilePath(chamId, recipeName);
  614. if (!_rcpContext.EnableEdit(path))
  615. return false;
  616. FileInfo fi = new FileInfo(path);
  617. if (!fi.Directory.Exists)
  618. fi.Directory.Create();
  619. File.WriteAllText(path, RecipeUnity.ConvertJsonString(recipeContent), Encoding.UTF8);
  620. }
  621. catch (Exception ex)
  622. {
  623. LOG.WriteExeption("保存recipe file 出错", ex);
  624. if (notifyUI)
  625. {
  626. WarningDialog(string.Format(Resources.RecipeFileManager_SaveRecipe_RecipeFile0SaveFailed, recipeName));
  627. }
  628. ret = false;
  629. }
  630. return ret;
  631. }
  632. /// <summary>
  633. /// save recipe content
  634. /// </summary>
  635. /// <param name="chamId"></param>
  636. /// <param name="recipeName"></param>
  637. /// <param name="recipeContent"></param>
  638. /// <returns></returns>
  639. public bool SaveRecipe(string chamId,string type, string recipeName, string recipeContent, bool clearBarcode, bool notifyUI)
  640. {
  641. bool ret = true;
  642. try
  643. {
  644. var path = GenerateRecipeFilePath(chamId, type, recipeName);
  645. if (!_rcpContext.EnableEdit(path))
  646. return false;
  647. FileInfo fi = new FileInfo(path);
  648. if (!fi.Directory.Exists)
  649. fi.Directory.Create();
  650. File.WriteAllText(path, RecipeUnity.ConvertJsonString(recipeContent), Encoding.UTF8);
  651. }
  652. catch (Exception ex)
  653. {
  654. LOG.WriteExeption("保存recipe file 出错", ex);
  655. if (notifyUI)
  656. {
  657. WarningDialog(string.Format(Resources.RecipeFileManager_SaveRecipe_RecipeFile0SaveFailed, recipeName));
  658. }
  659. ret = false;
  660. }
  661. return ret;
  662. }
  663. /// <summary>
  664. /// move recipe file
  665. /// </summary>
  666. /// <param name="chamId"></param>
  667. /// <param name="recipeName"></param>
  668. /// <returns></returns>
  669. public bool MoveRecipeFile(string chamId, string recipeName, string tragetFolderName, bool clearBarcode, bool notifyUI)
  670. {
  671. bool ret = true;
  672. try
  673. {
  674. var path = getRecipeDirPath(chamId);
  675. string fullFileName = path + recipeName + ".rcp";
  676. string tragetFullFilePath = path + tragetFolderName;
  677. File.Move(fullFileName, tragetFullFilePath + "\\" + recipeName.Split('\\')[recipeName.Split('\\').Length - 1] + ".rcp");
  678. if (notifyUI)
  679. {
  680. InfoDialog(string.Format(Resources.RecipeFileManager_MoveRecipe_RecipeFile0MoveCompleted, recipeName));
  681. }
  682. else
  683. {
  684. LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format(Resources.RecipeFileManager_MoveRecipe_RecipeFile0MoveCompleted, recipeName));
  685. }
  686. }
  687. catch (Exception ex)
  688. {
  689. LOG.WriteExeption("移动 recipe file 出错", ex);
  690. if (notifyUI)
  691. {
  692. WarningDialog(string.Format(Resources.RecipeFileManager_MoveRecipe_RecipeFile0MoveFailed, recipeName));
  693. }
  694. ret = false;
  695. }
  696. return ret;
  697. }
  698. /// <summary>
  699. /// create a new recipe folder
  700. /// </summary>
  701. /// <param name="chamId"></param>
  702. /// <param name="folderName"></param>
  703. /// <returns></returns>
  704. public bool CreateFolder(string chamId, string folderName)
  705. {
  706. try
  707. {
  708. Directory.CreateDirectory(getRecipeDirPath(chamId) + folderName);
  709. InfoDialog(string.Format(Resources.RecipeFileManager_CreateFolder_RecipeFolder0Created, folderName));
  710. }
  711. catch (Exception ex)
  712. {
  713. LOG.WriteExeption("创建recipe folder 出错", ex);
  714. WarningDialog(string.Format(Resources.RecipeFileManager_CreateFolder_RecipeFolder0CreateFailed, folderName));
  715. return false;
  716. }
  717. return true;
  718. }
  719. /// <summary>
  720. /// Rename recipe folder name
  721. /// </summary>
  722. /// <param name="chamId"></param>
  723. /// <param name="oldName"></param>
  724. /// <param name="newName"></param>
  725. /// <returns></returns>
  726. public bool RenameFolder(string chamId, string oldName, string newName)
  727. {
  728. try
  729. {
  730. string oldPath = getRecipeDirPath(chamId) + oldName;
  731. string newPath = getRecipeDirPath(chamId) + newName;
  732. Directory.Move(oldPath, newPath);
  733. InfoDialog(string.Format(Resources.RecipeFileManager_RenameFolder_RecipeFolder0renamed, oldName, newName));
  734. }
  735. catch (Exception ex)
  736. {
  737. LOG.WriteExeption("重命名recipe folder 出错", ex);
  738. WarningDialog(string.Format(Resources.RecipeFileManager_RenameFolder_RecipeFolder0RenameFailed, oldName, newName));
  739. return false;
  740. }
  741. return true;
  742. }
  743. private string GetRecipeBody(string chamberId, string nodePath)
  744. {
  745. if (_rcpContext == null)
  746. return string.Empty;
  747. string schema = _rcpContext.GetRecipeDefiniton(chamberId);
  748. XmlDocument dom = new XmlDocument();
  749. dom.LoadXml(schema);
  750. XmlNode node = dom.SelectSingleNode(nodePath);
  751. return node.OuterXml;
  752. }
  753. /// <summary>
  754. /// get reactor's recipe format define file
  755. /// </summary>
  756. /// <param name="chamId"></param>
  757. /// <returns></returns>
  758. public string GetRecipeFormatXml(string chamberId)
  759. {
  760. return GetRecipeBody(chamberId, "/Aitex/TableRecipeFormat");
  761. }
  762. /// <summary>
  763. /// get reactor's template recipe file
  764. /// </summary>
  765. /// <param name="chamId"></param>
  766. /// <returns></returns>
  767. public string GetRecipeTemplate(string chamberId)
  768. {
  769. if (_rcpContext != null)
  770. return _rcpContext.GetRecipeTemplate(chamberId);
  771. return GetRecipeBody(chamberId, "/Aitex/TableRecipeData");
  772. }
  773. /// <summary>
  774. /// get reactor's template recipe file
  775. /// </summary>
  776. /// <param name="chamId"></param>
  777. /// <returns></returns>
  778. public string GetRecipeSchema(string chamberId)
  779. {
  780. if (_rcpContext == null)
  781. return string.Empty;
  782. string schema = _rcpContext.GetRecipeDefiniton(chamberId);
  783. XmlDocument dom = new XmlDocument();
  784. dom.LoadXml(schema);
  785. XmlNode node = dom.SelectSingleNode("/Aitex/TableRecipeSchema");
  786. return node.InnerXml;
  787. }
  788. public string GetRecipeByBarcode(string chamberId, string barcode)
  789. {
  790. try
  791. {
  792. string recipePath = PathManager.GetRecipeDir() + chamberId + "\\";
  793. var di = new DirectoryInfo(recipePath);
  794. var fis = di.GetFiles("*.rcp", SearchOption.AllDirectories);
  795. XmlDocument xml = new XmlDocument();
  796. foreach (var fi in fis)
  797. {
  798. string str = fi.FullName.Substring(recipePath.Length);
  799. if (!str.Contains("HistoryRecipe\\"))
  800. {
  801. xml.Load(fi.FullName);
  802. if (xml.SelectSingleNode(string.Format("/TableRecipeData[@Barcode='{0}']", barcode)) != null)
  803. {
  804. return str.Substring(0, str.LastIndexOf('.'));
  805. }
  806. }
  807. }
  808. return string.Empty;
  809. }
  810. catch (Exception ex)
  811. {
  812. LOG.WriteExeption(ex);
  813. return string.Empty;
  814. }
  815. }
  816. #region Sequence
  817. private string GetSequenceConfig(string nodePath)
  818. {
  819. if (_seqContext == null)
  820. return string.Empty;
  821. string schema = _seqContext.GetConfigXml();
  822. XmlDocument dom = new XmlDocument();
  823. dom.LoadXml(schema);
  824. XmlNode node = dom.SelectSingleNode(nodePath);
  825. return node.OuterXml;
  826. }
  827. public string GetSequence(string sequenceName, bool needValidation)
  828. {
  829. string seq = string.Empty;
  830. try
  831. {
  832. using (StreamReader fs = new StreamReader(GenerateSequenceFilePath(SequenceFolder, sequenceName)))
  833. {
  834. seq = fs.ReadToEnd();
  835. fs.Close();
  836. }
  837. if (needValidation && !_seqContext.Validation(seq))
  838. {
  839. LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"Read {sequenceName} failed, validation failed");
  840. seq = string.Empty;
  841. }
  842. }
  843. catch (Exception ex)
  844. {
  845. LOG.WriteExeption(ex);
  846. seq = string.Empty;
  847. }
  848. return seq;
  849. }
  850. private void TryAppendNode(XmlElement preOrNextNode, XmlElement curPM,XmlDocument dom, bool isLeft)
  851. {
  852. if (preOrNextNode != null && preOrNextNode.GetAttribute("Position") != "LL" && preOrNextNode.GetAttribute("Position") != "PM")
  853. {
  854. XmlElement newNode = dom.CreateElement("Step");
  855. newNode.SetAttribute("Position", "LL");
  856. newNode.SetAttribute("LLSelection", "LLA,LLB");
  857. if (isLeft)
  858. curPM.ParentNode.InsertBefore(newNode, curPM);
  859. else
  860. curPM.ParentNode.InsertAfter(newNode, curPM);
  861. }
  862. }
  863. public string GetSequenceAndTryAppendLL(string sequenceName, bool needValidation)
  864. {
  865. string seq = string.Empty;
  866. try
  867. {
  868. seq = GetSequence(sequenceName, needValidation);
  869. if(!string.IsNullOrWhiteSpace(seq))
  870. {
  871. XmlDocument dom = new XmlDocument();
  872. dom.LoadXml(seq);
  873. XmlNodeList lstStepNode = dom.SelectNodes("Aitex/TableSequenceData/Step");
  874. if (lstStepNode != null)
  875. {
  876. List<XmlElement> pmList = new List<XmlElement>();
  877. foreach (XmlElement nodeStep in lstStepNode)
  878. {
  879. var positionValue = nodeStep.GetAttribute("Position");
  880. if (positionValue == "PM")
  881. {
  882. pmList.Add(nodeStep);
  883. }
  884. }
  885. foreach (XmlElement pmNode in pmList)
  886. {
  887. TryAppendNode((XmlElement)pmNode.PreviousSibling, pmNode, dom, true);
  888. TryAppendNode((XmlElement)pmNode.NextSibling, pmNode, dom, false);
  889. }
  890. using (var ms = new MemoryStream())
  891. using (var writer = new XmlTextWriter(ms, null))
  892. {
  893. writer.Formatting = Formatting.Indented;
  894. dom.Save(writer);
  895. return Encoding.UTF8.GetString(ms.ToArray());
  896. }
  897. }
  898. }
  899. }
  900. catch(Exception ex)
  901. {
  902. LOG.WriteExeption(ex);
  903. seq = string.Empty;
  904. }
  905. return seq;
  906. }
  907. public List<string> GetSequenceNameList()
  908. {
  909. var result = new List<string>();
  910. try
  911. {
  912. string recipePath = PathManager.GetRecipeDir() + SequenceFolder + "\\";
  913. var di = new DirectoryInfo(recipePath);
  914. var fis = di.GetFiles("*.seq", SearchOption.AllDirectories);
  915. foreach (var fi in fis)
  916. {
  917. string str = fi.FullName.Substring(recipePath.Length);
  918. str = str.Substring(0, str.LastIndexOf('.'));
  919. result.Add(str);
  920. }
  921. }
  922. catch (Exception ex)
  923. {
  924. LOG.WriteExeption(ex);
  925. }
  926. return result;
  927. }
  928. public bool DeleteSequence(string sequenceName)
  929. {
  930. try
  931. {
  932. var path = GenerateSequenceFilePath(SequenceFolder, sequenceName);
  933. if (!_seqContext.EnableEdit(path))
  934. return false;
  935. File.Delete(path);
  936. LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"sequence {sequenceName} deleted");
  937. }
  938. catch (Exception ex)
  939. {
  940. LOG.WriteExeption(ex);
  941. return false;
  942. }
  943. return true;
  944. }
  945. public bool SaveSequence(string sequenceName, string sequenceContent, bool notifyUI)
  946. {
  947. bool ret = true;
  948. try
  949. {
  950. var path = GenerateSequenceFilePath(SequenceFolder, sequenceName);
  951. if (!_seqContext.EnableEdit(path))
  952. return false;
  953. FileInfo fi = new FileInfo(path);
  954. if (!fi.Directory.Exists)
  955. {
  956. fi.Directory.Create();
  957. }
  958. XmlDocument xml = new XmlDocument();
  959. xml.LoadXml(sequenceContent);
  960. XmlTextWriter writer = new XmlTextWriter(path, null);
  961. writer.Formatting = Formatting.Indented;
  962. xml.Save(writer);
  963. writer.Close();
  964. if (notifyUI)
  965. {
  966. EV.PostPopDialogMessage(EventLevel.Information, "Save Complete", $"Sequence {sequenceName} saved ");
  967. }
  968. else
  969. {
  970. LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"Sequence {sequenceName} saved ");
  971. }
  972. }
  973. catch (Exception ex)
  974. {
  975. LOG.WriteExeption(ex);
  976. if (notifyUI)
  977. {
  978. EV.PostPopDialogMessage(EventLevel.Alarm, "Save Error", $"save sequence {sequenceName} failed, " + ex.Message);
  979. }
  980. ret = false;
  981. }
  982. return ret;
  983. }
  984. public bool SaveAsSequence(string sequenceName, string sequenceContent)
  985. {
  986. var path = GenerateSequenceFilePath(SequenceFolder, sequenceName);
  987. if (File.Exists(path))
  988. {
  989. LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"save sequence {sequenceName} failed, already exist");
  990. return false;
  991. }
  992. return SaveSequence(sequenceName, sequenceContent, false);
  993. }
  994. public bool RenameSequence(string oldName, string newName)
  995. {
  996. try
  997. {
  998. var path = GenerateSequenceFilePath(SequenceFolder, oldName);
  999. if (!_seqContext.EnableEdit(path))
  1000. return false;
  1001. if (File.Exists(GenerateSequenceFilePath(SequenceFolder, newName)))
  1002. {
  1003. LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"{newName} already exist, rename failed");
  1004. return false;
  1005. }
  1006. else
  1007. {
  1008. File.Move(path, GenerateSequenceFilePath(SequenceFolder, newName));
  1009. LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"sequence {oldName} renamed to {newName}");
  1010. }
  1011. }
  1012. catch (Exception ex)
  1013. {
  1014. LOG.WriteExeption(ex);
  1015. return false;
  1016. }
  1017. return true;
  1018. }
  1019. public string GetSequenceFormatXml()
  1020. {
  1021. return GetSequenceConfig("/Aitex/TableSequenceFormat");
  1022. }
  1023. internal bool DeleteSequenceFolder(string folderName)
  1024. {
  1025. try
  1026. {
  1027. Directory.Delete(PathManager.GetRecipeDir() + SequenceFolder + "\\" + folderName, true);
  1028. LOG.Write(eEvent.EV_SEQUENCE,ModuleName.System, "Folder " + folderName + "deleted");
  1029. }
  1030. catch (Exception ex)
  1031. {
  1032. //LOG.Write(ex, "delete sequence folder exception");
  1033. LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"can not delete folder {folderName}, {ex.Message}");
  1034. return false;
  1035. }
  1036. return true;
  1037. }
  1038. internal bool CreateSequenceFolder(string folderName)
  1039. {
  1040. try
  1041. {
  1042. Directory.CreateDirectory(PathManager.GetRecipeDir() + SequenceFolder + "\\" + folderName);
  1043. LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, "Folder " + folderName + "created");
  1044. }
  1045. catch (Exception ex)
  1046. {
  1047. //LOG.Write(ex, "sequence folder create exception");
  1048. LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"can not create folder {folderName}, {ex.Message}");
  1049. return false;
  1050. }
  1051. return true;
  1052. }
  1053. internal bool RenameSequenceFolder(string oldName, string newName)
  1054. {
  1055. try
  1056. {
  1057. string oldPath = PathManager.GetRecipeDir() + SequenceFolder + "\\" + oldName;
  1058. string newPath = PathManager.GetRecipeDir() + SequenceFolder + "\\" + newName;
  1059. Directory.Move(oldPath, newPath);
  1060. LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"rename folder from {oldName} to {newName}");
  1061. }
  1062. catch (Exception ex)
  1063. {
  1064. //LOG.Write(ex, "rename sequence folder failed");
  1065. LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"can not rename folder {oldName}, {ex.Message}");
  1066. return false;
  1067. }
  1068. return true;
  1069. }
  1070. public string GetXmlSequenceList(string chamberId)
  1071. {
  1072. XmlDocument doc = new XmlDocument();
  1073. DirectoryInfo curFolderInfo = new DirectoryInfo(PathManager.GetRecipeDir() + SequenceFolder + "\\");
  1074. doc.AppendChild(GenerateSequenceList(chamberId, curFolderInfo, doc));
  1075. return doc.OuterXml;
  1076. }
  1077. XmlElement GenerateSequenceList(string chamberId, DirectoryInfo currentDir, XmlDocument doc)
  1078. {
  1079. int trimLength = (PathManager.GetRecipeDir() + SequenceFolder + "\\").Length;
  1080. XmlElement folderEle = doc.CreateElement("Folder");
  1081. folderEle.SetAttribute("Name", currentDir.FullName.Substring(trimLength));
  1082. DirectoryInfo[] dirInfos = currentDir.GetDirectories();
  1083. foreach (DirectoryInfo dirInfo in dirInfos)
  1084. {
  1085. folderEle.AppendChild(GenerateSequenceList(chamberId, dirInfo, doc));
  1086. }
  1087. FileInfo[] fileInfos = currentDir.GetFiles("*.seq");
  1088. foreach (FileInfo fileInfo in fileInfos)
  1089. {
  1090. XmlElement fileNd = doc.CreateElement("File");
  1091. string fileStr = fileInfo.FullName.Substring(trimLength).TrimStart(new char[] { '\\' }); ;
  1092. fileStr = fileStr.Substring(0, fileStr.LastIndexOf("."));
  1093. fileNd.SetAttribute("Name", fileStr);
  1094. folderEle.AppendChild(fileNd);
  1095. }
  1096. return folderEle;
  1097. }
  1098. #endregion
  1099. }
  1100. }