using Khd.Core.Domain.Dto.webapi;
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Wcs.Global;
using Masuit.Tools.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Z.EntityFramework.Plus;
namespace Khd.Core.Wcs.Wcs
{
///
/// 五楼AGV调度
///
public class FiveFloorAGV
{
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;
private readonly BasePlcpoint LineIsPallet;
private readonly BasePlcpoint LineSerialNO;
private readonly BasePlcpoint LineFeedSeriaNo;
int FloorNo { get; set; }
string EquipNo = "";
int agvID = 10;
public FiveFloorAGV(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"));
this.LineIsPallet = this.ScanPoint.First(t => t.plcpointNo.Contains("ispallet"));
this.LineSerialNO = this.ScanPoint.First(t => t.plcpointNo.Contains("serialno"));
this.LineFeedSeriaNo = this.ScanPoint.First(t => t.plcpointNo.Contains("feedserialno"));
//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 Thread(MonitorInLocatorPoint);
FlowPointThread.Start();
}
public void MonitorInLocatorPoint()
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService();
List taskType = new() { 1, 2, 3, 7, 8 };
while (true)
{
try
{
var taskList = dbContext.WcsTask.Where(t => t.nextPointId == agvID && taskType.Contains(t.taskType)).ToList();
if (taskList.Count == 0)
{
LogManager.Info(FloorNo + "楼AGV无任务");
}
foreach (var item in taskList)
{
SendAndUpdateTask(item);
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
finally
{
Thread.Sleep(1000);
}
}
}
public List GetTask(int floorNo, string equipNo)
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService();
List wcsTask = new List();
var wareHouseList = StaticData.WmsBaseWarehouse.ToList();
var equip = StaticData.BaseEquip.Where(t => t.floorNo == floorNo && t.equipType == 4).FirstOrDefault();
//入库类型
List taskInType = new List { 1, 3, 5, 7 };
List taskOutType = new List { 2, 4, 6, 8 };
try
{
//获取条码号,如果该条码任务存在就继续任务,如果条码不存在,创建入库任务并调度agv
var taskList = StaticData.WcsTask.Where(t => t.nextPointId == equip.objid).ToList();
if (taskList.Count() == 0)
{ return null; }
wcsTask = taskList;
}
catch (Exception ex)
{
LogManager.Info(floorNo + "楼AGV异常" + ex.Message);
throw;
}
return wcsTask;
}
public void SendAndUpdateTask(WcsTask task)
{
//获取
if (task == null) return;
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService();
WcsToWms wcsToWms = new WcsToWms();
//首先判断是否已下发指令
var cmd = dbContext.WcsCmd.Where(t => t.taskId == task.objid).FirstOrDefault();
var locList = dbContext.WmsBaseLocation.Where(t => t.activeFlag == "1").ToList();
if (locList.Count == 0) return;
//指令表存在说明已下发
if (cmd == null)
{ //获取下发agv指令
string ip = ""; int port = 0; string url = "";
RequestAGVTaskDto agvtask = new RequestAGVTaskDto();
agvtask.reqCode = task.objid.ToString();
//var json = JsonConvert.SerializeObject(agvtask);
//HttpHelper.SendPostMessage(ip, port, url, json);
agvtask.positionCodePath = new List();
Position p = new Position();
WmsBaseLocation putPos = new WmsBaseLocation(); //放料点
WmsBaseLocation setPos = new WmsBaseLocation(); //取料点
if (task.taskType == 5) //入库
{
setPos = locList.Where(t => t.locationId == task.nextPointId).FirstOrDefault();
}
else
{
setPos = locList.Where(t => t.locationId == task.nextPointId).FirstOrDefault();
}
putPos = locList.Where(t => t.locationId == task.nextPointId).FirstOrDefault();
p.podCode = setPos?.agvPositionCode;
p.podTyp = "";
agvtask.positionCodePath.Add(p);
p.podCode = putPos?.agvPositionCode;
p.podTyp = "";
//取料点
agvtask.positionCodePath.Add(p);
agvtask.taskTyp = "";
//给agv创建任务
wcsToWms.genAgvSchedulingTask(agvtask);
//未下发给agv下发指令
WcsCmd taskCmd = new WcsCmd()
{
taskId = task.objid,
cmdType = task.taskType,
serialNo = task.serialNo,
equipmentNo = task.equipmentNo,
cmdStatus = 1,
createBy = FloorNo + "楼AGV",
createTime = DateTime.Now,
};
dbContext.Add(taskCmd);
dbContext.SaveChanges();
//更新任务表
dbContext.WcsTask.Where(t => t.objid == task.objid).Update(t => new WcsTask()
{
currPointId = task.currPointId,
currPointNo = task.currPointNo,
nextPointId = task.nextPointId,
nextPointNo = task.nextPointNo,
updateTime = DateTime.Now,
updateBy = "agv出库",
});
}
}
}
}