using Khd.Core.Domain.Dto.webapi; using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; using Khd.Core.Library.Mapper; using Khd.Core.Wcs.Global; using Masuit.Tools.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Z.EntityFramework.Plus; namespace Khd.Core.Wcs.Wcs { /// /// 背负式AGV /// public class FiveFloorBearAgv { List ScanPoint { get; set; }//点位信息 private readonly IHost _host; private readonly Plc.S7.Plc _plc; private readonly BasePlcpoint LineRFID; private readonly BasePlcpoint LineWcsrun; private readonly BasePlcpoint LineSignal; int FloorNo { get; set; } private readonly string EquipNo = ""; int agvID = 10; public FiveFloorBearAgv(IHost host, Plc.S7.Plc plc, int floor, string equipNo) { this._host = host; this._plc = plc; FloorNo = floor; EquipNo = equipNo; this.ScanPoint = StaticData.BasePlcpointList.Where(t => t.floorNo == floor).ToList();//加载当前站点所对应的点位 this.LineRFID = this.ScanPoint.First(t => t.plcpointNo.Contains("RFID")); this.LineWcsrun = this.ScanPoint.First(t => t.plcpointNo.Contains("wcsrun")); this.LineSignal = this.ScanPoint.First(t => t.plcpointNo.Contains("linesignal")); //var lineRFID = this._plc.Read(NodeSettingCarNo.plcpointAddress); try { //默认启动,清理plc的上位机写入点位值 //this._plc.Write(LineRFID.plcpointAddress, MainCentralControl.QingKongDianWei); } catch (Exception ex) { Console.WriteLine("楼层" + floor + " 初始化数据异常" + ex.Message); LogManager.Error(ex); } } /// /// 启动上件扫描监听 /// public void StartPoint() { Thread FlowPointThread = new(MonitorInLocatorPoint); FlowPointThread.Start(); Console.WriteLine(DateTime.Now + ":五楼AGV上件扫描监听启动成功"); LogManager.Info("五楼AGV上件扫描监听启动成功"); } public void MonitorInLocatorPoint() { using var scope = _host.Services.CreateScope(); using var dbContext = scope.ServiceProvider.GetRequiredService(); List taskType = new() { 1, 3, 5, 7 }; BaseEquip baseEquip = StaticData.BaseEquip.First(t => t.objid == agvID); while (true) { try { dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload()); var taskList = dbContext.WcsTask .Where(t => t.nextPointId == agvID) .OrderBy(t => t.createTime).ToList(); if (taskList.Count == 0) { LogManager.Info(FloorNo + "楼AGV无任务"); } foreach (var item in taskList)//出库 { if (item.taskStatus == 0)//下发任务 { BaseEquip startEquip = StaticData.BaseEquip.First(t => t.objid == item.startPointId); BaseEquip endEquip = StaticData.BaseEquip.First(t => t.objid == item.endPointId); BaseDictionary baseDictionary = dbContext.BaseDictionary.First(t => t.objid == item.taskType); WcsCmd wcsCmd = new() { objid = StaticData.SnowId.NextId(), cmdStatus = 0, taskId = item.objid, useFlag = 1, cmdType = item.taskType, containerNo = item.containerNo, createTime = DateTime.Now, createBy = FloorNo + "楼AGV", currPointId = item.startPointId, currPointNo = startEquip.agvPositionCode, nextPointId = item.endPointId, nextPointNo = endEquip.agvPositionCode, }; var agvTask = new RequestAGVTaskDto { reqCode = StaticData.SnowId.NextId().ToString(), positionCodePath = new List { new () { positionCode=wcsCmd.currPointNo, type=baseDictionary.agvType }, new () { positionCode=wcsCmd.nextPointNo, type=baseDictionary.agvType } }, taskTyp = baseDictionary.dicValue, ctnrTyp = "2", }; string message = JsonConvert.SerializeObject(agvTask); string result = HttpHelper.SendPostMessage(baseEquip.serverIp, baseEquip.serverPort.Value, "rcms/services/rest/hikRpcService/genAgvSchedulingTask", message); var reponseMessage = JsonConvert.DeserializeObject(result); if (reponseMessage != null && reponseMessage.code == "0") { wcsCmd.taskCode = reponseMessage.data; wcsCmd.cmdStatus = 1; item.taskStatus = 1; dbContext.Update(item); dbContext.WcsTaskLog.Where(t => t.objid == item.objid).Update(t => new WcsTaskLog() { taskStatus = 1 }); WcsCmdLog wcsCmdLog = CoreMapper.Map(wcsCmd); dbContext.Add(wcsCmdLog); dbContext.Add(wcsCmd); dbContext.SaveChanges(); } else { LogManager.Info("下发小车任务失败" + result); } } else { if (item.useFlag == 1)//接料位-工位默认为1,工位到接料位需Mes通知 { WcsCmd? wcsCmd = dbContext.WcsCmd.FirstOrDefault(t => t.taskId == item.objid); if (wcsCmd != null) { if (wcsCmd.cmdStatus == 3) { var agvTask = new RequestAGVTaskDto { reqCode = StaticData.SnowId.NextId().ToString(), taskCode = wcsCmd.taskCode }; string message = JsonConvert.SerializeObject(agvTask); string result = HttpHelper.SendPostMessage(baseEquip.serverIp, baseEquip.serverPort.Value, "rcms/services/rest/hikRpcService/continueTask", message); ReponseMessage? reponseMessage = JsonConvert.DeserializeObject(result); if (reponseMessage != null && reponseMessage.message == "成功") { dbContext.WcsCmd.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmd() { cmdStatus = 4 }); dbContext.WcsCmdLog.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmdLog() { cmdStatus = 4 }); dbContext.WcsTask.Where(t => t.objid == item.objid).Update(t => new WcsTask() { taskStatus = 4 }); dbContext.WcsTaskLog.Where(t => t.objid == item.objid).Update(t => new WcsTaskLog() { taskStatus = 4 }); dbContext.SaveChanges(); } } else if (wcsCmd.cmdStatus == 5) { dbContext.WcsCmd.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmd() { cmdStatus = 6 }); dbContext.WcsCmdLog.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmdLog() { cmdStatus = 6 }); dbContext.WcsTask.Where(t => t.objid == item.objid).Update(t => new WcsTask() { taskStatus = 6 }); dbContext.WcsTaskLog.Where(t => t.objid == item.objid).Update(t => new WcsTaskLog() { taskStatus = 6 }); dbContext.SaveChanges(); } else if (wcsCmd.cmdStatus == 7) { var agvTask = new RequestAGVTaskDto { reqCode = StaticData.SnowId.NextId().ToString(), taskCode = wcsCmd.taskCode }; string message = JsonConvert.SerializeObject(agvTask); string result = HttpHelper.SendPostMessage(baseEquip.serverIp, baseEquip.serverPort.Value, "rcms/services/rest/hikRpcService/continueTask", message); ReponseMessage? reponseMessage = JsonConvert.DeserializeObject(result); if (reponseMessage != null && reponseMessage.message == "成功") { dbContext.WcsCmd.Where(t => t.objid == wcsCmd.objid).Delete(); dbContext.WcsCmdLog.Where(t => t.objid == wcsCmd.objid).Update(t => new WcsCmdLog() { cmdStatus = 8 }); dbContext.WcsTask.Where(t => t.objid == item.objid).Delete(); dbContext.WcsTaskLog.Where(t => t.objid == item.objid).Update(t => new WcsTaskLog() { taskStatus = 8 }); if (item.taskType == 48) { dbContext.WmsRawReturn.Where(t => t.rawReturnId == item.orderId).Update(t => new WmsRawReturn { executeStatus = "2" }); } else { } dbContext.SaveChanges(); } } } } } break; } } catch (Exception ex) { LogManager.Error(ex); } finally { Thread.Sleep(1000); } } } } }