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.Threading;
using System.Threading.Tasks;
namespace Aucma.Scada.Business
{
///
/// 入库业务逻辑
///
public sealed class InStoreBusiness
{
#region 单例实现
private static readonly Lazy lazy = new Lazy(() => 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 接口引用
///
/// 货道信息
///
private IBaseSpaceInfoService _spaceInfoService;
///
/// 实时任务
///
private IRealTaskInfoService _taskInfoService;
#endregion
#region 委托事件
///
/// 初始化入库任务
///
///
public delegate void RefreshInStoreTask(RealTaskInfo taskInfos);
public event RefreshInStoreTask RefreshInStoreTaskEvent;
///
/// 扫码信息刷新
///
///
///
///
///
public delegate void RefreshScanMateriaCode(string materialCode, string materialName, string spaceName, string materialType);
public event RefreshScanMateriaCode RefreshScanMateriaCodeEvent;
///
/// 日志信息刷新
///
///
public delegate void RefreshLogMessage(string message);
public event RefreshLogMessage RefreshLogMessageEvent;
#endregion
private InStoreBusiness()
{
_spaceInfoService = registerServices.GetService();
_taskInfoService = registerServices.GetService();
taskHandle.InStoreFinsihEvent += FoamTaskFeedback;
grabImage.RefreshMaterialCodeStrEvent += InStore;
grabImage.RefreshLogMessageEvent += PrintLogInfoMessage;
StartPassDown();
}
///
/// 入库
///
///
private void InStore(string materialCode)
{
try
{
PrintLogInfoMessage($"扫码成功,物料码:{materialCode}");
string materialType = materialCode;
var spaceInfo = _spaceInfoService.InStoreGetSpaceInfoByMaterialType(appConfig.foamStoreCode, materialType);
if (spaceInfo != null)
{
PrintLogInfoMessage($"匹配货道:{spaceInfo.spaceName}");
RefreshScanMateriaCodeEvent?.Invoke(materialCode, materialType, spaceInfo.spaceName, appConfig.foamStoreCode); //刷新界面扫码信息
CreateInStoreTask(spaceInfo, materialCode); //创建入库任务
}
else
{
//报警停线
PrintLogInfoMessage($"物料码:{materialCode};未匹配到可用货道");
}
}
catch (Exception ex)
{
PrintLogErrorMessage("入库业务异常", ex);
}
}
///
/// 创建入库任务
///
///
private void CreateInStoreTask(BaseSpaceInfo spaceInfo, string materialCode)
{
//生成入库任务依次下发至PLC
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 = System.Guid.NewGuid().ToString("N").Substring(0, 6); ;
realTaskInfo.planAmount = 1;
realTaskInfo.taskStatus = 1;
realTaskInfo.createTime = DateTime.Now;
bool result = _taskInfoService.AddTaskInfo(realTaskInfo);
if (result)
{
PrintLogInfoMessage("入库任务创建成功");
RefreshInStoreTaskEvent?.Invoke(realTaskInfo);
}
else
{
PrintLogInfoMessage("入库任务创建失败");
}
}
#region 轮询获取入库任务下发至PLC,等待PLC执行反馈,完成后再次下发
private SemaphoreSlim semaphore = new SemaphoreSlim(0);
private void StartPassDown()
{
Task.Run(() =>
{
while (true)
{
PassDownShellTask();
Thread.Sleep(1000);
}
});
}
///
/// 依次获取泡后任务队列进行下发
///
///
///
private void PassDownShellTask()
{
RealTaskInfo taskInfo = GetAwaitSendTask(appConfig.foamStoreCode);
if (taskInfo != null)
{
PrintLogInfoMessage($"下发泡后入库任务:{taskInfo.taskCode};仓库{taskInfo.storeCode};货道:{taskInfo.spaceCode}");
//taskHandle.SendShellTask_InStore(taskInfo);
Thread.Sleep(200);
if (taskHandle.SendShellTask_InStore(taskInfo))
{
PrintLogInfoMessage($"泡后入库任务:{taskInfo.taskCode};下发成功,等待PLC执行反馈");
taskInfo.taskStatus = 2;
_taskInfoService.UpdateTaskInfo(taskInfo);
//if (semaphore.Wait(TimeSpan.FromSeconds(20)))
//{
// // 收到反馈
// PrintLogInfoMessage("收到反馈,继续执行");
//}
//else
//{
// PrintLogInfoMessage("超时未反馈");
//}
semaphore.Wait(); //一直堵塞直到信号量释放
PrintLogInfoMessage($"泡后入库任务:{taskInfo.taskCode};执行完成");
}
else
{
PrintLogInfoMessage($"泡后入库任务:{taskInfo.taskCode};下发失败,请排除PLC连接");
}
}
else
{
PrintLogInfoMessage("未获取到需要下发的泡后入库任务");
}
Thread.Sleep(5000);
}
///
/// 获取待执行的入库任务
///
///
///
private RealTaskInfo GetAwaitSendTask(string storeCode)
{
RealTaskInfo taskInfo = null;
try
{
taskInfo = _taskInfoService.GetTaskInfoByStoreCode(storeCode, 1);
//if (taskInfo != null)
//{
// taskInfo.taskStatus = 2;
// _taskInfoService.UpdateTaskInfo(taskInfo);
//}
}
catch (Exception ex)
{
PrintLogErrorMessage("获取待执行的入库任务异常", ex);
}
return taskInfo;
}
///
/// 泡后执行反馈
///
private void FoamTaskFeedback(string taskCode)
{
Thread.Sleep(4000);
PrintLogInfoMessage("泡后执行完成,自动释放信号量");
semaphore.Release();
InStoreFinish(taskCode);
}
#endregion
///
/// 入库完成
///
///
///
///
private void InStoreFinish(string taskCode)
{
try
{
var taskInfo = _taskInfoService.GetTaskInfoByTaskCode(taskCode, appConfig.foamStoreCode);
if (taskInfo != null)
{
var spaceInfo = _spaceInfoService.GetSpaceInfoBySpaceCode(taskInfo.storeCode, taskInfo.spaceCode);
if (spaceInfo != null)
{
//读取PLC获取货道信息:存放数量、在途数量
spaceInfo.materialType = taskInfo.materialType;
_spaceInfoService.UpdateSpaceInfo(spaceInfo);
}
//清除任务信息
_taskInfoService.DeleteTaskInfo(taskCode, appConfig.foamStoreCode);
}
}
catch (Exception ex)
{
PrintLogErrorMessage("入库完成逻辑处理异常", ex);
}
}
///
/// 日志输出,界面刷新同时记录文件
///
///
private void PrintLogInfoMessage(string message)
{
RefreshLogMessageEvent?.Invoke(message);
logHelper.Info(message);
}
///
/// 异常日志输出
///
///
///
private void PrintLogErrorMessage(string message, Exception ex = null)
{
RefreshLogMessageEvent?.Invoke(message);
logHelper.Error(message, ex);
}
///
/// 截图条码
///
///
///
private string SubStringMaterialCode(string materialCode)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(materialCode))
{
result = materialCode.Substring(2, 10);
}
return result;
}
public List GetInStoreTask()
{
return _taskInfoService.GetTaskInfosByStoreCode(new string[] { appConfig.foamStoreCode }, 1);
}
}
}