From 78d5eca8ea11c60ac88ebf133329fddad7c2aad2 Mon Sep 17 00:00:00 2001
From: huanghongfeng <huanghongfeng@hnkhzn.com>
Date: 星期一, 22 九月 2025 10:20:26 +0800
Subject: [PATCH] 任务

---
 代码管理/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs |  225 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 183 insertions(+), 42 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
index ebb0563..684b1e5 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
@@ -1,75 +1,216 @@
-锘縰sing HslCommunication;
-using HslCommunication.Core;
-using HslCommunication.WebSocket;
-using Microsoft.AspNetCore.Http;
-using Microsoft.VisualBasic.FileIO;
-using Newtonsoft.Json;
-using Quartz;
-using Quartz.Util;
-using SixLabors.ImageSharp.PixelFormats;
+锘縰sing 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;
 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
 {
     [DisallowConcurrentExecution]
     public class LogJob : IJob
     {
-        static object sendData = null;
+        // 瀹氫箟鍏佽鍒犻櫎鐨勬牴鐩綍
+        private static readonly string LogRootPath = Path.GetFullPath(Environment.CurrentDirectory + "/Log");
+        private static readonly string PlatformInfoPath = Path.GetFullPath(Environment.CurrentDirectory + "/Log/绔欏彴璇诲彇淇℃伅璁板綍");
+
         public Task Execute(IJobExecutionContext context)
         {
             try
             {
-               
                 Task.Run(() =>
                 {
                     try
                     {
-                        while (true)
-                        {
-                            Run(5);
-                        }
-                    }
-                    catch { }
-                });
+                            // 娓呯悊鏃ュ織鏂囦欢
+                            CleanLogFilesOnly(LogRootPath, TimeSpan.FromMinutes(43200));
+                            CleanAllInPlatformInfo(PlatformInfoPath, TimeSpan.FromMinutes(43200));
 
+                            Thread.Sleep(1000 * 10); // 10绉掑悗鍐嶆鎵ц
+                    }
+                    catch (Exception ex)
+                    {
+                        Console.WriteLine($"鏃ュ織浠诲姟鎵ц寮傚父: {ex.Message}");
+                    }
+                });
             }
             catch (Exception ex)
             {
-                Console.Out.WriteLine(nameof(LogJob) + ":" + ex.Message);
+                Console.WriteLine($"{nameof(LogJob)} 鍒濆鍖栧紓甯�: {ex.Message}");
             }
 
             return Task.CompletedTask;
         }
-        private static void Run(int saveDays)
+
+        /// <summary>
+        /// 妫�鏌ヨ矾寰勬槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
+        /// </summary>
+        /// <param name="path">瑕佹鏌ョ殑璺緞</param>
+        /// <param name="allowedRoot">鍏佽鐨勬牴鐩綍</param>
+        /// <returns>鏄惁鍏佽鍒犻櫎</returns>
+        private static bool IsPathAllowed(string path, string allowedRoot)
         {
-            sendData = new { code = "1002", Messagest = $"杞﹁疆SN鍙凤細123123123" };
-            CacheData.WebSocket.PublishAllClientPayload(sendData.Serialize());
+            try
+            {
+                var fullPath = Path.GetFullPath(path);
+                return fullPath.StartsWith(allowedRoot, StringComparison.OrdinalIgnoreCase);
+            }
+            catch
+            {
+                return false;
+            }
         }
 
-        public class CacheData
+        /// <summary>
+        /// 鍙竻鐞哃og鐩綍涓嬬殑鏂囦欢
+        /// </summary>
+        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
+        /// <param name="maxAge">鏂囦欢鏈�澶т繚鐣欐椂闂�</param>
+        private static void CleanLogFilesOnly(string directoryPath, TimeSpan maxAge)
         {
-            public static WebSocketServer WebSocket = new WebSocketServer();
-
-            public static void StartServer()
+            if (!Directory.Exists(directoryPath))
             {
-                WebSocket = new WebSocketServer();
-                WebSocket.ServerStart(1880);
+                //Console.WriteLine($"鐩綍涓嶅瓨鍦�: {directoryPath}");
+                return;
+            }
+
+            try
+            {
+                var now = DateTime.Now;
+                // 鍙幏鍙栧綋鍓嶇洰褰曠殑鏂囦欢锛屼笉鍖呭惈瀛愮洰褰�
+                var files = Directory.GetFiles(directoryPath, "*", SearchOption.TopDirectoryOnly);
+                int deletedCount = 0;
+
+                foreach (var file in files)
+                {
+                    try
+                    {
+                        // 妫�鏌ユ枃浠舵槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
+                        if (!IsPathAllowed(file, LogRootPath))
+                        {
+                            Console.WriteLine($"璺宠繃闈濴og鐩綍鏂囦欢: {file}");
+                            continue;
+                        }
+
+                        // 鑾峰彇鏂囦欢鍒涘缓鏃堕棿
+                        var creationTime = File.GetCreationTime(file);
+
+                        // 妫�鏌ユ枃浠舵槸鍚﹁秴杩囨寚瀹氭椂闂�
+                        if (now - creationTime > maxAge)
+                        {
+                            File.Delete(file);
+                            deletedCount++;
+                            //Console.WriteLine($"宸插垹闄ゆ枃浠�: {file}");
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        Console.WriteLine($"鍒犻櫎鏂囦欢 {file} 鏃跺嚭閿�: {ex.Message}");
+                    }
+                }
+
+                //Console.WriteLine($"鐩綍 {directoryPath} 娓呯悊瀹屾垚锛屽垹闄や簡 {deletedCount} 涓枃浠�");
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"娓呯悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 娓呯悊绔欏彴璇诲彇淇℃伅璁板綍鐩綍涓嬬殑鎵�鏈夊唴瀹瑰拰瀛愮洰褰�
+        /// </summary>
+        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
+        /// <param name="maxAge">鏂囦欢鏈�澶т繚鐣欐椂闂�</param>
+        private static void CleanAllInPlatformInfo(string directoryPath, TimeSpan maxAge)
+        {
+            if (!Directory.Exists(directoryPath))
+            {
+                //Console.WriteLine($"鐩綍涓嶅瓨鍦�: {directoryPath}");
+                return;
+            }
+
+            try
+            {
+                var now = DateTime.Now;
+                int deletedCount = 0;
+
+                // 鍒犻櫎鎵�鏈夎繃鏈熺殑鏂囦欢
+                var files = Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
+                foreach (var file in files)
+                {
+                    try
+                    {
+                        // 妫�鏌ユ枃浠舵槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
+                        if (!IsPathAllowed(file, PlatformInfoPath))
+                        {
+                            Console.WriteLine($"璺宠繃闈炵珯鍙颁俊鎭洰褰曟枃浠�: {file}");
+                            continue;
+                        }
+
+                        var creationTime = File.GetCreationTime(file);
+                        if (now - creationTime > maxAge)
+                        {
+                            File.Delete(file);
+                            deletedCount++;
+                            //Console.WriteLine($"宸插垹闄ゆ枃浠�: {file}");
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        Console.WriteLine($"鍒犻櫎鏂囦欢 {file} 鏃跺嚭閿�: {ex.Message}");
+                    }
+                }
+
+                // 鍒犻櫎鎵�鏈夌┖鐩綍锛堝寘鎷瓙鐩綍锛�
+                DeleteAllEmptySubdirectories(directoryPath);
+
+                //Console.WriteLine($"鐩綍 {directoryPath} 娓呯悊瀹屾垚锛屽垹闄や簡 {deletedCount} 涓枃浠�");
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"娓呯悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 閫掑綊鍒犻櫎鎵�鏈夌┖瀛愮洰褰�
+        /// </summary>
+        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
+        private static void DeleteAllEmptySubdirectories(string directoryPath)
+        {
+            if (!Directory.Exists(directoryPath))
+                return;
+
+            try
+            {
+                // 閫掑綊澶勭悊鎵�鏈夊瓙鐩綍
+                foreach (var subDirectory in Directory.GetDirectories(directoryPath))
+                {
+                    // 妫�鏌ョ洰褰曟槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
+                    if (!IsPathAllowed(subDirectory, PlatformInfoPath))
+                    {
+                        Console.WriteLine($"璺宠繃闈炵珯鍙颁俊鎭洰褰�: {subDirectory}");
+                        continue;
+                    }
+
+                    DeleteAllEmptySubdirectories(subDirectory);
+
+                    // 妫�鏌ュ瓙鐩綍鏄惁涓虹┖
+                    var files = Directory.GetFiles(subDirectory);
+                    var directories = Directory.GetDirectories(subDirectory);
+
+                    if (files.Length == 0 && directories.Length == 0)
+                    {
+                        Directory.Delete(subDirectory);
+                        //Console.WriteLine($"宸插垹闄ょ┖鐩綍: {subDirectory}");
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"澶勭悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
             }
         }
     }
-
-
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3