using HH.WCS.Mobox3.HD.api; using HH.WCS.Mobox3.HD.core; using HH.WCS.Mobox3.HD.device; using HH.WCS.Mobox3.HD.dispatch; using HH.WCS.Mobox3.HD.models; using HH.WCS.Mobox3.HD.util; using HH.WCS.Mobox3.HD.wms; using Newtonsoft.Json; using NLog.Fluent; using S7.Net; using SqlSugar; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Security.Cryptography; using static HH.WCS.Mobox3.HD.api.DigitHelper; using static HH.WCS.Mobox3.HD.api.WmsController; using static HH.WCS.Mobox3.HD.core.Monitor; using static HH.WCS.Mobox3.HD.dispatch.ShopFloorControl; using static HH.WCS.Mobox3.HD.process.TaskProcess; using static HH.WCS.Mobox3.HD.util.Settings; using static HH.WCS.Mobox3.HD.wms.WCSHelper; using static System.Runtime.CompilerServices.RuntimeHelpers; namespace HH.WCS.Mobox3.HD.process { internal class TaskProcess { #region 任务相关 //--------------------------------------------------任务相关-------------------------------------------------- /// /// 取货卸货完成,缓存位状态更新 /// /// /// internal static void CacheBitUpdate(WCSTask mst, bool load) { var trayCarryCount = mst.N_CNTR_COUNT > 0 ? mst.N_CNTR_COUNT : 1; if (load) { Console.WriteLine($"任务{mst.S_CODE} 货位{mst.S_START_LOC}取货完成,起点解绑容器{mst.S_CNTR_CODE}"); LogHelper.Info($"任务{mst.S_CODE} 货位{mst.S_START_LOC}取货完成,起点解绑容器{mst.S_CNTR_CODE}"); LocationHelper.UnBindingLoc(mst.S_START_LOC, mst.S_CNTR_CODE.Split(',').ToList()); } else { Console.WriteLine($"任务{mst.S_CODE} 货位{mst.S_END_LOC}卸货完成,终点绑定容器{mst.S_CNTR_CODE}"); LogHelper.Info($"任务{mst.S_CODE} 货位{mst.S_END_LOC}卸货完成,起点解绑容器{mst.S_CNTR_CODE}"); LocationHelper.BindingLoc(mst.S_END_LOC, mst.S_CNTR_CODE.Split(',').ToList()); } } /// /// 任务取消,缓存位状态更新 /// /// internal static void CacheBitCancelUpdate(WCSTask mst) { //任务取消,取货完成前的,起点的loadingCount和终点unLoadingCount都清除,取货完成的只处理终点 if (WCSHelper.CheckActionRecordExist(mst.S_CODE, 4)) { //根据客户现场要求,如果取货完成任务失败人工拉到终点,我们就当卸货完成处理;如果是人工拉走到其它区域,我们就解锁终点,删除托盘。 //终点绑定 CacheBitUpdate(mst, false); LocationHelper.UnLockLoc(mst.S_END_LOC); } else { //起点终点解锁 LocationHelper.UnLockLoc(mst.S_START_LOC); LocationHelper.UnLockLoc(mst.S_END_LOC); //货位容器解绑 LocationHelper.UnBindingLoc(mst.S_START_LOC, new List { mst.S_CNTR_CODE }); } } /// /// 安全请求 /// /// /// /// /// internal static void OperateReq(string no, int state, string forkliftNo, string extData) { if (state == 1101) { //请求取货, } if (state == 1102) { //请求卸货, //根据终点判断,是cb02的入口,判断内存中状态(要状态时间),允许卸货,通知agv改参数 var dic = new Dictionary(); //< Req >< Order No = 'TN2302020002' ParamNo = '18' Param1 = '12' /> dic.Add("No", no); dic.Add("ParamNo", "8"); dic.Add("Param1", "1"); NDC.ChangeOrder(dic); //改完参数车子就会自己卸货 } if (state == 1103) { //大铁框叉走以后通知,我们要通知输送线 } } /// /// 任务拦截 /// /// /// internal static bool Intercept(WCSTask mst) { var result = true; if (! mst.S_TYPE.Equals("移库")) { List wcsTasks = WCSHelper.GetTaskListBySrcNo(mst.S_OP_CODE); foreach (var task in wcsTasks) { if (task.N_PRIORITY == 9 && task.N_B_STATE < 3) { result = false; } } } return result; } /// /// 任务状态更新处理 /// /// /// internal static void OperateStatus(WCSTask mst, int state) { if (state == 4) { CacheBitUpdate(mst, true); } if (state == 6)//卸货完成 { CacheBitUpdate(mst, false); } if (state == 7) { CacheBitCancelUpdate(mst); } } private static object locLocker = new object(); /// /// 堆叠库区出入库任务申请 /// /// /// /// /// /// /// /// /// internal static bool ApplyTN_Task(Location ls, ref List cntrs, string area, string itemCode, string itemBatch, string taskType, bool insStock = true) { var result = false; lock (locLocker) { try { if (insStock) { Console.WriteLine($"MoboxHelperCreateTask: {area}-{itemCode}-{itemBatch}-{taskType}"); var endTN_Location = GetLocation4In(area, itemCode, itemBatch, 3); if (endTN_Location != null) { var endLayer = endTN_Location.N_CURRENT_NUM == 0 ? 1 : 2; var taskNo = DateTime.Now.Ticks.ToString(); result = TaskProcess.CreateTransport(ls.S_CODE, endTN_Location.S_CODE, taskType, cntrs, 1, endLayer, 3, 70); } else { Console.WriteLine($"MoboxHelperCreateTask: 未找到终点货位"); } } else { var startTN_Location = GetLocation4Out(area, itemCode, itemBatch, 3); if (startTN_Location != null) { var startLayer = startTN_Location.N_CURRENT_NUM <= 3 ? 1 : 2; var taskNo = DateTime.Now.Ticks.ToString(); var carryCount = startTN_Location.N_CURRENT_NUM > 3 ? startTN_Location.N_CURRENT_NUM - 3 : startTN_Location.N_CURRENT_NUM; //出库要从起点获取托盘 var cntrList = LocationHelper.GetLocCntr(startTN_Location.S_CODE); if (cntrList.Count == startTN_Location.N_CURRENT_NUM) { cntrs = cntrList.OrderByDescending(a => a.T_CREATE).Take(carryCount).Select(a => a.S_CNTR_CODE).ToList(); result = TaskProcess.CreateTransport(startTN_Location.S_CODE, ls.S_CODE, taskType, cntrs, startLayer, 1, carryCount, 65); } else { Console.WriteLine($"起点托盘数量和货位容器表不符合,请检查【货位表】和【货位容器表】"); } } } } catch (Exception ex) { Console.WriteLine("MoboxHelperCreateTask:" + ex.Message); LogHelper.Error("MoboxHelperCreateTask:" + ex.Message, ex); } } return result; } private static Location GetLocation4Out(string area, string itemCode, string itemBatch, int v) { throw new NotImplementedException(); } private static Location GetLocation4In(string area, string itemCode, string itemBatch, int v) { throw new NotImplementedException(); } /// /// 普通货架区的出入库申请 /// /// /// /// /// /// /// /// internal static bool ApplyNormalTN_Task(Location ls, ref List cntrs, string area, string taskType, string itemCode, bool insStock = true) { var result = false; lock (locLocker) { try { if (insStock) { Console.WriteLine($"MoboxHelperCreateTask: {area}-{taskType}"); var endTN_Location = new Location(); if (endTN_Location != null) { var taskNo = DateTime.Now.Ticks.ToString(); result = TaskProcess.CreateTransport(ls.S_CODE, endTN_Location.S_CODE, taskType, cntrs, 70); } else { Console.WriteLine($"MoboxHelperCreateTask: 未找到终点货位"); } } else { var startTN_Location = new Location(); if (startTN_Location != null) { //出库要从起点获取托盘 var cntrList = LocationHelper.GetLocCntr(startTN_Location.S_CODE); if (cntrList.Count == startTN_Location.N_CURRENT_NUM) { result = TaskProcess.CreateTransport(startTN_Location.S_CODE, ls.S_CODE, taskType, new List { cntrList[0].S_CNTR_CODE }, 65); } else { Console.WriteLine($"起点托盘数量和货位容器表不符合,请检查【货位表】和【货位容器表】"); } } } } catch (Exception ex) { Console.WriteLine("MoboxHelperCreateTask:" + ex.Message); LogHelper.Error("MoboxHelperCreateTask:" + ex.Message, ex); } } return result; } /// /// 推送任务 /// /// internal static bool SendTask(WCSTask mst) { var result = true; var startLoc = LocationHelper.GetLocation(mst.S_START_LOC); var endLoc = LocationHelper.GetLocation(mst.S_END_LOC); if (mst.N_B_STATE == 0) { if (mst.S_SCHEDULE_TYPE == "RB") { var itemOutLoc = getOutLocation(mst.S_START_LOC); var itemInLoc = getOutLocation(mst.S_END_LOC); if (itemOutLoc != null && itemOutLoc.N_CURRENT_NUM > 0) { if (itemOutLoc.N_LOCK_STATE == 0) { ApiHelper.shiftStock(itemOutLoc, mst.S_OP_CODE); } } else if(itemInLoc != null && itemInLoc.N_CURRENT_NUM > 0) { if (itemInLoc.N_LOCK_STATE == 0) { ApiHelper.shiftStock(itemInLoc, mst.S_OP_CODE); } } else { // 发送堆垛机任务数据 LogHelper.Info("推送堆垛机任务,任务号:" + mst.S_CODE, "哈电"); if (sendDDJTaskData(mst)) { mst.N_B_STATE = 1; WCSHelper.UpdateStatus(mst); } } } else if (mst.S_SCHEDULE_TYPE == "HA") { //调第三方接口 var model = new HanAo.TaskInfoModel { requestPk = mst.S_CODE, frmPos = mst.S_START_LOC, toPos = mst.S_END_LOC, trkType = mst.S_OP_NAME == "入库" ? "1" : "2", contNo = mst.S_CNTR_CODE }; if (HanAo.CreateOrder(model)) { mst.N_B_STATE = 1; WCSHelper.UpdateStatus(mst); } } else if (mst.S_SCHEDULE_TYPE == "HL" && endLoc != null) { var model = new HL.TaskInfoModel { orderId = mst.S_CODE, operateType = mst.S_OP_NAME == "入库" ? "10" : "20", trayCode = mst.S_CNTR_CODE, fetchPosition = startLoc.S_CODE, fetchLevel = startLoc.N_LAYER.ToString(), deliverPosition = endLoc.S_CODE, deliverLevel = endLoc.N_LAYER.ToString(), priority = mst.N_PRIORITY.ToString(), source = "wms", }; if (mst.S_TYPE.Contains("钢卷")) { var cntrItemRels = ItemHelper.GetCntrItemByCntrCode(mst.S_CNTR_CODE); model.coilNo = cntrItemRels[0].S_COIL_NO; model.shootNo = cntrItemRels[0].S_SHOOT_NO; model.heatNo = cntrItemRels[0].S_HEAT_NO; model.spec = cntrItemRels[0].S_ITEM_SPEC; } else if (mst.S_TYPE.Contains("模具")) { var mouldCntr = MouldHelper.GetMouldCntr(new models.MouldCntr() { S_CNTR_CODE = mst.S_CNTR_CODE }); model.mouldTyep = mouldCntr.S_MOULD_TYPE; model.mouldNo = mouldCntr.S_MOULD_NO; } if (HL.CreateOrder(model)) { mst.N_B_STATE = 1; WCSHelper.UpdateStatus(mst); } } else if (mst.S_SCHEDULE_TYPE == "LD") { TransferTiledInfo transferTiledInfo = new TransferTiledInfo() { businessType = "station_to_station", comment = mst.S_TYPE, endStationCode = mst.S_END_LOC, endAreaCode = mst.S_END_AREA, startStationCode = mst.S_START_LOC, startAreaCode = mst.S_START_AREA }; var cntrItems = ItemHelper.GetCntrItemByCntrCode(mst.S_CNTR_CODE); if (cntrItems != null && cntrItems.Count > 0) { transferTiledInfo.workCode = cntrItems[0].S_WORK_NO; transferTiledInfo.setCode = cntrItems[0].S_DEPART_NO; transferTiledInfo.partDrawingCode = cntrItems[0].S_PARTDRAW_NO; transferTiledInfo.processCode = cntrItems[0].S_OPER_NO; } // 物流调度 TODO logisticsTaskId != null string logisticsTaskId = ShopFloorControl.transferTiled(transferTiledInfo); if (logisticsTaskId != null) { WMSTask wmsTask = WMSHelper.GetWmsTask(mst.S_OP_CODE); wmsTask.S_OP_DEF_CODE = logisticsTaskId; WMSHelper.UpdateTask(wmsTask); mst.N_B_STATE = 1; WCSHelper.UpdateStatus(mst); }; } } return result; } public static Location getOutLocation(string locCode) { var db = new SqlHelper().GetInstance(); Location loc = LocationHelper.GetLoc(locCode); if (loc != null && loc.N_DEEP == 2 && loc.N_POS == 1) { return db.Queryable().Where(it => it.S_AREA_CODE == loc.S_AREA_CODE && (it.N_ROW == loc.N_ROW + 1 || it.N_ROW == loc.N_ROW - 1) && it.N_COL == loc.N_COL && it.N_LAYER == loc.N_LAYER && it.N_ROADWAY == loc.N_ROADWAY && it.N_POS == 2).First(); } return null; } internal static bool sendDDJTaskData(WCSTask mst) { var startLoc = LocationHelper.GetLocation(mst.S_START_LOC); var endLoc = LocationHelper.GetLocation(mst.S_END_LOC); // 1.查询对应输送线的堆垛机是否可入库、出库 bool allow = false; if (mst.S_TYPE.Contains("入库")) { LinePlcInfo linePlcInfo = WCSHelper.GetLinePlcInfo(mst.S_START_LOC); PipelineSignalInfo lineSignalInfo = WCSHelper.readPipelineInfo(linePlcInfo); LogHelper.Info("堆垛机任务推送 ==> 检测输送线堆垛机信息,线体:" + linePlcInfo.code + ",(入 ddjInfo = 2)信息:" + JsonConvert.SerializeObject(lineSignalInfo), "哈电"); if (lineSignalInfo != null && lineSignalInfo.ddjInfo == 2 || mst.S_ERR != null) { endLoc.N_ROW = rowChange(endLoc.N_ROW); startLoc.N_LAYER = 0; startLoc.N_COL = linePlcInfo.code; allow = true; } } else if (mst.S_TYPE.Contains("出库")) { LinePlcInfo linePlcInfo = WCSHelper.GetLinePlcInfo(mst.S_END_LOC); PipelineSignalInfo lineSignalInfo = WCSHelper.readPipelineInfo(linePlcInfo); LogHelper.Info("堆垛机任务推送 ==> 检测输送线堆垛机信息,线体:" + linePlcInfo.code + ",(出 ddjInfo = 1)信息:" + JsonConvert.SerializeObject(lineSignalInfo), "哈电"); if (lineSignalInfo != null && lineSignalInfo.ddjInfo == 1 || mst.S_ERR != null) { startLoc.N_ROW = rowChange(startLoc.N_ROW); endLoc.N_LAYER = 0; endLoc.N_COL = linePlcInfo.code; allow = true; } } if (mst.S_TYPE.Contains("移库")) { startLoc.N_ROW = rowChange(startLoc.N_ROW); endLoc.N_ROW = rowChange(endLoc.N_ROW); allow = true; } string plc = mst.S_EQ_NO; if (plc != null) { short[] resSignal = S7Helper.ReadInt(plc, 551, 0, 8); int taskNo1 = S7Helper.ReadDint(plc, 551, 16); int taskNo2 = S7Helper.ReadDint(plc, 550, 24); LogHelper.Info(plc + "号堆垛机任务推送 ==> 条件:taskNo1(读)= 0 ,taskNo2(写)= 0 ;系统任务号:" + mst.S_CODE + ",taskNo1:" + JsonConvert.SerializeObject(taskNo1) + " taskNo2:" + JsonConvert.SerializeObject(taskNo2), "哈电"); LogHelper.Info(plc + "号堆垛机任务推送 ==> 条件:resSignal[2] = 1,resSignal[3] = 1,resSignal[4] = 1;推送前堆垛机信号:" + JsonConvert.SerializeObject(resSignal), "哈电"); // 判断是否可发送堆垛机任务 if (allow && taskNo1 == 0 && taskNo2 == 0 && resSignal != null && resSignal[2] == 1 && resSignal[3] == 1 && resSignal[4] == 1) { // 3.写入堆垛机任务 short HandShake = 0; short handShakeValues = S7Helper.ReadInt(plc, 551, 0); if (handShakeValues != null) { if (handShakeValues == 0) { HandShake = 1; } else if (handShakeValues == 1) { HandShake = 0; } } LogHelper.Info("hhhhhhhhhhh", "哈电"); // 正常任务 命令 1.搬运 重置任务 命令 9.仅放货 short commandType = 1; if (mst.S_ERR != null) { commandType = 9; } // TODO s7 wcs -> plc 写入任务 try { RBWriteTaskInfo rBWriteTaskInfo = new RBWriteTaskInfo() { HandShake = HandShake, SRM_Num = short.Parse(mst.S_EQ_NO), CommandType = commandType, Source_Z_S1 = (short)startLoc.N_ROW, Source_X_S1 = (short)startLoc.N_COL, Source_Y_S1 = (short)startLoc.N_LAYER, Source_L_S1 = (short)startLoc.N_POS, Dest_Z_S1 = (short)endLoc.N_ROW, Dest_X_S1 = (short)endLoc.N_COL, Dest_Y_S1 = (short)endLoc.N_LAYER, Dest_L_S1 = (short)endLoc.N_POS, Task_S1 = int.Parse(mst.S_CODE.Substring(8)), // TODO 任务号待确定 ConfirmSignal1 = 1, }; RBWriteTask(plc, rBWriteTaskInfo); Console.WriteLine("堆垛机任务信号写入成功:" + JsonConvert.SerializeObject(rBWriteTaskInfo)); LogHelper.Info("堆垛机任务信号写入成功:" + JsonConvert.SerializeObject(rBWriteTaskInfo), "哈电"); } catch(Exception e) { LogHelper.Info("堆垛机任务信号写入错误,错误原因:" + e.Message, "哈电"); return false; } return true; } } else { Console.WriteLine("设备号不能为空"); } return false; } public static int rowChange(int row) { switch (row) { case 5: row = 1; break; case 6: row = 2; break; case 7: row = 3; break; } return row; } /// /// 堆垛机任务写入(推送任务) /// public static void RBWriteTask(string plc ,RBWriteTaskInfo model) { S7Helper.WriteInt(plc,550, 0, model.HandShake); S7Helper.WriteInt(plc, 550, 2, model.SRM_Num); S7Helper.WriteInt(plc, 550, 4, model.CommandType); S7Helper.WriteInt(plc, 550, 6, model.FireCommandType); S7Helper.WriteInt(plc, 550, 8, model.Source_Z_S1); S7Helper.WriteInt(plc, 550, 10, model.Source_X_S1); S7Helper.WriteInt(plc, 550, 12, model.Source_Y_S1); S7Helper.WriteInt(plc, 550, 14, model.Source_L_S1); S7Helper.WriteInt(plc, 550, 16, model.Dest_Z_S1); S7Helper.WriteInt(plc, 550, 18, model.Dest_X_S1); S7Helper.WriteInt(plc, 550, 20, model.Dest_Y_S1); S7Helper.WriteInt(plc, 550, 22, model.Dest_L_S1); S7Helper.WriteDint(plc, 550, 24, model.Task_S1); S7Helper.WriteInt(plc, 550, 28, model.ConfirmSignal1); S7Helper.WriteInt(plc, 550, 30, model.GoodType1); } public class RBWriteTaskInfo { //WCS与堆垛机握手信号,WCS每个周期将HandShake的值取反写入HandShake,3秒内握手信号未发生改变认为通讯中断 public short HandShake { get; set; } public short SRM_Num { get; set; } //堆垛机编号 //1:工位1搬运;4: 工位1移动(仅移动到目标地址);7 :工位1仅取货;9 :工位1仅放货;12:删除任务13:复位 public short CommandType { get; set; } public short FireCommandType { get; set; } // 消防备用 public short Source_Z_S1 { get; set; } // 取货排 1/2/3/4 public short Source_X_S1 { get; set; } // 取货列 public short Source_Y_S1 { get; set; } // 取货层 public short Source_L_S1 { get; set; } // 取货深(保留) public short Dest_Z_S1 { get; set; } // 放货排 public short Dest_X_S1 { get; set; } // 放货列 public short Dest_Y_S1 { get; set; } // 放货层 public short Dest_L_S1 { get; set; } // 放货深(保留) public int Task_S1 { get; set; } // 任务号 /// /// 写1时,PLC开始解析工位1任务命令类型及储位信息,注意写1时确保位置信息,命令类型等已写入,否则会出现解析错误或不解析情况 /// 当收到PLC的完成时(TaskStatus1=3)上位写3确认,PLC收到确认,清除任务状态 /// 0:默认值; /// 1:开始执行任务; /// 3:工位1任务正常完成确认; /// 8:上位写8为任务删除 /// 10.收到上位强制完成信号 /// public short ConfirmSignal1 { get; set; } // 状态码 public short GoodType1 { get; set; } // 货物类型(备用) } private static List freeLineInfos = new List(); internal static freeLineBit GetFreeLineBit(string area, int row, int col) { if (freeLineInfos.Count == 0) { freeLineInfos = LocationHelper.GetAllFreeLineInfo(); } var res = freeLineInfos.Where(a => a.S_AREA_CODE == area && a.N_ROW == row && col >= a.START_COL && col <= a.END_COL).FirstOrDefault(); if (res != null) { return new freeLineBit { area = area, row = row, minCol = res.START_COL, maxCol = res.END_COL, bit1 = res.FIRST_POINT, bit2 = res.SECOND_POINT }; } return null; } public class freeLineBit { public string area { get; set; } public int row { get; set; } public int minCol { get; set; } public int maxCol { get; set; } public int bit1 { get; set; } public int bit2 { get; set; } } /// /// 创建搬运任务 /// /// /// /// /// /// /// /// /// /// public static bool CreateTransport(string start, string end, string taskType, List cntrs, int startLayer, int endLayer, int trayCarryCount = 1, int priority = 1) { var result = false; //批次号存托盘号,1~3个托盘 var trayCodes = string.Join(",", cntrs); var taskNo = DateTime.Now.Ticks.ToString(); var res = WCSHelper.CreateTask(taskNo, start, end, taskType, priority, trayCodes, trayCarryCount, startLayer, endLayer); if (res) { result = true; //任务创建成功,起点货位出库锁定,终点货位入库锁定 LocationHelper.LockLoc(start, 1); LocationHelper.LockLoc(end, 2); } return result; } public static bool CreateTransport(string start, string end, string taskType, List cntrs, int priority = 1) { var result = false; //批次号存托盘号,1~3个托盘 var trayCodes = string.Join(",", cntrs); var taskNo = DateTime.Now.Ticks.ToString(); var res = WCSHelper.CreateTask(taskNo, start, end, taskType, priority, trayCodes, 1, 1, 1); if (res) { result = true; LocationHelper.LockLoc(start, 2); LocationHelper.LockLoc(end, 1); } return result; } #endregion } }