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.

319 lines
17 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;
2 years ago
using Khd.Core.Plc;
2 years ago
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 wcsrun01;
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 hoistertrayin06;
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 reserialno06;
2 years ago
2 years ago
public FirstFloor(IHost host, Plc.S7.Plc plc)
2 years ago
{
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.wcsrun01 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("wcsrun01"));
2 years ago
2 years ago
//一楼提升机流水号 读
2 years ago
this.serialno06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("Hoister") && t.plcpointNo.Contains("serialno06"));
2 years ago
//一楼提升机状态 读
2 years ago
this.equipstate06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("Hoister") && t.plcpointNo.Contains("equipstate06"));
2 years ago
//一楼提升机任务状态 读
2 years ago
this.hoistertrayin06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("Hoister") && t.plcpointNo.Contains("hoistertrayin06"));
2 years ago
//一楼提升机当前楼层 写
2 years ago
this.currentfloor06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("Hoister") && t.plcpointNo.Contains("currentfloor06"));
2 years ago
//一楼提升机目的楼层 写
2 years ago
this.targetfloor06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("Hoister") && t.plcpointNo.Contains("targetfloor06"));
2 years ago
//一楼提升机到位信号 读
2 years ago
this.reserialno06 = StaticData.BasePlcpointList.First(t => t.equipmentNo.Contains("Hoister") && t.plcpointNo.Contains("reserialno06"));
2 years ago
2 years ago
}
2 years ago
2 years ago
/// <summary>
2 years ago
/// 启动上件扫描监听
2 years ago
/// </summary>
public void StartPoint()
{
Thread firstFloorLine = new Thread(FirstFloorLine);
firstFloorLine.IsBackground = true;
firstFloorLine.Start();
2 years ago
Console.WriteLine(DateTime.Now + ":一楼接驳位线程启动成功");
LogManager.Info("一楼接驳位线程启动成功");
2 years ago
Thread firstFloorHoister = new Thread(FirstFloorHoister);
firstFloorHoister.IsBackground = true;
firstFloorHoister.Start();
2 years ago
Console.WriteLine(DateTime.Now + ":一楼提升机线程启动成功");
LogManager.Info("一楼提升机线程启动成功");
2 years ago
}
2 years ago
2 years ago
/// <summary>
/// 启动一楼接驳位线程
/// </summary>
private void FirstFloorLine()
{
2 years ago
List<int?> Itpyes = new() { 1, 3, 5, 7 };
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
2 years ago
while (true)
{
try
{
var RFID001Value = this._plc.Read(this.RFID001.plcpointAddress); //一楼RFID 读
var linesignal01Value = this._plc.Read(this.linesignal01.plcpointAddress); //到位信号 读
var wcsrun01Value = this._plc.Read(this.wcsrun01.plcpointAddress); //去向 写
2 years ago
2 years ago
//正常读到plc值
if (linesignal01Value != null && RFID001Value != null)
2 years ago
{
2 years ago
//正常托盘到位
if (Convert.ToInt32(linesignal01Value) == 1)
2 years ago
{
2 years ago
//判断task表里没有该rfid的未完成的入库信息未下发去向
var task = dbContext.WcsTask.Where(t => t.containerNo == RFID001Value.ToString() && t.taskStatus < 1).FirstOrDefault();
if (task == null)
2 years ago
{
2 years ago
//入库
2 years ago
2 years ago
//根据托盘号获取物料码
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)
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
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
var newTask = new WcsTask()
{
objid = StaticData.SnowId.NextId(),
serialNo = 1235,
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,
fromFloorNo = 1,
floorNo = TargetFloor.warehouseFloor,
useFlag = 1,
createBy = "一楼接驳位",
createTime = DateTime.Now,
remark = "一楼创建入库任务"
};
dbContext.Add(newTask);
2 years ago
}
}
2 years ago
}
2 years ago
}
}
2 years ago
else if (Itpyes.Contains(task.taskType))
{
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();
}
//出库
else
{
//清空托盘绑定
//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
}
}
}
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()
{
2 years ago
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
2 years ago
while (true)
{
try
{
2 years ago
var serialno06Value = this._plc.Read(this.serialno06.plcpointAddress); //提升机流水号 读
var equipstate06Value = this._plc.Read(this.equipstate06.plcpointAddress); //提升机状态 读
2 years ago
var hoisterTrayIn06Value = this._plc.Read(this.hoistertrayin06.plcpointAddress); //提升机货物到位状态 读
2 years ago
var currentfloor06Value = this._plc.Read(this.currentfloor06.plcpointAddress); //提升机当前楼层 写
var targetfloor06Value = this._plc.Read(this.targetfloor06.plcpointAddress); //提升机目的楼层 写
2 years ago
var reserialno06 = this._plc.Read(this.reserialno06.plcpointAddress); //反馈流水号
2 years ago
2 years ago
//正常读到plc值
2 years ago
if (targetfloor06Value != null && serialno06Value != null && equipstate06Value != null && currentfloor06Value != null && reserialno06 != null && hoisterTrayIn06Value != 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).OrderBy(t => t.createTime).FirstOrDefault();
if (wcsTask != null)
2 years ago
{
2 years ago
if (wcsTask.taskStatus == 0)//创建
2 years ago
{
2 years ago
if (Convert.ToInt32(currentfloor06Value) == wcsTask.fromFloorNo)
2 years ago
{
2 years ago
wcsTask.taskStatus = 2;
wcsTask.updateBy = "提升机线程";
wcsTask.updateTime = DateTime.Now;
wcsTask.remark = "提升机任务执行中";
BasePlcpoint basePlcpoint = StaticData.BasePlcpointList.First(t => t.floorNo == wcsTask.fromFloorNo && t.plcpointNo.Contains("wcsrun"));
this._plc.WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength);
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
else
2 years ago
{
2 years ago
wcsTask.taskStatus = 1;
wcsTask.updateBy = "提升机线程";
wcsTask.updateTime = DateTime.Now;
wcsTask.remark = "提升机任务执行中";
this._plc.WriteToPoint(this.targetfloor06.plcpointAddress, wcsTask.fromFloorNo.ToString(),this.targetfloor06.plcpointLength);//目的地楼层
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
2 years ago
if (wcsTask.taskStatus == 1)
2 years ago
{
2 years ago
if (Convert.ToInt32(currentfloor06Value) == wcsTask.fromFloorNo)//提升机当前楼层为初始地楼层
2 years ago
{
2 years ago
wcsTask.taskStatus = 2;
wcsTask.updateBy = "提升机线程";
wcsTask.updateTime = DateTime.Now;
wcsTask.remark = "提升机任务执行中";
BasePlcpoint basePlcpoint = StaticData.BasePlcpointList.First(t => t.floorNo == wcsTask.fromFloorNo && t.plcpointNo.Contains("wcsrun"));
this._plc.WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength);
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
2 years ago
if (wcsTask.taskStatus == 2)
2 years ago
{
2 years ago
if (Convert.ToInt32(hoisterTrayIn06Value) == 1)//托盘已经进提升机
2 years ago
{
2 years ago
wcsTask.taskStatus = 3;
wcsTask.updateBy = "提升机线程";
wcsTask.updateTime = DateTime.Now;
wcsTask.remark = "提升机任务执行完成";
this._plc.WriteToPoint(this.targetfloor06.plcpointAddress, wcsTask.floorNo.ToString(),this.targetfloor06.plcpointLength);//目的地楼层
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
2 years ago
if (wcsTask.taskStatus == 3 && Convert.ToInt32(currentfloor06Value) == wcsTask.floorNo)//任务状态为3且当前楼层为任务的目的楼层
2 years ago
{
2 years ago
wcsTask.taskStatus = 4;
wcsTask.updateBy = "提升机线程";
wcsTask.updateTime = DateTime.Now;
wcsTask.remark = "提升机任务执行完成";
BasePlcpoint basePlcpoint = StaticData.BasePlcpointList.First(t => t.floorNo == wcsTask.fromFloorNo && t.plcpointNo.Contains("wcsrun"));
this._plc.WriteToPoint(basePlcpoint.plcpointAddress, "2", basePlcpoint.plcpointLength);//去向为2表示提升机已到达目的地让货出去
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
}
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
2 years ago
Thread.Sleep(1000);
2 years ago
}
}
}
}