PMMethods.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. using Aitex.Common.Util;
  2. using Aitex.Core.Common;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.DBCore;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Device.Unit;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.Routine;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.UI.Control;
  15. using Aitex.Core.Util;
  16. using Caliburn.Micro.Core;
  17. using DocumentFormat.OpenXml.Wordprocessing;
  18. using FurnaceRT.Devices;
  19. using FurnaceRT.Equipments.Boats;
  20. using FurnaceRT.Equipments.Systems;
  21. using FurnaceRT.Equipments.WaferRobots;
  22. using FurnaceRT.FAs;
  23. using MECF.Framework.Common.CommonData.EnumData;
  24. using MECF.Framework.Common.DataCenter;
  25. using MECF.Framework.Common.Device;
  26. using MECF.Framework.Common.Device.Bases;
  27. using MECF.Framework.Common.Equipment;
  28. using MECF.Framework.Common.Extens;
  29. using MECF.Framework.Common.FAServices;
  30. using MECF.Framework.Common.Jobs;
  31. using MECF.Framework.Common.SubstrateTrackings;
  32. using MECF.Framework.Common.Utilities;
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Data;
  36. using System.IO;
  37. using System.Linq;
  38. using System.Text;
  39. using System.Threading;
  40. using System.Threading.Tasks;
  41. using System.Xml.Linq;
  42. using TwinCAT.Ads.Internal;
  43. using static Aitex.Core.RT.Device.Unit.IoBoat;
  44. using static log4net.Appender.RollingFileAppender;
  45. namespace FurnaceRT.Equipments.PMs
  46. {
  47. /// <summary>
  48. /// 分布类 定义各种方法
  49. /// </summary>
  50. public partial class PMModule
  51. {
  52. private static Dictionary<string, Dictionary<string, string>> _allWaferTypeNode = new Dictionary<string, Dictionary<string, string>>();
  53. private void InitOtherData()
  54. {
  55. DATA.Subscribe($"System.CompareFileDataA", () => _compareADic, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  56. DATA.Subscribe($"System.CompareFileDataB", () => _compareBDic, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  57. DATA.Subscribe($"System.SCDataLastWriteTime", () => GetSCDataLastWriteTime(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  58. DATA.Subscribe($"System.BackUpFileData", () => GetAllBackUpFiles(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  59. }
  60. private void InitOtherOP()
  61. {
  62. OP.Subscribe($"System.BackUpFileData", (string cmd, object[] args) =>
  63. {
  64. BackUpFileDataMethod();
  65. return true;
  66. });
  67. OP.Subscribe($"System.CreateZIP", (string cmd, object[] args) =>
  68. {
  69. CreateZIPMethod();
  70. return true;
  71. });
  72. OP.Subscribe($"System.CompareFileData", CompareFileDataMethod);
  73. OP.Subscribe($"System.RollBackFileData", RollBackFileDataMethod);
  74. }
  75. #region 备份/ZIP
  76. private Dictionary<string, string> _compareADic = new Dictionary<string, string>();
  77. private Dictionary<string, string> _compareBDic = new Dictionary<string, string>();
  78. private int _backUpFileMaxNumber = 10;
  79. private Dictionary<string, List<string>> GetAllBackUpFiles()
  80. {
  81. Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
  82. string sourcePath = $"{PathManager.GetCfgDir()}";
  83. string backUpFolderPath = Path.Combine(sourcePath, BackUpDireEnum.BackUp.ToString());
  84. if (!Directory.Exists(backUpFolderPath))
  85. {
  86. Directory.CreateDirectory(backUpFolderPath);
  87. }
  88. var allDires = new DirectoryInfo(backUpFolderPath).GetDirectories();
  89. foreach (var item in allDires)
  90. {
  91. var direFiles = item.GetFiles().OrderByDescending(a => a.CreationTime).Select(a => a.Name).ToList();
  92. if (result.ContainsKey(item.Name))
  93. {
  94. result[item.Name] = direFiles;
  95. }
  96. else
  97. {
  98. result.Add(item.Name, direFiles);
  99. }
  100. }
  101. return result;
  102. }
  103. private void CreateZIPMethod()
  104. {
  105. _backUpFileMaxNumber = SC.GetValue<bool>("System.IsSimulatorMode") ? 3 : SC.GetValue<int>("System.BackUpFileMaxNumber");
  106. var startupPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion;
  107. string sourcePath = Path.GetDirectoryName(PathManager.GetAppDir());
  108. string zipPath = $"{PathManager.GetAppDir()}\\ZIP";
  109. if (SC.ContainsItem("System.ZIPToDesktop") && SC.GetValue<bool>("System.ZIPToDesktop"))
  110. zipPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\ZIP";
  111. string timestamp = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
  112. string zipAllPath = $"{zipPath}\\{timestamp}_{startupPath}.zip";
  113. if (!Directory.Exists(zipPath))
  114. Directory.CreateDirectory(zipPath);
  115. var allFiles = Directory.GetFiles(zipPath).OrderBy(f => new FileInfo(f).CreationTime).ToArray();
  116. if (allFiles.Count() >= _backUpFileMaxNumber)
  117. File.Delete(allFiles[0]);
  118. if (File.Exists(zipAllPath))
  119. File.Delete(zipAllPath);
  120. CreateZIP(sourcePath, zipAllPath);
  121. }
  122. private async void CreateZIP(string sourcePath, string zipAllPath)
  123. {
  124. await ZIPUtil.ZipAllExceptLogFolderAsync(sourcePath, zipAllPath);
  125. }
  126. private bool RollBackFileDataMethod(out string reason, int time, object[] param)
  127. {
  128. reason = string.Empty;
  129. if (param == null || param.Length == 0)
  130. return true;
  131. //先备份当前数据
  132. BackUpFileDataMethod();
  133. //在进行RollBack
  134. string sourcePath = $"{PathManager.GetCfgDir()}";
  135. var scDataPath = $"{sourcePath}\\_sc.data";
  136. var rollBackScDataPath = $"{sourcePath}\\{BackUpDireEnum.BackUp}\\{BackUpDireEnum.SC}\\{param[0]}";
  137. if (!File.Exists(rollBackScDataPath))
  138. return false;
  139. if (File.Exists(scDataPath))
  140. File.Delete(scDataPath);
  141. File.Copy(rollBackScDataPath, scDataPath, true);
  142. return true;
  143. }
  144. private bool CompareFileDataMethod(out string reason, int time, object[] param)
  145. {
  146. reason = string.Empty;
  147. if (param == null || param.Count() == 0)
  148. return true;
  149. string sourcePath = $"{Directory.GetCurrentDirectory()}";
  150. var dataPara = param[0].ToString().Split(',');
  151. if (dataPara.Count() == 1)
  152. {
  153. var compareAPath = $"{PathManager.GetCfgDir()}\\{BackUpDireEnum.BackUp}\\{BackUpDireEnum.SC}\\{dataPara[0]}";
  154. var compareBPath = $"{PathManager.GetCfgDir()}\\_sc.data";
  155. _compareADic = GetScDataByFilePath(compareAPath);
  156. _compareBDic = GetScDataByFilePath(compareBPath);
  157. }
  158. else if (dataPara.Count() == 2)
  159. {
  160. var compareAPath = $"{PathManager.GetCfgDir()}\\{BackUpDireEnum.BackUp}\\{BackUpDireEnum.SC}\\{dataPara[0]}";
  161. var compareBPath = $"{PathManager.GetCfgDir()}\\{BackUpDireEnum.BackUp}\\{BackUpDireEnum.SC}\\{dataPara[1]}";
  162. _compareADic = GetScDataByFilePath(compareAPath);
  163. _compareBDic = GetScDataByFilePath(compareBPath);
  164. }
  165. return true;
  166. }
  167. private string GetSCDataLastWriteTime()
  168. {
  169. var compareBPath = $"{PathManager.GetCfgDir()}\\_sc.data";
  170. return File.GetLastWriteTime(compareBPath).ToString("yyyy-MM-dd HH-mm-ss");
  171. }
  172. private Dictionary<string, string> GetScDataByFilePath(string filePath)
  173. {
  174. Dictionary<string, string> result = new Dictionary<string, string>();
  175. try
  176. {
  177. // 加载XML文件
  178. XDocument xmlDoc = XDocument.Load(filePath);
  179. // 获取所有的scdata元素
  180. var scDataElements = xmlDoc.Descendants("scdata");
  181. // 遍历每个scdata元素并打印其属性
  182. foreach (var element in scDataElements)
  183. {
  184. string name = element.Attribute("name")?.Value ?? "N/A";
  185. string value = element.Attribute("value")?.Value ?? "N/A";
  186. if (result.ContainsKey(name))
  187. {
  188. result[name] = value;
  189. }
  190. else
  191. {
  192. result.Add(name, value);
  193. }
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. // 如果发生异常,捕获并显示错误消息
  199. Console.WriteLine("Error reading file: " + ex.Message);
  200. }
  201. return result;
  202. }
  203. private async void BackUpFileDataMethod()
  204. {
  205. _backUpFileMaxNumber = SC.GetValue<bool>("System.IsSimulatorMode") ? 3 : SC.GetValue<int>("System.BackUpFileMaxNumber");
  206. var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
  207. string sourcePath = $"{PathManager.GetAppDir()}";
  208. string newSourcePath = $"{PathManager.GetCfgDir()}";
  209. string backUpFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}");
  210. string scFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}\\{BackUpDireEnum.SC}");
  211. string ioFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}\\{BackUpDireEnum.IO}");
  212. string recipeFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}\\{BackUpDireEnum.Recipes}");
  213. string gasXmlFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}\\{BackUpDireEnum.GasXml}");
  214. string objectsFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}\\{BackUpDireEnum.Objects}");
  215. string parametersFolderPath = Path.Combine(newSourcePath, $"{BackUpDireEnum.BackUp}\\{BackUpDireEnum.Parameters}");
  216. // 检查 SC 文件夹并复制 sc.data 文件
  217. await CheckAndCopyFile(scFolderPath, Path.Combine(newSourcePath, "_sc.data"), $"{dateTime}@", "_sc.data");
  218. // 检查 IO 文件夹并复制 IO 文件夹内容
  219. await CheckAndCopyFolder(ioFolderPath, Path.Combine(newSourcePath, $"{BackUpDireEnum.IO}"), dateTime);
  220. // 检查 Recipe 文件夹并复制 Recipe 文件夹内容
  221. await CheckAndCopyFolder(recipeFolderPath, Path.Combine(sourcePath, $"{BackUpDireEnum.Recipes}"), dateTime);
  222. // 检查 GasXml 文件夹并复制 GasXml 文件夹内容
  223. await CheckAndCopyFolder(gasXmlFolderPath, Path.Combine($"{newSourcePath}", $"{BackUpDireEnum.GasXml}"), dateTime);
  224. // 检查 Objects 文件夹并复制 Objects 文件夹内容
  225. await CheckAndCopyFolder(objectsFolderPath, Path.Combine(sourcePath, $"{BackUpDireEnum.Objects}"), dateTime);
  226. // 检查 Parameters 文件夹并复制 Parameters 文件夹内容
  227. await CheckAndCopyFolder(parametersFolderPath, Path.Combine(sourcePath, $"{BackUpDireEnum.Parameters}"), dateTime);
  228. GetAllBackUpFiles();
  229. }
  230. private async Task CheckAndCopyFile(string targetFolderPath, string sourceFilePath, string prefixFileName, string fileName)
  231. {
  232. if (!Directory.Exists(targetFolderPath))
  233. Directory.CreateDirectory(targetFolderPath);
  234. var allpath = $"{prefixFileName}{fileName}";
  235. string destinationFilePath = Path.Combine(targetFolderPath, allpath);
  236. try
  237. {
  238. var filesWithSameName = Directory.GetFiles(targetFolderPath)
  239. .Where(f => Path.GetFileName(f).Contains(fileName))
  240. .OrderBy(f => new FileInfo(f).CreationTime)
  241. .ToArray();
  242. // If there are more than 10 files, delete the oldest one
  243. if (filesWithSameName.Length >= _backUpFileMaxNumber)
  244. {
  245. File.Delete(filesWithSameName[0]);
  246. }
  247. if (File.Exists(destinationFilePath))
  248. return;
  249. await Task.Run(() => File.Copy(sourceFilePath, destinationFilePath, true));
  250. }
  251. catch (Exception ex)
  252. {
  253. Console.WriteLine($"Error copying {fileName}: {ex.Message}");
  254. }
  255. }
  256. private async Task CheckAndCopyFolder(string targetFolderPath, string sourceFolderPath, string dateTime)
  257. {
  258. if (!Directory.Exists(targetFolderPath))
  259. Directory.CreateDirectory(targetFolderPath);
  260. if (!Directory.Exists(sourceFolderPath))
  261. {
  262. return;
  263. }
  264. try
  265. {
  266. await CopyDirectory(sourceFolderPath, targetFolderPath, dateTime);
  267. }
  268. catch (Exception ex)
  269. {
  270. Console.WriteLine($"Error copying folder {sourceFolderPath}: {ex.Message}");
  271. }
  272. }
  273. private async Task CopyDirectory(string sourceDirName, string destDirName, string dateTime)
  274. {
  275. // Get the subdirectories for the specified directory.
  276. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  277. if (!dir.Exists)
  278. {
  279. throw new DirectoryNotFoundException(
  280. "Source directory does not exist or could not be found: "
  281. + sourceDirName);
  282. }
  283. DirectoryInfo[] dirs = dir.GetDirectories();
  284. // If the destination directory doesn't exist, create it.
  285. if (!Directory.Exists(destDirName))
  286. Directory.CreateDirectory(destDirName);
  287. var newFies = new DirectoryInfo(destDirName).GetFiles().OrderBy(f => f.CreationTime).Select(a => a.Name).ToList();
  288. // Get the files in the directory and copy them to the new location.
  289. FileInfo[] files = dir.GetFiles();
  290. foreach (FileInfo file in files)
  291. {
  292. var sameFiles = newFies.Where(a => a.Contains(file.Name)).ToArray();
  293. if (sameFiles.Length >= _backUpFileMaxNumber)
  294. {
  295. File.Delete(Path.Combine(destDirName, sameFiles[0]));
  296. }
  297. var newName = $"{dateTime}@{file.Name}";
  298. if (newFies.Contains(newName))
  299. {
  300. continue;
  301. }
  302. string tempPath = Path.Combine(destDirName, newName);
  303. await Task.Run(() => file.CopyTo(tempPath, true));
  304. }
  305. // Recursively call CopyDirectory on each subdirectory.
  306. foreach (DirectoryInfo subdir in dirs)
  307. {
  308. string temppath = Path.Combine(destDirName, subdir.Name);
  309. await CopyDirectory(subdir.FullName, temppath, dateTime);
  310. }
  311. }
  312. #endregion
  313. #region WaferType配置顔色
  314. public void InitAllWaferTypeNode()
  315. {
  316. _allWaferTypeNode = SC.GetAllWaferTypeColor();
  317. }
  318. public string GetCarrierUIColor(string carrierType, CarrierStatus carrierStatus)
  319. {
  320. var defaultColor = "#ccc";
  321. if (carrierType == null || !_allWaferTypeNode.ContainsKey(carrierType))
  322. return defaultColor;
  323. if (!_allWaferTypeNode[carrierType].ContainsKey(carrierStatus.ToString()))
  324. return defaultColor;
  325. return _allWaferTypeNode[carrierType][carrierStatus.ToString()];
  326. }
  327. #endregion
  328. #region axis轴数据上传/下发
  329. private void AxisDataLoad(object[] args)
  330. {
  331. var trigDataLoad = DEVICE.GetDevice<IoTrigger>($"{ModuleName.PM1.ToString()}.TrigDataLoad");
  332. var trigDataSend = DEVICE.GetDevice<IoTrigger>($"{ModuleName.PM1.ToString()}.TrigDataSend");
  333. trigDataSend.SetTrigger(false, out _);
  334. trigDataLoad.SetTrigger(true, out _);
  335. Thread.Sleep(1500);
  336. var paras = args[0].ToString().Split(',');
  337. foreach (var item in paras)
  338. {
  339. var path = $"System.AxisParameters.{item}";
  340. if (!SC.ContainsItem(path))
  341. continue;
  342. var ioName = $"{ModuleName.PM1}.{item}";
  343. var dataValue = IO.AO[ioName].FloatValue;
  344. SC.SetItemValue(path, dataValue);
  345. }
  346. trigDataLoad.SetTrigger(false, out _);
  347. }
  348. private void AxisDataSend(object[] args)
  349. {
  350. var trigDataLoad = DEVICE.GetDevice<IoTrigger>($"{ModuleName.PM1.ToString()}.TrigDataLoad");
  351. var trigDataSend = DEVICE.GetDevice<IoTrigger>($"{ModuleName.PM1.ToString()}.TrigDataSend");
  352. trigDataSend.SetTrigger(true, out _);
  353. trigDataLoad.SetTrigger(false, out _);
  354. var paras = args[0].ToString().Split(',');
  355. foreach (var item in paras)
  356. {
  357. var path = $"System.AxisParameters.{item}";
  358. if (!SC.ContainsItem(path))
  359. continue;
  360. var ioName = $"{ModuleName.PM1}.{item}";
  361. var scDataValue = SC.GetValue<double>(path);
  362. IO.AO[ioName].FloatValue = (float)scDataValue;
  363. }
  364. trigDataSend.SetTrigger(false, out _);
  365. }
  366. #endregion
  367. #region RecipeAbnormalEnd相关
  368. public void RecipeAbnormalEnd(string module, string recipeName, string stepNumber, string recipeAction = "")
  369. {
  370. var pjId = Singleton<EquipmentManager>.Instance.GetFirstPJId();
  371. AddRecipeAbnormalEndDict(pjId, true, recipeAction);
  372. EV.Notify(UniversalEvents.RecipeAbnormalEnd, new SerializableDictionary<string, object>()
  373. {
  374. {DVIDName.RecipeID, recipeName }
  375. });
  376. }
  377. public void AddRecipeAbnormalEndDict(string pjId, bool isAbnormalEnd = false, string recipeAction = "")
  378. {
  379. if (string.IsNullOrEmpty(pjId))
  380. return;
  381. if (_recipeAbnormalEndDict != null)
  382. _recipeAbnormalEndDict.TryAddValue(pjId, new KeyValuePair<string, bool>(recipeAction, isAbnormalEnd), false);
  383. }
  384. public string GetRecipeAbnormalEndByPJId(string pjId)
  385. {
  386. if (_recipeAbnormalEndDict == null || _recipeAbnormalEndDict.Count == 0)
  387. return ConstantsCommon.NormalEnd;
  388. if (!_recipeAbnormalEndDict.TryGetValue(pjId, out var data))
  389. return ConstantsCommon.NormalEnd;
  390. _recipeAbnormalEndDict.Remove(pjId);
  391. if (data.Value)
  392. {
  393. LOG.Info($"RecipeAbnormalEnd:pjId={pjId},first trigger Action={data.Key}");
  394. return ConstantsCommon.AbnormalEnd;
  395. }
  396. return ConstantsCommon.NormalEnd;
  397. }
  398. #endregion
  399. private Dictionary<string, string> GetHeatersData()
  400. {
  401. Dictionary<string, string> result = new Dictionary<string, string>();
  402. if (_heaters == null || _heaters.Count == 0)
  403. {
  404. return result;
  405. }
  406. foreach (var item in _heaters)
  407. {
  408. result.Add(item.Display, item.TempFeedback.ToString("f1"));
  409. }
  410. return result;
  411. }
  412. public bool CheckRecipeIsCompleted(ProcessJobInfo pj)
  413. {
  414. var pm = Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM1] as PMModule;
  415. if (pm == null || pm.RecipeRunningInfo == null || pm.RecipeRunningInfo.RecipeStepList == null)
  416. {
  417. return true;
  418. }
  419. return pm.IsRecipeCompleted;
  420. }
  421. }
  422. public enum BackUpDireEnum
  423. {
  424. SC,
  425. GasXml,
  426. IO,
  427. Objects,
  428. Parameters,
  429. Recipes,
  430. BackUp,
  431. Config,
  432. }
  433. }