PMMethods.cs 18 KB

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