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/OutStoreTaskHandle.cs

232 lines
8.1 KiB
C#

using Aucma.Scada.Model.domain;
using HighWayIot.Config;
using HighWayIot.Log4net;
using HighWayIot.Plc;
using System;
using System.Collections.Generic;
11 months ago
using System.Threading;
using System.Threading.Tasks;
namespace Aucma.Scada.Business
{
/// <summary>
/// 出库任务处理
/// </summary>
internal sealed class OutStoreTaskHandle
{
#region 单例实现
private static readonly Lazy<OutStoreTaskHandle> lazy = new Lazy<OutStoreTaskHandle>(() => new OutStoreTaskHandle());
public static OutStoreTaskHandle Instance
{
get
{
return lazy.Value;
}
}
#endregion
#region 对象引用
11 months ago
private LogHelper logHelper = LogHelper.Instance;
private AppConfig appConfig = AppConfig.Instance;
private PlcConfig plcConfig = PlcConfig.Instance;
11 months ago
private PlcPool _pool = PlcPool.Instance;
#endregion
11 months ago
#region 私有变量
/// <summary>
/// 字典存放PLC连接
/// </summary>
private Dictionary<string, IPlc> _plcDictionary = new Dictionary<string, IPlc>();
/// <summary>
/// 泡后任务编号,PLC反馈后进行赋值
/// </summary>
11 months ago
private string shellTaskCode = string.Empty;
/// <summary>
/// 内胆任务编号,PLC反馈后进行赋值
/// </summary>
11 months ago
private string linerTaskCode = string.Empty;
#endregion
#region 委托事件
/// <summary>
/// 出库完成
/// </summary>
/// <param name="storeCode"></param>
/// <param name="taskCode"></param>
public delegate void OutStoreFinsih(string storeCode, string taskCode);
public event OutStoreFinsih OutStoreFinsihEvent;
#endregion
private OutStoreTaskHandle()
{
11 months ago
_plcDictionary = _pool.GetAll();
}
#region 泡后出库任务下发处理
11 months ago
/// <summary>
/// 泡后出库任务下发
11 months ago
/// </summary>
/// <param name="taskInfo"></param>
public bool SendFoamTask_OutStore(RealTaskInfo taskInfo)
{
11 months ago
bool result = false;
try
{
IPlc _plc = _plcDictionary[taskInfo.storeCode];
if (_plc != null)
{
if (_plc.IsConnected)
{
//写入货道号
_plc.writeStringByAddress(plcConfig.out_foam_spaceCode, taskInfo.spaceCode);
11 months ago
//写入出库数量
_plc.writeInt32ByAddress(plcConfig.out_foam_amount, taskInfo.planAmount);
11 months ago
//写入应答字
_plc.writeInt32ByAddress(plcConfig.out_foam_answer, 1);
11 months ago
//写入任务号
_plc.writeStringByAddress(plcConfig.out_foam_task, taskInfo.taskCode);
11 months ago
//写入完成后读取应答字进行复位
ReadShellAnswer_OutStore(taskInfo.taskCode);
result = true;
}
else
{
logHelper.Info($"仓库{taskInfo.storeCode}PLC未连接");
}
}
else
{
logHelper.Info($"PLC信息为空通过{taskInfo.storeCode}未获取到该仓库对应的PLC信息");
}
}
catch (Exception ex)
{
logHelper.Error("泡后出库任务下发异常", ex);
11 months ago
}
return result;
}
11 months ago
/// <summary>
/// 读取泡后出库应答
11 months ago
/// </summary>
private void ReadShellAnswer_OutStore(string taskCode)
{
11 months ago
lock (string.Empty)
{
11 months ago
bool isFlag = true;
IPlc _plc = _plcDictionary[appConfig.foamStoreCode];
11 months ago
try
{
Task.Run(() =>
{
if (_plc != null)
{
if (_plc.IsConnected)
{
do
{
//读取PLC应答字为2时上位机清空写入的出库内容
if (_plc.readInt32ByAddress(plcConfig.out_foam_answer) == 2)
11 months ago
{
//写入货道号
_plc.writeStringByAddress(plcConfig.out_foam_spaceCode, string.Empty);
11 months ago
//写入出库数量
_plc.writeInt32ByAddress(plcConfig.out_foam_amount, 0);
11 months ago
//写入应答字
_plc.writeInt32ByAddress(plcConfig.out_foam_answer, 0);
11 months ago
//写入任务号
_plc.writeStringByAddress(plcConfig.out_foam_task, string.Empty);
11 months ago
isFlag = false;
ReadShellFinish_OutStore(taskCode);
}
Thread.Sleep(1000);
11 months ago
} while (isFlag);
}
else
{
logHelper.Info($"仓库{appConfig.foamStoreCode}PLC未连接");
11 months ago
}
}
else
{
logHelper.Info($"PLC信息为空通过{appConfig.foamStoreCode}未获取到该仓库对应的PLC信息");
11 months ago
}
});
}
catch (Exception ex)
{
logHelper.Error("读取泡后出库应答字异常", ex);
11 months ago
}
}
}
/// <summary>
/// 读取泡后出库完成
11 months ago
/// </summary>
private void ReadShellFinish_OutStore(string taskCode)
{
lock (string.Empty)
{
bool isFlag = true;
IPlc _plc = _plcDictionary[appConfig.foamStoreCode];
11 months ago
shellTaskCode = taskCode;
try
{
Task.Run(() =>
{
if (_plc != null)
{
if (_plc.IsConnected)
{
do
{
//读取PLC出库任务完成
if (_plc.readInt32ByAddress(plcConfig.out_foam_finish) == 1)
11 months ago
{
_plc.writeInt32ByAddress(plcConfig.out_foam_finish, 0);
11 months ago
//string taskCode = _plc.readStringByAddress(plcConfig.out_foam_task, 10);
11 months ago
OutStoreFinsihEvent?.Invoke(appConfig.foamStoreCode, taskCode);
11 months ago
isFlag = false;
}
Thread.Sleep(1000);
11 months ago
} while (isFlag);
}
else
{
logHelper.Info($"仓库{appConfig.foamStoreCode}PLC未连接");
11 months ago
}
}
else
{
logHelper.Info($"PLC信息为空通过{appConfig.foamStoreCode}未获取到该仓库对应的PLC信息");
11 months ago
}
});
}
catch (Exception ex)
{
logHelper.Error("读取泡后出库出库完成异常", ex);
}
11 months ago
}
}
#endregion
}
}