You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
9.9 KiB
C#

1 year ago
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
2 years ago
using Khd.Core.Library.Mapper;
1 year ago
using Khd.Core.Plc.S7;
using Khd.Core.Wcs.Global;
using Masuit.Tools.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2 years ago
using Z.EntityFramework.Plus;
namespace Khd.Core.Wcs.Wcs
{
/// <summary>
2 years ago
/// 三楼接驳位调度
/// </summary>
2 years ago
public class ThirdFloorPoint
{
private readonly IHost _host;
2 years ago
private readonly BasePlcpoint LineRFID;
private readonly BasePlcpoint LineSignal;
int FloorNo { get; set; }
1 year ago
public ThirdFloorPoint(IHost host, Plc.S7.Plc plc, int floor)
{
this._host = host;
FloorNo = floor;
1 year ago
this.LineRFID = StaticData.BasePlcpointList.First(t => t.plcpointNo == "RFID003");
this.LineSignal = StaticData.BasePlcpointList.First(t => t.plcpointNo == "linesignal03");
}
/// <summary>
/// 启动上件扫描监听
/// </summary>
public void StartPoint()
{
2 years ago
Thread FlowPointThread = new Thread(MonitorInLocatorPoint);
FlowPointThread.Start();
2 years ago
Console.WriteLine(DateTime.Now + ":三楼接驳位调度启动成功");
LogManager.Info("三楼接驳位调度启动成功");
}
public void MonitorInLocatorPoint()
{
2 years ago
List<int> ITypes = new List<int> { 1, 3, 5, 7 };
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
while (true)
{
try
{
2 years ago
dbContext.ChangeTracker.Entries().ToList().ForEach(e => e.Reload());
//入库任务
1 year ago
var rfid = StaticData.PlcDic[0].ReadRFID(LineRFID.plcpointAddress);
var isSignal = StaticData.PlcDic[0].Read(LineSignal.plcpointAddress);
2 years ago
if (rfid != null && isSignal != null)
{
2 years ago
if (Convert.ToInt32(isSignal) == 1)//托盘到位
{
2 years ago
BaseEquip baseEquip = StaticData.BaseEquip.First(t => t.floorNo == 3 && t.equipType == 1);
1 year ago
var wcsTask = dbContext.WcsTask.FirstOrDefault(t => t.nextPointId == baseEquip.objid);
2 years ago
var AgvEquip = StaticData.BaseEquip.First(t => t.floorNo == 3 && t.equipType == 4);//背负Agv
2 years ago
if (wcsTask != null)//如果不是null
{
if (wcsTask.taskStatus == 5)//提升机任务是完成状态
{
1 year ago
var wmsBaseLocations = dbContext.WmsBaseLocation.Where(t => t.warehouseFloor == FloorNo)
.Where(t => t.activeFlag == "1")
.Where(t => t.delFlag == "0")
.Where(t => t.locationScrapType == "1")
.Where(t => t.locationStatus == "1")
1 year ago
.OrderByDescending(t => t.locRow)
.ThenByDescending(t => t.locColumn)
1 year ago
.ToList();
2 years ago
1 year ago
List<string> containerCodes = wmsBaseLocations.Where(t => t.locDeep == 1).Select(t => t.containerCode).ToList();//深库位的托盘
List<MesBasePalletInfo> mesBasePalletInfos = dbContext.MesBasePalletInfo
.Where(t => t.materialId == wcsTask.materialId)
.Where(t => containerCodes.Contains(t.palletInfoCode)).ToList();//深库位的托盘的物料等于当前任务的物料
2 years ago
1 year ago
1 year ago
var bill = from a in mesBasePalletInfos
1 year ago
join b in wmsBaseLocations.Where(t => t.locDeep == 1 && !string.IsNullOrEmpty(t.containerCode)) on a.palletInfoCode equals b.containerCode
1 year ago
select new { b };//等于当前任务的物料的托盘的库位信息
2 years ago
1 year ago
var outBill = from b in wmsBaseLocations
from a in bill
1 year ago
where a.b.layerNum == b.layerNum
&& b.locDeep == 2
1 year ago
&& b.locRow == (a.b.locRow % 2 == 0 ? a.b.locRow - 1 : a.b.locRow + 1)
1 year ago
&& a.b.locColumn == b.locColumn
&& b.locationStatus == "1"
&& string.IsNullOrEmpty(b.containerCode)
1 year ago
select new { b };//在上面的基础上获取对应托盘的外侧库位的空库位信息
1 year ago
WmsBaseLocation? wmsBaseLocation = outBill.FirstOrDefault()?.b;//先找相同物料的外侧库位
1 year ago
wmsBaseLocation ??= wmsBaseLocations.Where(t => string.IsNullOrEmpty(t.containerCode)).FirstOrDefault(t => t.locDeep == 1);//找不到再找深库位
1 year ago
wmsBaseLocation ??= wmsBaseLocations.Where(t => string.IsNullOrEmpty(t.containerCode)).FirstOrDefault();//找不到再找任意库位
1 year ago
1 year ago
if (wmsBaseLocations.Count > 0 && wmsBaseLocation != null)
{
dbContext.Remove(wcsTask);
WcsTask newTask = CoreMapper.Map<WcsTask>(wcsTask);
newTask.objid = StaticData.SnowId.NextId();
newTask.taskStatus = 0;//创建状态
newTask.updateTime = DateTime.Now;
newTask.currPointId = baseEquip.objid;
newTask.currPointNo = baseEquip.equipNo;
newTask.nextPointId = AgvEquip.objid;
newTask.nextPointNo = AgvEquip.equipNo;
newTask.endPointId = wmsBaseLocation.locationId;
newTask.endPointNo = wmsBaseLocation.locationCode;
1 year ago
newTask.taskType = 39;
1 year ago
newTask.useFlag = 1;
wmsBaseLocation.locationStatus = "2";
dbContext.Update(wmsBaseLocation);
1 year ago
dbContext.Add(newTask);
WcsTaskLog wcsTaskLog = CoreMapper.Map<WcsTaskLog>(newTask);
1 year ago
dbContext.Add(wcsTaskLog);
dbContext.SaveChanges();
}
else
{
Console.WriteLine(DateTime.Now + ":三楼楼接驳位调度入库任务,未找到库位");
LogManager.Info("三楼接驳位调度入库任务,未找到库位");
2 years ago
}
}
else if (wcsTask.taskStatus == 6)//小车任务是完成状态,说明是出库
{
BaseEquip nextEquip = StaticData.BaseEquip.First(t => t.equipType == 2);//提升机
1 year ago
dbContext.Remove(wcsTask);
WcsTask newTask = CoreMapper.Map<WcsTask>(wcsTask);
newTask.objid = StaticData.SnowId.NextId();
1 year ago
newTask.containerNo = rfid;
1 year ago
newTask.nextPointId = nextEquip.objid;
newTask.nextPointNo = nextEquip.equipNo;
newTask.currPointId = baseEquip.objid;
newTask.currPointNo = baseEquip.equipNo;
newTask.fromFloorNo = FloorNo;
newTask.taskStatus = 0;
newTask.updateTime = DateTime.Now;
dbContext.Add(newTask);
WcsTaskLog wcsTaskLog = CoreMapper.Map<WcsTaskLog>(newTask);
dbContext.Add(wcsTaskLog);
2 years ago
dbContext.SaveChanges();
}
}
}
}
}
catch (Exception ex)
{
1 year ago
if (ex is PlcException)
{
try
{
foreach (var item in StaticData.PlcDic)
{
if (item.Value.IP == ex.Message)
{
StaticData.PlcDic[item.Key] = new Plc.S7.Plc(item.Value.CPU, item.Value.IP, item.Value.Port, item.Value.Rack, item.Value.Slot);
StaticData.PlcDic[item.Key].Open();
}
}
}
catch
{
}
}
LogManager.Error(ex);
}
finally
{
Thread.Sleep(1000);
}
}
}
}
}