wangxinhui
2026-03-17 febcd0c73b51b17f484ff9a9a189b1f85130c2f7
wcs-server-hbf/WIDESEA_WCS/Jobs/RGV/AluminumCaseJob.cs
@@ -21,6 +21,12 @@
using HslCommunication;
using SqlSugar;
using WIDESEA_Core.Extensions;
using static System.Collections.Specialized.BitVector32;
using WIDESEA_WCS.WCSClient.Modbus;
using static FreeSql.Internal.GlobalFilter;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System.Reflection.Metadata;
using Dm;
namespace WIDESEA_WCS
{
@@ -32,6 +38,7 @@
    {
        FreeDB freeDB = new FreeDB();
        static List<HCJCenterEqDB> centerEqDBList;
        static List<HCJCenterEqDB> centerJRDBList;
        private static object createTaskObj = new object();
        public Task Execute(IJobExecutionContext context)
        {
@@ -54,8 +61,321 @@
                client.Connect();
                return;
            }
            //铝壳缓存架业务作业
            LKHCJdispatch();
            if (client.PLCName.Contains("铝壳"))
            {
                //铝壳缓存架业务作业
                LKHCJdispatch();
            }
            else
            {
                //卷绕缓存架业务作业
                JRHCJdispatch();
            }
        }
        /// <summary>
        /// 卷绕缓存架业务作业
        /// </summary>
        private void JRHCJdispatch()
        {
            //铝壳缓存架搬运
            PLCClient pLCClientJR = WCSService.Clients.Find(V => V.PLCName==PLCConst.PLCNameJR);
            if (pLCClientJR != null)
            {
                string tsjJson = File.ReadAllText(AppContext.BaseDirectory + "/Config/卷绕缓存架.json", Encoding.UTF8);
                centerJRDBList = JsonConvert.DeserializeObject<List<HCJCenterEqDB>>(tsjJson);
                if (centerJRDBList == null)
                {
                    return;
                }
                PLCClient agvzzplc = WCSService.Clients.Find(v => v.PLCName == "组装AGV调度");
                UpdateJRHCJ(pLCClientJR);
                #region 卷绕叫料
                List<base_ware_location> locations = freeDB.Select<base_ware_location>()
                .Where(x => (x.tpgd == 0 && x.gd1 == 0 && x.gd2 == 0 && x.gd3 == 0 && x.gd4 == 0 && x.status == 1 && x.task == 2 && x.logic_layer == "1" && x.location_state == "LocationState_Empty") && (x.upper_code.Contains("QJHCJ10") || x.upper_code.Contains("QJHCJ11")))
                .ToList();
                foreach (var item in locations)
                {
                    if (item.geartype == "暂无")
                    {
                        WriteLog.GetLog().Write($"{item.upper_code}{item.geartype}---{DateTime.Now}", $"卷绕上料请求失败");
                        continue;
                    }
                    List<base_routing_table> routing_table = freeDB.Select<base_routing_table>()
                .Where(v => v.route_end == item.upper_code).ToList();
                    if (routing_table.Count > 0)//无料才能生成任务&& location_QJ.tpgd==0
                    {
                        List<string> route_begans = routing_table.Select(x => x.route_began).ToList();
                        base_ware_location ware_Location = freeDB.Select<base_ware_location>()
                .Where(d => route_begans.Contains(d.upper_code) && d.location_state == "LocationState_Stored" && d.logic_col == 1 && d.tpgd == 1 && d.status == 1 && d.task == 2 && d.geartype == item.geartype).OrderBy(d => d.update_time).First();
                        if (ware_Location != null)
                        {
                            base_ware_location jkloction = ware_Location;
                            #region 记录上料请求信号
                            WriteLog.GetLog().Write($"{item.upper_code}上料请求---{DateTime.Now}", $"卷绕上料请求");
                            #endregion
                            //再次判断是否有任务存在
                            dt_agvtask agvtaskOld = freeDB.Select<dt_agvtask>().Where(x => x.agv_toaddress == item.upper_code).First();
                            if (agvtaskOld != null && item.task == 2)
                            {
                                item.logic_col = 1;//设置良品物料
                                item.task = 1;//设置任务生成1
                                var locationOffOld1 = freeDB.Update(item).UpdateColumns(t => new
                                {
                                    t.logic_col,
                                    t.task
                                }).ExecuteAffrows();
                                continue;
                            }
                            dt_agvtask agvtask = new dt_agvtask()
                            {
                                agv_id = Guid.NewGuid(),
                                agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
                                agv_fromaddress = jkloction.upper_code,
                                agv_toaddress = item.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 = ""
                            };
                            item.logic_col = 1;//设置良品物料
                            item.task = 1;//设置任务生成1
                            var locationOff1 = freeDB.Update(item).UpdateColumns(t => new
                            {
                                t.logic_col,
                                t.task
                            }).ExecuteAffrows();
                            jkloction.logic_col = 1;//设置良品物料
                            jkloction.task = 1;//设置任务生成1
                            var locationOff2 = freeDB.Update(jkloction).UpdateColumns(t => new
                            {
                                t.logic_col,
                                t.task
                            }).ExecuteAffrows();
                            freeDB.Add(agvtask);
                        }
                        else
                        {
                            WriteLog.GetLog().Write($"{item.upper_code}未找到满足送往切卷的极库料---{DateTime.Now}", $"卷绕上料请求失败");
                        }
                    }
                }
                #endregion
            }
        }
        private void UpdateJRHCJ(PLCClient client)
        {
            ModbusTcpNet ModbusPLCClient = client.ModbusPLCClient.ModbusTcpNetClient;
            var stationList = centerJRDBList.FirstOrDefault(t => client.PLCName.Contains(t.HCJCenterName));
            foreach (var item in stationList.stationInfos)
            {
                base_ware_location location = freeDB.Select<base_ware_location>()
                .Where(x => x.upper_code == item.HCJCode)
                .First();
                ////Console.WriteLine($"{station.stationCode}------{DateTime.Now}");
                if (location == null)
                {
                    continue;
                }
                string wlgd1DB = item.RPLCDB[0];//物料光电1状态信号点位
                string wlgd2DB = item.RPLCDB[1];//物料光电2状态信号点位
                string wlgd3DB = item.RPLCDB[2];//物料光电3状态信号点位
                string wlgd4DB = item.RPLCDB[3];//物料光电4状态信号点位
                string tpgdDB = item.RPLCDB[4];//托盘光电状态信号点位
                string buttonDB = item.RPLCDB[5];//按钮状态信号点位
                ////确认按钮(绿色)
                OperateResult<bool> R_TBR_XT1 = ModbusPLCClient.ReadBool(buttonDB);
                OperateResult<bool> R_TBR_XT2 = ModbusPLCClient.ReadBool(buttonDB);
                WriteLog.GetLog().Write($"{item.HCJName}缓存架,连接{R_TBR_XT1.IsSuccess}{R_TBR_XT2.IsSuccess}", "卷绕缓存架");
                if (!R_TBR_XT1.IsSuccess && !R_TBR_XT2.IsSuccess)
                {
                    //Console.WriteLine($"-{ModbusPLCClient.IpAddress}---{station.stationCode}----{ButtonEnsureDB}--------false--{R_TBR_XT.Message}----------------");
                    location.location_state = LocationStateEnum.LocationState_Close.ToString();
                    location.tpgd = -1;
                    location.update_time = DateTime.Now;
                    location.wireless_time = DateTime.Now;
                    var locationOff = freeDB.Update(location).UpdateColumns(t => new
                    {
                        t.location_state,
                        t.tpgd,
                        t.update_time,
                        t.wireless_time
                    }).ExecuteAffrows();
                    continue;
                }
                OperateResult<bool> ButtonEnsure1 = ModbusPLCClient.ReadBool(buttonDB);
                OperateResult<bool> ButtonEnsure2 = ModbusPLCClient.ReadBool(buttonDB);
                //托盘光电状态
                OperateResult<bool> tpgdstate1 = ModbusPLCClient.ReadBool(tpgdDB);
                OperateResult<bool> tpgdstate2 = ModbusPLCClient.ReadBool(tpgdDB);
                //物料光电1状态
                OperateResult<bool> wlgd1state1 = ModbusPLCClient.ReadBool(wlgd1DB);
                OperateResult<bool> wlgd1state2 = ModbusPLCClient.ReadBool(wlgd1DB);
                //物料光电2状态
                OperateResult<bool> wlgd2state1 = ModbusPLCClient.ReadBool(wlgd2DB);
                OperateResult<bool> wlgd2state2 = ModbusPLCClient.ReadBool(wlgd2DB);
                //物料光电3状态
                OperateResult<bool> wlgd3state1 = ModbusPLCClient.ReadBool(wlgd3DB);
                OperateResult<bool> wlgd3state2 = ModbusPLCClient.ReadBool(wlgd3DB);
                //物料光电4状态
                OperateResult<bool> wlgd4state1 = ModbusPLCClient.ReadBool(wlgd4DB);
                OperateResult<bool> wlgd4state2 = ModbusPLCClient.ReadBool(wlgd4DB);
                //根据读到的PLC信息,轮询更新数据库信息
                location.update_time = DateTime.Now;
                location.wireless_time = DateTime.Now;
                if (ButtonEnsure1.IsSuccess)
                {
                    location.logic_layer = ButtonEnsure1.Content ? "1" : "0";
                }
                if (ButtonEnsure2.IsSuccess)
                {
                    location.logic_layer = ButtonEnsure2.Content ? "1" : "0";
                }
                if (tpgdstate1.IsSuccess)
                {
                    location.tpgd = !tpgdstate1.Content ? 1 : 0;
                }
                if (tpgdstate2.IsSuccess)
                {
                    location.tpgd = !tpgdstate2.Content ? 1 : 0;
                }
                #region 物料光电1
                if (wlgd1state1.IsSuccess)
                {
                    location.gd1 = !wlgd1state1.Content ? 1 : 0; ;
                }
                if (wlgd1state2.IsSuccess)
                {
                    location.gd1 = !wlgd1state2.Content ? 1 : 0; ;
                }
                #endregion
                #region 物料光电2
                if (wlgd2state1.IsSuccess)
                {
                    location.gd2 = !wlgd2state1.Content ? 1 : 0; ;
                }
                if (wlgd2state2.IsSuccess)
                {
                    location.gd2 = !wlgd2state2.Content ? 1 : 0; ;
                }
                #endregion
                #region 物料光电3
                if (wlgd3state1.IsSuccess)
                {
                    location.gd3 = !wlgd3state1.Content ? 1 : 0; ;
                }
                if (wlgd3state2.IsSuccess)
                {
                    location.gd3 = !wlgd3state2.Content ? 1 : 0; ;
                }
                #endregion
                #region 物料光电4
                if (wlgd4state1.IsSuccess)
                {
                    location.gd4 = !wlgd4state1.Content ? 1 : 0; ;
                }
                if (wlgd4state2.IsSuccess)
                {
                    location.gd4 = !wlgd4state2.Content ? 1 : 0; ;
                }
                #endregion
                string LampYellowDB = item.WPLCDB[0];//黄灯信号点位
                string LampGreenDB = item.WPLCDB[1];//绿灯信号点位
                string LampRedDB = item.WPLCDB[2];//红灯信号点位
                //货架存在任务任务时
                if (location.tpgd == 1)
                {
                    location.location_state = LocationStateEnum.LocationState_Stored.ToString();
                    OperateResult<bool> Yellow1 = ModbusPLCClient.ReadBool(LampYellowDB);
                    OperateResult<bool> Yellow2 = ModbusPLCClient.ReadBool(LampYellowDB);
                    if (location.logic_col == 2)
                    {
                        ModbusPLCClient.Write(LampRedDB, true); //红灯
                        ModbusPLCClient.Write(LampRedDB, true); //红灯
                    }
                    if (location.task == 1 && (Yellow1.Content || Yellow2.Content))
                    {
                        //ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        //ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                        ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                    }
                    else if(location.task == 1 && (!Yellow1.Content || !Yellow2.Content))
                    {
                        //ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        //ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                        ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                    }
                    else
                    {
                        ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                        ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                        ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                        ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                        ModbusPLCClient.Write(LampRedDB, false); //红灯
                        ModbusPLCClient.Write(LampRedDB, false); //红灯
                    }
                }
                else
                {
                    location.location_state = LocationStateEnum.LocationState_Empty.ToString();
                    OperateResult<bool> Yellow1 = ModbusPLCClient.ReadBool(LampYellowDB);
                    OperateResult<bool> Yellow2 = ModbusPLCClient.ReadBool(LampYellowDB);
                    if (location.logic_col == 2)
                    {
                        ModbusPLCClient.Write(LampRedDB, true); //红灯
                        ModbusPLCClient.Write(LampRedDB, true); //红灯
                    }
                    if (location.task == 1 && (Yellow1.Content || Yellow2.Content))
                    {
                        ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                        ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                    }
                    else if (location.task == 1 && (!Yellow1.Content || !Yellow2.Content))
                    {
                        ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                        ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                        ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                    }
                    else
                    {
                        ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                        ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                        ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                        ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                        ModbusPLCClient.Write(LampRedDB, false); //红灯
                        ModbusPLCClient.Write(LampRedDB, false); //红灯
                    }
                }
                var executeAffrows = freeDB.Update(location).UpdateColumns(t => new
                {
                    t.location_state,
                    t.logic_layer,
                    t.update_time,
                    t.wireless_time,
                    t.tpgd,
                    t.gd1,
                    t.gd2,
                    t.gd3,
                    t.gd4
                }).ExecuteAffrows();
            }
        }
        private void LKHCJdispatch()
        {
@@ -445,6 +765,42 @@
                doorState_1f_cx += 30;
           
            }
            //入壳缓存架
            double comErr_rk = 9450.0;//是否掉线
            double doorCode_rk = 9452.0;//缓存架ID
            double doorState_rk = 9474.0;//缓存架物料状态(1:空位,2:空托盘,3:满托盘)
            var dt_stationinfo_rk = freeDB.Select<dt_stationinfo>().Where(d => d.stationType == "入壳缓存架").OrderBy(t => t.sort).ToList();
            foreach (var item in dt_stationinfo_rk)
            {
                if (item.connect == true)
                {
                    //掉线
                }
                else
                {
                    if (item.tpgd == true && item.wlgd == true)
                    {
                        OperateResult W_HCJ_FullState = agvzzplc.SiemensPLCClient.SiemensS7NetClient.Write("DB1003." + doorState_rk, Convert.ToInt16(3));
                    }
                    else if (item.tpgd == true && item.wlgd == false)
                    {
                        OperateResult W_HCJ_FullState = agvzzplc.SiemensPLCClient.SiemensS7NetClient.Write("DB1003." + doorState_rk, Convert.ToInt16(2));
                    }
                    else
                    {
                        OperateResult W_HCJ_FullState = agvzzplc.SiemensPLCClient.SiemensS7NetClient.Write("DB1003." + doorState_rk, Convert.ToInt16(1));
                    }
                }
                OperateResult W_HCJ_StationCode = agvzzplc.SiemensPLCClient.SiemensS7NetClient.Write("DB1003." + doorCode_rk, item.stationCode.ToString());
                OperateResult W_HCJ_ComErr = agvzzplc.SiemensPLCClient.SiemensS7NetClient.Write("DB1003." + comErr_rk, item.connect);
                comErr_1f_cx += 30;
                doorCode_1f_cx += 30;
                doorState_1f_cx += 30;
            }
        }
        private void agvtaskAdd(string agv_remark, string begin, string end,bool communi)
@@ -641,7 +997,7 @@
                        t.LampYellow
                    }).ExecuteAffrows();
                }
                else if (stationList.HCJCenterName == "铝壳缓存架一楼")
                else if (stationList.HCJCenterName == "铝壳缓存架一楼" && !item.HCJName.Contains("入壳缓存架"))
                {
                    string ButtonResetDB = item.RPLCDB[0];  //复位按钮(黄色)信号点位
                    string ButtonEnsureDB = item.RPLCDB[1];//确认按钮(绿色)信号点位
@@ -820,6 +1176,142 @@
                        t.LampYellow
                    }).ExecuteAffrows();
                }
                else if (stationList.HCJCenterName == "铝壳缓存架一楼" && item.HCJName.Contains("入壳缓存架"))
                {
                    string ButtonEnsureDB = item.RPLCDB[0];//确认按钮(绿色)信号点位
                    string tpgdstateDB = item.RPLCDB[1];//托盘光电状态信号点位
                    string wlgdstateDB = item.RPLCDB[2];//物料光电状态信号点位
                    ////确认按钮(绿色)
                    OperateResult<bool> R_TBR_XT = ModbusPLCClient.ReadBool(ButtonEnsureDB);
                    WriteLog.GetLog().Write($"{item.HCJName}缓存架,连接{R_TBR_XT.IsSuccess}", "入壳缓存架");
                    if (!R_TBR_XT.IsSuccess)
                    {
                        //Console.WriteLine($"-{ModbusPLCClient.IpAddress}---{station.stationCode}----{ButtonEnsureDB}--------false--{R_TBR_XT.Message}----------------");
                        station.connect = true;
                        var stationoff = freeDB.Update(station).UpdateColumns(t => new
                        {
                            t.connect
                        }).ExecuteAffrows();
                        continue;
                    }
                    bool ButtonEnsure = ModbusPLCClient.ReadBool(ButtonEnsureDB).Content;
                    //托盘光电状态
                    bool pgdstate = ModbusPLCClient.ReadBool(tpgdstateDB).Content;
                    //物料光电状态
                    bool wlgdstate = ModbusPLCClient.ReadBool(wlgdstateDB).Content;
                    //根据读到的PLC信息,轮询更新数据库信息
                    station.getLastTime = DateTime.Now;
                    station.greenbutton = ButtonEnsure;
                    station.tpgd = pgdstate;
                    station.wlgd = wlgdstate;
                    string LampYellowDB = item.WPLCDB[0];//黄灯信号点位
                    string LampGreenDB = item.WPLCDB[1];//绿灯信号点位
                    string LampRedDB = item.WPLCDB[2];//红灯信号点位
                    //货架存在任务任务时
                    if (station.task)
                    {
                        if (station.LampYellow)
                        {
                            ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                            ModbusPLCClient.Write(LampRedDB, false); //红灯
                            ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                                                                       // Console.WriteLine($"{station.stationCode}---亮---{DateTime.Now}");
                            station.LampRed = false;
                            station.LampYellow = false;
                            station.LampGreen = false;
                        }
                        else
                        {
                            ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                            ModbusPLCClient.Write(LampRedDB, false); //红灯
                            ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                                                                       // Console.WriteLine($"{station.stationCode}---灭---{DateTime.Now}");
                            station.LampRed = false;
                            station.LampYellow = true;
                            station.LampGreen = false;
                        }
                    }
                    else
                    {
                        //当有人按下绿色按钮时:
                        //将缓存架的灯光亮绿灯
                        if (ButtonEnsure && pgdstate && wlgdstate)
                        {
                            DateTime date = DateTime.Now;
                            DateTime time2 = station.greenbuttontime;
                            Double STAMP = (date - time2).TotalSeconds;
                            if (STAMP > 4)
                            {
                                if (station.LampGreen)
                                {
                                    ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                                    station.LampGreen = false;
                                }
                                else
                                {
                                    ModbusPLCClient.Write(LampGreenDB, true); //绿灯
                                    station.LampGreen = true;
                                }
                                //ModbusPLCClient.Write(LampRedDB, false); //红灯
                                ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                                //station.LampRed = false;
                                station.LampYellow = false;
                                station.greenbuttontime = DateTime.Now;
                            }
                        }
                        else if (station.LampGreen && (!pgdstate || !wlgdstate))
                        {
                            ModbusPLCClient.Write(LampRedDB, false); //红灯
                            ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                            ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                            station.LampRed = false;
                            station.LampYellow = false;
                            station.LampGreen = false;
                        }
                        //检测托盘光电有料,但未按确认按钮,亮黄灯
                        else if (!station.LampGreen && pgdstate)
                        {
                            // ModbusPLCClient.Write(LampRedDB, false); //红灯
                            ModbusPLCClient.Write(LampYellowDB, true); //黄灯
                            ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                                                                       // station.LampRed = false;
                            station.LampYellow = true;
                            station.LampGreen = false;
                        }
                        else if (!station.LampGreen && !pgdstate && !wlgdstate)
                        {
                            ModbusPLCClient.Write(LampRedDB, false); //红灯
                            ModbusPLCClient.Write(LampYellowDB, false); //黄灯
                            ModbusPLCClient.Write(LampGreenDB, false); //绿灯
                            station.LampRed = false;
                            station.LampYellow = false;
                            station.LampGreen = false;
                        }
                    }
                    station.connect = false;
                    var executeAffrows = freeDB.Update(station).UpdateColumns(t => new
                    {
                        t.connect,
                        t.getLastTime,
                        t.greenbutton,
                        t.tpgd,
                        t.wlgd,
                        t.LampRed,
                        t.LampGreen,
                        t.LampYellow
                    }).ExecuteAffrows();
                }
            }
        }