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#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Khd.Core.Application;
using Khd.Core.Application.Interface;
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>
/// 一楼线程(包括接驳位,提升机调度)
/// </summary>
public class FirstFloor
{
private readonly IHost _host;
private readonly Plc.S7.Plc _plc;
private long F01 = 1;
private long T01 = 6;
/// <summary>
/// 一楼RFID 读
/// </summary>
private readonly BasePlcpoint RFID001;
/// <summary>
/// 到位信号 读
/// </summary>
private readonly BasePlcpoint linesignal01 ;
/// <summary>
/// 是否有托盘 读
/// </summary>
private readonly BasePlcpoint ispallet01 ;
/// <summary>
/// 去向 写
/// </summary>
private readonly BasePlcpoint wcsrun01 ;
/// <summary>
/// 流水号 读
/// </summary>
private readonly BasePlcpoint serialno01 ;
/// <summary>
/// 提升机流水号 读
/// </summary>
private readonly BasePlcpoint serialno06 ;
/// <summary>
/// 提升机状态 读
/// </summary>
private readonly BasePlcpoint equipstate06 ;
/// <summary>
/// 提升机任务状态 读
/// </summary>
private readonly BasePlcpoint taskstate06 ;
/// <summary>
/// 提升机当前楼层 写
/// </summary>
private readonly BasePlcpoint currentfloor06 ;
/// <summary>
/// 提升机目的楼层 写
/// </summary>
private readonly BasePlcpoint targetfloor06 ;
/// <summary>
/// 提升机到位信号 读
/// </summary>
private readonly BasePlcpoint linesignal06 ;
/// <summary>
/// 一楼提升机前允许取 读
/// </summary>
private readonly BasePlcpoint hoisterallowedtake01 ;
/// <summary>
/// 一楼提升机前反馈流水号 读
/// </summary>
private readonly BasePlcpoint feedserialno01 ;
/// <summary>
/// 二楼提升机前允许取 读
/// </summary>
private readonly BasePlcpoint hoisterallowedtake02 ;
/// <summary>
/// 二楼提升机前反馈流水号 读
/// </summary>
private readonly BasePlcpoint feedserialno02 ;
/// <summary>
/// 三楼提升机前允许取 读
/// </summary>
private readonly BasePlcpoint hoisterallowedtake03 ;
/// <summary>
/// 三楼提升机前反馈流水号 读
/// </summary>
private readonly BasePlcpoint feedserialno03 ;
/// <summary>
/// 四楼提升机前允许取 读
/// </summary>
private readonly BasePlcpoint hoisterallowedtake04 ;
/// <summary>
/// 四楼提升机前反馈流水号 读
/// </summary>
private readonly BasePlcpoint feedserialno04 ;
/// <summary>
/// 五楼提升机前允许取 读
/// </summary>
private readonly BasePlcpoint hoisterallowedtake05 ;
/// <summary>
/// 五楼提升机前反馈流水号 读
/// </summary>
private readonly BasePlcpoint feedserialno05 ;
public FirstFloor(IHost host, Plc.S7.Plc plc, string siteNo)
{
this._host = host;
this._plc = plc;
//一楼RFID 读
this.RFID001 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("RFID001"));
//到位信号 读
this.linesignal01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("linesignal01"));
//是否有托盘 读
this.ispallet01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("ispallet01"));
//去向 写
this.wcsrun01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("wcsrun01"));
//流水号 读
this.serialno01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("serialno01"));
//一楼提升机流水号 读
this.serialno06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("serialno06"));
//一楼提升机状态 读
this.equipstate06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("equipstate06"));
//一楼提升机任务状态 读
this.taskstate06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("taskstate06"));
//一楼提升机当前楼层 写
this.currentfloor06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("currentfloor06"));
//一楼提升机目的楼层 写
this.targetfloor06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("targetfloor06"));
//一楼提升机到位信号 读
this.linesignal06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("linesignal06"));
//一楼提升机前允许取
this.hoisterallowedtake01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake01"));
//一楼提升机前反馈流水号
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"));
}
/// <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("启动一楼提升机线程");
}
/// <summary>
/// 启动一楼接驳位线程
/// </summary>
private void FirstFloorLine()
{
List<int?> Itpyes = new List<int?>() { 1, 3, 5, 7 };
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); //流水号 读
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)
{
//判断task表里没有该rfid的未完成的入库信息未下发去向
var task = dbContext.WcsTask.Where(t => t.containerNo == RFID001Value.ToString() && t.taskStatus < 1).FirstOrDefault();
if (task == null || Itpyes.Contains(task.taskType))
{
//入库
if (task == null || Itpyes.Contains(task.taskType))
{
//根据托盘号获取物料码
var material = dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value.ToString()).FirstOrDefault();
if (material != null)
{
var warehouseId = dbContext.WmsWarehouseMaterial.Where(t => t.storageType == "1" && t.storageId == material.materialId).FirstOrDefault()?.warehouseId;
if (warehouseId != null)
{
var TargetFloor = dbContext.WmsBaseWarehouse.Where(t => t.warehouseId == warehouseId).FirstOrDefault();
if (TargetFloor != null)
{
//插入task表
var dic = StaticData.BaseDictionary.Where(t => t.dicKey == "TaskType" && t.ud1 == "I" && t.dicField == TargetFloor.warehouseInstockType).FirstOrDefault();
if (dic != null)
{
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";
task.taskStatus = 0;
task.updateBy = "一楼接驳位线程";
task.updateTime = DateTime.Now;
task.remark = "一楼创建入库任务";
dbContext.WcsTask.Update(task);
dbContext.SaveChanges();
}
//下发去向
this._plc.Write(this.wcsrun01.plcpointAddress, 1);
}
}
}
}
}
//出库
{
//清空托盘绑定
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();
}
}
}
}
}
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
Thread.Sleep(1000);
}
}
/// <summary>
/// 提升机线程
/// </summary>
private void FirstFloorHoister()
{
while (true)
{
try
{
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); //五楼提升机前反馈流水号
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)
{
//提升机空闲
if (Convert.ToInt32(equipstate06Value) == 0)
{
var wcsTask = dbContext.WcsTask.Where(t => t.nextPointId == T01).Where(t => t.taskStatus > 1).FirstOrDefault();
if (wcsTask == null)
{
//一楼到位
if (hoisterallowedtake01Value != null && feedserialno01Value != null && Convert.ToInt32(hoisterallowedtake01Value) == 1 && Convert.ToInt32(feedserialno01Value) > 0)
{
var task = dbContext.WcsTask.Where(t => t.serialNo == Convert.ToInt32(feedserialno01Value)).FirstOrDefault();
if (task != null)
{
if (task.nextPointId == T01)
{
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 1);
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();
continue;
}
}
}
}
//二楼到位
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)
{
if (task.nextPointId == T01)
{
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
{
//给提升机下发去向
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();
continue;
}
}
}
}
//三楼到位
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)
{
if (task.nextPointId == T01)
{
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 3);
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();
continue;
}
}
}
}
//四楼到位
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)
{
if (task.nextPointId == T01)
{
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 4);
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();
continue;
}
}
}
}
//五楼到位
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)
{
if (task.nextPointId == T01)
{
var nextPoint = dbContext.BaseEquip
.Where(t => t.equipType == 1)
.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
if (nextPoint != null)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 5);
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();
continue;
}
}
}
}
}
else
{
Console.WriteLine($"{DateTime.Now}:提升机有任务未反,流水号为{wcsTask.serialNo}······");
LogManager.Info($"提升机有任务未反,流水号为{wcsTask.serialNo}······");
}
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
Thread.Sleep(1000);
}
}
}
}