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.

308 lines
11 KiB
C#

using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Aucma.Core.HwPLc;
using Aucma.Core.Scanner;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Aucma.Core.Palletiz.Business
{
/// <summary>
///分垛入库业务处理
/// </summary>
public class InstoreBusiness
{
#region 单例实现
private static readonly InstoreBusiness lazy = new InstoreBusiness();
public static InstoreBusiness Instance
{
get
{
return lazy;
}
}
#endregion
#region 变量定义
public readonly List<ScannerModel> allScanners = Appsettings.app<ScannerModel>("ScannerServer").ToList();
#endregion
#region 接口引用
private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(InstoreBusiness));
private readonly IBaseSpaceInfoServices? _baseSpaceInfoServices = App.ServiceProvider.GetService<IBaseSpaceInfoServices>();
private readonly ICodeBindingRecordServices? _codeBindingServices = App.ServiceProvider.GetService<ICodeBindingRecordServices>();
#endregion
public void test()
{
}
#region 扫码入库处理
/// <summary>
///
/// </summary>
/// <param name="SNCode">成品码</param>
/// <param name="scannerIp">扫码器ip</param>
/// <returns></returns>
public async Task InStore(string SNCode,string scannerIp)
{
try
{
List<ScannerModel> allScanners = Appsettings.app<ScannerModel>("ScannerServer").ToList();
ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp);
// plc下发结果
bool plcResult = false;
// 下发plc的货道号
List<int> spaceNumList = new List<int>();
//1.根据成品码找货道
List<BaseSpaceInfo> spaceList = getSpaceBySNCode(SNCode);
// 根据货道信息判断下发plc信号
if (spaceList == null)
{
logHelper.Error("未找到匹配货道,请手动入库!");
// 刷新页面提示信息
return;
}
if (spaceList.Count == 1)
{
spaceNumList.Add(int.Parse(spaceList[0].SpaceCode.Substring(5, 3)));
spaceNumList.Add(0);
plcResult = sendAndAnswerPlc(scannerIp, spaceList[0].RotationRange, spaceNumList);
spaceList[0].LastSpace = spaceList[0].SpaceCode;
}
// 暂时只考虑两个货道入一种型号
else if (spaceList.Count >= 2)
{
// 大产品占两道
if (spaceList[0].IsTwoSpace == 1)
{
spaceNumList.Add(int.Parse(spaceList[0].SpaceCode.Substring(5, 3)));
spaceNumList.Add(int.Parse(spaceList[1].SpaceCode.Substring(5, 3)));
plcResult = sendAndAnswerPlc(scannerIp, spaceList[0].RotationRange, spaceNumList);
}
else
{
// last不等于自己可以先入自己否则入另一条货道
if (!spaceList[0].LastSpace.Equals(spaceList[0].SpaceCode))
{
spaceNumList.Add(int.Parse(spaceList[0].SpaceCode.Substring(5, 3)));
spaceNumList.Add(0);
plcResult = sendAndAnswerPlc(scannerIp, spaceList[0].RotationRange, spaceNumList);
spaceList[0].LastSpace = spaceList[0].SpaceCode;
}
else
{
spaceNumList.Add(0);
spaceNumList.Add(int.Parse(spaceList[1].SpaceCode.Substring(5, 3)));
sendAndAnswerPlc(scannerIp, spaceList[0].RotationRange, spaceNumList);
spaceList[0].LastSpace = spaceList[1].SpaceCode;
}
}
}
if(plcResult==true)
{
// 刷新界面,更新入库记录,货道信息
}
else
{
// 界面提示,手动入库
}
}
catch (Exception ex)
{
logHelper.Error(ex.Message.ToString());
return;
}
}
/// <summary>
/// 根据成品码找货道
/// </summary>
/// <param name="SNCode"></param>
/// <returns></returns>
private List<BaseSpaceInfo> getSpaceBySNCode(string SNCode)
{
try
{
CodeBindingRecord bindingRecord = _codeBindingServices.Query(c => c.ProductCode.Equals(SNCode)).FirstOrDefault();
if (bindingRecord == null) return null;
return _baseSpaceInfoServices.Query(s => s.MaterialType.Equals(bindingRecord.BoxCode.Substring(7, 10)));
}
catch (Exception ex)
{
logHelper.Error(ex.Message.ToString());
return null;
}
}
/// <summary>
/// 大产品占据两条货道,根据一条货道找到另一条货道
/// </summary>
/// <param name="spaceInfo"></param>
private string getOtherSpace(BaseSpaceInfo spaceInfo)
{
// 找到当前货道匹配的另一条货道
int num = int.Parse(spaceInfo.SpaceCode.Substring(5, 3));
string otherSpaceCode = string.Empty;
if (num % 2 == 0) // 偶数如7,8当前8找7
{
otherSpaceCode = spaceInfo.SpaceCode.Substring(0, 5) + (num - 1).ToString("D3");
}
else
{
otherSpaceCode = spaceInfo.SpaceCode.Substring(0, 5) + (num + 1).ToString("D3");
}
return otherSpaceCode;
}
#endregion
#region plc信号下发
/// <summary>
/// 下发plc入库信号
/// </summary>
/// <param name="scannerIp">扫码器ip</param>
/// <param name="range">转向角度</param>
/// <param name="spaceNum">货道号int集合</param>
/// <returns></returns>
private bool sendAndAnswerPlc(string scannerIp,int range,List<int> spaceNum)
{
bool result = false;
try
{
PlcModel obj = getPlcByScanner(scannerIp);
if (obj != null)
{
if (sendPlc(obj, range, spaceNum))
{
result = waitAnswerPlc(obj);
}
}
else
{
logHelper.Error("plc未连接");
return false;
}
return result;
}
catch (Exception ex)
{
logHelper.Error(ex.Message.ToString());
return result;
}
}
/// <summary>
/// 下发plc信号
/// </summary>
/// <param name="obj"></param>
/// <param name="range"></param>
/// <param name="spaceNum"></param>
/// <returns></returns>
private bool sendPlc(PlcModel obj, int range, List<int> spaceNum)
{
try
{
bool result = false;
DateTime targetTime = DateTime.Now.AddSeconds(8);
while (true)
{
if (DateTime.Now > targetTime) // plc超最大时限无反馈
{
logHelper.Error("等待plc放行反馈信号超时");
return false;
}
// 应答字允许下发
if (obj.plc.ReadInt32("D102") == 1)
{
//旋转角度
obj.plc.WriteInt32("D110", range);
//货道号
obj.plc.WriteInt32("D112", spaceNum[0]);
obj.plc.WriteInt32("D114", spaceNum[1]);
result = true;
break;
}
Thread.Sleep(500);
}
return result;
}
catch (Exception ex)
{
logHelper.Error(ex.Message);
return false;
}
}
/// <summary>
/// 等待plc信号反馈
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private bool waitAnswerPlc(PlcModel obj)
{
try
{
bool result = false;
DateTime targetTime = DateTime.Now.AddSeconds(8);
while (true)
{
if (DateTime.Now > targetTime) // plc超最大时限无反馈
{
logHelper.Error("等待plc放行反馈信号超时");
return false;
}
// 应答字允许下发
if (obj.plc.ReadInt32("D102") == 2)
{
result = true;
break;
}
Thread.Sleep(500);
}
return result;
}
catch (Exception ex)
{
logHelper.Error(ex.Message.ToString());
return false;
}
}
/// <summary>
/// 根据扫码器ip确定是属于哪个plc
/// </summary>
/// <param name="scannerIp"></param>
/// <returns></returns>
private PlcModel getPlcByScanner(string scannerIp)
{
PlcModel obj = null;
ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp);
if (model.Id < 3)
{
obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("A库Plc"));
}
else
{
obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("B库Plc"));
}
return obj;
}
#endregion
}
}