| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289 |
- using Aitex.Common.Util;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Util;
- using Aitex.Core.Utilities;
- using Aitex.Core.WCF;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.Common.Properties;
- using MECF.Framework.Common.RecipeCenter;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Xml;
- using System.Xml.Schema;
- using PunkHPX8_Core;
- using System.Linq;
- using System.Collections.ObjectModel;
- using Aitex.Core.RT.Routine;
- using System.Windows.Media.Converters;
- namespace Aitex.Core.RT.RecipeCenter
- {
- public class RecipeFileManager : Singleton<RecipeFileManager>
- {
- //sequence文件 统一放在 Recipes/Sequence 文件夹下面
- public const string SequenceFolder = "Sequence";
- public const string SourceModule = "Recipe";
- public const string ProductionFolder = "Production";
- public const string EngineeringFolder = "Engineering";
- private bool _recipeIsValid;
- private List<string> _validationErrors = new List<string>();
- private List<string> _validationWarnings = new List<string>();
-
- IRecipeFileContext _rcpContext;
- private ISequenceFileContext _seqContext;
- public void Initialize(IRecipeFileContext context)
- {
- Initialize(context, null, true);
- }
- public void Initialize(IRecipeFileContext context, bool enableService)
- {
- Initialize(context, null, enableService);
- }
- public void Initialize(IRecipeFileContext rcpContext, ISequenceFileContext seqContext, bool enableService)
- {
- _rcpContext = rcpContext == null ? new DefaultRecipeFileContext() : rcpContext;
- _seqContext = seqContext == null ? new DefaultSequenceFileContext() : seqContext;
- CultureSupported.UpdateCoreCultureResource(CultureSupported.English);
- //if (enableService)
- //{
- // Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
- // {
- // typeof(RecipeService)
- // });
- //}
- var dir = string.Format("{0}{1}\\", PathManager.GetRecipeDir(), SequenceFolder);
- DirectoryInfo di = new DirectoryInfo(dir);
- if (!di.Exists)
- {
- di.Create();
- }
- }
- private void ValidationEventHandler(object sender, ValidationEventArgs e)
- {
- switch (e.Severity)
- {
- case XmlSeverityType.Error:
- _validationErrors.Add(e.Message);
- _recipeIsValid = false;
- break;
- case XmlSeverityType.Warning:
- _validationWarnings.Add(e.Message);
- break;
- }
- }
- /// <summary>
- /// XML schema checking
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="recipeName"></param>
- /// <param name="recipeContent"></param>
- /// <param name="reason"></param>
- /// <returns></returns>
- public bool ValidateRecipe(string chamberId, string recipeName, string recipeContent, out List<string> reason)
- {
- try
- {
- XmlDocument document = new XmlDocument();
- document.LoadXml(recipeContent);
- MemoryStream schemaStream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(GetRecipeSchema(chamberId)));
- XmlReader xmlSchemaReader = XmlReader.Create(schemaStream);
- XmlSchema schema1 = XmlSchema.Read(xmlSchemaReader, ValidationEventHandler);
- document.Schemas.Add(schema1);
- document.LoadXml(recipeContent);
- ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
- _recipeIsValid = true;
- _validationErrors = new List<string>();
- _validationWarnings = new List<string>();
- // Validates recipe.
- document.Validate(eventHandler);
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- _recipeIsValid = false;
- }
- if (!_recipeIsValid && _validationErrors.Count == 0)
- {
- _validationErrors.Add(Resources.RecipeFileManager_ValidateRecipe_XMLSchemaValidateFailed);
- }
- reason = _validationErrors;
- return _recipeIsValid;
- }
- /// <summary>
- /// This method will be invoked by two places:
- /// (1) Load a recipe from server to GUI for editing (do not need validation when loading, do validation when saving);
- /// (2) Load a recipe from recipe engine to run process(always do a validation before run recipe);
- /// </summary>
- /// <param name="recipeName"></param>
- /// <param name="needValidation">indicate whether a recipe format validation is needed or not</param>
- /// <returns></returns>
- public string LoadRecipe(string chamberId, string recipeName, bool needValidation)
- {
- string rcp = string.Empty;
- try
- {
- using (StreamReader fs = new StreamReader(GenerateRecipeFilePath(chamberId, recipeName)))
- {
- rcp = fs.ReadToEnd();
- fs.Close();
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
- rcp = string.Empty;
- }
- return rcp;
- }
- public string LoadRecipeByPath(string path)
- {
- string rcp = string.Empty;
- try
- {
- using (StreamReader fs = new StreamReader(path))
- {
- rcp = fs.ReadToEnd();
- fs.Close();
- }
- }
- catch (Exception ex)
- {
- // LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
- rcp = string.Empty;
- }
- return rcp;
- }
-
- /// <summary>
- /// Get recipe list
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="includingUsedRecipe"></param>
- /// <returns></returns>
- public IEnumerable<string> GetRecipes(string chamberId, bool includingUsedRecipe)
- {
- return _rcpContext.GetRecipes(chamberId, includingUsedRecipe);
- }
- /// <summary>
- /// 加载工程指定类型Recipe
- /// </summary>
- /// <param name="recipeType"></param>
- /// <returns></returns>
- public ObservableCollection<RecipeNode> GetEngineeringRecipesByType(string recipeType)
- {
- try
- {
- ObservableCollection<RecipeNode> treeNodes = new ObservableCollection<RecipeNode>();
- string recipePath = PathManager.GetRecipeDir();
- var di = new DirectoryInfo(recipePath);
- DirectoryInfo[] infos = di.GetDirectories();
- var recipes = new List<string>();
- foreach (var info in infos)
- {
- if (info.Name == "Engineering")
- {
- FileInfo[] files = info.GetFiles();
- recipes.Add(info.Name);
- foreach (FileInfo file in files)
- {
- if (file.Name.Contains($".{recipeType}."))
- {
- RecipeNode childNode = new RecipeNode();
- childNode.Name = file.Name.Substring(0, file.Name.IndexOf($".{recipeType}"));
- childNode.NodeType = RecipeNodeType.File;
- childNode.RecipeLocation = info.Name;
- childNode.FileName = file.Name;
- childNode.RecipeFullFileName = file.FullName;
- treeNodes.Add(childNode);
- }
- }
- }
- }
- return treeNodes;
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- return new ObservableCollection<RecipeNode>();
- }
- }
- /// <summary>
- /// 加载指定类型的Recipe
- /// </summary>
- /// <param name="recipeType"></param>
- /// <returns></returns>
- public ObservableCollection<RecipeNode> GetRecipesByType(string recipeType)
- {
- try
- {
- ObservableCollection<RecipeNode> treeNodes = new ObservableCollection<RecipeNode>();
- string recipePath = PathManager.GetRecipeDir() ;
- var di = new DirectoryInfo(recipePath);
- DirectoryInfo[] infos= di.GetDirectories();
- var recipes = new List<string>();
- foreach (var info in infos)
- {
- RecipeNode treeNode = new RecipeNode();
- treeNode.Name = info.Name;
- treeNode.NodeType = RecipeNodeType.Directory;
- treeNode.Children =new ObservableCollection<RecipeNode>();
- FileInfo[] files = info.GetFiles();
- recipes.Add(info.Name);
- foreach(FileInfo file in files)
- {
- if (file.Name.Contains($".{recipeType}."))
- {
- RecipeNode childNode = new RecipeNode();
- childNode.Name = file.Name.Substring(0,file.Name.IndexOf($".{recipeType}"));
- childNode.NodeType = RecipeNodeType.File;
- childNode.RecipeLocation = info.Name;
- childNode.FileName = file.Name;
- childNode.RecipeFullFileName = file.FullName;
- treeNode.Children.Add(childNode);
- }
- }
- treeNodes.Add(treeNode);
- }
- return treeNodes;
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- return new ObservableCollection<RecipeNode>();
- }
- }
- /// <summary>
- /// 获取目录集合节点
- /// </summary>
- /// <param name="directories"></param>
- /// <returns></returns>
- public ObservableCollection<RecipeNode> GetRecipeByDirectoryList(List<string> directories)
- {
- try
- {
- ObservableCollection<RecipeNode> treeNodes = new ObservableCollection<RecipeNode>();
- string recipePath = PathManager.GetRecipeDir();
- var di = new DirectoryInfo(recipePath);
- DirectoryInfo[] infos = di.GetDirectories();
- var recipes = new List<string>();
- foreach (var info in infos)
- {
- if (directories.Contains(info.Name))
- {
- RecipeNode treeNode = new RecipeNode();
- treeNode.Name = info.Name;
- treeNode.NodeType = RecipeNodeType.Directory;
- treeNode.Children = new ObservableCollection<RecipeNode>();
- FileInfo[] files = info.GetFiles();
- recipes.Add(info.Name);
- foreach (FileInfo file in files)
- {
- if (file.Name.EndsWith($".rcp"))
- {
- RecipeNode childNode = new RecipeNode();
- string recipeTypeName = file.Name.Substring(0, file.Name.IndexOf($".rcp"));
- int recipeTypeIndex = recipeTypeName.LastIndexOf(".");
- if (recipeTypeIndex != -1)
- {
- childNode.Name = recipeTypeName.Substring(0, recipeTypeIndex);
- childNode.NodeType = RecipeNodeType.File;
- childNode.RecipeLocation = info.Name;
- childNode.FileName = file.Name;
- childNode.RecipeFullFileName = file.FullName;
- treeNode.Children.Add(childNode);
- }
- }
- }
- treeNodes.Add(treeNode);
- }
- }
- return treeNodes;
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- return new ObservableCollection<RecipeNode>();
- }
- }
- /// <summary>
- /// 删除Recipe文件
- /// </summary>
- /// <param name="fullPath"></param>
- /// <returns></returns>
- public bool DeleteRecipeByFullPath(string fullPath)
- {
- try
- {
- if (File.Exists(fullPath))
- {
- File.Delete(fullPath);
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("delete recipe file error", ex);
- return false;
- }
- return true;
- }
- public bool PromoteRecipe(List<string> FilePaths)
- {
- try
- {
- foreach (var SourceFilePath in FilePaths)
- {
- string TargetFilePath = SourceFilePath.Replace("Engineering", "Production");
- File.Copy(SourceFilePath, TargetFilePath, true);
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Promote recipe file error", ex);
- return false;
- }
- return true;
- }
- /// <summary>
- /// 加载Recipe
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="recipeFileFullName"></param>
- /// <returns></returns>
- public T LoadGenericityRecipe<T>(string recipeFileFullName)
- {
- try
- {
- return CustomXmlSerializer.Deserialize<T>(new FileInfo(recipeFileFullName));
- }
- catch
- {
- return default(T);
- }
- }
- /// <summary>
- /// Get recipe list in xml format
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="includingUsedRecipe"></param>
- /// <returns></returns>
- public string GetXmlRecipeList(string chamberId, bool includingUsedRecipe)
- {
- XmlDocument doc = new XmlDocument();
- var baseFolderPath = getRecipeDirPath(chamberId);
- DirectoryInfo curFolderInfo = new DirectoryInfo(baseFolderPath);
- doc.AppendChild(GenerateRecipeList(chamberId, curFolderInfo, doc, includingUsedRecipe));
- return doc.OuterXml;
- }
- /// <summary>
- /// generate recipe list information in current directory
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="currentDir"></param>
- /// <param name="doc"></param>
- /// <returns></returns>
- XmlElement GenerateRecipeList(string chamberId, DirectoryInfo currentDir, XmlDocument doc, bool includingUsedRecipe)
- {
- int trimLength = getRecipeDirPath(chamberId).Length;
- XmlElement folderEle = doc.CreateElement("Folder");
- folderEle.SetAttribute("Name", currentDir.FullName.Substring(trimLength));
- DirectoryInfo[] dirInfos = currentDir.GetDirectories();
- foreach (DirectoryInfo dirInfo in dirInfos)
- {
- if (!includingUsedRecipe && dirInfo.Name == "HistoryRecipe")
- continue;
- folderEle.AppendChild(GenerateRecipeList(chamberId, dirInfo, doc, includingUsedRecipe));
- }
- FileInfo[] fileInfos = currentDir.GetFiles("*.rcp");
- foreach (FileInfo fileInfo in fileInfos)
- {
- XmlElement fileNd = doc.CreateElement("File");
- string fileStr = fileInfo.FullName.Substring(trimLength).TrimStart(new char[] { '\\' }); ;
- fileStr = fileStr.Substring(0, fileStr.LastIndexOf("."));
- fileNd.SetAttribute("Name", fileStr);
- folderEle.AppendChild(fileNd);
- }
- return folderEle;
- }
- /// <summary>
- /// Delete a recipe by recipe name
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="recipeName"></param>
- /// <returns></returns>
- public bool DeleteRecipe(string chamberId, string recipeName)
- {
- try
- {
- var path = GenerateRecipeFilePath(chamberId, recipeName);
- if (!_rcpContext.EnableEdit(path))
- return false;
- File.Delete(path);
- InfoDialog(string.Format(Resources.RecipeFileManager_DeleteRecipe_RecipeFile0DeleteSucceeded, recipeName));
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Delete recipe file error", ex);
- WarningDialog(string.Format(Resources.RecipeFileManager_DeleteRecipe_RecipeFile0DeleteFailed, recipeName));
- return false;
- }
- return true;
- }
- /// <summary>
- /// Rename recipe
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="oldName"></param>
- /// <param name="newName"></param>
- /// <returns></returns>
- public bool RenameRecipe(string chamId, string oldName, string newName)
- {
- try
- {
- var path = GenerateRecipeFilePath(chamId, newName);
- if (!_rcpContext.EnableEdit(path))
- return false;
- if (File.Exists(path))
- {
- WarningDialog(string.Format(Resources.RecipeFileManager_RenameRecipe_RecipeFile0FileExisted, oldName));
- return false;
- }
- else
- {
- File.Move(GenerateRecipeFilePath(chamId, oldName), GenerateRecipeFilePath(chamId, newName));
- InfoDialog(string.Format(Resources.RecipeFileManager_RenameRecipe_RecipeFile0Renamed, oldName, newName));
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Rename recipe file error", ex);
- WarningDialog(string.Format(Resources.RecipeFileManager_RenameRecipe_RecipeFile0RenameFailed, oldName, newName));
- return false;
- }
- return true;
- }
- private void InfoDialog(string message)
- {
- _rcpContext.PostInfoDialogMessage(message);
- }
- private void WarningDialog(string message)
- {
- _rcpContext.PostWarningDialogMessage(message);
- }
- /// <summary>
- /// get recipe's file path
- /// </summary>
- /// <param name="recipeName"></param>
- /// <returns></returns>
- private string GenerateRecipeFilePath(string chamId, string recipeName)
- {
- return getRecipeDirPath(chamId) + recipeName + ".rcp";
- }
- private string GenerateRecipeFilePath2(string chamId, string type,string recipeName)
- {
- return getRecipeDirPath(chamId) +type+"\\"+ recipeName + ".rcp";
- }
- private string GenerateSequenceFilePath(string chamId, string recipeName)
- {
- return getRecipeDirPath(chamId) + recipeName + ".seq";
- }
- /// <summary>
- /// get recipe's dir path
- /// </summary>
- /// <param name="recipeName"></param>
- /// <returns></returns>
- private string getRecipeDirPath(string chamId)
- {
- var dir = string.Format("{0}{1}\\", PathManager.GetRecipeDir(), chamId);
- DirectoryInfo di = new DirectoryInfo(dir);
- if (!di.Exists) di.Create();
- return dir;
- }
- /// <summary>
- /// 保存Recipe
- /// </summary>
- /// <param name="root"></param>
- /// <param name="recipeName"></param>
- /// <param name="recipe"></param>
- /// <param name="recipeType"></param>
- public void SaveGenericityRecipe(string root, string recipeName, string recipeType, object recipe)
- {
- try
- {
- string recipeDir = PathManager.GetRecipeDir();
- string fullName = $"{recipeDir}{root}\\{recipeName}.{recipeType}.rcp";
- CustomXmlSerializer.Serialize(recipe, fullName);
- }
- catch (Exception ex)
- {
- LOG.Write(eEvent.ERR_EXCEPTION, "Recipe", $"Saving Recipe is failed. Can't access the target recipe file:{recipeName}.{recipeType}.rcp");
- }
- }
- /// <summary>
- /// delete a recipe folder
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="folderName"></param>
- /// <returns></returns>
- public bool DeleteFolder(string chamId, string folderName)
- {
- try
- {
- Directory.Delete(getRecipeDirPath(chamId) + folderName, true);
- InfoDialog(string.Format(Resources.RecipeFileManager_DeleteFolder_RecipeFolder0DeleteSucceeded, folderName));
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Delete recipe folder error", ex);
- WarningDialog(string.Format("recipe folder {0} delete failed", folderName));
- return false;
- }
- return true;
- }
- /// <summary>
- /// save as recipe content
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="recipeName"></param>
- /// <param name="recipeContent"></param>
- /// <returns></returns>
- public bool SaveAsRecipe(string chamId, string recipeName, string recipeContent)
- {
- var path = GenerateRecipeFilePath(chamId, recipeName);
- //if (File.Exists(path))
- //{
- // WarningDialog(string.Format(Resources.RecipeFileManager_SaveAsRecipe_RecipeFile0savefailed, recipeName));
- // return false;
- //}
- return SaveRecipe(chamId, recipeName, recipeContent, true, true);
- }
- public bool SaveAsRecipe2(string chamId,string type, string recipeName, string recipeContent)
- {
- var path = GenerateRecipeFilePath(chamId, recipeName);
- //if (File.Exists(path))
- //{
- // WarningDialog(string.Format(Resources.RecipeFileManager_SaveAsRecipe_RecipeFile0savefailed, recipeName));
- // return false;
- //}
- return SaveRecipe2(chamId,type, recipeName, recipeContent, true, true);
- }
- /// <summary>
- /// save recipe content
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="recipeName"></param>
- /// <param name="recipeContent"></param>
- /// <returns></returns>
- public bool SaveRecipe(string chamId, string recipeName, string recipeContent, bool clearBarcode, bool notifyUI)
- {
- bool ret = true;
- try
- {
- var path = GenerateRecipeFilePath(chamId, recipeName);
- if (!_rcpContext.EnableEdit(path))
- return false;
-
- FileInfo fi = new FileInfo(path);
- if (!fi.Directory.Exists)
- fi.Directory.Create();
-
- File.WriteAllText(path, RecipeUnity.ConvertJsonString(recipeContent), Encoding.UTF8);
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Save recipe file error", ex);
- if (notifyUI)
- {
- WarningDialog(string.Format(Resources.RecipeFileManager_SaveRecipe_RecipeFile0SaveFailed, recipeName));
- }
- ret = false;
- }
- return ret;
- }
- /// <summary>
- /// save recipe content
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="recipeName"></param>
- /// <param name="recipeContent"></param>
- /// <returns></returns>
- public bool SaveRecipe2(string chamId,string type, string recipeName, string recipeContent, bool clearBarcode, bool notifyUI)
- {
- bool ret = true;
- try
- {
- var path = GenerateRecipeFilePath2(chamId, type, recipeName);
- if (!_rcpContext.EnableEdit(path))
- return false;
- FileInfo fi = new FileInfo(path);
- if (!fi.Directory.Exists)
- fi.Directory.Create();
- File.WriteAllText(path, RecipeUnity.ConvertJsonString(recipeContent), Encoding.UTF8);
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Save recipe file error", ex);
- if (notifyUI)
- {
- WarningDialog(string.Format(Resources.RecipeFileManager_SaveRecipe_RecipeFile0SaveFailed, recipeName));
- }
- ret = false;
- }
- return ret;
- }
- /// <summary>
- /// move recipe file
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="recipeName"></param>
- /// <returns></returns>
- public bool MoveRecipeFile(string chamId, string recipeName, string tragetFolderName, bool clearBarcode, bool notifyUI)
- {
- bool ret = true;
- try
- {
- var path = getRecipeDirPath(chamId);
- string fullFileName = path + recipeName + ".rcp";
- string tragetFullFilePath = path + tragetFolderName;
- File.Move(fullFileName, tragetFullFilePath + "\\" + recipeName.Split('\\')[recipeName.Split('\\').Length - 1] + ".rcp");
- if (notifyUI)
- {
- InfoDialog(string.Format(Resources.RecipeFileManager_MoveRecipe_RecipeFile0MoveCompleted, recipeName));
- }
- else
- {
- LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format(Resources.RecipeFileManager_MoveRecipe_RecipeFile0MoveCompleted, recipeName));
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Move recipe file error", ex);
- if (notifyUI)
- {
- WarningDialog(string.Format(Resources.RecipeFileManager_MoveRecipe_RecipeFile0MoveFailed, recipeName));
- }
- ret = false;
- }
- return ret;
- }
- /// <summary>
- /// create a new recipe folder
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="folderName"></param>
- /// <returns></returns>
- public bool CreateFolder(string chamId, string folderName)
- {
- try
- {
- Directory.CreateDirectory(getRecipeDirPath(chamId) + folderName);
- InfoDialog(string.Format(Resources.RecipeFileManager_CreateFolder_RecipeFolder0Created, folderName));
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Create recipe folder error", ex);
- WarningDialog(string.Format(Resources.RecipeFileManager_CreateFolder_RecipeFolder0CreateFailed, folderName));
- return false;
- }
- return true;
- }
- /// <summary>
- /// Rename recipe folder name
- /// </summary>
- /// <param name="chamId"></param>
- /// <param name="oldName"></param>
- /// <param name="newName"></param>
- /// <returns></returns>
- public bool RenameFolder(string chamId, string oldName, string newName)
- {
- try
- {
- string oldPath = getRecipeDirPath(chamId) + oldName;
- string newPath = getRecipeDirPath(chamId) + newName;
- Directory.Move(oldPath, newPath);
- InfoDialog(string.Format(Resources.RecipeFileManager_RenameFolder_RecipeFolder0renamed, oldName, newName));
- }
- catch (Exception ex)
- {
- LOG.WriteExeption("Rename recipe folder error", ex);
- WarningDialog(string.Format(Resources.RecipeFileManager_RenameFolder_RecipeFolder0RenameFailed, oldName, newName));
- return false;
- }
- return true;
- }
- private string GetRecipeBody(string chamberId, string nodePath)
- {
- if (_rcpContext == null)
- return string.Empty;
- string schema = _rcpContext.GetRecipeDefiniton(chamberId);
- XmlDocument dom = new XmlDocument();
- dom.LoadXml(schema);
- XmlNode node = dom.SelectSingleNode(nodePath);
- return node.OuterXml;
- }
- /// <summary>
- /// get reactor's recipe format define file
- /// </summary>
- /// <param name="chamId"></param>
- /// <returns></returns>
- public string GetRecipeFormatXml(string chamberId)
- {
- return GetRecipeBody(chamberId, "/Aitex/TableRecipeFormat");
- }
- /// <summary>
- /// get reactor's template recipe file
- /// </summary>
- /// <param name="chamId"></param>
- /// <returns></returns>
- public string GetRecipeTemplate(string chamberId)
- {
- if (_rcpContext != null)
- return _rcpContext.GetRecipeTemplate(chamberId);
- return GetRecipeBody(chamberId, "/Aitex/TableRecipeData");
- }
- /// <summary>
- /// get reactor's template recipe file
- /// </summary>
- /// <param name="chamId"></param>
- /// <returns></returns>
- public string GetRecipeSchema(string chamberId)
- {
- if (_rcpContext == null)
- return string.Empty;
- string schema = _rcpContext.GetRecipeDefiniton(chamberId);
- XmlDocument dom = new XmlDocument();
- dom.LoadXml(schema);
- XmlNode node = dom.SelectSingleNode("/Aitex/TableRecipeSchema");
- return node.InnerXml;
- }
- #region Sequence
- private string GetSequenceConfig(string nodePath)
- {
- if (_seqContext == null)
- return string.Empty;
- string schema = _seqContext.GetConfigXml();
- XmlDocument dom = new XmlDocument();
- dom.LoadXml(schema);
- XmlNode node = dom.SelectSingleNode(nodePath);
- return node.OuterXml;
- }
- public string GetSequence(string sequenceName, bool needValidation)
- {
- string seq = string.Empty;
- try
- {
- using (StreamReader fs = new StreamReader(GenerateSequenceFilePath(SequenceFolder, sequenceName)))
- {
- seq = fs.ReadToEnd();
- fs.Close();
- }
- if (needValidation && !_seqContext.Validation(seq))
- {
- LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"Read {sequenceName} failed, validation failed");
- seq = string.Empty;
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- seq = string.Empty;
- }
- return seq;
- }
- private void TryAppendNode(XmlElement preOrNextNode, XmlElement curPM,XmlDocument dom, bool isLeft)
- {
- if (preOrNextNode != null && preOrNextNode.GetAttribute("Position") != "LL" && preOrNextNode.GetAttribute("Position") != "PM")
- {
- XmlElement newNode = dom.CreateElement("Step");
- newNode.SetAttribute("Position", "LL");
- newNode.SetAttribute("LLSelection", "LLA,LLB");
- if (isLeft)
- curPM.ParentNode.InsertBefore(newNode, curPM);
- else
- curPM.ParentNode.InsertAfter(newNode, curPM);
- }
- }
- public string GetSequenceAndTryAppendLL(string sequenceName, bool needValidation)
- {
- string seq = string.Empty;
- try
- {
- seq = GetSequence(sequenceName, needValidation);
- if(!string.IsNullOrWhiteSpace(seq))
- {
- XmlDocument dom = new XmlDocument();
- dom.LoadXml(seq);
- XmlNodeList lstStepNode = dom.SelectNodes("Aitex/TableSequenceData/Step");
- if (lstStepNode != null)
- {
- List<XmlElement> pmList = new List<XmlElement>();
- foreach (XmlElement nodeStep in lstStepNode)
- {
- var positionValue = nodeStep.GetAttribute("Position");
- if (positionValue == "PM")
- {
- pmList.Add(nodeStep);
- }
- }
- foreach (XmlElement pmNode in pmList)
- {
- TryAppendNode((XmlElement)pmNode.PreviousSibling, pmNode, dom, true);
- TryAppendNode((XmlElement)pmNode.NextSibling, pmNode, dom, false);
- }
- using (var ms = new MemoryStream())
- using (var writer = new XmlTextWriter(ms, null))
- {
- writer.Formatting = Formatting.Indented;
- dom.Save(writer);
- return Encoding.UTF8.GetString(ms.ToArray());
- }
- }
- }
- }
- catch(Exception ex)
- {
- LOG.WriteExeption(ex);
- seq = string.Empty;
- }
- return seq;
- }
- public List<string> GetSequenceNameList()
- {
- var result = new List<string>();
- try
- {
- string recipePath = PathManager.GetRecipeDir() + SequenceFolder + "\\";
- var di = new DirectoryInfo(recipePath);
- var fis = di.GetFiles("*.seq.*", SearchOption.AllDirectories);
- foreach (var fi in fis)
- {
- string str = fi.FullName.Substring(recipePath.Length);
- str = str.Substring(0, str.LastIndexOf('.'));
- result.Add(str);
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- }
- return result;
- }
- public bool DeleteSequence(string sequenceName)
- {
- try
- {
- var path = GenerateSequenceFilePath(SequenceFolder, sequenceName);
- if (!_seqContext.EnableEdit(path))
- return false;
- File.Delete(path);
- LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"sequence {sequenceName} deleted");
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- return false;
- }
- return true;
- }
- public bool SaveSequence(string sequenceName, string sequenceContent, bool notifyUI)
- {
- bool ret = true;
- try
- {
- var path = GenerateSequenceFilePath(SequenceFolder, sequenceName);
- if (!_seqContext.EnableEdit(path))
- return false;
- FileInfo fi = new FileInfo(path);
- if (!fi.Directory.Exists)
- {
- fi.Directory.Create();
- }
- XmlDocument xml = new XmlDocument();
- xml.LoadXml(sequenceContent);
- XmlTextWriter writer = new XmlTextWriter(path, null);
- writer.Formatting = Formatting.Indented;
- xml.Save(writer);
- writer.Close();
- if (notifyUI)
- {
- EV.PostPopDialogMessage(EventLevel.Information, "Save Complete", $"Sequence {sequenceName} saved ");
- }
- else
- {
- LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"Sequence {sequenceName} saved ");
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- if (notifyUI)
- {
- EV.PostPopDialogMessage(EventLevel.Alarm, "Save Error", $"save sequence {sequenceName} failed, " + ex.Message);
- }
- ret = false;
- }
- return ret;
- }
- public bool SaveAsSequence(string sequenceName, string sequenceContent)
- {
- var path = GenerateSequenceFilePath(SequenceFolder, sequenceName);
- if (File.Exists(path))
- {
- LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"save sequence {sequenceName} failed, already exist");
- return false;
- }
- return SaveSequence(sequenceName, sequenceContent, false);
- }
- public bool RenameSequence(string oldName, string newName)
- {
- try
- {
- var path = GenerateSequenceFilePath(SequenceFolder, oldName);
- if (!_seqContext.EnableEdit(path))
- return false;
- if (File.Exists(GenerateSequenceFilePath(SequenceFolder, newName)))
- {
- LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"{newName} already exist, rename failed");
- return false;
- }
- else
- {
- File.Move(path, GenerateSequenceFilePath(SequenceFolder, newName));
- LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"sequence {oldName} renamed to {newName}");
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- return false;
- }
- return true;
- }
- public string GetSequenceFormatXml()
- {
- return GetSequenceConfig("/Aitex/TableSequenceFormat");
- }
- internal bool DeleteSequenceFolder(string folderName)
- {
- try
- {
- Directory.Delete(PathManager.GetRecipeDir() + SequenceFolder + "\\" + folderName, true);
- LOG.Write(eEvent.EV_SEQUENCE,ModuleName.System, "Folder " + folderName + "deleted");
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, "delete sequence folder exception");
- LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"can not delete folder {folderName}, {ex.Message}");
- return false;
- }
- return true;
- }
- internal bool CreateSequenceFolder(string folderName)
- {
- try
- {
- Directory.CreateDirectory(PathManager.GetRecipeDir() + SequenceFolder + "\\" + folderName);
- LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, "Folder " + folderName + "created");
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, "sequence folder create exception");
- LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"can not create folder {folderName}, {ex.Message}");
- return false;
- }
- return true;
- }
- internal bool RenameSequenceFolder(string oldName, string newName)
- {
- try
- {
- string oldPath = PathManager.GetRecipeDir() + SequenceFolder + "\\" + oldName;
- string newPath = PathManager.GetRecipeDir() + SequenceFolder + "\\" + newName;
- Directory.Move(oldPath, newPath);
- LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, $"rename folder from {oldName} to {newName}");
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, "rename sequence folder failed");
- LOG.Write(eEvent.WARN_SEQUENCE, ModuleName.System, $"can not rename folder {oldName}, {ex.Message}");
- return false;
- }
- return true;
- }
- public string GetXmlSequenceList(string chamberId)
- {
- XmlDocument doc = new XmlDocument();
- DirectoryInfo curFolderInfo = new DirectoryInfo(PathManager.GetRecipeDir() + SequenceFolder + "\\");
- doc.AppendChild(GenerateSequenceList(chamberId, curFolderInfo, doc));
- return doc.OuterXml;
- }
- XmlElement GenerateSequenceList(string chamberId, DirectoryInfo currentDir, XmlDocument doc)
- {
- int trimLength = (PathManager.GetRecipeDir() + SequenceFolder + "\\").Length;
- XmlElement folderEle = doc.CreateElement("Folder");
- folderEle.SetAttribute("Name", currentDir.FullName.Substring(trimLength));
- DirectoryInfo[] dirInfos = currentDir.GetDirectories();
- foreach (DirectoryInfo dirInfo in dirInfos)
- {
- folderEle.AppendChild(GenerateSequenceList(chamberId, dirInfo, doc));
- }
- FileInfo[] fileInfos = currentDir.GetFiles("*.seq");
- foreach (FileInfo fileInfo in fileInfos)
- {
- XmlElement fileNd = doc.CreateElement("File");
- string fileStr = fileInfo.FullName.Substring(trimLength).TrimStart(new char[] { '\\' }); ;
- fileStr = fileStr.Substring(0, fileStr.LastIndexOf("."));
- fileNd.SetAttribute("Name", fileStr);
- folderEle.AppendChild(fileNd);
- }
- return folderEle;
- }
- /// <summary>
- /// 加载
- /// </summary>
- /// <returns></returns>
- public List<DqdrRecipe> GetProctionDqdrRecipes()
- {
- List<string> lst = GetSequenceList("dqdr", "Production");
- List<DqdrRecipe> recipes = new List<DqdrRecipe>();
- foreach(string item in lst)
- {
- string path = $"{PathManager.GetRecipeDir()}{ProductionFolder}\\{item}.dqdr.rcp";
- DqdrRecipe recipe = LoadGenericityRecipe<DqdrRecipe>(path);
- if(recipe != null)
- {
- recipes.Add(recipe);
- }
- }
- return recipes;
- }
- /// <summary>
- /// 获取Sequence(Recipes)文件路径的子目录
- /// </summary>
- /// <returns></returns>
- public List<String> GetSequenceList(string recipeType, string sequenceType)
- {
-
- var result = new List<string>();
- try
- {
- string recipePath;
- if (sequenceType == "Engineering")
- {
- recipePath = PathManager.GetRecipeDir() + EngineeringFolder + "\\";
- }
- else if(sequenceType == "Production")
- {
- recipePath = PathManager.GetRecipeDir() + ProductionFolder + "\\";
- }
- else
- {
- return null;
- }
-
- var di = new DirectoryInfo(recipePath);
- var fis = di.GetFiles($"*.{recipeType}.*", SearchOption.TopDirectoryOnly);
- foreach (var fi in fis)
- {
- string str = fi.FullName.Substring(recipePath.Length);
- str = str.Substring(0, str.LastIndexOf($".{recipeType}"));
- result.Add(str);
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- }
- return result;
- }
- #endregion
- #region FA相关
- /// <summary>
- /// 获取recipe集合
- /// </summary>
- /// <returns></returns>
- public string[] GetProductionRecipeList()
- {
- List<string> lst = new List<string>();
- try
- {
- string path = PathManager.GetRecipeDir() + ProductionFolder + "\\";
- DirectoryInfo directoryInfo = new DirectoryInfo(path);
- FileInfo[] files = directoryInfo.GetFiles($"*.rcp", SearchOption.TopDirectoryOnly);
- foreach (var fi in files)
- {
- lst.Add($"{ProductionFolder}\\{fi.Name}");
- }
- }
- catch(Exception ex)
- {
- LOG.WriteExeption(ex);
- }
- return lst.ToArray();
- }
- /// <summary>
- /// 加载recipe内容
- /// </summary>
- /// <param name="recipeName"></param>
- /// <returns></returns>
- public string LoadRecipeContent(string recipeName)
- {
- string rcp = string.Empty;
- try
- {
- string path = PathManager.GetRecipeDir() + ProductionFolder + "\\";
- using (StreamReader fs = new StreamReader($"{PathManager.GetRecipeDir()}{ProductionFolder}\\{recipeName}"))
- {
- rcp = fs.ReadToEnd();
- fs.Close();
- }
- }
- catch (Exception ex)
- {
- LOG.WriteExeption($"load recipe file failed, {recipeName}", ex);
- rcp = string.Empty;
- }
- return rcp;
- }
- #endregion
- }
- }
|