wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
wcs-server-hbf/WIDESEA_WCS/Jobs/AGV/AGVJob.cs
@@ -8,6 +8,7 @@
using System.Runtime.Serialization.Json;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Entity.DomainModels.Equipment;
using WIDESEA_Services.Services;
@@ -15,6 +16,7 @@
using WIDESEA_WCS.JobsPart.AGV;
using WIDESEA_WCS.SchedulerExecute.AGV;
using WIDESEA_WCS.WCSClient;
using static FreeSql.Internal.GlobalFilter;
namespace WIDESEA_WCS
{
@@ -24,6 +26,7 @@
    [DisallowConcurrentExecution]
    public class AGVJob : JobBase, IJob
    {
        private static object lockObj1 = 0;
        public Task Execute(IJobExecutionContext context)
        {
            try
@@ -37,90 +40,100 @@
        private void DoAction(IJobExecutionContext context)
        {
            try
            lock (lockObj1)
            {
                PLCClient client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
                AGVSchedulerExecute.SendAGVTask1(client);
                // 清除7日以上的Logs日志记录
                string date = DateTime.Now.ToString("yyyy-MM-dd");
                string json = File.ReadAllText(AppContext.BaseDirectory + "modified_data.json", Encoding.UTF8);
                var logstime = JsonConvert.DeserializeObject<timelist>(json);
                if (date != logstime.logtime)
                if (lockObj1.ToString() == "0")
                {
                    logstime.logtime = date;
                    string modifiedJson = JsonConvert.SerializeObject(logstime);
                    File.WriteAllText("modified_data.json", modifiedJson);
                    string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
                    DateTime tenDaysAgo = DateTime.Now.AddDays(-7); // 获取当前时间的七天之前的日期
                    DeleteFilesAndFolders(folderPath, tenDaysAgo);
                }
            }
            catch (Exception ex)
            {
                    try
                    {
                        lockObj1 = 1;
                        Task task = Task.Run(() =>
                        {
                            List<string> AGVS = new List<string> { "组装AGV调度", "正极AGV调度", "负极AGV调度" };
                            while (true)
                            {
                                foreach (var item in AGVS)
                                {
                                    PLCClient pLCClient = WCSService.Clients.Find(v => v.PLCName == item);
                                    if (pLCClient == null)
                                    {
                                        continue;
                                    }
                                    AGVSchedulerExecute.SendAGVTask1(pLCClient);
                                }
                                Thread.Sleep(200);
                            }
                        });
                throw new Exception(ex.Message);
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                try
                                {
                                    string date = DateTime.Now.ToString("yyyy-MM-dd");
                                    string json = File.ReadAllText(AppContext.BaseDirectory + "modified_data.json", Encoding.UTF8);
                                    var logstime = JsonConvert.DeserializeObject<timelist>(json);
                                    if (date != logstime.logtime)
                                    {
                                        logstime.logtime = date;
                                        string modifiedJson = JsonConvert.SerializeObject(logstime);
                                        File.WriteAllText("modified_data.json", modifiedJson);
                                        string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
                                        DateTime tenDaysAgo = DateTime.Now.AddDays(-7); // 获取当前时间的七天之前的日期
                                        DeleteFilesAndFolders(folderPath, tenDaysAgo);
                                    }
                                    Thread.Sleep(1000 * 60 * 60 * 24);
                                }
                                catch
                                {
                                    Thread.Sleep(1000 * 60 * 60);
                                }
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        lockObj1 = 0;
                        throw new Exception(ex.Message);
                    }
                }
            }
            
            // 清楚7日以上的Logs日志记录
            //try
            //{
            //    string date = DateTime.Now.ToString("yyyy-MM-dd");
            //    string json = File.ReadAllText(AppContext.BaseDirectory + "modified_data.json", Encoding.UTF8);
            //    var logstime = JsonConvert.DeserializeObject<timelist>(json);
            //    if (date != logstime.logtime)
            //    List<string> AGVS = new List<string> { "组装AGV调度", "正极AGV调度", "负极AGV调度", "组装AGV调度" };
            //    //foreach (var item in AGVS)
            //    {
            //        logstime.logtime = date;
            //        string modifiedJson = JsonConvert.SerializeObject(logstime);
            //        File.WriteAllText("modified_data.json", modifiedJson);
            //        string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            //        DateTime tenDaysAgo = DateTime.Now.AddDays(-7); // 获取当前时间的七天之前的日期
            //        DeleteFilesAndFolders(folderPath, tenDaysAgo);
            //        //PLCClient pLCClient = WCSService.Clients.Find(v => v.PLCName == item);
            //        //if (pLCClient == null)
            //        //{
            //        //    continue;
            //        //}
            //        PLCClient pLCClient = context.JobDetail.JobDataMap["JobParams"] as PLCClient;
            //        AGVSchedulerExecute.SendAGVTask1(pLCClient);
            //    }
            //    //string date = DateTime.Now.ToString("yyyy-MM-dd");
            //    //string json = File.ReadAllText(AppContext.BaseDirectory + "modified_data.json", Encoding.UTF8);
            //    //var logstime = JsonConvert.DeserializeObject<timelist>(json);
            //    //if (date != logstime.logtime)
            //    //{
            //    //    logstime.logtime = date;
            //    //    string modifiedJson = JsonConvert.SerializeObject(logstime);
            //    //    File.WriteAllText("modified_data.json", modifiedJson);
            //    //    string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            //    //    DateTime tenDaysAgo = DateTime.Now.AddDays(-7); // 获取当前时间的七天之前的日期
            //    //    DeleteFilesAndFolders(folderPath, tenDaysAgo);
            //    //}
            //}
            //catch (Exception)
            //catch (Exception ex)
            //{
            //    throw new Exception(ex.Message);
            //}
            #region
            //PLCClient client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
            //AGVServer agvServer = new AGVServer();
            //if (client != null)
            //{
            //    try
            //    {
            //        AGVSchedulerExecute.SendAGVTask1(client);
            //    }
            //    catch (Exception) { }
            //    try
            //    {
            //        agvServer.UpdateAgvTask(client);
            //    }
            //    catch (Exception) { }
            //    // 清楚7日以上的Logs日志记录
            //    try
            //    {
            //        string date = DateTime.Now.ToString("yyyy-MM-dd");
            //        string json = File.ReadAllText(AppContext.BaseDirectory + "modified_data.json", Encoding.UTF8);
            //        var logstime = JsonConvert.DeserializeObject<timelist>(json);
            //        if (date != logstime.logtime)
            //        {
            //            logstime.logtime = date;
            //            string modifiedJson = JsonConvert.SerializeObject(logstime);
            //            File.WriteAllText("modified_data.json", modifiedJson);
            //            string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            //            DateTime tenDaysAgo = DateTime.Now.AddDays(-7); // 获取当前时间的七天之前的日期
            //            DeleteFilesAndFolders(folderPath, tenDaysAgo);
            //        }
            //    }
            //    catch (Exception)
            //    {
            //        throw;
            //    }
            //}
            #endregion
        }
        static void DeleteFilesAndFolders(string path, DateTime dateTime)
@@ -141,7 +154,7 @@
                        else if (fileOrFolder is DirectoryInfo)
                        {
                            DeleteFilesAndFolders(fileOrFolder.FullName, dateTime);
                            ((DirectoryInfo)fileOrFolder).Delete();
                            ((DirectoryInfo)fileOrFolder).Delete(true);
                        }
                    }
                    catch (Exception ex)