PMMethods.cs 18 KB

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