分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-04-27 0b5ccdca6263cf7a2cee460f30c76ef1efea2811
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_WCS/Jobs/equipment/PipelineJob.cs
@@ -11,15 +11,18 @@
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.TaskNo;
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.JobsPart.Common;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static System.Collections.Specialized.BitVector32;
namespace WIDESEA_WCS
@@ -34,20 +37,6 @@
        {
            try
            {
                var client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
                if (client == null)
                {
                    return Task.CompletedTask;
                }
                //自动重连
                if (!client.IsConnected)
                {
                    client.Connect();
                    return Task.CompletedTask;
                }
                //DoAction(client);
                ExecuteJob(context, DoAction);
            }
            catch { }
@@ -63,7 +52,10 @@
                client.Connect();
                return;
            }
            client.WriteByOrder("W_Palpitate", client.ReadByOrder<bool>("R_Palpitate", "心跳"));
            Loadinglevel(client);
            AutoEmptyTray(client);
            StationState(client);
        }
        /// <summary>
        /// ä¸Šæ–™åŒº
@@ -86,13 +78,7 @@
                ///查找上料区的货位
                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);//信号为false桁架停止进入
                }
                var Stations = stationinfoRepository.Find(x => x.area == area_code(number)).OrderBy(x => x.lastUpdateTime);//根据时间先后排序
                var Station = Stations?.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).OrderBy(x => x.quantity).FirstOrDefault();
                if (Station != null)
@@ -108,22 +94,36 @@
                    if (list.Count != Station.quantity)
                    {
                        Station.location_state = LocationStateEnum.Abnormal.ToString();
                        Station.remark = "车轮数量与SN号数量不一致";
                        stationinfoRepository.Update(Station, true);
                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                        throw new Exception("上料位车轮数量与SN号数量不一致,上料位编号:" + Station.stationCode);
                        #region æ—¥å¿—记录
                        WriteDBLog.Error("上料区", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}车轮数量与SN号数量不一致", "PCS");
                        #endregion
                        return;
                    }
                    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无
                    var AreaNr = Gantry_client.ReadByOrder<Int16>("W_AreaNr", number);//读取货位号信息
                    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);
                        if (AreaNr == 0)
                        {
                            Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)Convert.ToInt16(QueryMateriel(Station.stationType) == null ? QueryMateriel1(Station.stationType).TypeId : QueryMateriel(Station.stationType).TypeId), number);//车轮类型
                            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_id", SNS[SNS.Length - 1], number);//车轮SN号
                            Gantry_client.WriteByOrder("W_RequestUnload", true, number);
                            #region æ—¥å¿—记录
                            WriteDBLog.Success("上料区申请", $"读取托盘光电信号:{PalletSignal}\n读取第一个车轮光电信号:{MaterialSignal}\n\n" +
                                $"写入桁架信息:\n区域货位号:{area}\n托盘上的第几个车轮:{SNS.Length}\n托盘类型:{1}\n车轮类型:{(QueryMateriel(Station.stationType).TypeId == null ? QueryMateriel1(Station.stationType).TypeId : QueryMateriel(Station.stationType).TypeId)}" +
                                $"\n车轮SN号:{SNS[SNS.Length - 1]}\nW_RequestUnload:true", "PCS");
                            #endregion
                        }
                    }
                    else
                    {
@@ -139,10 +139,12 @@
                        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);//信号为false桁架停止进入
                            throw new Exception($"未查询到SN号:{SNS[SNS.Length - 1]}的订单,上料位编号:{Station.stationCode}");
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = $"未查询到SN号:{SNS[SNS.Length - 1]}的订单";
                            stationinfoRepository.Update(Station, true);
                            WriteDBLog.Error("上料区信息查询", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}未查询到SN号:{SNS[SNS.Length - 1]}的订单", "PCS");
                            return;
                        }
                    }
                    var finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);//夹取完成
@@ -151,41 +153,225 @@
                        Gantry_client.WriteByOrder("W_Storage_update", false, number);//货位状态更新
                    if (finished && !updatefinished)
                    {
                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                        Updatemes(SNS[SNS.Length - 1]);
                        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;
                            Station.tray_status = "EmptyTray";
                        }
                        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);//信号为false桁架停止进入
                        //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);//货位状态更新
                        {
                            WriteDBLog.Error("取料完成", $"上料位信息更新失败!上料位编号:{Station.stationCode}", "PCS");
                            return;
                        }
                        #region æ¸…空信息
                        Gantry_client.WriteByOrder("W_AreaNr", (Int16)0, number);//区域货位号
                        Gantry_client.WriteByOrder("W_IndexNr", (Int16)0, number);//托盘上的第几个车轮
                        Gantry_client.WriteByOrder("W_Storage_Type", (Int16)0, number); //托盘类型1-横放;2-竖放
                        Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)0, number);//车轮类型
                        #endregion
                        Gantry_client.WriteByOrder("W_Storage_update", true, number);//货位状态更新
                        #region æ—¥å¿—记录
                        WriteDBLog.Success("取料完成", $"写入桁架信息:\n货位状态更新:{true}\nW_RequestUnload:{false}", "PCS");
                        #endregion
                        PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                        MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//读取货物信号:1:有,2无
                        if (Station.quantity > 0 && (PalletSignal != 1 || MaterialSignal != 1))
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = "车轮数量大于0,光电检测无托盘或无车轮";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, number);//报警
                            WriteDBLog.Error(number + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                        }
                        else if (Station.quantity < 1 && MaterialSignal == 1)
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = "车轮数量小于0,光电检测有车轮";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, number);//报警
                            WriteDBLog.Error(number + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                        }
                    }
                }
                else
                    Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("上料区", $"错误信息:{ex.Message}", "PCS");
            }
        }
        /// <summary>
        /// è‡ªåŠ¨å–ç©ºæ‰˜
        /// </summary>
        /// <param name="client"></param>
        private void AutoEmptyTray(PLCClient client)
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                Idt_agvtaskRepository agvtaskService = new dt_agvtaskRepository(Context);
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                List<string> stationCodes = new List<string>() { "S01001001", "S01001002", "W01001001", "W01001002", "W01001003" };
                foreach (var stationCode in stationCodes)
                {
                    if (agvtaskService.Find(x => x.agv_fromaddress == stationCode /*|| x.agv_toaddress == stationCode*/).Any())
                        continue;
                    var station = stationinfoRepository.Find(x => x.stationCode == stationCode && x.enable).FirstOrDefault();
                    if (station == null) continue;
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == 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 == 2 && (station.stationCode.Contains("S0100100") ? station.tray_status == "EmptyTray" : true))
                    {
                        var area = station.tray_type == "SmallTray" ? "11" : "10";
                        var task = agvtaskService.Find(x => (x.agv_toaddress.Contains("W") || x.agv_toaddress.Contains("X")) && x.agv_taskstate == "Queue" /*&& x.agv_tasktype == "TaskType_EmptyPallet" */ && x.agv_Traytype == station.tray_type).OrderByDescending(x => x.agv_grade).ThenBy(x => x.agv_createtime).FirstOrDefault();
                        if (task != null)
                        {
                            task.agv_fromaddress = station.stationCode;
                            task.StarQuantity = 0;
                            task.agv_taskstate = "Create";
                            if (station.stationCode.Contains("S0100100"))
                                task.agv_grade = 3;
                            agvtaskService.Update(task, true);
                            station.location_state = LocationStateEnum.Busy.ToString();
                            stationinfoRepository.Update(station, true);
                            continue;
                        }
                        #region ç©ºæ‰˜å ç›˜
                        var EmptyStations = stationinfoRepository.Find(x => x.stationCode.Contains("DD") && x.quantity < 5 && x.enable).ToList();
                        var EmptyStation = EmptyStations.Where(x => x.tray_type == station.tray_type && x.location_state == LocationStateEnum.Stroge.ToString()).FirstOrDefault();
                        if (EmptyStation == null)
                            EmptyStation = EmptyStations.Where(x => x.location_state == LocationStateEnum.Empty.ToString()).FirstOrDefault();
                        #endregion
                        if (EmptyStation == null)//查找库内空托盘
                            EmptyStation = GetStation.EmptyPalletStation(area);
                        if (EmptyStation != null)
                        {
                            #region åŒä¸€ä¸ªç©ºæ‰˜ä½åªèƒ½åŒæ—¶ç”Ÿæˆä¸€ä¸ªç©ºæ‰˜ä»»åŠ¡
                            if (agvtaskService.Find(x => x.agv_fromaddress == EmptyStation.stationCode || x.agv_toaddress == EmptyStation.stationCode).Any())
                                return;
                            #endregion
                            dt_agvtask agvtask = new dt_agvtask()
                            {
                                agv_fromaddress = station.stationCode,
                                agv_id = Guid.NewGuid(),
                                agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                agv_grade = station.stationCode.Contains("S0100100") ? 3 : 1,
                                agv_createtime = DateTime.Now,
                                agv_taskstate = "Create",
                                //agv_materielid = station.stationType,
                                agv_qty = 1,
                                StarQuantity = 0,
                                EndQuantity = EmptyStation.quantity,
                                agv_tasktype = "TaskType_EmptyPallet",
                                agv_toaddress = EmptyStation.stationCode,
                                agv_userid = "系统",
                                agv_TrayStatus = "EmptyTray",//station.tray_status,
                                agv_Traytype = station.tray_type,
                            };
                            agvtaskService.Add(agvtask, true);
                            station.location_state = LocationStateEnum.Busy.ToString();
                            stationinfoRepository.Update(station, true);
                            EmptyStation.location_state = LocationStateEnum.Busy.ToString();
                            stationinfoRepository.Update(EmptyStation, true);
                            WriteDBLog.Success("创建自动取空托任务", $"任务编号:{agvtask.agv_tasknum}", "PCS");
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //WritePCSLog.LogAdd(requestin.AreaNr.ToString(), respone.success == 1 ? "成功 " : "失败", "WMS", "AGV", json, JsonConvert.SerializeObject(respone), remark, "检测线上料区", ex.Message);
                WriteDBLog.Error("创建自动取空托任务", $"错误信息:{ex.Message}", "PCS");
            }
        }
        /// <summary>
        /// æ£€æµ‹å…‰ç”µçŠ¶æ€ä¸Žç³»ç»Ÿè´§ä½çŠ¶æ€æ˜¯å¦ä¸€è‡´
        /// </summary>
        /// <param name="client"></param>
        private void StationState(PLCClient client)
        {
            try
            {
                List<string> names = new List<string>() { "1单元下料区", "2单元下料区", "3单元下料区" };/*, "上料区"*/
                foreach (var name in names)
                {
                    VOLContext Context = new VOLContext();
                    Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                    var Stations = stationinfoRepository.Find(x => x.area == area_code(name) && x.enable && !x.location_state.Contains("Busy") && x.location_state != LocationStateEnum.Abnormal.ToString());
                    foreach (var Station in Stations)
                    {
                        var PalletSignal = client.ReadByOrder<Int16>("R_PalletSignal", Station.stationCode);//读取托盘信号:1:有,2无
                        var MaterialSignal = client.ReadByOrder<Int16>("R_MaterialSignal", Station.stationCode);//读取货物信号:1:有,2无
                        if (Station.quantity > 0 && (PalletSignal != 1 || MaterialSignal != 1))//系统货位有车轮,光电检测无车轮
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = "车轮数量大于0,光电检测无托盘或无车轮";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteDBLog.Error(name + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                            continue;
                        }
                        if (Station.location_state == LocationStateEnum.Stroge.ToString() && PalletSignal != 1)
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = "光电检测无托盘";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteDBLog.Error(name + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                            continue;
                        }
                        if (Station.location_state == LocationStateEnum.Empty.ToString() && (PalletSignal == 1 || MaterialSignal == 1))//系统货位为空,光电检测有料
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = "光电检测有料";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteDBLog.Error(name + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                            continue;
                        }
                        //if (Station.quantity < 1 && MaterialSignal == 1)//系统货位无车轮,光电检测有车轮
                        //{
                        //    Station.location_state = LocationStateEnum.Abnormal.ToString();
                        //    Station.remark = "货位无车轮,光电检测有车轮";
                        //    stationinfoRepository.Update(Station, true);
                        //    client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                        //    continue;
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                //throw;
            }
        }
        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();
            VOLContext Context = new VOLContext();
            Idt_areainfoRepository areainfoRepository = new dt_areainfoRepository(Context);
            var areainfo = areainfoRepository.Find(x => x.area_name == area_name).FirstOrDefault();
            return areainfo.area_code.ToString();
        }
@@ -197,18 +383,54 @@
            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);
            var materielinfo = dataRepository.Find(x => x.Description == type).OrderBy(x => x.TypeId).FirstOrDefault();
            return materielinfo;
        }
        public static string QueryMateriel(int  typeId)
        public static dt_geometry_data_detectionline QueryMateriel1(string type)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_data_detectionlineRepository detectionlineRepository = new dt_geometry_data_detectionlineRepository(Context);
            var materielinfo1 = detectionlineRepository.Find(x => x.Description == type).OrderBy(x => x.TypeId).FirstOrDefault();
            return materielinfo1;
        }
        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;
        }
        /// <summary>
        /// æ›´æ”¹å·¥å•状态
        /// </summary>
        /// <param name="SN"></param>
        public static void Updatemes(string SN)
        {
            VOLContext Context = new VOLContext();
            Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
            Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
            //var mes_Detail = freeDB.Select<dt_mes_detail>().Where(x => x.SN == SN).First();
            var Mes_Work = workinfoRepository.Find(x => x.SN == SN && x.processCode == "28").FirstOrDefault();
            var mes_Detail = mes_DetailRepository.Find(x => x.SN == SN && x.jobID == Mes_Work.jobID).FirstOrDefault();
            if (mes_Detail != null)
            {
                mes_Detail.Status = "上线";
                mes_Detail.FinishTime = DateTime.Now;
                //freeDB.DataBase.Update<dt_mes_detail>().SetSource(mes_Detail).UpdateColumns(x => new { x.Status, x.FinishTime }).ExecuteAffrows();
                mes_DetailRepository.Update(mes_Detail, true);
                //var mes_Head = freeDB.Select<dt_mes_head>().Where(x => x.jobID == mes_Detail.jobID).First();
                var mes_Head = mes_HeadRepository.Find(x => x.jobID == mes_Detail.jobID).FirstOrDefault();
                if (mes_Head != null)
                {
                    mes_Head.finishNum = mes_Head.finishNum + 1;
                    //freeDB.DataBase.Update<dt_mes_head>().SetSource(mes_Head).UpdateColumns(x => new { x.finishNum }).ExecuteAffrows();
                    mes_HeadRepository.Update(mes_Head, true);
                }
            }
        }
    }
}