| | |
| | | using HslCommunication; |
| | | using HslCommunication.Core; |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.VisualBasic.FileIO; |
| | | using Newtonsoft.Json; |
| | | using Quartz; |
| | | using SixLabors.ImageSharp.PixelFormats; |
| | | using Quartz; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Reflection.Metadata.Ecma335; |
| | | using System.Text; |
| | | using System.IO; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Common.Log; |
| | | using WIDESEAWCS_Communicator; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_QuartzJob; |
| | | using WIDESEAWCS_QuartzJob.DeviceBase; |
| | | using WIDESEAWCS_QuartzJob.StackerCrane.Enum; |
| | | |
| | | namespace WIDESEAWCS_Tasks |
| | | { |
| | |
| | | { |
| | | try |
| | | { |
| | | |
| | | Task.Run(() => |
| | | { |
| | | try |
| | | { |
| | | WriteLog.GetLog("LogJob").Write($"æ¥å¿è®°å½ï¼{DateTime.Now}", "LogJob"); |
| | | Run(10); |
| | | Run2(10); |
| | | |
| | | // 设置ä¿ç天æ°ä¸º30天 |
| | | int saveDays = 30; |
| | | |
| | | // æ¸
çLogç®å½ä¸çææè¿ææä»¶å¤¹ |
| | | CleanLogDirectory(saveDays); |
| | | |
| | | Thread.Sleep(1000 * 10); |
| | | } |
| | | catch { } |
| | | catch (Exception ex) |
| | | { |
| | | WriteLog.GetLog("LogJob").Write($"æ¥å¿æ¸
çåºéï¼{ex.Message}", "LogJob"); |
| | | } |
| | | }); |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | |
| | | return Task.CompletedTask; |
| | | } |
| | | private static void Run(int saveDays) |
| | | |
| | | /// <summary> |
| | | /// æ¸
çLogç®å½ä¸çææè¿ææ¥å¿æä»¶å¤¹ |
| | | /// </summary> |
| | | /// <param name="saveDays">ä¿ç天æ°</param> |
| | | private static void CleanLogDirectory(int saveDays) |
| | | { |
| | | var logPath = System.Environment.CurrentDirectory + "/log"; |
| | | if (Directory.Exists(logPath)) |
| | | // è·åLogç®å½è·¯å¾ |
| | | var logBasePath = Path.Combine(Environment.CurrentDirectory, "Log"); |
| | | |
| | | if (!Directory.Exists(logBasePath)) |
| | | { |
| | | var nowTime = DateTime.Now; |
| | | var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories); |
| | | WriteLog.GetLog("LogJob").Write($"Logç®å½ä¸åå¨ï¼{logBasePath}", "LogJob"); |
| | | return; |
| | | } |
| | | |
| | | foreach (var dirpath in dayDir) |
| | | var nowTime = DateTime.Now; |
| | | var cutoffDate = nowTime.AddDays(-saveDays); |
| | | |
| | | WriteLog.GetLog("LogJob").Write($"å¼å§æ¸
çæ¥å¿ï¼ä¿ç天æ°ï¼{saveDays}ï¼æªæ¢æ¥æï¼{cutoffDate:yyyy-MM-dd}", "LogJob"); |
| | | |
| | | // è·åLogç®å½ä¸çææåç®å½ |
| | | var allSubDirs = Directory.GetDirectories(logBasePath, "*", SearchOption.AllDirectories); |
| | | |
| | | foreach (var dirPath in allSubDirs) |
| | | { |
| | | try |
| | | { |
| | | //æä»¶å¤¹å |
| | | var dirName = Path.GetFileName(dirpath).Replace("_", "-"); |
| | | // è·åæä»¶å¤¹å |
| | | var dirName = Path.GetFileName(dirPath); |
| | | |
| | | DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null); |
| | | if ((nowTime - targetDate).TotalDays >= saveDays) |
| | | // å°è¯è§£æä¸ºæ¥æï¼æ¯æå¤ç§æ ¼å¼ï¼ |
| | | if (TryParseDateFolder(dirName, out DateTime folderDate)) |
| | | { |
| | | |
| | | //å 餿件åæä»¶å¤¹ |
| | | try |
| | | // 夿æ¯å¦è¿æ |
| | | if (folderDate < cutoffDate) |
| | | { |
| | | var flies = Directory.GetFiles(dirpath); |
| | | WriteLog.GetLog("LogJob").Write($"å é¤è¿ææ¥å¿æä»¶å¤¹ï¼{dirPath}ï¼æ¥æï¼{folderDate:yyyy-MM-dd}", "LogJob"); |
| | | |
| | | foreach (var item in flies) |
| | | { |
| | | File.Delete(item); |
| | | } |
| | | Directory.Delete(dirpath); |
| | | // éå½å 餿件夹åå
¶å
容 |
| | | Directory.Delete(dirPath, true); |
| | | } |
| | | catch { } |
| | | else |
| | | { |
| | | WriteLog.GetLog("LogJob").Write($"ä¿çæ¥å¿æä»¶å¤¹ï¼{dirPath}ï¼æ¥æï¼{folderDate:yyyy-MM-dd}", "LogJob"); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | WriteLog.GetLog("LogJob").Write($"å¤çæä»¶å¤¹ {dirPath} æ¶åºéï¼{ex.Message}", "LogJob"); |
| | | } |
| | | } |
| | | |
| | | WriteLog.GetLog("LogJob").Write($"æ¥å¿æ¸
ç宿", "LogJob"); |
| | | } |
| | | |
| | | private static void Run2(int saveDays) |
| | | /// <summary> |
| | | /// å°è¯è§£ææ¥ææä»¶å¤¹åç§° |
| | | /// </summary> |
| | | /// <param name="folderName">æä»¶å¤¹åç§°</param> |
| | | /// <param name="date">è§£æåºçæ¥æ</param> |
| | | /// <returns>æ¯å¦æåè§£æ</returns> |
| | | private static bool TryParseDateFolder(string folderName, out DateTime date) |
| | | { |
| | | var logPath = System.Environment.CurrentDirectory + "/txt"; |
| | | if (Directory.Exists(logPath)) |
| | | // æ¯æçæ¥ææ ¼å¼ |
| | | string[] dateFormats = |
| | | { |
| | | var nowTime = DateTime.Now; |
| | | var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories); |
| | | "yyyy-MM-dd", // 2025-12-07 |
| | | "yyyyMMdd", // 20251207 |
| | | "yyyy_MM_dd", // 2025_12_07 |
| | | "yyyy.MM.dd", // 2025.12.07 |
| | | "yyyyå¹´MMæddæ¥" // 2025å¹´12æ07æ¥ |
| | | }; |
| | | |
| | | foreach (var dirpath in dayDir) |
| | | { |
| | | //æä»¶å¤¹å |
| | | var dirName = Path.GetFileName(dirpath).Replace("_", "-"); |
| | | // å°è¯è§£ææ¥æ |
| | | bool success = DateTime.TryParseExact( |
| | | folderName, |
| | | dateFormats, |
| | | System.Globalization.CultureInfo.InvariantCulture, |
| | | System.Globalization.DateTimeStyles.None, |
| | | out date |
| | | ); |
| | | |
| | | DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null); |
| | | if ((nowTime - targetDate).TotalDays >= saveDays) |
| | | { |
| | | |
| | | //å 餿件åæä»¶å¤¹ |
| | | try |
| | | { |
| | | var flies = Directory.GetFiles(dirpath); |
| | | |
| | | foreach (var item in flies) |
| | | { |
| | | File.Delete(item); |
| | | } |
| | | Directory.Delete(dirpath); |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | } |
| | | return success; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | } |