| | |
| | | using 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; |
| | | 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; |
| | | 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> |
| | | /// åªæ¸
çLogç®å½ä¸çæä»¶ |
| | | /// </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($"è·³è¿éLogç®å½æä»¶: {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}"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | } |