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 { /// /// 一楼线程(包括接驳位,提升机调度) /// public class FirstFloor { private readonly IHost _host; private readonly Plc.S7.Plc _plc; private long F01 = 1; private long T01 = 6; /// /// 一楼RFID 读 /// private readonly BasePlcpoint RFID001; /// /// 到位信号 读 /// private readonly BasePlcpoint linesignal01 ; /// /// 是否有托盘 读 /// private readonly BasePlcpoint ispallet01 ; /// /// 去向 写 /// private readonly BasePlcpoint wcsrun01 ; /// /// 流水号 读 /// private readonly BasePlcpoint serialno01 ; /// /// 提升机流水号 读 /// private readonly BasePlcpoint serialno06 ; /// /// 提升机状态 读 /// private readonly BasePlcpoint equipstate06 ; /// /// 提升机任务状态 读 /// private readonly BasePlcpoint taskstate06 ; /// /// 提升机当前楼层 写 /// private readonly BasePlcpoint currentfloor06 ; /// /// 提升机目的楼层 写 /// private readonly BasePlcpoint targetfloor06 ; /// /// 提升机到位信号 读 /// private readonly BasePlcpoint linesignal06 ; /// /// 一楼提升机前允许取 读 /// private readonly BasePlcpoint hoisterallowedtake01 ; /// /// 一楼提升机前反馈流水号 读 /// private readonly BasePlcpoint feedserialno01 ; /// /// 二楼提升机前允许取 读 /// private readonly BasePlcpoint hoisterallowedtake02 ; /// /// 二楼提升机前反馈流水号 读 /// private readonly BasePlcpoint feedserialno02 ; /// /// 三楼提升机前允许取 读 /// private readonly BasePlcpoint hoisterallowedtake03 ; /// /// 三楼提升机前反馈流水号 读 /// private readonly BasePlcpoint feedserialno03 ; /// /// 四楼提升机前允许取 读 /// private readonly BasePlcpoint hoisterallowedtake04 ; /// /// 四楼提升机前反馈流水号 读 /// private readonly BasePlcpoint feedserialno04 ; /// /// 五楼提升机前允许取 读 /// private readonly BasePlcpoint hoisterallowedtake05 ; /// /// 五楼提升机前反馈流水号 读 /// 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")); } /// /// 启动线程 /// 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("启动一楼提升机线程"); } /// /// 启动一楼接驳位线程 /// private void FirstFloorLine() { List Itpyes = new List() { 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()) { //正常读到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); } } /// /// 提升机线程 /// 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(); //正常读到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); } } } }