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 { /// ///分垛入库业务处理 /// public class InstoreBusiness { #region 单例实现 private static readonly InstoreBusiness lazy = new InstoreBusiness(); public static InstoreBusiness Instance { get { return lazy; } } #endregion #region 变量定义 public readonly List allScanners = Appsettings.app("ScannerServer").ToList(); #endregion #region 接口引用 private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(InstoreBusiness)); private readonly IBaseSpaceInfoServices? _baseSpaceInfoServices = App.ServiceProvider.GetService(); private readonly ICodeBindingRecordServices? _codeBindingServices = App.ServiceProvider.GetService(); #endregion public void test() { } #region 扫码入库处理 /// /// /// /// 成品码 /// 扫码器ip /// public async Task InStore(string SNCode,string scannerIp) { try { List allScanners = Appsettings.app("ScannerServer").ToList(); ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp); // plc下发结果 bool plcResult = false; // 下发plc的货道号 List spaceNumList = new List(); //1.根据成品码找货道 List 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; } } /// /// 根据成品码找货道 /// /// /// private List 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; } } /// /// 大产品占据两条货道,根据一条货道找到另一条货道 /// /// 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信号下发 /// /// 下发plc入库信号 /// /// 扫码器ip /// 转向角度 /// 货道号int集合 /// private bool sendAndAnswerPlc(string scannerIp,int range,List 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; } } /// /// 下发plc信号 /// /// /// /// /// private bool sendPlc(PlcModel obj, int range, List 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; } } /// /// 等待plc信号反馈 /// /// /// 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; } } /// /// 根据扫码器ip确定是属于哪个plc /// /// /// 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 } }