wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
wcs-server-hbf/WIDESEA_WCS/SchedulerExecute/Equipment/Z_TBSB_DownTask.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.LoctionEnum;
using WIDESEA_Common.Tools;
@@ -28,138 +29,141 @@
        {
            if (Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 1) == 0)
            {
                try
                Task.Run(() =>
                {
                    VOLContext Context = new VOLContext();
                    Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
                    Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
                    Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
                    List<string> TBSBS = new List<string> { "涂布机正极1", "涂布机正极2", "涂布机正极3", "涂布机正极4" };
                    try
                    {
                        VOLContext Context = new VOLContext();
                        Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
                        Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
                        Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
                        List<string> TBSBS = new List<string> { "涂布机正极1", "涂布机正极2", "涂布机正极3", "涂布机正极4" };
                    if (WCSService.Clients == null)
                    {
                        return;
                    }
                    //循环处理涂布设备下料请求
                    foreach (var TBSB in TBSBS)
                    {
                        PLCClient plc = WCSService.Clients.Find(v => v.PLCName == TBSB);
                        if (plc == null)
                        if (WCSService.Clients == null)
                        {
                            continue;
                            return;
                        }
                        string SB_Up_Location = "ZJXL-TBSB" + TBSB.Replace("涂布机正极", "");//拼接涂布下料口
                                                                                        //查看当前涂布机设备是否有AGV任务
                        var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
                        //有结束本次循环
                        if (SBtask != null) { continue; }
                        //当前设备有没有下料请求
                        bool GYR_RFX = plc.OmronPLCClient.OmronFinsNetClient.ReadBool("D11293.00").Content;
                        //Convert.ToBoolean(plc.ReadValue("R_Feedingrequest".ToString(), SB_Up_Location));
                        if (GYR_RFX)
                        //循环处理涂布设备下料请求
                        foreach (var TBSB in TBSBS)
                        {
                            //找到路由规则(以设备为起点的路由规则)
                            List<base_routing_table> Routing_Table = routingRepository.Find(d => d.route_began == SB_Up_Location);
                            base_ware_location location_TB = null;
                            if (Routing_Table.Count > 0)
                            PLCClient plc = WCSService.Clients.Find(v => v.PLCName == TBSB);
                            if (plc == null)
                            {
                                foreach (var item in Routing_Table)
                                continue;
                            }
                            string SB_Up_Location = "ZJXL-TBSB" + TBSB.Replace("涂布机正极", "");//拼接涂布下料口
                                                                                            //查看当前涂布机设备是否有AGV任务
                            var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
                            //有结束本次循环
                            if (SBtask != null) { continue; }
                            //当前设备有没有下料请求
                            bool GYR_RFX = plc.OmronPLCClient.OmronFinsNetClient.ReadBool("D11293.00").Content;
                            //Convert.ToBoolean(plc.ReadValue("R_Feedingrequest".ToString(), SB_Up_Location));
                            if (GYR_RFX)
                            {
                                #region 记录下料请求信号
                                WriteLog.GetLog().Write($"{plc.PLCName}下料请求:{GYR_RFX}---{DateTime.Now}", $"正极涂布下料请求");
                                #endregion
                                //找到路由规则(以设备为起点的路由规则)
                                List<base_routing_table> Routing_Table = routingRepository.Find(d => d.route_began == SB_Up_Location);
                                base_ware_location location_TB = null;
                                if (Routing_Table.Count > 0)
                                {
                                    //找先找涂布区域缓存架
                                    location_TB = locRepository.FindFirst(t => t.upper_code == item.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() &&t.tpgd==0 && t.area == "ZJ-TBHCJ" && t.status == 1 && t.task == 2);
                                    if (location_TB != null)
                                    foreach (var item in Routing_Table)
                                    {
                                        //location = location_TB.FirstOrDefault();//如果找到,赋值后退出循环
                                        break;
                                    }
                                }
                                //以上循环未找到合适货位,再找箔材库三号位缓存架
                                if (location_TB == null)
                                {
                                    //定位铂材库路由,找到铂材库货位号
                                    base_routing_table Loc_BCK = Routing_Table.FirstOrDefault(t => t.route_end.Contains("ZJ-BCHCJ"));
                                    if (Loc_BCK != null)
                                    {
                                        //根据根据货位号找到铂材库位置并确认是否有料
                                        location_TB = locRepository.FindFirst(t => t.upper_code == Loc_BCK.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.status == 1 && t.task == 2);
                                        if (location_TB==null)
                                        //找先找涂布区域缓存架
                                        location_TB = locRepository.FindFirst(t => t.upper_code == item.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.tpgd == 0 && t.area == "ZJ-TBHCJ" && t.status == 1 && t.task == 2);
                                        if (location_TB != null)
                                        {
                                                return;
                                            //location = location_TB.FirstOrDefault();//如果找到,赋值后退出循环
                                            break;
                                        }
                                    }
                                    //以上循环未找到合适货位,再找箔材库三号位缓存架
                                    if (location_TB == null)
                                    {
                                        //定位铂材库路由,找到铂材库货位号
                                        base_routing_table Loc_BCK = Routing_Table.FirstOrDefault(t => t.route_end.Contains("ZJ-BCHCJ"));
                                        if (Loc_BCK != null)
                                        {
                                            //根据根据货位号找到铂材库位置并确认是否有料
                                            location_TB = locRepository.FindFirst(t => t.upper_code == Loc_BCK.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.status == 1 && t.task == 2);
                                            if (location_TB == null)
                                            {
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                            if (location_TB != null)
                            {
                                WebResponseContent content = new WebResponseContent();
                                locRepository.DbContextBeginTransaction(() =>
                                if (location_TB != null)
                                {
                                    //生成涂布机下料任务
                                    dt_agvtask agvtask = new dt_agvtask()
                                {
                                    agv_id = Guid.NewGuid(),
                                    agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
                                    agv_fromaddress = SB_Up_Location,//以设备未起点
                                    agv_toaddress = location_TB.upper_code,
                                    agv_code = "正极AGV调度",
                                    agv_remark = "涂布下料任务",
                                    agv_taskstate = AGVTaskStateEnum.Create.ToString(),
                                    agv_tasktype = AGVTaskTypeEnum.TaskType_Outbound.ToString(),
                                    agv_worktype = 1,//工作类型
                                    agv_materielid = "",
                                    agv_qty = 1,
                                    agv_createtime = DateTime.Now,
                                    agv_grade = 0,//任务优先级
                                    agv_userid = "WCS",
                                    agv_barcode = "",
                                };
                                    location_TB.update_time = DateTime.Now;
                                    location_TB.task = 1;
                                    var entry = locRepository.DbContext.ChangeTracker.Entries<base_ware_location>().FirstOrDefault();
                                    if (entry != null)
                                    WebResponseContent content = new WebResponseContent();
                                    locRepository.DbContextBeginTransaction(() =>
                                    {
                                        entry.State = EntityState.Detached;
                                    }
                                    int x = locRepository.Update(location_TB, d => new { d.update_time, d.task}, true);
                                    if (x == 1)
                                    {
                                        agvRepository.Add(agvtask, true);
                                        WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"生成任务:{agvtask.agv_tasknum},终点货位【{location_TB.upper_code}】状态:托盘光电检测:{location_TB.tpgd},状态:{location_TB.location_state},更新时间:{location_TB.wireless_time}", $"【{TBSB}】下料任务生成");
                                        WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"----------------任务生成成功  {DateTime.Now} -------------- {Environment.NewLine}", $"【{TBSB}】下料任务生成");
                                        return content.OK();
                                    }
                                    else
                                    {
                                        return content.Error();
                                    }
                                });
                            }
                                        //生成涂布机下料任务
                                        dt_agvtask agvtask = new dt_agvtask()
                                        {
                                            agv_id = Guid.NewGuid(),
                                            agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
                                            agv_fromaddress = SB_Up_Location,//以设备未起点
                                            agv_toaddress = location_TB.upper_code,
                                            agv_code = "正极AGV调度",
                                            agv_remark = "涂布下料任务",
                                            agv_taskstate = AGVTaskStateEnum.Create.ToString(),
                                            agv_tasktype = AGVTaskTypeEnum.TaskType_Outbound.ToString(),
                                            agv_worktype = 1,//工作类型
                                            agv_materielid = "",
                                            agv_qty = 1,
                                            agv_createtime = DateTime.Now,
                                            agv_grade = 0,//任务优先级
                                            agv_userid = "WCS",
                                            agv_barcode = "",
                                        };
                                        location_TB.update_time = DateTime.Now;
                                        location_TB.task = 1;
                                        var entry = locRepository.DbContext.ChangeTracker.Entries<base_ware_location>().FirstOrDefault();
                                        if (entry != null)
                                        {
                                            entry.State = EntityState.Detached;
                                        }
                                        int x = locRepository.Update(location_TB, d => new { d.update_time, d.task }, true);
                                        if (x == 1)
                                        {
                                            agvRepository.Add(agvtask, true);
                                            //WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"生成任务:{agvtask.agv_tasknum},终点货位【{location_TB.upper_code}】状态:托盘光电检测:{location_TB.tpgd},状态:{location_TB.location_state},更新时间:{location_TB.wireless_time}", $"【{TBSB}】下料任务生成");
                                            //WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"----------------任务生成成功  {DateTime.Now} -------------- {Environment.NewLine}", $"【{TBSB}】下料任务生成");
                                            return content.OK();
                                        }
                                        else
                                        {
                                            return content.Error();
                                        }
                                    });
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    catch (Exception ex)
                    {
                        WriteLog.GetLog().Write($"正极涂布下料:{DateTime.Now} --------------{ex}", $"正极涂布下料");
                    }
                    finally
                    {
                    //WriteLog.Info($"任务生成").Write($"结束时间:   {DateTime.Now}  异常信息:{ex.Message} -------------- {Environment.NewLine}", $"任务生成");
                    throw;
                }
                finally
                {
                    Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 0);
                }
                        Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 0);
                    }
                });
            }
        }