using HslCommunication;
|
using HslCommunication.Core;
|
using Microsoft.VisualBasic.FileIO;
|
using Newtonsoft.Json;
|
using Quartz;
|
using SixLabors.ImageSharp.PixelFormats;
|
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.Threading.Tasks;
|
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
|
{
|
public Task Execute(IJobExecutionContext context)
|
{
|
try
|
{
|
#region
|
//CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
|
|
//Console.Out.WriteLine(commonStackerCrane.DeviceName + "状态:" + commonStackerCrane.StackerCraneStatusDes);
|
|
//Console.Out.WriteLine(commonStackerCrane.DeviceName + "手自动状态:" + commonStackerCrane.StackerCraneAutoStatusDes);
|
|
//Console.Out.WriteLine(commonStackerCrane.DeviceName + "作业状态:" + commonStackerCrane.StackerCraneWorkStatusDes);
|
|
//Console.Out.WriteLine(commonStackerCrane.DeviceName + "当前任务号:" + commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.CurrentTaskNum));
|
|
//commonStackerCrane.SetValue(StackerCraneDBName.CurrentTaskNum, DateTime.Now.Second);
|
|
//OperateResult<TimeSpan> operateResult = commonStackerCrane.Communicator.Wait("DB1.18", 1000, 60000, 1);
|
//Console.Out.WriteLine(operateResult.Serialize());
|
|
//byte[] bytes = commonStackerCrane.Communicator.Read("DB1.1", 2);
|
//Console.Out.WriteLine(bytes.ToBoolArray().ToList().Serialize());
|
|
//bool[] bools = new bool[] { true, false, true };
|
//byte[] bytes1 = bools.ToByteArray();
|
//Console.Out.WriteLine(bytes1.ToList().Serialize());
|
|
|
|
//Console.Out.WriteLine(DateTime.Now);
|
|
//Console.Out.WriteLine();
|
#endregion
|
Task.Run(() =>
|
{
|
while (true)
|
{
|
try
|
{
|
string date = DateTime.Now.ToString("yyyy-MM-dd");
|
string json = File.ReadAllText(AppContext.BaseDirectory + "Modified_Date.json", Encoding.UTF8);
|
var LogsTime = JsonConvert.DeserializeObject<TimeList>(json);
|
if (date != LogsTime.LogTime)
|
{
|
LogsTime.LogTime = date;
|
string modifiedJson = JsonConvert.SerializeObject(LogsTime);
|
File.WriteAllText("Modified_Date.json", modifiedJson);
|
string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
|
DateTime tenDaysAgo = DateTime.Now.AddDays(-14); // 获取当前时间的七天之前的日期
|
|
DeleteLogs(folderPath, tenDaysAgo);
|
}
|
|
Thread.Sleep(1000 * 60 * 60 * 24);
|
}
|
catch
|
{
|
Thread.Sleep(1000 * 60 * 60);
|
}
|
}
|
});
|
|
}
|
catch (Exception ex)
|
{
|
Console.Out.WriteLine(nameof(LogJob) + ":" + ex.Message);
|
}
|
|
return Task.CompletedTask;
|
}
|
public static void DeleteLogs(string path, DateTime dateTime)
|
{
|
DirectoryInfo directory = new DirectoryInfo(path);
|
foreach (FileSystemInfo file in directory.GetFileSystemInfos())
|
{
|
if (file is FileInfo && ((FileInfo)file).LastWriteTime < dateTime ||
|
file is DirectoryInfo && ((DirectoryInfo)file).CreationTime < dateTime)
|
{
|
try
|
{
|
if (file is FileInfo)
|
{
|
((FileInfo)file).Delete();
|
}
|
else if (file is DirectoryInfo)
|
{
|
DeleteLogs(file.FullName, dateTime);
|
((DirectoryInfo)file).Delete(true);
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine("无法删除文件或文件夹: " + file.FullName + "\n错误信息: " + ex.Message);
|
}
|
}
|
}
|
}
|
public class TimeList
|
{
|
public string MM { get; set; }
|
|
public string LogTime { get; set; }
|
}
|
}
|
|
|
}
|