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.

352 lines
19 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.Plc.S7;
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;
2 years ago
private Plc.S7.Plc _plc;
private readonly long F01 = 1;
private readonly 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>
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
//一楼提升机流水号 读
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()
{
2 years ago
Thread firstFloorLine = new(FirstFloorLine)
{
IsBackground = true
};
2 years ago
firstFloorLine.Start();
2 years ago
Console.WriteLine(DateTime.Now + ":一楼接驳位线程启动成功");
LogManager.Info("一楼接驳位线程启动成功");
2 years ago
2 years ago
Thread firstFloorHoister = new(FirstFloorHoister)
{
IsBackground = true
};
2 years ago
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
{
2 years ago
var RFID001Value = this._plc.ReadRFID(this.RFID001.plcpointAddress); //一楼RFID 读
2 years ago
var linesignal01Value = this._plc.Read(this.linesignal01.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的未完成的入库信息未下发去向
2 years ago
var task = dbContext.WcsTask.Where(t => t.containerNo == RFID001Value && t.taskStatus < 1).FirstOrDefault();
2 years ago
if (task == null)
2 years ago
{
2 years ago
//入库
2 years ago
2 years ago
//根据托盘号获取物料码
2 years ago
var material = dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value).FirstOrDefault();
2 years ago
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(),
2 years ago
serialNo = SystemData.GetSerialNo(dbContext),
2 years ago
equipmentNo = "F01",
taskType = Convert.ToInt32(dic.dicValue),
taskStatus = 0,
2 years ago
containerNo = RFID001Value,
2 years ago
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))//按照正常任务下发
2 years ago
{
2 years ago
if (task.nextPointId != T01)
{
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();
}
2 years ago
}
//出库
else
{
//清空托盘绑定
2 years ago
var material = dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value).FirstOrDefault();
if (material != null)
{
material.bindAmount = 0;
material.materialBarcode = null;
material.materialCode = null;
material.materialId = null;
material.materialName = null;
material.updateBy = "SYS";
material.updateTime = DateTime.Now;
dbContext.Update(material);
//dbContext.MesBasePalletInfo.Where(t => t.palletInfoCode == RFID001Value).Delete();
dbContext.SaveChanges();
}
2 years ago
}
2 years ago
}
}
}
catch (Exception ex)
{
2 years ago
if (ex is PlcException)
{
try
{
this._plc = new Plc.S7.Plc(this._plc.CPU, this._plc.IP, this._plc.Port, this._plc.Rack, this._plc.Slot);
this._plc.Open();
}
catch
{
}
}
2 years ago
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); //提升机当前楼层 读
2 years ago
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 && Convert.ToInt32(hoisterTrayIn06Value) == 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"));
2 years ago
this._plc.WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
this._plc.WriteToPoint(this.serialno06.plcpointAddress, wcsTask.serialNo.ToString(), this.serialno06.plcpointLength.ToString());
2 years ago
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 = "提升机任务执行中";
2 years ago
this._plc.WriteToPoint(this.targetfloor06.plcpointAddress, wcsTask.fromFloorNo.ToString(), this.targetfloor06.plcpointLength.ToString());//目的地楼层
this._plc.WriteToPoint(this.serialno06.plcpointAddress, wcsTask.serialNo.ToString(), this.serialno06.plcpointLength.ToString());
2 years ago
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
2 years ago
if (wcsTask.taskStatus == 1 && Convert.ToInt32(reserialno06) == wcsTask.serialNo)
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"));
2 years ago
this._plc.WriteToPoint(basePlcpoint.plcpointAddress, "1", basePlcpoint.plcpointLength.ToString());
2 years ago
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
2 years ago
if (wcsTask.taskStatus == 2 && Convert.ToInt32(reserialno06) == wcsTask.serialNo)
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 = "提升机任务执行完成";
2 years ago
this._plc.WriteToPoint(this.targetfloor06.plcpointAddress, wcsTask.floorNo.ToString(), this.targetfloor06.plcpointLength.ToString());//目的地楼层
2 years ago
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
2 years ago
}
2 years ago
if (wcsTask.taskStatus == 3 && Convert.ToInt32(currentfloor06Value) == wcsTask.floorNo && Convert.ToInt32(reserialno06) == wcsTask.serialNo)//任务状态为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"));
2 years ago
this._plc.WriteToPoint(basePlcpoint.plcpointAddress, "2", basePlcpoint.plcpointLength.ToString());//去向为2表示提升机已到达目的地让货出去
2 years ago
dbContext.Update(wcsTask);
dbContext.SaveChanges();
2 years ago
}
}
}
}
}
catch (Exception ex)
{
2 years ago
if (ex is PlcException)
{
try
{
this._plc = new Plc.S7.Plc(this._plc.CPU, this._plc.IP, this._plc.Port, this._plc.Rack, this._plc.Slot);
this._plc.Open();
}
catch
{
}
}
2 years ago
LogManager.Error(ex);
}
2 years ago
Thread.Sleep(1000);
2 years ago
}
}
}
}