From 2c392482f061f790ff3709dae4edcc0836b25efb Mon Sep 17 00:00:00 2001 From: liuwf Date: Mon, 27 Nov 2023 19:13:07 +0800 Subject: [PATCH] change . --- .../Business/offLineBusiness.cs | 248 +++++++++++++++++- .../ViewModels/IndexPageViewModel.cs | 5 +- 2 files changed, 244 insertions(+), 9 deletions(-) diff --git a/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs b/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs index 1a118d7a..f52b35f7 100644 --- a/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs +++ b/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs @@ -1,6 +1,8 @@ using Admin.Core.IService; using Admin.Core.Model; +using Admin.Core.Model.Model_New; using Admin.Core.Service; +using Aucma.Core.PLc; using Aucma.Core.ProductOffLine; using log4net; using Microsoft.Extensions.DependencyInjection; @@ -31,16 +33,19 @@ namespace Aucma.Core.ProductOffLine.Business } #endregion - System.Timers.Timer timer = new System.Timers.Timer(1000); - #region 接口引用 - private IBaseBomInfoServices _baseBomInfoService; + private static readonly log4net.ILog log = LogManager.GetLogger(typeof(OffLineBusiness)); + private readonly IBaseOrderInfoServices _baseOrderInfoServices; + private readonly IProductPlanInfoServices? _productPlanInfoServices; + private readonly IProductOffLineServices? _productOffLineServices; #endregion + + #region 委托事件 - + /// /// 扫码信息刷新 /// @@ -53,16 +58,245 @@ namespace Aucma.Core.ProductOffLine.Business #endregion + #region 变量定义 + /// + /// 定时器 + /// + System.Timers.Timer timer = new System.Timers.Timer(1000); + + #endregion + public OffLineBusiness() { - _baseBomInfoService = App.ServiceProvider.GetService(); - BaseBomInfo bom = _baseBomInfoService.FirstAsync().Result; - Console.WriteLine(); + + } public void test() { } + + + + /// + /// 扫描的条码处理 + /// + /// + /// 扫码器方向 + private async void ReceiveCode(string code, int scannerNo) + { + //1.扫描的SN条码去条码系统查询GET_BARCODE_DATA 返回校验数据 + // string result = await _productOffLineServices.QueryChecked(code); + string result = "Y@1104@16160030000000910780@000010034895@@ @000000@000000009000000807@BCD-160C,家电下乡@@BCD-160C@皓月白-家电下乡@161601300@160@1-00版@家电下乡产品@默认@2010-09-01"; + // 如果校验失败,下发报警 + if ("N".Equals(result.Substring(0, 1))) + { + sendPlcError(); + // 刷新页面提示信息 result.Substring(2) + return; + } + //2.查询到的数据分割处理,结果中用"@"号分隔,刷新页面显示并存到scada数据库(表待建) + string[] resultArray = result.Split('@'); + // 输出结果 + foreach (string item in resultArray) + { + Console.WriteLine(item); + } + + //3.分垛A,B库逻辑(先左边读到的条码分A库,右边读到的B库==>预留分库逻辑) + + //4.下发plc信号 + bool flag = SendPlcPass(scannerNo); + if (!flag) + { + log.Error("获取plc反馈失败"); + return; + } + Console.WriteLine(code); + return; + //5.调条码系统保存入库接口SaveBarcodeInfo + string saveResult = await _productOffLineServices.SaveBarcodeInfo("333", "ILS_SORT", "11111", 1); + if (string.IsNullOrEmpty(saveResult) || "E".Equals(saveResult.Substring(0, 1))) + { + log.Error("条码系统保存接口出错"); + sendPlcError(); + return; + } + //6.更新mes数据库完成数量和时间(BASE_ORDERINFO,PRODUCT_PLANINFO) 注意:查询的字段可能需要修改,确保数据一致 + + //6.1截取订单号去查询更新BASE_ORDERINFO,全放到service处理 + string orderCode = "11215484"; + List orders = await _baseOrderInfoServices.QueryAsync(x => x.OrderCode == orderCode); + BaseOrderInfo order = orders.FirstOrDefault(); + if (order.CompleteAmount == 0) + { + order.CompleteAmount++; + order.BeginDate = DateTime.Now; + } + if (order.CompleteAmount == order.OrderAmount) + { + order.EndDate = DateTime.Now; + } + order.UpdatedTime = DateTime.Now; + _baseOrderInfoServices.UpdateAsync(order); + //6.2根据订单号去更新PRODUCT_PLANINFO + List productInfos = await _productPlanInfoServices.QueryAsync(x => x.OrderCode == orderCode); + ProductPlanInfo productInfo = productInfos.FirstOrDefault(); + if (productInfo.CompleteAmount == 0) + { + productInfo.CompleteAmount++; + productInfo.BeginTime = DateTime.Now; + } + if (productInfo.CompleteAmount == productInfo.PlanAmount) + { + productInfo.EndTime = DateTime.Now; + } + productInfo.UpdatedTime = DateTime.Now; + await _productPlanInfoServices.UpdateAsync(productInfo); + + } + + + #region plc交互 + private SemaphoreSlim semaphore = new SemaphoreSlim(0); + /// + /// 下发plc放行信号,根据接收到的scannerNo不同,两条道的plc信号地址也不同 + /// + /// + private bool SendPlcPass(int scannerNo) + { + bool result = false; + try + { + var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("成品下线Plc")); + if (obj != null) + { + if (obj.plc.IsConnected) + { + if (scannerNo == 1) + { + // 写入入库方向,两条道plc地址不同 + obj.plc.WriteInt32("D100", scannerNo); + // 往plc写入放行信号 + obj.plc.WriteInt32("D102", 1); + } + else + { + // 写入入库方向,两条道plc地址不同 + obj.plc.WriteInt32("D200", scannerNo); + // 往plc写入放行信号 + obj.plc.WriteInt32("D202", 1); + } + + // 等待plc反馈信号 + waitPlcSignal(scannerNo); + // semaphore.Wait(); + result = true; + } + else + { + log.Info("成品下线plc连接失败,请检查plc连接"); + } + } + else + { + log.Info("获取plc连接对象信息为空"); + } + } + catch (Exception ex) + { + log.Error("下发plc放行信号方法SendPlcPass()出现异常,ex:" + ex); + } + return result; + } + /// + /// 读取plc放行反馈 + /// + /// + private void waitPlcSignal(int scannerNo) + { + try + { + bool isFlag = true; + var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("成品下线Plc")); + + if (obj != null && obj.plc.IsConnected) + { + do + { + if (scannerNo == 1) + { + // 读取plc反馈信号 + if (obj.plc.ReadInt32("D102") == 2) + { + Console.WriteLine("收到plc放行成功"); + // 清空数据 + obj.plc.WriteInt32("D100", 0); + obj.plc.WriteInt32("D102", 0); + isFlag = false; + } + } + else + { + // 读取plc反馈信号 + if (obj.plc.ReadInt32("D202") == 2) + { + Console.WriteLine("收到plc放行成功"); + // 清空数据 + obj.plc.WriteInt32("D200", 0); + obj.plc.WriteInt32("D202", 0); + isFlag = false; + } + } + Thread.Sleep(1000); + } while (isFlag); + // 释放信号量 + //semaphore.Release(); + } + else + { + log.Info("成品下线plc连接失败"); + } + + } + catch (Exception ex) + { + log.Error("读取plc放行反馈方法waitPlcSignal()出现异常,ex:" + ex); + } + } + + /// + /// 下发plc报警信号 + /// + private void sendPlcError() + { + try + { + var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("成品下线Plc")); + if (obj != null) + { + if (obj.plc.IsConnected) + { + // 下发报警信号 + obj.plc.WriteInt32("D104", 1); + } + else + { + log.Info("成品下线plc连接失败,请检查plc连接"); + } + } + else + { + log.Info("获取plc连接对象信息为空"); + } + } + catch (Exception ex) + { + log.Error("下发plc放报警信号方法sendPlcError()出现异常,ex:" + ex); + } + } + #endregion + } } diff --git a/Aucma.Core.ProductOffLine/ViewModels/IndexPageViewModel.cs b/Aucma.Core.ProductOffLine/ViewModels/IndexPageViewModel.cs index 9f20923a..c9d62057 100644 --- a/Aucma.Core.ProductOffLine/ViewModels/IndexPageViewModel.cs +++ b/Aucma.Core.ProductOffLine/ViewModels/IndexPageViewModel.cs @@ -44,13 +44,15 @@ namespace Aucma.Core.ProductOffLine.ViewModels private readonly IProductPlanInfoServices? _productPlanInfoServices; private readonly IProductOffLineServices ? _productOffLineServices; System.Timers.Timer timer = new System.Timers.Timer(1000); + + private OffLineBusiness offLineBusiness = OffLineBusiness.Instance; int a = 0; public IndexPageViewModel() { offLineBusiness.test(); - _productOffLineServices = App.ServiceProvider.GetService(); + _productOffLineServices = App.ServiceProvider.GetService(); _baseOrderInfoServices = App.ServiceProvider.GetService(); _productPlanInfoServices = App.ServiceProvider.GetService(); MvCodeHelper.ReceiveCodeEvent += ReceiveCode; @@ -454,7 +456,6 @@ namespace Aucma.Core.ProductOffLine.ViewModels { log.Info("成品下线plc连接失败"); } - } catch (Exception ex) {