PMMethods.cs 18 KB

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