RecipeFileManager.cs 41 KB

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