PMMethods.cs 19 KB

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