From 8f392cc88b0768b74efca3b68785cf5aa1c38e70 Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期三, 06 十一月 2024 14:28:20 +0800
Subject: [PATCH] 代码更新

---
 wcs-server-hbf/WIDESEA_WCS/Jobs/AGV/AGVJob.cs |  157 ++++++++++++++++++++++++++++------------------------
 1 files changed, 85 insertions(+), 72 deletions(-)

diff --git a/wcs-server-hbf/WIDESEA_WCS/Jobs/AGV/AGVJob.cs b/wcs-server-hbf/WIDESEA_WCS/Jobs/AGV/AGVJob.cs
index 48fe9ee..6d4a4c7 100644
--- a/wcs-server-hbf/WIDESEA_WCS/Jobs/AGV/AGVJob.cs
+++ b/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璋冨害", "姝f瀬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璋冨害", "姝f瀬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)

--
Gitblit v1.9.3