RecipeFileManager.cs 46 KB

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