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.
Aucma.Scada/Aucma.Scada.Business/InStoreBusiness.cs

549 lines
19 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Aucma.Scada.HikRobot;
using Aucma.Scada.Model.domain;
using HighWayIot.Config;
using HighWayIot.Log4net;
using HighWayIot.Repository.service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using static Aucma.Scada.Business.InStoreTaskHandle;
namespace Aucma.Scada.Business
{
/// <summary>
/// 入库业务逻辑
/// </summary>
public sealed class InStoreBusiness
{
#region 单例实现
private static readonly Lazy<InStoreBusiness> lazy = new Lazy<InStoreBusiness>(() => new InStoreBusiness());
public static InStoreBusiness Instance
{
get
{
return lazy.Value;
}
}
#endregion
#region 对象引用
private LogHelper logHelper = LogHelper.Instance;
private AppConfig appConfig = AppConfig.Instance;
private RegisterServices registerServices = RegisterServices.Instance;
private InStoreTaskHandle taskHandle = InStoreTaskHandle.Instance;
private GrabImage grabImage = GrabImage.Instance;
#endregion
#region 接口引用
/// <summary>
/// 货道信息
/// </summary>
private IBaseSpaceInfoService _spaceInfoService;
/// <summary>
/// 实时任务
/// </summary>
private IRealTaskInfoService _taskInfoService;
private IBaseSpaceDetailService _baseSpaceDetailService;
private IBaseBomInfoService _baseBomInfoService;
private IRecordInStoreService _recordInStore;
#endregion
#region 委托事件
/// <summary>
/// 初始化入库任务
/// </summary>
/// <param name="message"></param>
public delegate void RefreshInStoreTask(RealTaskInfo taskInfos, bool isFinsih = false);
public event RefreshInStoreTask RefreshInStoreTaskEvent;
/// <summary>
/// 扫码信息刷新
/// </summary>
/// <param name="materialCode"></param>
/// <param name="materialName"></param>
/// <param name="spaceName"></param>
/// <param name="materialType"></param>
public delegate void RefreshScanMateriaCode(string materialCode, string materialName, string spaceName, string materialType);
public event RefreshScanMateriaCode RefreshScanMateriaCodeEvent;
/// <summary>
/// 日志信息刷新
/// </summary>
/// <param name="message"></param>
public delegate void RefreshLogMessage(string message);
public event RefreshLogMessage RefreshLogMessageEvent;
#endregion
private InStoreBusiness()
{
_spaceInfoService = registerServices.GetService<IBaseSpaceInfoService>();
_taskInfoService = registerServices.GetService<IRealTaskInfoService>();
_baseSpaceDetailService = registerServices.GetService<IBaseSpaceDetailService>();
_baseBomInfoService = registerServices.GetService<IBaseBomInfoService>();
_recordInStore = registerServices.GetService<IRecordInStoreService>();
taskHandle.InStoreFinsihEvent += TaskFeedback;
grabImage.RefreshMaterialCodeStrEvent += InStore;
grabImage.RefreshLogMessageEvent += PrintLogInfoMessage;
StartPassDown();
//Task.Run(() =>
//{
// Thread.Sleep(6000);
// for (int i = 1; i < 2; i++)
// {
// //InStore(appConfig.shellStoreCode, "B2360000078110300" + i.ToString().PadLeft(2, '0'));
// //Thread.Sleep(1000);
// //InStore(appConfig.linerStoreCode, "L2360000078810240" + i.ToString().PadLeft(2, '0'));
// //Thread.Sleep(1000);
// }
//});
}
/// <summary>
/// 入库
/// </summary>
/// <param name="storeCode"></param>
/// <param name="materialType"></param>
private void InStore(string storeCode, string materialCode)
{
try
{
PrintLogInfoMessage($"扫码成功,物料码:{materialCode}");
string materialType = SubStringMaterialCode(materialCode);
#region Delete By wenjy 2023-10-30 11:41:00 取消通过数据库获取货道数量、在途量改为实时读取PLC数据
//var spaceInfo = _spaceInfoService.InStoreGetSpaceInfoByMaterialType(storeCode, materialType);
#endregion
var spaceInfo = GetSpaceInfoByMaterialType(storeCode, materialType);
if (spaceInfo != null)
{
PrintLogInfoMessage($"匹配货道:{spaceInfo.spaceName}");
RefreshScanMateriaCodeEvent?.Invoke(materialCode, GetMaterialName(materialType), spaceInfo.spaceName, storeCode); //刷新界面扫码信息
var result = CreateInStoreTask(spaceInfo, materialCode); //创建入库任务
if (result)
{
spaceInfo.onRouteAmount += 1;
_spaceInfoService.UpdateSpaceInfo(spaceInfo);
}
}
else
{
//报警停线
PrintLogInfoMessage($"物料码:{materialCode};未匹配到可用货道");
}
}
catch (Exception ex)
{
PrintLogErrorMessage("入库业务异常", ex);
}
}
/// <summary>
/// 创建入库任务
/// </summary>
/// <param name="spaceInfo"></param>
private bool CreateInStoreTask(BaseSpaceInfo spaceInfo, string materialCode)
{
bool result = false;
#region 任务赋值
RealTaskInfo realTaskInfo = new RealTaskInfo();
realTaskInfo.taskType = 1;
realTaskInfo.taskCode = System.Guid.NewGuid().ToString("N").Substring(0, 6);
realTaskInfo.storeCode = spaceInfo.storeCode;
realTaskInfo.spaceCode = spaceInfo.spaceCode;
realTaskInfo.materialType = spaceInfo.materialType;
realTaskInfo.materialCode = materialCode;
realTaskInfo.planAmount = 1;
realTaskInfo.taskStatus = 1;
realTaskInfo.createTime = DateTime.Now;
#endregion
result = _taskInfoService.AddTaskInfo(realTaskInfo);
if (result)
{
PrintLogInfoMessage("入库任务创建成功");
RefreshInStoreTaskEvent?.Invoke(realTaskInfo);
result = true;
}
else
{
PrintLogInfoMessage("入库任务创建失败");
}
return result;
}
#region 轮询获取入库任务下发至PLC等待PLC执行反馈完成后再次下发
private SemaphoreSlim shellSemaphore = new SemaphoreSlim(0);
private SemaphoreSlim linerSemaphore = new SemaphoreSlim(0);
private void StartPassDown()
{
Task.Run(() =>
{
Thread.Sleep(5000);
while (true)
{
PassDownShellTask();
Thread.Sleep(1000);
}
});
Task.Run(() =>
{
Thread.Sleep(5000);
while (true)
{
PassDownLinerTask();
Thread.Sleep(1000);
}
});
}
/// <summary>
/// 依次获取箱壳任务队列进行下发
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void PassDownShellTask()
{
RealTaskInfo taskInfo = GetAwaitSendTask(appConfig.shellStoreCode);
if (taskInfo != null)
{
PrintLogInfoMessage($"下发箱壳入库任务:{taskInfo.taskCode};仓库{taskInfo.storeCode};货道:{taskInfo.spaceCode}");
//taskHandle.SendShellTask_InStore(taskInfo);
if (taskHandle.SendShellTask_InStore(taskInfo))
{
PrintLogInfoMessage($"箱壳入库任务:{taskInfo.taskCode}下发成功等待PLC执行反馈");
taskInfo.taskStatus = 2;
_taskInfoService.UpdateTaskInfo(taskInfo);
RefreshInStoreTaskEvent?.Invoke(taskInfo);
//if (shellSemaphore.Wait(TimeSpan.FromSeconds(20)))
//{
// // 收到反馈
// PrintLogInfoMessage("收到反馈,继续执行");
//}
//else
//{
// PrintLogInfoMessage("超时未反馈");
//}
shellSemaphore.Wait(); //一直堵塞直到信号量释放
PrintLogInfoMessage($"箱壳入库任务:{taskInfo.taskCode};执行完成");
RefreshInStoreTaskEvent?.Invoke(taskInfo,true);
}
else
{
PrintLogInfoMessage($"箱壳入库任务:{taskInfo.taskCode}下发失败请排除PLC连接");
}
}
else
{
PrintLogInfoMessage("未获取到需要下发的箱壳入库任务");
}
Thread.Sleep(3000);
}
/// <summary>
/// 依次获取内胆任务队列进行下发
/// </summary>
private void PassDownLinerTask()
{
RealTaskInfo taskInfo = GetAwaitSendTask(appConfig.linerStoreCode);
if (taskInfo != null)
{
PrintLogInfoMessage($"下发内胆入库任务:{taskInfo.taskCode};仓库{taskInfo.storeCode};货道:{taskInfo.spaceCode}");
if (taskHandle.SendLinerTask_InStore(taskInfo))
{
PrintLogInfoMessage($"内胆入库任务:{taskInfo.taskCode}下发成功等待PLC执行反馈");
taskInfo.taskStatus = 2;
_taskInfoService.UpdateTaskInfo(taskInfo);
RefreshInStoreTaskEvent?.Invoke(taskInfo);
//if (shellSemaphore.Wait(TimeSpan.FromSeconds(20)))
//{
// // 收到反馈
// PrintLogInfoMessage("收到反馈,继续执行");
//}
//else
//{
// PrintLogInfoMessage("超时未反馈");
//}
linerSemaphore.Wait(); //一直堵塞直到信号量释放
PrintLogInfoMessage($"内胆入库任务:{taskInfo.taskCode};执行完成");
RefreshInStoreTaskEvent?.Invoke(taskInfo,true);
}
else
{
PrintLogInfoMessage($"内胆入库任务:{taskInfo.taskCode}下发失败请排除PLC连接");
}
}
else
{
PrintLogInfoMessage("未获取到需要下发的内胆入库任务");
}
Thread.Sleep(3000);
}
/// <summary>
/// 获取待执行的入库任务
/// </summary>
/// <param name="storeCode"></param>
/// <returns></returns>
private RealTaskInfo GetAwaitSendTask(string storeCode)
{
RealTaskInfo taskInfo = null;
try
{
taskInfo = _taskInfoService.GetTaskInfoByStoreCode(storeCode, 1);
}
catch (Exception ex)
{
PrintLogErrorMessage("获取待执行的入库任务异常", ex);
}
return taskInfo;
}
private void TaskFeedback(string storeCode,string taskCode)
{
if(storeCode == appConfig.shellStoreCode)
{
ShellTaskFeedback(taskCode);
}
else
{
LinerTaskFeedback(taskCode);
}
}
/// <summary>
/// 箱壳执行反馈
/// </summary>
private void ShellTaskFeedback(string taskCode)
{
PrintLogInfoMessage("箱壳执行完成,自动释放信号量");
InStoreFinish(taskCode, appConfig.shellStoreCode);
shellSemaphore.Release();
}
/// <summary>
/// 内胆执行反馈
/// </summary>
private void LinerTaskFeedback(string taskCode)
{
PrintLogInfoMessage("内胆执行完成,自动释放信号量");
InStoreFinish(taskCode, appConfig.linerStoreCode);
linerSemaphore.Release();
}
#endregion
/// <summary>
/// 入库完成
/// </summary>
/// <param name="storeCode"></param>
/// <param name="spaceCode"></param>
/// <param name="materialType"></param>
private void InStoreFinish(string taskCode, string storeCode)
{
try
{
var taskInfo = _taskInfoService.GetTaskInfoByTaskCode(taskCode, storeCode);
if (taskInfo != null)
{
var spaceInfo = _spaceInfoService.GetSpaceInfoBySpaceCode(taskInfo.storeCode, taskInfo.spaceCode);
if (spaceInfo != null)
{
//读取PLC获取货道信息存放数量、在途数量,
//实际应用改为读取PLC库存信息
spaceInfo.materialType = taskInfo.materialType;
spaceInfo.spaceStock = spaceInfo.spaceStock + 1;
spaceInfo.onRouteAmount -= 1;
_spaceInfoService.UpdateSpaceInfo(spaceInfo);
#region 添加货道明细
BaseSpaceDetail spaceDetail = new BaseSpaceDetail();
spaceDetail.materialType = taskInfo.materialType;
spaceDetail.materialCode = taskInfo.materialCode;
spaceDetail.materialName = GetMaterialName(taskInfo.materialType);
spaceDetail.storeCode = spaceInfo.storeCode;
spaceDetail.spaceCode = spaceInfo.spaceCode;
spaceDetail.materialAmount = 1;
_baseSpaceDetailService.InsertSpaceDetail(spaceDetail);
#endregion
#region 添加入库任务
RecordInstore recordInstore = new RecordInstore();
recordInstore.storeCode = taskInfo.storeCode;
recordInstore.spaceCode = taskInfo.spaceCode;
recordInstore.materialCode = taskInfo.materialCode;
recordInstore.materialType = taskInfo.materialType;
recordInstore.materialName = GetMaterialName(taskInfo.materialType);
recordInstore.inStoreAmount = 1;
recordInstore.inStoreTime = DateTime.Now;
recordInstore.barcodeCode = taskInfo.materialCode;
_recordInStore.InsertRecordInStore(recordInstore);
#endregion
}
//清除任务信息
_taskInfoService.DeleteTaskInfo(taskCode, storeCode);
}
}
catch (Exception ex)
{
PrintLogErrorMessage("入库完成逻辑处理异常", ex);
}
}
/// <summary>
/// 日志输出,界面刷新同时记录文件
/// </summary>
/// <param name="message"></param>
private void PrintLogInfoMessage(string message)
{
RefreshLogMessageEvent?.Invoke(message);
logHelper.Info(message);
}
/// <summary>
/// 异常日志输出
/// </summary>
/// <param name="message"></param>
/// <param name="ex"></param>
private void PrintLogErrorMessage(string message, Exception ex = null)
{
RefreshLogMessageEvent?.Invoke(message);
logHelper.Error(message, ex);
}
/// <summary>
/// 截取条码
/// </summary>
/// <param name="materialCode"></param>
/// <returns></returns>
private string SubStringMaterialCode(string materialCode)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(materialCode))
{
result = materialCode.Substring(2, 10);
}
return result;
}
public List<RealTaskInfo> GetInStoreTask()
{
return _taskInfoService.GetTaskInfosByStoreCode(new string[] { appConfig.shellStoreCode,appConfig.linerStoreCode }, 1);
}
/// <summary>
/// 通过BOM获取物料名称
/// </summary>
/// <param name="materialType"></param>
/// <returns></returns>
public string GetMaterialName(string materialType)
{
string materialName = string.Empty;
var info = _baseBomInfoService.GetBomInfoByMaterialCode(materialType);
if(info != null)
{
materialName = info.materialName;
}
return materialName;
}
/// <summary>
/// 获取仓库物料库存
/// </summary>
/// <returns></returns>
public List<dynamic> GetMaterialStock()
{
return _spaceInfoService.GetMaterialStock(appConfig.shellStoreCode, appConfig.linerStoreCode);
}
/// <summary>
/// 通过PLC读取货道信息在途数量、在库数量、货道状态)
/// </summary>
/// <param name="storeCode"></param>
/// <param name="materialType"></param>
/// <returns></returns>
private BaseSpaceInfo GetSpaceInfoByMaterialType(string storeCode,string materialType)
{
BaseSpaceInfo result = null;
try
{
List<BaseSpaceInfo> info = _spaceInfoService.GetBaseSpaceInfosByMaterialType(storeCode, materialType);
if (info != null)
{
if (info.Count > 0)
{
foreach (BaseSpaceInfo item in info)
{
var spaceInfo = taskHandle.ReadSpaceInfoByPlc(item);
item.spaceStock = spaceInfo.spaceStock;
item.onRouteAmount = spaceInfo.onRouteAmount;
item.spaceStatus = spaceInfo.spaceStatus;
}
result = info.Where(x=>x.spaceStatus == 1 && x.spaceCapacity > 0 ? x.spaceCapacity > (x.spaceStock + x.onRouteAmount) : 1 == 1).OrderByDescending(x => x.spaceStock).OrderBy(x=>x.spaceCode).First();
}
}
}catch (Exception ex)
{
PrintLogErrorMessage("货道信息读取异常", ex);
}
return result;
}
}
}