|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SlnMesnac.Business.@base;
|
|
|
|
|
using SlnMesnac.Common;
|
|
|
|
|
using SlnMesnac.Config;
|
|
|
|
@ -7,6 +8,7 @@ using SlnMesnac.Model.dto;
|
|
|
|
|
using SlnMesnac.Plc;
|
|
|
|
|
using SlnMesnac.Repository.service;
|
|
|
|
|
using SlnMesnac.Rfid;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
@ -14,6 +16,7 @@ using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
|
|
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
|
@ -47,29 +50,57 @@ namespace SlnMesnac.Business
|
|
|
|
|
|
|
|
|
|
private readonly IMesProductPlanService _mesProductPlanService;
|
|
|
|
|
private readonly IMesPrdBarCodeService _barCodeTaskService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IMesBaseBarcodeInfoService _mesBaseBarcodeInfoService;
|
|
|
|
|
public readonly IMesPrdBarCodeService _mesPrdBarCodeService;
|
|
|
|
|
private readonly IRealPalletTaskService _realPalletTaskService;
|
|
|
|
|
|
|
|
|
|
public ProdCompletionBusiness(ILogger<BaseBusiness> logger, AppConfig appConfig, List<PlcAbsractFactory> plcFactories, List<RfidAbsractFactory> rfidFactories, IMesProductPlanService mesProductPlanService, IMesPrdBarCodeService barCodeTaskService, IServiceProvider serviceProvider) : base(logger, appConfig, plcFactories, rfidFactories, serviceProvider)
|
|
|
|
|
private ISqlSugarClient sqlSugarClient;
|
|
|
|
|
|
|
|
|
|
public ProdCompletionBusiness(ISqlSugarClient _sqlSugarClient,ILogger<BaseBusiness> logger, AppConfig appConfig, List<PlcAbsractFactory> plcFactories, List<RfidAbsractFactory> rfidFactories, IMesProductPlanService mesProductPlanService, IMesPrdBarCodeService barCodeTaskService, IServiceProvider serviceProvider) : base(logger, appConfig, plcFactories, rfidFactories, serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
sqlSugarClient = _sqlSugarClient;
|
|
|
|
|
_mesProductPlanService = mesProductPlanService;
|
|
|
|
|
_barCodeTaskService = barCodeTaskService;
|
|
|
|
|
_realPalletTaskService = serviceProvider.GetRequiredService<IRealPalletTaskService>();
|
|
|
|
|
_mesBaseBarcodeInfoService = serviceProvider.GetRequiredService<IMesBaseBarcodeInfoService>();
|
|
|
|
|
_mesPrdBarCodeService = serviceProvider.GetRequiredService<IMesPrdBarCodeService>();
|
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void test()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string epc = "A123456";
|
|
|
|
|
_mesProductPlanService.GetStartedProdPlan(out MesProductPlanDto productPlanDto);
|
|
|
|
|
string bigCode = GenerateBigBarcode(productPlanDto, epc);
|
|
|
|
|
BindingBarCode(bigCode, epc);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
DeviceOutPutHandle();
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
EmptyPalletHandle();
|
|
|
|
|
});
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
DeviceOutPutHandle();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设备产出逻辑处理
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DeviceOutPutHandle()
|
|
|
|
|
private async void DeviceOutPutHandle()
|
|
|
|
|
{
|
|
|
|
|
// 读取RFID失败重试次数,超过5次推送报警
|
|
|
|
|
int readRfidAmount = 0;
|
|
|
|
|
while(true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
@ -78,60 +109,206 @@ namespace SlnMesnac.Business
|
|
|
|
|
|
|
|
|
|
if (plc == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("读取设备产出信号,PLC连接信息为空......");
|
|
|
|
|
_logger.LogInformation("读取小包出口信号,PLC连接信息为空......");
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (plc.readInt16ByAddress(GetPlcAddressByConfigKey("设备产出")) != 1)
|
|
|
|
|
if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("2楼码垛出口到位信号")))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("等待设备产出信号触发......");
|
|
|
|
|
_logger.LogInformation("等待小包出口信号触发......");
|
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mesProductPlanService.GetStartedProdPlan(out MesProductPlanDto productPlanDto);
|
|
|
|
|
string epcStr = await ReadEpcStrByRfidKeyAsync("secondFloorOut");
|
|
|
|
|
if (string.IsNullOrEmpty(epcStr))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("小包出口到位信号触发,读取RFID失败......");
|
|
|
|
|
//判断前一位置是否有托盘缓存
|
|
|
|
|
RealPalletTask realPalletTask = _realPalletTaskService.Query().FirstOrDefault();
|
|
|
|
|
if (realPalletTask != null)
|
|
|
|
|
{
|
|
|
|
|
epcStr = realPalletTask.PalletCode;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
readRfidAmount++;
|
|
|
|
|
if(readRfidAmount == 5)
|
|
|
|
|
{
|
|
|
|
|
//TODO 推送报警
|
|
|
|
|
_logger.LogError("小包出口到位信号触发,读取RFID失败超过5次......");
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(1000 * 5);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RefreshMessage($"小包出口读取到RFID条码{epcStr},开始绑定流程:");
|
|
|
|
|
|
|
|
|
|
_mesProductPlanService.GetStartedProdPlan(out MesProductPlanDto productPlanDto);
|
|
|
|
|
if (productPlanDto == null)
|
|
|
|
|
{
|
|
|
|
|
//TODO:没有计划的话,处理方式待定
|
|
|
|
|
throw new ArgumentException($"未获取到正在执行的生产计划");
|
|
|
|
|
}
|
|
|
|
|
plc.writeInt16ByAddress(GetPlcAddressByConfigKey("设备产出"), 0);
|
|
|
|
|
//生成小包条码
|
|
|
|
|
string barCode = $"JYHB{(string.IsNullOrEmpty(productPlanDto.MaterialCode) ? "000000000" : productPlanDto.MaterialCode)}-000001";
|
|
|
|
|
//if (_barCodeTasks.Count > 0)
|
|
|
|
|
//{
|
|
|
|
|
// var info = _barCodeTasks.OrderByDescending(x => x.RecordTime).Where(x=>x.MaterialCode == productPlanDto.MaterialCode);
|
|
|
|
|
// if(info.Count() > 0)
|
|
|
|
|
// {
|
|
|
|
|
// var task = info.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
// ExtractNumber(task.BarCode, out int orFlag);
|
|
|
|
|
|
|
|
|
|
// barCode = $"JYHB{(string.IsNullOrEmpty(productPlanDto.MaterialCode) ? "000000000" : productPlanDto.MaterialCode)}-{orFlag.ToString().PadLeft(6,'0')}";
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//RealBarCodeTask barCodeTask = new RealBarCodeTask()
|
|
|
|
|
//{
|
|
|
|
|
// BarCode = barCode,
|
|
|
|
|
// PlanCode = productPlanDto.PlanCode,
|
|
|
|
|
// MaterialCode = productPlanDto.MaterialCode,
|
|
|
|
|
// RecordTime = DateTime.Now
|
|
|
|
|
//};
|
|
|
|
|
//_barCodeTaskService.InsertBarCodeTask(barCodeTask);
|
|
|
|
|
|
|
|
|
|
//RefreshBarCodeTaskListEvent?.Invoke(_barCodeTasks);
|
|
|
|
|
sqlSugarClient.AsTenant().BeginTran();
|
|
|
|
|
//1.生成大条码插入mes_barcode_info;
|
|
|
|
|
string bigCode =GenerateBigBarcode(productPlanDto, epcStr);
|
|
|
|
|
//2.40个小条码及大条码绑定托盘号,小条码绑定大条码
|
|
|
|
|
BindingBarCode(bigCode,epcStr);
|
|
|
|
|
//3.更新mes_product_plan及mes_product_order表,更新已生产数量
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlSugarClient.AsTenant().CommitTran();
|
|
|
|
|
|
|
|
|
|
plc.writeBoolByAddress(GetPlcAddressByConfigKey("2楼码垛出口到位信号"), false);
|
|
|
|
|
readRfidAmount = 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
sqlSugarClient.AsTenant().RollbackTran();
|
|
|
|
|
// TODO:推送报警
|
|
|
|
|
_logger.LogError($"DeviceOutPutHandle异常:{ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成大条码插入mes_barcode_info;
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="productPlanDto"></param>
|
|
|
|
|
/// <param name="epcStr"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private string GenerateBigBarcode(MesProductPlanDto productPlanDto,string epcStr)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
string bigBarcode = productPlanDto.PlanCode+DateTime.Now.ToString("_MMddHHmmss");
|
|
|
|
|
|
|
|
|
|
MesBaseBarcodeInfo mesBaseBarcodeInfo = new MesBaseBarcodeInfo();
|
|
|
|
|
mesBaseBarcodeInfo.printTime = DateTime.Now;
|
|
|
|
|
mesBaseBarcodeInfo.printPerson = "SlnMesnac";
|
|
|
|
|
mesBaseBarcodeInfo.printFlag = "1";
|
|
|
|
|
mesBaseBarcodeInfo.batchFlag = "0";
|
|
|
|
|
mesBaseBarcodeInfo.barcodeType = "3";
|
|
|
|
|
mesBaseBarcodeInfo.barcodeInfo = bigBarcode;
|
|
|
|
|
mesBaseBarcodeInfo.batchCode = bigBarcode;
|
|
|
|
|
mesBaseBarcodeInfo.palletInfoCode = epcStr;
|
|
|
|
|
mesBaseBarcodeInfo.materialId = productPlanDto.MaterialId;
|
|
|
|
|
mesBaseBarcodeInfo.amount = 1;
|
|
|
|
|
mesBaseBarcodeInfo.printNumber = 1;
|
|
|
|
|
mesBaseBarcodeInfo.productionDate = DateTime.Now;
|
|
|
|
|
mesBaseBarcodeInfo.acceptedDate = DateTime.Now;
|
|
|
|
|
mesBaseBarcodeInfo.planCode = productPlanDto.PlanCode;
|
|
|
|
|
mesBaseBarcodeInfo.safeFlag = productPlanDto.SaleOrderId ==0 ?"1" : "0";
|
|
|
|
|
mesBaseBarcodeInfo.saleOrderId = productPlanDto.SaleOrderId;
|
|
|
|
|
mesBaseBarcodeInfo.saleorderCode = productPlanDto.SaleorderCode;
|
|
|
|
|
mesBaseBarcodeInfo.bindStatus = "1";
|
|
|
|
|
mesBaseBarcodeInfo.bindTime = DateTime.Now;
|
|
|
|
|
mesBaseBarcodeInfo.updateBy = "SlnMesnac";
|
|
|
|
|
mesBaseBarcodeInfo.updateTime = DateTime.Now;
|
|
|
|
|
mesBaseBarcodeInfo.completeFlag = "1";
|
|
|
|
|
mesBaseBarcodeInfo.singleFlag = "0";
|
|
|
|
|
mesBaseBarcodeInfo.barcodeSpec = productPlanDto.MaterialName;
|
|
|
|
|
sqlSugarClient.AsTenant().GetConnection("mes").Insertable(mesBaseBarcodeInfo).ExecuteCommand();
|
|
|
|
|
return bigBarcode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 40个小条码及大条码绑定托盘号,小条码绑定大条码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bigBarcode"></param>
|
|
|
|
|
/// <param name="epcStr"></param>
|
|
|
|
|
private void BindingBarCode(string bigBarcode,string epcStr)
|
|
|
|
|
{
|
|
|
|
|
List<MesPrdBarcodeInfo> list = _mesPrdBarCodeService.GetUseBarCodeList();
|
|
|
|
|
if(list==null || list.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("托盘未找到可以绑定的小条码");
|
|
|
|
|
}else if(list.Count > 40)
|
|
|
|
|
{
|
|
|
|
|
list = list.Take(40).ToList();
|
|
|
|
|
}
|
|
|
|
|
foreach (var item in list)
|
|
|
|
|
{
|
|
|
|
|
item.PalletInfoCode = epcStr;
|
|
|
|
|
item.MesBarcodeInfo = bigBarcode;
|
|
|
|
|
item.BindTime = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
sqlSugarClient.AsTenant().GetConnection("mes").Updateable(list).ExecuteCommand();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdatePlanInfoAndOrder(MesProductPlanDto productPlanDto)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 空托盘进入码垛位,读取RFID备用
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async void EmptyPalletHandle()
|
|
|
|
|
{
|
|
|
|
|
//读取PLC空托盘流转信号
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var plc = base.GetPlcByKey("plc");
|
|
|
|
|
if (plc == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("读取PLC空托盘码垛到位信号,PLC连接信息为空......");
|
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("2楼码垛到位信号")))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("等待空托盘到位信号触发......");
|
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//读取RFID、获取当前正在执行的计划、将当前计划、工单等信息与托盘绑定
|
|
|
|
|
RefreshMessage("空托盘到位信号触发成功,读取托盘RFID信息");
|
|
|
|
|
|
|
|
|
|
string epcStr = await ReadEpcStrByRfidKeyAsync("secondFloorPallet");
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(epcStr))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("空托盘到位信号触发,读取RFID失败......");
|
|
|
|
|
Thread.Sleep(1000*10);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefreshMessage($"空托盘RFID信息读取成功,标签信息:{epcStr}");
|
|
|
|
|
|
|
|
|
|
// 插入一条托盘记录
|
|
|
|
|
RealPalletTask realPalletTask = new RealPalletTask()
|
|
|
|
|
{
|
|
|
|
|
PalletCode = epcStr,
|
|
|
|
|
RecordTime = DateTime.Now,
|
|
|
|
|
};
|
|
|
|
|
bool result = _realPalletTaskService.Insert(realPalletTask);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
plc.writeBoolByAddress(GetPlcAddressByConfigKey("2楼码垛到位信号"), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
RefreshMessage($"空托盘进入码垛位处理异常:{e.Message}");
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据条码提取序列
|
|
|
|
|
/// </summary>
|
|
|
|
@ -153,18 +330,5 @@ namespace SlnMesnac.Business
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public void testPenCode()
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
// //生成小包条码
|
|
|
|
|
// string materialCode = "000000000";
|
|
|
|
|
// string barCode = $"JYHB{(string.IsNullOrEmpty(materialCode) ? "000000000" : materialCode)}-000001";
|
|
|
|
|
// ExtractNumber(task.BarCode, out int orFlag);
|
|
|
|
|
// barCode = $"JYHB{(string.IsNullOrEmpty(productPlanDto.MaterialCode) ? "000000000" : productPlanDto.MaterialCode)}-{orFlag.ToString().PadLeft(6, '0')}";
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|