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.

502 lines
32 KiB
C#

2 years ago
using Khd.Core.Application;
using Khd.Core.Application.Interface;
2 years ago
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
{
/// <summary>
2 years ago
/// 一楼线程(包括接驳位,提升机调度)
2 years ago
/// </summary>
public class FirstFloor
{
private readonly IHost _host;
private readonly Plc.S7.Plc _plc;
2 years ago
private long F01 = 1;
2 years ago
private long T01 = 6;
2 years ago
/// <summary>
/// 一楼RFID 读
/// </summary>
2 years ago
private readonly BasePlcpoint RFID001;
2 years ago
/// <summary>
/// 到位信号 读
/// </summary>
2 years ago
private readonly BasePlcpoint linesignal01 ;
2 years ago
/// <summary>
/// 是否有托盘 读
/// </summary>
2 years ago
private readonly BasePlcpoint ispallet01 ;
2 years ago
/// <summary>
/// 去向 写
/// </summary>
2 years ago
private readonly BasePlcpoint wcsrun01 ;
2 years ago
/// <summary>
/// 流水号 读
/// </summary>
2 years ago
private readonly BasePlcpoint serialno01 ;
2 years ago
/// <summary>
2 years ago
/// 提升机流水号 读
2 years ago
/// </summary>
2 years ago
private readonly BasePlcpoint serialno06 ;
2 years ago
/// <summary>
2 years ago
/// 提升机状态 读
2 years ago
/// </summary>
2 years ago
private readonly BasePlcpoint equipstate06 ;
2 years ago
/// <summary>
2 years ago
/// 提升机任务状态 读
2 years ago
/// </summary>
2 years ago
private readonly BasePlcpoint taskstate06 ;
2 years ago
/// <summary>
2 years ago
/// 提升机当前楼层 写
2 years ago
/// </summary>
2 years ago
private readonly BasePlcpoint currentfloor06 ;
2 years ago
/// <summary>
2 years ago
/// 提升机目的楼层 写
2 years ago
/// </summary>
2 years ago
private readonly BasePlcpoint targetfloor06 ;
2 years ago
/// <summary>
2 years ago
/// 提升机到位信号 读
2 years ago
/// </summary>
2 years ago
private readonly BasePlcpoint linesignal06 ;
2 years ago
/// <summary>
/// 一楼提升机前允许取 读
/// </summary>
2 years ago
private readonly BasePlcpoint hoisterallowedtake01 ;
2 years ago
/// <summary>
/// 一楼提升机前反馈流水号 读
/// </summary>
2 years ago
private readonly BasePlcpoint feedserialno01 ;
2 years ago
/// <summary>
/// 二楼提升机前允许取 读
/// </summary>
2 years ago
private readonly BasePlcpoint hoisterallowedtake02 ;
2 years ago
/// <summary>
/// 二楼提升机前反馈流水号 读
/// </summary>
2 years ago
private readonly BasePlcpoint feedserialno02 ;
2 years ago
/// <summary>
/// 三楼提升机前允许取 读
/// </summary>
2 years ago
private readonly BasePlcpoint hoisterallowedtake03 ;
2 years ago
/// <summary>
/// 三楼提升机前反馈流水号 读
/// </summary>
2 years ago
private readonly BasePlcpoint feedserialno03 ;
2 years ago
/// <summary>
/// 四楼提升机前允许取 读
/// </summary>
2 years ago
private readonly BasePlcpoint hoisterallowedtake04 ;
2 years ago
/// <summary>
/// 四楼提升机前反馈流水号 读
/// </summary>
2 years ago
private readonly BasePlcpoint feedserialno04 ;
2 years ago
/// <summary>
/// 五楼提升机前允许取 读
/// </summary>
2 years ago
private readonly BasePlcpoint hoisterallowedtake05 ;
2 years ago
/// <summary>
/// 五楼提升机前反馈流水号 读
/// </summary>
2 years ago
private readonly BasePlcpoint feedserialno05 ;
2 years ago
2 years ago
public FirstFloor(IHost host, Plc.S7.Plc plc, string siteNo)
{
this._host = host;
this._plc = plc;
//一楼RFID 读
2 years ago
this.RFID001 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("RFID001"));
2 years ago
//到位信号 读
2 years ago
this.linesignal01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("linesignal01"));
2 years ago
//是否有托盘 读
2 years ago
this.ispallet01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("ispallet01"));
2 years ago
//去向 写
2 years ago
this.wcsrun01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("wcsrun01"));
2 years ago
//流水号 读
2 years ago
this.serialno01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("serialno01"));
2 years ago
//一楼提升机流水号 读
2 years ago
this.serialno06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("serialno06"));
2 years ago
//一楼提升机状态 读
2 years ago
this.equipstate06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("equipstate06"));
2 years ago
//一楼提升机任务状态 读
2 years ago
this.taskstate06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("taskstate06"));
2 years ago
//一楼提升机当前楼层 写
2 years ago
this.currentfloor06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("currentfloor06"));
2 years ago
//一楼提升机目的楼层 写
2 years ago
this.targetfloor06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("targetfloor06"));
2 years ago
//一楼提升机到位信号 读
2 years ago
this.linesignal06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("linesignal06"));
2 years ago
//一楼提升机前允许取
2 years ago
this.hoisterallowedtake01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake01"));
2 years ago
//一楼提升机前反馈流水号
2 years ago
this.feedserialno01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno01"));
this.hoisterallowedtake02 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("SecondFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake02"));
this.feedserialno02 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("SecondFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno02"));
this.hoisterallowedtake03 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("ThirdFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake03"));
this.feedserialno03 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("ThirdFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno03"));
this.hoisterallowedtake04 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FourthlyFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake04"));
this.feedserialno04 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FourthlyFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno04"));
this.hoisterallowedtake05 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FifthFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake05"));
this.feedserialno05 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FifthFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno05"));
2 years ago
2 years ago
}
2 years ago
2 years ago
/// <summary>
/// 启动线程
/// </summary>
public void StartPoint()
{
Thread firstFloorLine = new Thread(FirstFloorLine);
firstFloorLine.IsBackground = true;
firstFloorLine.Start();
Console.WriteLine("启动一楼接驳位线程");
Thread firstFloorHoister = new Thread(FirstFloorHoister);
firstFloorHoister.IsBackground = true;
firstFloorHoister.Start();
Console.WriteLine("启动一楼提升机线程");
}
2 years ago
2 years ago
/// <summary>
/// 启动一楼接驳位线程
/// </summary>
private void FirstFloorLine()
{
2 years ago
List<int?> Itpyes = new List<int?>() { 1, 3, 5, 7 };
2 years ago
while (true)
{
try
{
var RFID001Value = this._plc.Read(this.RFID001.plcpointAddress); //一楼RFID 读
var linesignal01Value = this._plc.Read(this.linesignal01.plcpointAddress); //到位信号 读
var ispallet01Value = this._plc.Read(this.ispallet01.plcpointAddress); //是否有托盘 读
var wcsrun01Value = this._plc.Read(this.wcsrun01.plcpointAddress); //去向 写
var serialno01Value = this._plc.Read(this.serialno01.plcpointAddress); //流水号 读
2 years ago
2 years ago
using (var scope = _host.Services.CreateScope())
{
using (var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>())
{
//正常读到plc值
if (linesignal01Value != null && RFID001Value != null || ispallet01Value != null || serialno01Value != null)
{
//正常托盘到位
if (Convert.ToInt32(linesignal01Value) != 0 && Convert.ToInt32(serialno01Value) != 0 && Convert.ToInt32(ispallet01Value) == 1 && Convert.ToInt32(ispallet01Value) != 0)
{
2 years ago
//判断task表里没有该rfid的未完成的入库信息未下发去向
2 years ago
var task = dbContext.WcsTask.Where(t => t.containerNo == RFID001Value.ToString() && t.taskStatus < 1).FirstOrDefault();
2 years ago
if (task == null || Itpyes.Contains(task.taskType))
2 years ago
{
2 years ago
//入库
2 years ago
if (task == null || Itpyes.Contains(task.taskType))
2 years ago
{
2 years ago
//根据托盘号获取物料码
var material = dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value.ToString()).FirstOrDefault();
if (material != null)
2 years ago
{
2 years ago
var warehouseId = dbContext.WmsWarehouseMaterial.Where(t => t.storageType == "1" && t.storageId == material.materialId).FirstOrDefault()?.warehouseId;
if (warehouseId != null)
2 years ago
{
2 years ago
var TargetFloor = dbContext.WmsBaseWarehouse.Where(t => t.warehouseId == warehouseId).FirstOrDefault();
if (TargetFloor != null)
2 years ago
{
2 years ago
//插入task表
2 years ago
var dic = StaticData.BaseDictionary.Where(t => t.dicKey == "TaskType" && t.ud1 == "I" && t.dicField == TargetFloor.warehouseInstockType).FirstOrDefault();
if (dic != null)
2 years ago
{
2 years ago
if (task == null)
{
var newTask = new WcsTask()
{
objid = StaticData.SnowId.NextId(),
serialNo = Convert.ToInt32(serialno01Value),
equipmentNo = "F01",
taskType = Convert.ToInt32(dic.dicValue),
taskStatus = 0,
containerNo = RFID001Value.ToString(),
materialNo = material.materialCode,
materialId = material.materialId,
qty = Convert.ToInt32(material.bindAmount),
startPointId = F01,
startPointNo = "F01",
currPointId = F01,
currPointNo = "F01",
nextPointId = T01,
nextPointNo = "T01",
endPointId = warehouseId,
floorNo = TargetFloor.warehouseFloor,
useFlag = 1,
createBy = "一楼接驳位",
createTime = DateTime.Now,
remark = "一楼创建入库任务"
};
dbContext.Add(newTask);
}
else
{
task.currPointId = F01;
task.currPointNo = "F01";
task.nextPointId = T01;
task.nextPointNo = "T01";
2 years ago
task.taskStatus = 0;
2 years ago
task.updateBy = "一楼接驳位线程";
task.updateTime = DateTime.Now;
task.remark = "一楼创建入库任务";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
}
//下发去向
this._plc.Write(this.wcsrun01.plcpointAddress, 1);
}
2 years ago
}
2 years ago
}
}
}
2 years ago
//出库
{
//清空托盘绑定
var material = dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value.ToString()).FirstOrDefault();
if (material != null)
{
dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value.ToString()).Delete();
dbContext.SaveChanges();
}
}
2 years ago
}
2 years ago
}
}
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
2 years ago
Thread.Sleep(1000);
2 years ago
}
}
/// <summary>
2 years ago
/// 提升机线程
2 years ago
/// </summary>
private void FirstFloorHoister()
{
while (true)
{
try
{
2 years ago
var serialno06Value = this._plc.Read(this.serialno06.plcpointAddress); //提升机流水号 读
var equipstate06Value = this._plc.Read(this.equipstate06.plcpointAddress); //提升机状态 读
var taskstate06Value = this._plc.Read(this.taskstate06.plcpointAddress); //提升机任务状态 读
var currentfloor06Value = this._plc.Read(this.currentfloor06.plcpointAddress); //提升机当前楼层 写
var targetfloor06Value = this._plc.Read(this.targetfloor06.plcpointAddress); //提升机目的楼层 写
var linesignal06Value = this._plc.Read(this.linesignal06.plcpointAddress); //提升机到位信号 读
var hoisterallowedtake01Value = this._plc.Read(this.hoisterallowedtake01.plcpointAddress); //一楼提升机前允许取
var feedserialno01Value = this._plc.Read(this.feedserialno01.plcpointAddress); //一楼提升机前反馈流水号
var hoisterallowedtake02Value = this._plc.Read(this.hoisterallowedtake02.plcpointAddress); //二楼提升机前允许取
var feedserialno02Value = this._plc.Read(this.feedserialno02.plcpointAddress); //二楼提升机前反馈流水号
var hoisterallowedtake03Value = this._plc.Read(this.hoisterallowedtake03.plcpointAddress); //三楼提升机前允许取
var feedserialno03Value = this._plc.Read(this.feedserialno03.plcpointAddress); //三楼提升机前反馈流水号
var hoisterallowedtake04Value = this._plc.Read(this.hoisterallowedtake04.plcpointAddress); //四楼提升机前允许取
var feedserialno04Value = this._plc.Read(this.feedserialno04.plcpointAddress); //四楼提升机前反馈流水号
var hoisterallowedtake05Value = this._plc.Read(this.hoisterallowedtake05.plcpointAddress); //五楼提升机前允许取
var feedserialno05Value = this._plc.Read(this.feedserialno05.plcpointAddress); //五楼提升机前反馈流水号
2 years ago
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
//正常读到plc值
if (targetfloor06Value != null && serialno06Value != null && equipstate06Value != null && taskstate06Value != null && currentfloor06Value != null && linesignal06Value != null)
2 years ago
{
2 years ago
//提升机空闲
if (Convert.ToInt32(equipstate06Value) == 0)
2 years ago
{
2 years ago
var wcsTask = dbContext.WcsTask.Where(t => t.nextPointId == T01).Where(t => t.taskStatus > 1).FirstOrDefault();
if (wcsTask == null)
2 years ago
{
2 years ago
//一楼到位
if (hoisterallowedtake01Value != null && feedserialno01Value != null && Convert.ToInt32(hoisterallowedtake01Value) == 1 && Convert.ToInt32(feedserialno01Value) > 0)
2 years ago
{
2 years ago
var task = dbContext.WcsTask.Where(t => t.serialNo == Convert.ToInt32(feedserialno01Value)).FirstOrDefault();
if (task != null)
2 years ago
{
2 years ago
if (task.nextPointId == T01)
2 years ago
{
2 years ago
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
2 years ago
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 1);
2 years ago
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
2 years ago
//修改task状态
task.currPointId = T01;
task.currPointNo = "T01";
task.nextPointId = nextPoint.objid;
task.nextPointNo = nextPoint.equipNo;
task.taskStatus = 1;
task.updateBy = "提升机线程";
task.updateTime = DateTime.Now;
task.remark = "一楼到位提升机线程";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
2 years ago
continue;
2 years ago
}
}
}
2 years ago
}
//二楼到位
if (hoisterallowedtake02Value != null && feedserialno02Value != null && Convert.ToInt32(hoisterallowedtake02Value) == 1 && Convert.ToInt32(feedserialno02Value) > 0)
{
var task = dbContext.WcsTask.Where(t => t.serialNo == Convert.ToInt32(feedserialno01Value)).FirstOrDefault();
if (task != null)
2 years ago
{
2 years ago
if (task.nextPointId == T01)
2 years ago
{
2 years ago
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
2 years ago
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 2);
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
//修改task状态
task.currPointId = T01;
task.currPointNo = "T01";
task.nextPointId = nextPoint.objid;
task.nextPointNo = nextPoint.equipNo;
task.taskStatus = 1;
task.updateBy = "提升机线程";
task.updateTime = DateTime.Now;
task.remark = "二楼到位提升机线程";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
2 years ago
continue;
2 years ago
}
}
}
2 years ago
}
//三楼到位
if (hoisterallowedtake03Value != null && feedserialno03Value != null && Convert.ToInt32(hoisterallowedtake03Value) == 1 && Convert.ToInt32(feedserialno03Value) > 0)
{
var task = dbContext.WcsTask.Where(t => t.serialNo == Convert.ToInt32(feedserialno01Value)).FirstOrDefault();
if (task != null)
2 years ago
{
2 years ago
if (task.nextPointId == T01)
2 years ago
{
2 years ago
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
2 years ago
{
//给提升机下发去向
2 years ago
this._plc.Write(this.currentfloor06.plcpointAddress, 3);
2 years ago
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
//修改task状态
task.currPointId = T01;
task.currPointNo = "T01";
task.nextPointId = nextPoint.objid;
task.nextPointNo = nextPoint.equipNo;
task.taskStatus = 1;
task.updateBy = "提升机线程";
task.updateTime = DateTime.Now;
task.remark = "三楼到位提升机线程";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
2 years ago
continue;
2 years ago
}
}
}
2 years ago
}
//四楼到位
if (hoisterallowedtake04Value != null && feedserialno04Value != null && Convert.ToInt32(hoisterallowedtake04Value) == 1 && Convert.ToInt32(feedserialno04Value) > 0)
{
var task = dbContext.WcsTask.Where(t => t.serialNo == Convert.ToInt32(feedserialno01Value)).FirstOrDefault();
if (task != null)
2 years ago
{
2 years ago
if (task.nextPointId == T01)
2 years ago
{
2 years ago
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
2 years ago
{
//给提升机下发去向
2 years ago
this._plc.Write(this.currentfloor06.plcpointAddress, 4);
2 years ago
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
//修改task状态
task.currPointId = T01;
task.currPointNo = "T01";
task.nextPointId = nextPoint.objid;
task.nextPointNo = nextPoint.equipNo;
task.taskStatus = 1;
task.updateBy = "提升机线程";
task.updateTime = DateTime.Now;
task.remark = "四楼到位提升机线程";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
2 years ago
continue;
2 years ago
}
}
}
2 years ago
}
//五楼到位
if (hoisterallowedtake05Value != null && feedserialno05Value != null && Convert.ToInt32(hoisterallowedtake05Value) == 1 && Convert.ToInt32(feedserialno05Value) > 0)
{
var task = dbContext.WcsTask.Where(t => t.serialNo == Convert.ToInt32(feedserialno01Value)).FirstOrDefault();
if (task != null)
2 years ago
{
2 years ago
if (task.nextPointId == T01)
2 years ago
{
2 years ago
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
2 years ago
{
//给提升机下发去向
2 years ago
this._plc.Write(this.currentfloor06.plcpointAddress, 5);
2 years ago
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
//修改task状态
task.currPointId = T01;
task.currPointNo = "T01";
task.nextPointId = nextPoint.objid;
task.nextPointNo = nextPoint.equipNo;
task.taskStatus = 1;
task.updateBy = "提升机线程";
task.updateTime = DateTime.Now;
task.remark = "五楼到位提升机线程";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
2 years ago
continue;
2 years ago
}
}
}
2 years ago
}
}
2 years ago
else
{
Console.WriteLine($"{DateTime.Now}:提升机有任务未反,流水号为{wcsTask.serialNo}······");
LogManager.Info($"提升机有任务未反,流水号为{wcsTask.serialNo}······");
}
2 years ago
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
2 years ago
Thread.Sleep(1000);
2 years ago
}
}
}
}