From 5662395113163ac87425f23aa0bff8853be92462 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期三, 06 三月 2024 10:46:27 +0800
Subject: [PATCH] 优化逻辑

---
 代码管理/PCS/WCS_Server/WIDESEA_WCS/Jobs/equipment/PipelineJob.cs |  179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 174 insertions(+), 5 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_WCS/Jobs/equipment/PipelineJob.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_WCS/Jobs/equipment/PipelineJob.cs"
index 3804ccf..3323394 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_WCS/Jobs/equipment/PipelineJob.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_WCS/Jobs/equipment/PipelineJob.cs"
@@ -1,10 +1,26 @@
-锘縰sing Quartz;
+锘縰sing HslCommunication;
+using Microsoft.AspNetCore.Mvc;
+using Newtonsoft.Json;
+using Quartz;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using System.Xml.Linq;
+using WIDESEA_Comm;
+using WIDESEA_Comm.LogInfo;
+using WIDESEA_Core.BaseProvider;
+using WIDESEA_Core.EFDbContext;
+using WIDESEA_Core.Extensions;
+using WIDESEA_Core.FreeDB;
+using WIDESEA_Entity.DomainModels;
+using WIDESEA_WCS.IRepositories;
+using WIDESEA_WCS.Jobs;
+using WIDESEA_WCS.Repositories;
 using WIDESEA_WCS.WCSClient;
+using static System.Collections.Specialized.BitVector32;
 
 namespace WIDESEA_WCS
 {
@@ -12,7 +28,7 @@
     /// 閾炬潯鏈�
     /// </summary>
     [DisallowConcurrentExecution]
-    public class PipelineJob : IJob
+    public class PipelineJob : JobBase, IJob
     {
         public Task Execute(IJobExecutionContext context)
         {
@@ -31,15 +47,168 @@
                     return Task.CompletedTask;
                 }
 
-                DoAction(client);
+                //DoAction(client);
+                ExecuteJob(context, DoAction);
             }
             catch { }
             return Task.CompletedTask;
         }
 
-        private void DoAction(PLCClient client)
+        private void DoAction(IJobExecutionContext context)
         {
-            throw new NotImplementedException();
+            var client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
+            //鑷姩閲嶈繛
+            if (!client.IsConnected)
+            {
+                client.Connect();
+                return;
+            }
+            Loadinglevel(client);
+        }
+        /// <summary>
+        /// 涓婃枡鍖�
+        /// </summary>
+        /// <param name="client"></param>
+        private void Loadinglevel(PLCClient client, string number = "涓婃枡鍖�")
+        {
+            try
+            {
+                VOLContext Context = new VOLContext();
+                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
+                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
+                Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
+                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
+                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
+                var Gantry_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "妗佹灦");
+                if (Gantry_client == null) throw new Exception("妗佹灦璋冨害鏈嶅姟鏈紑鍚紒");
+                if (!Gantry_client.IsConnected) throw new Exception("涓庢鏋惰繛鎺ヨ秴鏃讹紒");
+                var Gantryplc = repository.FindFirst(x => x.plcinfo_name == Gantry_client.PLCName);
+
+
+                ///鏌ユ壘涓婃枡鍖虹殑璐т綅
+                var Stations = stationinfoRepository.Find(x => x.area == area_code(number));
+                //缂撳瓨鏋舵湭鍚敤绂佹妗佹灦杩涘叆
+                foreach (var station in Stations)
+                {
+                    if (!station.enable)
+                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//淇″彿涓篺alse妗佹灦鍋滄杩涘叆
+                }
+
+                var Station = Stations?.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).OrderBy(x => x.quantity).FirstOrDefault();
+                if (Station != null)
+                {
+                    var SNS = Station.bindSN.Split(",");
+                    List<string> list = new List<string>();
+                    foreach (var SN in SNS)
+                    {
+                        if (!string.IsNullOrEmpty(SN))
+                            list.Add(SN);
+                    }
+                    //缂撳瓨鏋朵笂杞﹁疆鏁伴噺涓嶴N鍙锋暟閲忎笉涓�鑷�
+                    if (list.Count != Station.quantity)
+                    {
+                        Station.location_state = LocationStateEnum.Abnormal.ToString();
+                        stationinfoRepository.Update(Station, true);
+                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//淇″彿涓篺alse妗佹灦鍋滄杩涘叆
+                        throw new Exception("涓婃枡浣嶈溅杞暟閲忎笌SN鍙锋暟閲忎笉涓�鑷达紝涓婃枡浣嶇紪鍙凤細" + Station.stationCode);
+                    }
+                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == Station.stationCode).ToList();
+                    var PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//璇诲彇鎵樼洏淇″彿:1:鏈�,2鏃�
+                    var MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//璇诲彇璐х墿淇″彿:1:鏈�,2鏃�
+                    if (PalletSignal == 1 && MaterialSignal == 1)
+                    {
+                        var area = Convert.ToInt16(Station.stationCode.Substring(Station.stationCode.Length - 1, 1));
+                        Gantry_client.WriteByOrder("W_AreaNr", (Int16)area, number);//鍖哄煙璐т綅鍙�
+                        Gantry_client.WriteByOrder("W_IndexNr", (Int16)SNS.Length, number);//鎵樼洏涓婄殑绗嚑涓溅杞�
+                        Gantry_client.WriteByOrder("W_Storage_Type", (Int16)1, number); //鎵樼洏绫诲瀷1-妯斁;2-绔栨斁
+                        Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)Convert.ToInt16(QueryMateriel(Station.stationType).TypeId), number);//杞﹁疆绫诲瀷
+                        Gantry_client.WriteByOrder("W_Wheel_id", SNS[SNS.Length - 1], number);//杞﹁疆SN鍙�
+                        Gantry_client.WriteByOrder("W_RequestUnload", true, number);
+                    }
+                    else
+                    {
+                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);
+                    }
+
+                    var Gantrydetails = plcRepository.Find(x => x.plcdetail_iotype == Gantryplc.plcinfo_iotyep && x.plcdetail_number == number).ToList();
+                    var Gantry_Out_of_Area = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Gantry_Out_of_Area").First(), Gantry_client);//妗佹灦鏄惁鍦ㄥ尯鍩熷唴
+
+                    var QueryDate = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_QueryDate").First(), Gantry_client);//淇℃伅鏌ヨ
+                    if (QueryDate)
+                    {
+                        var Date_Vaild = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Date_Vaild").First(), Gantry_client);//淇℃伅纭
+                        if (!Date_Vaild)//淇℃伅鏈夎锛岃褰曟棩蹇�
+                        {
+                            Station.location_state = LocationStateEnum.Abnormal.ToString();
+                            stationinfoRepository.Update(Station, true);
+                            Gantry_client.WriteByOrder("W_RequestUnload", false, number);//淇″彿涓篺alse妗佹灦鍋滄杩涘叆
+                            throw new Exception($"鏈煡璇㈠埌SN鍙凤細{SNS[SNS.Length - 1]}鐨勮鍗曪紝涓婃枡浣嶇紪鍙凤細{Station.stationCode}");
+                        }
+                    }
+                    var finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);//澶瑰彇瀹屾垚
+                    var updatefinished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "W_Storage_update").First(), Gantry_client);//璐т綅鐘舵�佹洿鏂�
+                    if (!finished && updatefinished)
+                        Gantry_client.WriteByOrder("W_Storage_update", false, number);//璐т綅鐘舵�佹洿鏂�
+                    if (finished && !updatefinished)
+                    {
+                        Station.quantity = Station.quantity - 1;
+                        Station.bindSN = OperStr(SNS);
+                        if (Station.quantity <= 0)
+                        {
+                            Station.stationType = string.Empty;
+                            //Station.location_state = LocationStateEnum.Empty.ToString();
+                            Station.Number = string.Empty;
+                            Station.heatNumber = string.Empty;
+                        }
+                        var count = stationinfoRepository.Update(Station, true);
+                        if (count < 1)
+                            throw new Exception($"涓婃枡浣嶄俊鎭洿鏂板け璐ワ紒涓婃枡浣嶇紪鍙凤細{Station.stationCode}");
+                        Gantry_client.WriteByOrder("W_Storage_update", true, number);//璐т綅鐘舵�佹洿鏂�
+                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//淇″彿涓篺alse妗佹灦鍋滄杩涘叆
+                        //finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);
+                        //while (finished)
+                        //{
+                        //    finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);
+                        //    Gantry_client.WriteByOrder("W_Storage_update", false, number);//璐т綅鐘舵�佹洿鏂�
+                        //}
+                    }
+                    //else if (!finished && updatefinished)
+                    //    Gantry_client.WriteByOrder("W_Storage_update", false, number);//璐т綅鐘舵�佹洿鏂�
+
+                }
+            }
+            catch (Exception ex)
+            {
+                //WritePCSLog.LogAdd(requestin.AreaNr.ToString(), respone.success == 1 ? "鎴愬姛 " : "澶辫触", "WMS", "AGV", json, JsonConvert.SerializeObject(respone), remark, "妫�娴嬬嚎涓婃枡鍖�", ex.Message);
+            }
+        }
+        public static string area_code(string area_name)
+        {
+            FreeDB freeDB = new FreeDB();
+            var areainfo = freeDB.Select<dt_areainfo>().Where(x => x.area_name == area_name).First();
+            return areainfo.area_code.ToString();
+        }
+
+        private string OperStr(string[] strArrty)
+        {
+            string[] newstr = strArrty.RemoveLast(1);
+            string Newsn = string.Join(",", newstr);
+
+            return Newsn;
+        }
+        public static dt_geometry_data QueryMateriel(string type)
+        { 
+            VOLContext Context = new VOLContext();
+            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
+            var materielinfo = dataRepository.FindFirst(x => x.Description == type);
+            return materielinfo;
+        }
+        public static string QueryMateriel(int  typeId)
+        {
+            VOLContext Context = new VOLContext();
+            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
+            var materielinfo = dataRepository.FindFirst(x => x.TypeId == typeId);
+            return materielinfo.Description;
         }
     }
 }

--
Gitblit v1.9.3