From 42edc0045de6fe82196ece50701be6bc17fba233 Mon Sep 17 00:00:00 2001 From: liuwf Date: Thu, 21 Mar 2024 13:07:41 +0800 Subject: [PATCH] =?UTF-8?q?change-=E6=9D=A1=E7=A0=81=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E6=89=AB=E7=A0=81=E5=99=A8=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=BAtcp=E9=80=9A=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.Core.Socket/TouchSocketService.cs | 2 +- .../Aucma.Core.CodeBinding.csproj | 2 +- .../Business/GunBusiness.cs | 6 +- Aucma.Core.CodeBinding/Startup.cs | 16 +- .../ViewModels/IndexPageViewModel.cs | 274 +++++++++--------- .../ViewModels/MainWindowViewModel.cs | 27 +- Aucma.Core.CodeBinding/appsettings.json | 4 + 7 files changed, 185 insertions(+), 146 deletions(-) diff --git a/Admin.Core.Socket/TouchSocketService.cs b/Admin.Core.Socket/TouchSocketService.cs index 8633097d..f57678ad 100644 --- a/Admin.Core.Socket/TouchSocketService.cs +++ b/Admin.Core.Socket/TouchSocketService.cs @@ -32,7 +32,7 @@ namespace Admin.Core.Socket /// 扫码业务触发事件 /// /// - public delegate Task ReceiveCodeDelegate(string IP,string code); + public delegate void ReceiveCodeDelegate(string IP,string code); public static event ReceiveCodeDelegate? ReceiveCodeDelegateEvent; TcpService service; diff --git a/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj b/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj index 7edf5cca..499c8941 100644 --- a/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj +++ b/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net6.0-windows enable true diff --git a/Aucma.Core.CodeBinding/Business/GunBusiness.cs b/Aucma.Core.CodeBinding/Business/GunBusiness.cs index 9883ed22..37c3966e 100644 --- a/Aucma.Core.CodeBinding/Business/GunBusiness.cs +++ b/Aucma.Core.CodeBinding/Business/GunBusiness.cs @@ -42,7 +42,7 @@ namespace Aucma.Core.CodeBinding.Business /// /// /// - public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip); + public delegate void RefreshMaterialCodeStr(string ip,string materialCodeStr); public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; public static GunBusiness Instance @@ -106,13 +106,13 @@ namespace Aucma.Core.CodeBinding.Business { // mes条码 MESCode = str; - RefreshMaterialCodeStrEvent?.Invoke(str, MesScannerIp); + RefreshMaterialCodeStrEvent?.Invoke(MesScannerIp,str); } else { //SN码 SNCode = str; - RefreshMaterialCodeStrEvent?.Invoke(str,SNScannerIp); + RefreshMaterialCodeStrEvent?.Invoke(SNScannerIp,str); } if(!string.IsNullOrEmpty(MESCode) && !string.IsNullOrEmpty(SNCode)) { diff --git a/Aucma.Core.CodeBinding/Startup.cs b/Aucma.Core.CodeBinding/Startup.cs index 68c39dd8..7ac47453 100644 --- a/Aucma.Core.CodeBinding/Startup.cs +++ b/Aucma.Core.CodeBinding/Startup.cs @@ -6,6 +6,7 @@ using Admin.Core.IService.ISys; using Admin.Core.Model; using Admin.Core.Repository; using Admin.Core.Service; +using Admin.Core.Socket; using Admin.Core.Tasks; using Aucma.Core.RunPlc; using Aucma.Core.Scanner; @@ -15,6 +16,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System.Net.Sockets; using System.Text; namespace Aucma.Core.CodeBinding @@ -63,10 +65,13 @@ namespace Aucma.Core.CodeBinding //עҵ AddServices(services); + //socketɨ + services.AddTouchSocketSetup(); + //ɨ - services.AddScannerSetup(); + // services.AddScannerSetup(); // ɨǹ - // services.AddScannerGunSetup(); + // services.AddScannerGunSetup(); // services.AddJobSetup(); @@ -92,14 +97,14 @@ namespace Aucma.Core.CodeBinding /// /// /// - public void Configure(IApplicationBuilder app, IScannerService scannerService, IRunPlcService runPlcService, ISysTasksQzService tasksQzService, ISchedulerCenter schedulerCenter) // ISysTasksQzService tasksQzService, ISchedulerCenter schedulerCenter + public void Configure(IApplicationBuilder app, IRunPlcService runPlcService, ISysTasksQzService tasksQzService, ISchedulerCenter schedulerCenter, ITouchSocketService socket) // ISysTasksQzService tasksQzService, ISchedulerCenter schedulerCenter,IScannerService scannerService, { // ʹþ̬ļ app.UseStaticFiles(); // ȻȨм //app.UseAuthorization(); //ɨ - app.UseScannerMildd(scannerService); + // app.UseScannerMildd(scannerService); //ɨǹ // app.UseScannerGunMildd(scannerGunService); @@ -107,7 +112,8 @@ namespace Aucma.Core.CodeBinding app.UseQuartzJobMildd(tasksQzService, schedulerCenter); //PLC app.UsePlcMildd(runPlcService); - + // Socketɨ + app.UseTouchSocketMildd(socket); } #region ע diff --git a/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs b/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs index cb6b1579..b9785db7 100644 --- a/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs +++ b/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs @@ -38,6 +38,8 @@ using Admin.Core.Common; using Aucma.Core.CodeBinding.Business; using Aucma.Core.Scanner; using LiveCharts.Defaults; +using Admin.Core.Socket; +using System.Runtime.ConstrainedExecution; /* * 首页信息 */ @@ -49,17 +51,24 @@ namespace Aucma.Core.CodeBinding.ViewModels private readonly ICodeBindingRecordServices? _codeBindingRecordServices; private readonly IBaseMaterialInfoServices? _baseMaterialInfoServices; private readonly IBaseBomInfoServices? _baseBomInfoServices; - + private readonly IPrintBarCodeServices? _printBarCodeServices; // 过点数据表,物料完成记录MaterialCompletion private readonly IMaterialCompletionServices? _iMaterialCompletionServices; - // 静态变量存code1 - // private static string code1Str = string.Empty; + // 静态变量存code1,mes条码 + private static string code1Str = string.Empty; - // 静态变量存code2 - // private static string code2Str = string.Empty; + // 静态变量存code2,SN条码 + private static string code2Str = string.Empty; //配置文件扫码器列表 private static List allScanners = Appsettings.app("ScannerServer").ToList(); + + // mes扫码器ip + private static string MesScannerIp = allScanners.First(x => x.Name == "mes扫码器").Ip; + // sn扫码器ip + private static string SnScannerIp = allScanners.First(x => x.Name == "sn扫码器").Ip; + + public IndexPageViewModel() { try @@ -67,18 +76,20 @@ namespace Aucma.Core.CodeBinding.ViewModels _codeBindingRecordServices = App.ServiceProvider.GetService(); _baseBomInfoServices = App.ServiceProvider.GetService(); _baseMaterialInfoServices = App.ServiceProvider.GetService(); - + _printBarCodeServices = App.ServiceProvider.GetService(); _iMaterialCompletionServices = App.ServiceProvider.GetService(); - Business.MvCodeHelper.ReceiveCodeEvent += ReceiveCode; - Business.MvCodeHelper.BindingCodeEvent += BindingCode; - Business.MvCodeHelper.NoReadEvent += ReceiveNoRead; + // Business.MvCodeHelper.ReceiveCodeEvent += ReceiveCode; + // Business.MvCodeHelper.BindingCodeEvent += BindingCode; + // Business.MvCodeHelper.NoReadEvent += ReceiveNoRead; + TouchSocketService.ReceiveCodeDelegateEvent += ReceiveCode; + TouchSocketService.NoReadReceiveEvent += ReceiveNoRead; GunBusiness.BindingReceiveCodeEvent += BindingCode; GunBusiness.RefreshMaterialCodeStrEvent += ReceiveCode; - + LoadData(); - - + + //实时绑定条码和实时下发plc放行信号 realBindingAndSendPlc(); LoadCharts(); @@ -86,7 +97,7 @@ namespace Aucma.Core.CodeBinding.ViewModels //Task.Run(() => //{ // Thread.Sleep(4000); - + //}); @@ -97,47 +108,47 @@ namespace Aucma.Core.CodeBinding.ViewModels } } - + /// /// 实时绑定条码和实时下发plc放行信号 /// public void realBindingAndSendPlc() { - string tempCode1 ; + string tempCode1; string tempCode2; - //// 实时绑定条码 - //Task.Run(() => - //{ - // while(true) - // { - // try - // { - // if (!string.IsNullOrEmpty(code1Str) && !string.IsNullOrEmpty(code2Str)) - // { - // tempCode1 = code1Str; - // tempCode2 = code2Str; - // code1Str = string.Empty; - // code2Str = string.Empty; - // BindingCode(tempCode1, tempCode2); - // } - // } - // catch (Exception ex) - // { - // Console.WriteLine(ex.ToString()); - // } - // Thread.Sleep(500); - // } - //}); + // 实时绑定条码 + Task.Run(() => + { + while (true) + { + try + { + if (!string.IsNullOrEmpty(code1Str) && !string.IsNullOrEmpty(code2Str)) + { + BindingCode(code1Str, code2Str); + code1Str = string.Empty; + code2Str = string.Empty; + } + } + catch (Exception ex) + { + code1Str = string.Empty; + code2Str = string.Empty; + Console.WriteLine(ex.ToString()); + } + Thread.Sleep(500); + } + }); - // 心跳线程 + // 心跳线程 Task.Run(() => { try { Thread.Sleep(3000); var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("条码绑定Plc")); - if(obj != null) + if (obj != null) { while (true) { @@ -147,7 +158,7 @@ namespace Aucma.Core.CodeBinding.ViewModels Thread.Sleep(1000); } } - + } catch (Exception ex) { @@ -201,13 +212,13 @@ namespace Aucma.Core.CodeBinding.ViewModels { try { - - // LoadCharts(); + + // LoadCharts(); List records = null; - records = _codeBindingRecordServices.QueryAsync(x => x.BoxCode != null && x.RecordTime2 >= DateTime.Today && x.RecordTime2 < DateTime.Today.AddDays(1)).Result; + records = _codeBindingRecordServices.QueryAsync(x => x.BoxCode != null && x.RecordTime2 >= DateTime.Today && x.RecordTime2 < DateTime.Today.AddDays(1)).Result; - if (records != null && records.Count>0) + if (records != null && records.Count > 0) { records = records.OrderBy(x => x.RecordTime2).ToList(); Application.Current.Dispatcher.Invoke(() => @@ -226,7 +237,7 @@ namespace Aucma.Core.CodeBinding.ViewModels RecordTime = record.RecordTime2.ToString() }); - + } }); } @@ -244,30 +255,30 @@ namespace Aucma.Core.CodeBinding.ViewModels if (list == null || list.Count == 0) return; var result = from m in list - group m by GetSubstringBetweenCommas(m.BoxName) into g - select new CodeBindCharts() - { - BoxName = g.Key, - Amount = g.Sum(m=>m.Amount) - }; + group m by GetSubstringBetweenCommas(m.BoxName) into g + select new CodeBindCharts() + { + BoxName = g.Key, + Amount = g.Sum(m => m.Amount) + }; App.Current.Dispatcher.Invoke(() => { - - ProductionHourList.Clear(); - ModelStatistics.Clear(); + + ProductionHourList.Clear(); + ModelStatistics.Clear(); // 图表赋值 ChartValues achievement = new ChartValues(); - - + + int i = 0; double total = 0; - + foreach (var item in result) { ProductionHourList.Add(item.BoxName); - + achievement.Add(new ObservablePoint(i, item.Amount)); total += item.Amount; i++; @@ -283,7 +294,7 @@ namespace Aucma.Core.CodeBinding.ViewModels column.Foreground = Brushes.White; ModelStatistics.Add(column); - + }); } catch (Exception) @@ -397,13 +408,13 @@ namespace Aucma.Core.CodeBinding.ViewModels bindingInfo = value; OnPropertyChanged(nameof(BindingInfo)); } - } + } #region 日产量柱状图X轴日期 /// /// 日产量柱状图X轴日期 /// - private List productionHourList =new List(); + private List productionHourList = new List(); public List ProductionHourList { @@ -446,62 +457,67 @@ namespace Aucma.Core.CodeBinding.ViewModels /// private async void ReceiveNoRead(string scannerIp) { - ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp); - if (model.Id == 1) + // ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp); + if (scannerIp == MesScannerIp) { - log.Info("MES条码NoRead:"); - // 全局变量赋值 - // code1Str = string.Empty ; - // code2Str = string.Empty; - // 页面扫码信息清空 - Code1 = string.Empty; - Code2 = string.Empty; - Code1Time = string.Empty ; - Code2Time = string.Empty ; + // log.Info("MES条码NoRead:"); + Clear(); SendPlcStop(); RefreshAndWriteLog("MES条码未扫描到,NoRead报警,线体停止,请重新扫描!"); } - else if(model.Id == 2) + else if (SnScannerIp == scannerIp) { - RefreshAndWriteLog("SN条码未扫描到,NoRead报警,线体停止,请重新扫描!"); - // code1Str = string.Empty; - // code2Str = string.Empty; - // 页面扫码信息清空 - Code1 = string.Empty; - Code2 = string.Empty; - Code1Time = string.Empty; - Code2Time = string.Empty; + + Clear(); SendPlcStop(); + RefreshAndWriteLog("SN条码未扫描到,NoRead报警,线体停止,请重新扫描!"); } } - /// - /// 接收扫码器传输的条码,扫码器ip - /// - /// - /// - private void ReceiveCode(string codeStr,string scannerIp) + /// + /// 页面扫码信息清空 + /// + public void Clear() { + code1Str = string.Empty; + code2Str = string.Empty; + // 页面扫码信息清空 + Code1 = string.Empty; + Code2 = string.Empty; + Code1Time = string.Empty; + Code2Time = string.Empty; + BindingInfo = string.Empty; + } + + /// + /// 接收扫码器传输的条码,扫码器ip + /// + /// + /// + private void ReceiveCode(string scannerIp, string codeStr) + { + + // ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp); + if (MesScannerIp == scannerIp) + { + log.Info("扫描到MES条码:" + codeStr); + // 全局变量赋值,SN码扫描后使用 + code1Str = codeStr; + RefreshCode1(codeStr); + } + else if (SnScannerIp == scannerIp) + + { + Clear(); + + log.Info("扫描到成品条码:" + codeStr); + // 1.刷新界面条码信息 + // 全局变量赋值,mes条码扫描后使用 + code2Str = codeStr; + RefreshCode2(codeStr); + + } - - ScannerModel model = allScanners.FirstOrDefault(x => x.Ip == scannerIp); - if (model.Id == 1) - { - log.Info("扫描到MES条码:" + codeStr); - // 全局变量赋值,SN码扫描后使用 - // code1Str = codeStr; - RefreshCode1(codeStr); - } - else - { - log.Info("扫描到成品条码:" + codeStr); - // 1.刷新界面条码信息 - // 全局变量赋值,mes条码扫描后使用 - // code2Str = codeStr; - RefreshCode2(codeStr); - - } - #region // 2.创建任务更新数据库条码1 // CodeBindingRecord codeRecord = new CodeBindingRecord(); @@ -520,8 +536,8 @@ namespace Aucma.Core.CodeBinding.ViewModels //}); //log.Info("条码1记录更新"); #endregion - } - + } + private void RefreshCode1(string code1) { @@ -536,7 +552,7 @@ namespace Aucma.Core.CodeBinding.ViewModels DateTime time = System.DateTime.Now; Code2 = code2; Code2Time = time.ToString(); - + } @@ -547,7 +563,7 @@ namespace Aucma.Core.CodeBinding.ViewModels private void RefreshAndWriteLog(string logStr) { TimeSpan currentTime = DateTime.Now.TimeOfDay; - // DateTime time = System.DateTime.Now; + // DateTime time = System.DateTime.Now; string timeString = currentTime.ToString(@"hh\:mm\:ss"); BindingInfo = timeString + ":" + logStr; log.Info(BindingInfo); @@ -558,17 +574,17 @@ namespace Aucma.Core.CodeBinding.ViewModels /// /// MES码 /// SN码 - public void BindingCode(string code1,string code2) + public void BindingCode(string code1, string code2) { try { if (code1.Substring(0, 1) != "B") { - RefreshAndWriteLog("MES码:"+code1+",扫描错误,请重新扫描!"); + RefreshAndWriteLog("MES码:" + code1 + ",扫描错误,请重新扫描!"); SendPlcStop(); return; } - if (code2.Length!=20) + if (code2.Length != 20) { RefreshAndWriteLog("SN码:" + code2 + ",扫描错误,请重新扫描!"); SendPlcStop(); @@ -578,7 +594,7 @@ namespace Aucma.Core.CodeBinding.ViewModels // 1.数据库查询各个工序质检结果,不合格报警 //// 2.查询条码绑定记录表(内胆箱壳绑定处就应该插入记录),绑定SN码 - int Repeatflag = 0; + int Repeatflag = 0; CodeBindingRecord record = _codeBindingRecordServices.FirstAsync(x => x.BoxCode == code1).Result; if (record == null) { @@ -593,7 +609,7 @@ namespace Aucma.Core.CodeBinding.ViewModels } BaseMaterialInfo materialInfo = _baseMaterialInfoServices.FirstAsync(x => x.MaterialCode == code1.Substring(7, 10)).Result; - if(materialInfo == null) + if (materialInfo == null) { record.BoxName = ""; } @@ -608,15 +624,15 @@ namespace Aucma.Core.CodeBinding.ViewModels record.isPlcPass = 1; record.BindingResult = "成功"; bool result = false; - if (Repeatflag==0) + if (Repeatflag == 0) { // 没有记录,新加 - result = _codeBindingRecordServices.AddAsync(record).Result >0? true: false; + result = _codeBindingRecordServices.AddAsync(record).Result > 0 ? true : false; } - else if(Repeatflag==1) + else if (Repeatflag == 1) { result = _codeBindingRecordServices.UpdateAsync(record).Result; } - + if (result) { RefreshAndWriteLog("条码【" + record.BoxCode + "】与SN码【" + record.ProductCode + "】绑定成功"); @@ -658,8 +674,8 @@ namespace Aucma.Core.CodeBinding.ViewModels } finally { - // code1Str = string.Empty; - // code2Str = string.Empty; + // code1Str = string.Empty; + // code2Str = string.Empty; } } @@ -674,7 +690,7 @@ namespace Aucma.Core.CodeBinding.ViewModels { bool result = false; try - { + { var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("条码绑定Plc")); if (obj != null) { @@ -682,10 +698,10 @@ namespace Aucma.Core.CodeBinding.ViewModels // 往plc写入停止信号 obj.plc.WriteInt16("D7102", "1"); // 报警信号 - obj.plc.WriteInt16("D7101","1"); + obj.plc.WriteInt16("D7101", "1"); result = true; } - + } catch (Exception ex) { @@ -729,20 +745,20 @@ namespace Aucma.Core.CodeBinding.ViewModels /// /// /// - private void SendHeart(PlcModel obj,string flag) + private void SendHeart(PlcModel obj, string flag) { try { - // log.Info("心跳D7100写:"+flag); + // log.Info("心跳D7100写:"+flag); // 往plc写入停止信号 obj.plc.WriteInt16("D7100", flag); - // obj.plc.WriteInt32("D7102", 1); + // obj.plc.WriteInt32("D7102", 1); } catch (Exception ex) { log.Error("SendHeart异常,ex:" + ex); } - + } diff --git a/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs b/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs index 83e6b959..1f7bcabf 100644 --- a/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs +++ b/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs @@ -23,6 +23,8 @@ using System.Windows.Threading; using Aucma.Core.CodeBinding.Business; using MvCodeHelper = Aucma.Core.CodeBinding.Business.MvCodeHelper; using Aucma.Core.HwPLc; +using Admin.Core.Socket; +using System.Collections.Generic; namespace Aucma.Core.CodeBinding.ViewModels { @@ -31,11 +33,20 @@ namespace Aucma.Core.CodeBinding.ViewModels private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel)); private IndexPageView firstPage = new IndexPageView();//首页 private StatisticsPageView recordPage = new StatisticsPageView(); + + private static List allScanners = Appsettings.app("ScannerServer").ToList(); + // mes扫码器ip + private static string MesScannerIp = allScanners.First(x => x.Name == "mes扫码器").Ip; + // sn扫码器ip + private static string SnScannerIp = allScanners.First(x => x.Name == "sn扫码器").Ip; + + + /// /// 查询信息更新 /// /// - public delegate Task queryList(object obj); + public delegate Task queryList(object obj); public static event queryList? queryListEvent; public MainWindowViewModel() @@ -48,11 +59,12 @@ namespace Aucma.Core.CodeBinding.ViewModels timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += Timer_Tick; timer.Start(); - - // Job_AllState_Quartz.RefreshStateEvent += RefreshStatus; - MvCodeHelper.RefreshStateEvent += RefreshScanner; - InitHikRobot(); + + // Job_AllState_Quartz.RefreshStateEvent += RefreshStatus; + // MvCodeHelper.RefreshStateEvent += RefreshScanner; + TouchSocketService.RefreshStateEvent += RefreshScanner; + // InitHikRobot(); } #region 开启海康扫码器 @@ -111,11 +123,12 @@ namespace Aucma.Core.CodeBinding.ViewModels public void RefreshScanner(string ip, bool flag) { - if (ip == "SN") + + if (ip == SnScannerIp) { Scanner1State(flag); ; } - else if (ip == "mes") + else if (ip == MesScannerIp) { Scanner2State(flag); } diff --git a/Aucma.Core.CodeBinding/appsettings.json b/Aucma.Core.CodeBinding/appsettings.json index 0a9f8835..6b55e0bf 100644 --- a/Aucma.Core.CodeBinding/appsettings.json +++ b/Aucma.Core.CodeBinding/appsettings.json @@ -194,6 +194,10 @@ }, "ScannerGun": { //扫码枪 "Enabled": true + }, + "TouchSocket": { + "Enabled": true, + "Address": "10.10.92.123:5000" } }, "ScannerServer": [