PMMethods.cs 20 KB

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