dengjunjie
2025-06-12 9429653e8def2729014b45c7c75ec76e9aa6685b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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; }
        }
    }
 
 
}