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.

472 lines
28 KiB
C#

2 years ago
using Khd.Core.Domain.Dto.wcs;
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Thrift.Server;
using Thrift.Transport;
using ThriftService;
using static AngleSharp.Css.Values.CssRadialGradientValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Thrift.Protocol;
using System.Security.Cryptography.Xml;
using Z.EntityFramework.Plus;
using System.Net.NetworkInformation;
2 years ago
using Khd.Core.Application.Interface;
using Khd.Core.Application;
2 years ago
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;
private long T01 = 6;
2 years ago
/// <summary>
/// 一楼RFID 读
/// </summary>
BasePlcpoint? RFID001 { get; set; }
/// <summary>
/// 到位信号 读
/// </summary>
BasePlcpoint? linesignal01 { get; set; }
/// <summary>
/// 是否有托盘 读
/// </summary>
BasePlcpoint? ispallet01 { get; set; }
/// <summary>
/// 去向 写
/// </summary>
BasePlcpoint? wcsrun01 { get; set; }
/// <summary>
/// 流水号 读
/// </summary>
BasePlcpoint? serialno01 { get; set; }
2 years ago
2 years ago
/// <summary>
2 years ago
/// 提升机流水号 读
2 years ago
/// </summary>
BasePlcpoint? serialno06 { get; set; }
/// <summary>
2 years ago
/// 提升机状态 读
2 years ago
/// </summary>
BasePlcpoint? equipstate06 { get; set; }
/// <summary>
2 years ago
/// 提升机任务状态 读
2 years ago
/// </summary>
BasePlcpoint? taskstate06 { get; set; }
/// <summary>
2 years ago
/// 提升机当前楼层 写
2 years ago
/// </summary>
BasePlcpoint? currentfloor06 { get; set; }
/// <summary>
2 years ago
/// 提升机目的楼层 写
2 years ago
/// </summary>
BasePlcpoint? targetfloor06 { get; set; }
/// <summary>
2 years ago
/// 提升机到位信号 读
2 years ago
/// </summary>
BasePlcpoint? linesignal06 { get; set; }
2 years ago
/// <summary>
/// 一楼提升机前允许取 读
/// </summary>
BasePlcpoint? hoisterallowedtake01 { get; set; }
/// <summary>
/// 一楼提升机前反馈流水号 读
/// </summary>
BasePlcpoint? feedserialno01 { get; set; }
/// <summary>
/// 二楼提升机前允许取 读
/// </summary>
BasePlcpoint? hoisterallowedtake02 { get; set; }
/// <summary>
/// 二楼提升机前反馈流水号 读
/// </summary>
BasePlcpoint? feedserialno02 { get; set; }
/// <summary>
/// 三楼提升机前允许取 读
/// </summary>
BasePlcpoint? hoisterallowedtake03 { get; set; }
/// <summary>
/// 三楼提升机前反馈流水号 读
/// </summary>
BasePlcpoint? feedserialno03 { get; set; }
/// <summary>
/// 四楼提升机前允许取 读
/// </summary>
BasePlcpoint? hoisterallowedtake04 { get; set; }
/// <summary>
/// 四楼提升机前反馈流水号 读
/// </summary>
BasePlcpoint? feedserialno04 { get; set; }
/// <summary>
/// 五楼提升机前允许取 读
/// </summary>
BasePlcpoint? hoisterallowedtake05 { get; set; }
/// <summary>
/// 五楼提升机前反馈流水号 读
/// </summary>
BasePlcpoint? feedserialno05 { get; set; }
2 years ago
public FirstFloor(IHost host, Plc.S7.Plc plc, string siteNo)
{
this._host = host;
this._plc = plc;
//一楼RFID 读
this.RFID001 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorLine") &&t.plcpointNo.Contains("RFID001"));
//到位信号 读
this.linesignal01 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("linesignal01"));
//是否有托盘 读
this.ispallet01 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("ispallet01"));
//去向 写
this.wcsrun01 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("wcsrun01"));
//流水号 读
this.serialno01 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorLine") && t.plcpointNo.Contains("serialno01"));
2 years ago
2 years ago
//一楼提升机流水号 读
this.serialno06 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("serialno06"));
//一楼提升机状态 读
this.equipstate06 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("equipstate06"));
//一楼提升机任务状态 读
this.taskstate06 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("taskstate06"));
//一楼提升机当前楼层 写
this.currentfloor06 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("currentfloor06"));
//一楼提升机目的楼层 写
this.targetfloor06 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("targetfloor06"));
//一楼提升机到位信号 读
this.linesignal06 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorHoister") && t.plcpointNo.Contains("linesignal06"));
2 years ago
//一楼提升机前允许取
this.hoisterallowedtake01 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake01"));
//一楼提升机前反馈流水号
this.feedserialno01 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FirstFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno01"));
this.hoisterallowedtake02 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("SecondFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake02"));
this.feedserialno02 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("SecondFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno02"));
this.hoisterallowedtake03 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("ThirdFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake03"));
this.feedserialno03 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("ThirdFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno03"));
this.hoisterallowedtake04 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FourthlyFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake04"));
this.feedserialno04 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FourthlyFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno04"));
this.hoisterallowedtake05 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FifthFloorBeforeHoister") && t.plcpointNo.Contains("hoisterallowedtake05"));
this.feedserialno05 = StaticData.BasePlcpointList.FirstOrDefault(t => t.equipmentNo.Contains("FifthFloorBeforeHoister") && t.plcpointNo.Contains("feedserialno05"));
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("启动一楼提升机线程");
}
/// <summary>
/// 启动一楼接驳位线程
/// </summary>
private void FirstFloorLine()
{
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)
{
//根据托盘号获取物料码
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表
IWcsTaskApplication wcsTaskApplication=new WcsTaskApplication(scope.ServiceProvider);
Jc.SnowId.JcSnowId id = new Jc.SnowId.JcSnowId(1, 1);
var dic = StaticData.BaseDictionary.Where(t => t.dicKey == "TaskType" && t.ud1 == "I" && t.dicField == TargetFloor.warehouseInstockType).FirstOrDefault();
WcsTask newTask = new WcsTask()
{
objid = id.NextId(),
serialNo = Convert.ToInt32(serialno01Value),
equipmentNo = "F01",
taskType = Convert.ToInt32(dic.dicValue),
taskStatus = 0,
2 years ago
containerNo = RFID001Value.ToString(),
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,
floorNo=1,
useFlag=1,
createBy= "一楼接驳位",
createTime =DateTime.Now,
remark="一楼创建入库任务"
};
wcsTaskApplication.Add(newTask);
//下发去向
this._plc.Write(this.wcsrun01.plcpointAddress, 1);
}
}
}
}
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 (serialno06Value != null && equipstate06Value != null || taskstate06Value != null || linesignal06Value != null)
{
2 years ago
//提升机空闲
if ( Convert.ToInt32(equipstate06Value) == 0)
2 years ago
{
2 years ago
//一楼到位
if (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)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 1);
this._plc.Write(this.targetfloor06.plcpointAddress,task.floorNo);
var nextPoint = dbContext.BaseEquip.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
//修改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 (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)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 2);
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
var nextPoint = dbContext.BaseEquip.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
//修改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 (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)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 1);
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
var nextPoint = dbContext.BaseEquip.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
//修改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 (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)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 1);
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
var nextPoint = dbContext.BaseEquip.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
//修改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 (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)
{
//给提升机下发去向
this._plc.Write(this.currentfloor06.plcpointAddress, 1);
this._plc.Write(this.targetfloor06.plcpointAddress, task.floorNo);
var nextPoint = dbContext.BaseEquip.Where(t => t.floorNo == task.floorNo).FirstOrDefault();
//修改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;
}
2 years ago
}
}
}
}
}
catch (Exception ex)
{
LogManager.Error(ex);
}
2 years ago
Thread.Sleep(1000);
2 years ago
}
}
}
}