using Admin.Core.Common.Config; using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using StackExchange.Profiling.Internal; using Aucma.Core.HwPLc; using Org.BouncyCastle.Asn1.Tsp; using NetTaste; using System.Windows.Media.Media3D; using System.Windows.Forms; using Admin.Core.Model.Model_New; using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData; using Microsoft.IdentityModel.Logging; using Admin.Core.Socket; namespace Aucma.Core.BoxFoam.Business { public class InStoreBusiness { #region 单例实现 private static readonly InStoreBusiness lazy = new InStoreBusiness(); public static InStoreBusiness Instance { get { return lazy; } } #endregion /// /// 已下传的任务信息 /// private List TaskInfos = new List(); private Dictionary KeyValuePairs = new Dictionary(); #region 事件 /// /// 扫码信息刷新 /// /// 条码 /// 编码 /// 名称 /// 货到名称 public delegate Task RefreshDataGridDelegate(); public static event RefreshDataGridDelegate RefreshDataGridDelegateEvent; /// /// 扫码信息刷新 /// /// 条码 /// 编码 /// 名称 /// 货到名称 public delegate void RefreshScanMateriaCode(string materialBarCode, string materialCode, string materialName, string spaceName, string msg); public static event RefreshScanMateriaCode RefreshScanMateriaCodeEvent; /// /// 入库日志事件 /// /// public delegate void LogDelegate(string msg, string color); public static event LogDelegate LogDelegateEvent; /// /// 库存信息 /// /// public delegate void RefreshPageDelegate(); public static event RefreshPageDelegate RefreshPageDelegateEvent; #endregion private string storeCode = string.Empty; private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(InStoreBusiness)); #region 初始化 private readonly IBaseSpaceInfoServices? _baseSpaceInfoServices = App.ServiceProvider.GetService(); private readonly IRealTaskInfoServices? _realTaskInfoService = App.ServiceProvider.GetService(); private readonly IProductPlanInfoServices? _productPlanInfoServices = App.ServiceProvider.GetService(); private readonly IRecordInStoreServices? _recordInstoreServices = App.ServiceProvider.GetService(); private readonly IBaseStoreInfoServices? _baseStoreInfoServices = App.ServiceProvider.GetService(); private readonly IBaseSpaceDetailServices? _baseSpaceDetailServices = App.ServiceProvider.GetService(); // 过点数据表,物料完成记录MaterialCompletion private readonly IMaterialCompletionServices? _iMaterialCompletionServices = App.ServiceProvider.GetService(); private readonly IPrintBarCodeServices? _printBarCodeServices = App.ServiceProvider.GetService(); private readonly IBaseMaterialInfoServices? _baseMaterialInfoServices = App.ServiceProvider.GetService(); #endregion #region 初始化对象 private PlcSpaceConfig spaceConfig = PlcSpaceConfig.Instance; private SemaphoreSlim semaphore = new SemaphoreSlim(0); private List allSpaces = null; private List spaceAddresses = new List(); private SemaphoreSlim _lock = new SemaphoreSlim(1); #endregion #region 构造函数 public InStoreBusiness() { TouchSocketService.ReceiveCodeDelegateEvent += NewTask; storeCode = Appsettings.app("StoreInfo", "BeforeStoreCode");//泡前库code #region 更新库数据 Task.Run(async () => { await RealTimeUpdateStore(); }); #endregion } #endregion #region 根据条码入库 /// /// 根据条码入库——泡前库始终只有一条任务在 /// /// /// public async void NewTask(string IP,string materialBarCode) { if (string.IsNullOrEmpty(materialBarCode)) { LogDelegateEvent?.Invoke("未扫到二维码信息!","Red"); Console.WriteLine($"【{DateTime.Now}】未扫到二维码信息,请重新扫码!"); return; } bool flag = true; //创建入库任务 Console.WriteLine($"【{DateTime.Now}】扫码成功,物料条码:{materialBarCode}"); logHelper.Info($"扫码成功,物料条码:{materialBarCode}"); LogDelegateEvent?.Invoke($"扫码成功,物料条码:{materialBarCode}", "White"); #region 执行任务 string materialCode = SubString(materialBarCode); var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PqPlc")); if (obj != null) { if (obj.plc.IsConnected) { if (flag) { flag = false; try { LogDelegateEvent?.Invoke($"获取物料编码:{materialCode}", "White"); bool result= obj.plc.WriteString("DB200.132", materialCode);//写入物料号 if (result) { int space = 0; do { space = obj.plc.ReadInt16("DB200.438");//读取货道号,大约1.5s 可以获取 } while (space==0); Console.WriteLine($"物料MES码:{materialBarCode}》入库货道:{space}"); logHelper.Info($"物料MES码:{materialBarCode}》入库货道:{space}"); LogDelegateEvent?.Invoke($"物料MES码:{materialBarCode}》物料码:{materialCode}》入库货道:{space}", "White"); //更新货道入库 await EnterStore(materialBarCode, space); //更新过点数 await UpdateComplete(materialBarCode); Console.WriteLine($"更新物料信息成功!"); RefreshDataGridDelegateEvent?.Invoke(); } else { Console.WriteLine($"向PLC写入物料编码失败!"); } } catch (Exception ex) { logHelper.Error($"入库业务异常:{ex}"); LogDelegateEvent?.Invoke($"入库业务异常:{ex}", "Red"); Console.WriteLine($"入库业务异常:{ex.Message}"); } finally { flag = true; } } } } else { Console.WriteLine("PLC连接中..."); Thread.Sleep(3000); } #endregion await Task.CompletedTask; } #endregion #region 更新库数 public async Task RealTimeUpdateStore() { bool flag = true; while (true) { try { if (flag) { flag = false; var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PqPlc")); if (obj != null) { if (obj.plc.IsConnected) { var storeList = await _baseSpaceInfoServices.QueryAsync(d => d.StoreCode == storeCode); foreach (var item in storeList) { switch (item.SpaceCode) { case "PB01_001": item.SpaceStock= obj.plc.ReadInt16("DB200.440"); item.OnRouteAmount = item.SpaceStock-obj.plc.ReadInt16("DB200.88"); break; case "PB01_002": item.SpaceStock = obj.plc.ReadInt16("DB200.442"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.90"); break; case "PB01_003": item.SpaceStock = obj.plc.ReadInt16("DB200.444"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.92"); break; case "PB01_004": item.SpaceStock = obj.plc.ReadInt16("DB200.446"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.94"); break; case "PB01_005": item.SpaceStock = obj.plc.ReadInt16("DB200.448"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.96"); break; case "PB01_006": item.SpaceStock = obj.plc.ReadInt16("DB200.450"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.98"); break; default: break; } item.UpdatedTime = DateTime.Now; bool r= await _baseSpaceInfoServices.UpdateAsync(item); } //刷新库存信息 RefreshPageDelegateEvent?.Invoke(); } } } Thread.Sleep(1000); } catch (Exception ex) { Console.WriteLine($"异常:{ex.Message}"); } finally { flag = true; } } } #endregion #region 更新过点数 /// /// 更新过点数 /// /// /// public async Task UpdateComplete(string materialBarCode) { #region 更新过点数据 PrintBarCode print = await _printBarCodeServices.FirstAsync(x => x.MaterialBarcode == materialBarCode); MaterialCompletion completion = new MaterialCompletion(); completion.OrderCode = print.OrderCode; completion.MaterialBarcode = materialBarCode; completion.MaterialCode = print.MaterialCode; completion.MaterialName = print.MaterialName; completion.StationName = "1004"; completion.ProductLineCode = "CX_02"; completion.isDownLine = 0; completion.CompleteDate = DateTime.Now; int i = await _iMaterialCompletionServices.AddAsync(completion); Console.WriteLine($"过点数更新结果为{i > 0}"); #endregion } #endregion #region 任务执行入库 public async Task EnterStore(string barCode,int space) { //有获取不到情况,需要一直获取 string spaceCode = string.Empty; switch (space) { case 1: spaceCode = "PB01_001"; break; case 2: spaceCode = "PB01_002"; break; case 3: spaceCode = "PB01_003"; break; case 4: spaceCode = "PB01_004"; break; case 5: spaceCode = "PB01_005"; break; case 6: spaceCode = "PB01_006"; break; default: break; } //根据条码获取物料详情 var material = _printBarCodeServices.FirstAsync(d => d.MaterialBarcode == barCode).Result; RecordInStore recordInstore = new RecordInStore(); recordInstore.StoreCode = storeCode; recordInstore.StoreArea = ""; recordInstore.SpaceCode = spaceCode; string materialType = SubString(barCode);//截取中间物料条码 recordInstore.MaterialType = materialType; recordInstore.MaterialCode = barCode; recordInstore.InStoreTime = DateTime.Now; recordInstore.BarCodeCode = barCode; recordInstore.MaterialName = material.MaterialName; recordInstore.InStoreAmount =1; recordInstore.EntryPattern = 0; recordInstore.IsFlag = 0; recordInstore.CreatedTime = DateTime.Now; int result = await _recordInstoreServices.AddAsync(recordInstore); if (result > 0) { string message = $"物料[{material.MaterialName}],入库成功!"; RefreshScanMateriaCodeEvent?.Invoke(material.MaterialCode, barCode, material.MaterialName, spaceCode, message); //刷新界面扫码信息 LogDelegateEvent?.Invoke($"物料[{material.MaterialName}], 入库成功!","White"); RefreshDataGridDelegateEvent?.Invoke(); } else { LogDelegateEvent?.Invoke($"物料[{material.MaterialName}], 入库失败,任务回滚!", "Red"); logHelper.Error($"物料[{material.MaterialName}], 入库失败,任务回滚!"); } } #endregion #region 截取物料编码 public string SubString(string barCode) { string result = string.Empty; if (!string.IsNullOrEmpty(barCode)) { result = barCode.Substring(7, 10); } return result; } #endregion } }