From 2e755b98a9373d1d59fa2052e9705edab405b331 Mon Sep 17 00:00:00 2001 From: CaesarBao Date: Thu, 16 Apr 2026 10:40:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Emes=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E8=B0=83=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Common/StringChange.cs | 19 +++- SlnMesnac.Extensions/RfidFactorySetup.cs | 1 + SlnMesnac.Model/domain/MesOrderInfo.cs | 17 +++- SlnMesnac.Rfid/MeshttpClient.cs | 66 ++++++++++--- SlnMesnac.Rfid/TouchSocketSetup.cs | 1 + SlnMesnac.WPF/App.xaml.cs | 6 +- SlnMesnac.WPF/MainWindow.xaml | 2 +- SlnMesnac.WPF/Page/IndexPage/ChangeType.xaml | 20 ++-- .../Page/IndexPage/HistorySearch.xaml | 10 +- .../IndexPage/ProductionLineUserControl.xaml | 28 +++--- .../IndexPage/ChangeTypeViewModel.cs | 99 +++++++++++-------- .../IndexPage/ProductionLineViewModel.cs | 2 + SlnMesnac.WPF/appsettings.json | 3 +- 13 files changed, 183 insertions(+), 91 deletions(-) diff --git a/SlnMesnac.Common/StringChange.cs b/SlnMesnac.Common/StringChange.cs index b9cb9ce..e1910d5 100644 --- a/SlnMesnac.Common/StringChange.cs +++ b/SlnMesnac.Common/StringChange.cs @@ -1,4 +1,5 @@ using Nancy.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Drawing; @@ -6,7 +7,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; - +using JsonSerializer = System.Text.Json.JsonSerializer; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- @@ -320,5 +321,21 @@ namespace SlnMesnac.Common _numberToRegion.TryGetValue(regionNum, out var region); return region; } + + public T JTokenToEntity(JToken value) where T : class + { + if (value == null) + { + return null; + } + string json = value.ToString(); + if (string.IsNullOrEmpty(json)) + { + return null; + } + T ResponseEntity; + ResponseEntity = JsonSerializer.Deserialize(json); + return ResponseEntity; + } } } \ No newline at end of file diff --git a/SlnMesnac.Extensions/RfidFactorySetup.cs b/SlnMesnac.Extensions/RfidFactorySetup.cs index 11b2d5d..42f3ed4 100644 --- a/SlnMesnac.Extensions/RfidFactorySetup.cs +++ b/SlnMesnac.Extensions/RfidFactorySetup.cs @@ -93,6 +93,7 @@ namespace SlnMesnac.Extensions _rfid.ip = IP; _rfid.port = int.Parse(Port); _rfid.ConfigKey = item.Deviceid; + _rfid.FilterData = item.Name; //_rfid.FilterData = sensor_Infos.Find(x => x.Deviceid == item.Deviceid).Mapid; //bool connectResult = _rfid.Connect(IP, int.Parse(Port)); //_rfid._Action += RecvIdentifyData_Instance; diff --git a/SlnMesnac.Model/domain/MesOrderInfo.cs b/SlnMesnac.Model/domain/MesOrderInfo.cs index 1e4b41e..5531c2f 100644 --- a/SlnMesnac.Model/domain/MesOrderInfo.cs +++ b/SlnMesnac.Model/domain/MesOrderInfo.cs @@ -1,18 +1,29 @@ using System; using System.Collections.Generic; using System.Text; +using System.Text.Json.Serialization; namespace SlnMesnac.Model.domain { + public class MesApiResponse + { + public int Code { get; set; } + public string Msg { get; set; } + public List Data { get; set; } + } public class MesOrderInfo { - public MesOrderInfo() { } - + [JsonPropertyName("ProductSpec")] public string ProductSpec { get; set; } + [JsonPropertyName("OrderNo")] public string OrderNo { get; set; } + [JsonPropertyName("ProductCode")] public string ProductCode { get; set; } + [JsonPropertyName("ProductType")] public string ProductType { get; set; } + [JsonPropertyName("ProductName")] public string ProductName { get; set; } - public decimal PlanQty { get; set; } + [JsonPropertyName("PlanQty")] + public double PlanQty { get; set; } } } diff --git a/SlnMesnac.Rfid/MeshttpClient.cs b/SlnMesnac.Rfid/MeshttpClient.cs index 974f021..60df340 100644 --- a/SlnMesnac.Rfid/MeshttpClient.cs +++ b/SlnMesnac.Rfid/MeshttpClient.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Nancy; +using Nancy.Json; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Serilog; @@ -31,6 +32,7 @@ namespace SlnMesnac.TouchSocket private readonly SerilogHelper _logger; public static Action RefreshStateEvent; public static Action _RefreshLogMessageAction; + private string m_IpHost = ""; public MeshttpClient(AppConfig appConfig, SerilogHelper logger) { _appConfig = appConfig; @@ -43,21 +45,22 @@ namespace SlnMesnac.TouchSocket { try { + m_IpHost = IpHost; MESHttpClient = new WebApiClient(); await MESHttpClient.SetupAsync(new TouchSocketConfig() .SetRemoteIPHost(IpHost) .ConfigurePlugins(a => { })); - _logger.Info("正在连接:" + IpHost); + Console.WriteLine("正在连接:" + IpHost); await MESHttpClient.ConnectAsync(); Console.WriteLine("连接成功"); - _logger.Info(IpHost + "连接成功"); + Console.WriteLine(IpHost + "连接成功"); return MESHttpClient; } catch (Exception ex) { - _logger.Info("MES API连接ERROR: " + ex.Message); + Console.WriteLine("MES API连接ERROR: " + ex.Message); return null; } } @@ -86,32 +89,71 @@ namespace SlnMesnac.TouchSocket } - public async Task> GetMesOrderInfo(string lineCode, string datetime) + public async Task GetMesOrderInfo(string lineCode, string datetime) { + RFIDDeviceApiResponse rFIDDeviceApiResponse = new RFIDDeviceApiResponse(); try { var invokeOption_30s = new InvokeOption(30 * 1000) { FeedbackType = FeedbackType.WaitInvoke }; - if (MESHttpClient == null) + if (MESHttpClient != null) { - return null; + if (!MESHttpClient.Online) + { + Console.WriteLine("与MES客户端连接断开,尝试重新连接..."); + await MESHttpClient.SetupAsync(new TouchSocketConfig() + .SetRemoteIPHost(m_IpHost) + ); + Console.WriteLine("正在连接:" + m_IpHost); + await MESHttpClient.ConnectAsync(); + Console.WriteLine("连接成功"); + Console.WriteLine(m_IpHost + "连接成功"); + } } + var request = new WebApiRequest(); request.Method = HttpMethodType.Get; request.Querys = new KeyValuePair[] { new KeyValuePair("lineCode", lineCode), new KeyValuePair("planDate", datetime) }; - - var responseValue = await MESHttpClient.InvokeTAsync("/api/plan/get", invokeOption_30s, request); - + string responseValue = ""; + try + { + //responseValue = await MESHttpClient.InvokeTAsync("/ApiServer/get", invokeOption_30s, request); + responseValue = await MESHttpClient.InvokeTAsync("/api/plan/get", invokeOption_30s, request); + if (responseValue != null) + { + Console.WriteLine("MES接口返回:" + JsonSerializer.Serialize(responseValue)); + // _logger.Info("MES接口返回:" + JsonSerializer.Serialize(responseValue)); + rFIDDeviceApiResponse = JsonSerializer.Deserialize>(responseValue); + Console.WriteLine("MES接口返回成功"); + return responseValue; + } + else + { + rFIDDeviceApiResponse.Code = 201; + return "201"; + } + } + catch (Exception ex) + { + rFIDDeviceApiResponse.Code = 202; + Console.WriteLine(ex.ToString()); + _logger.Error(ex.Message); + return "202"; + } + + + //var responseValue = await MESHttpClient.InvokeTAsync("/ApiServer/get", invokeOption_30s, request); //JToken responseValue = await MESHttpClient.InvokeTAsync("Get:/api/plan", null, requestValue); - return JTokenToEntity>(responseValue); + } catch (Exception ex) { - _logger.Info("ERROR: " + ex.Message); - return new ApiResponse(); + rFIDDeviceApiResponse.Code = 203; + Console.WriteLine("ERROR: " + ex.Message); + return "203"; } } diff --git a/SlnMesnac.Rfid/TouchSocketSetup.cs b/SlnMesnac.Rfid/TouchSocketSetup.cs index ee56f94..7fc3ce7 100644 --- a/SlnMesnac.Rfid/TouchSocketSetup.cs +++ b/SlnMesnac.Rfid/TouchSocketSetup.cs @@ -47,6 +47,7 @@ namespace SlnMesnac.TouchSocket var _httpclient = service.GetService(); //_server.Init(6001); await _httpclient.CreateWebApiClientAsync("10.20.8.51:8005"); + //await _httpclient.CreateWebApiClientAsync("127.0.0.1:9881"); //_httpclient.CreateWebApiClient("127.0.0.1:9880"); } diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index 93912e5..edbebc5 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -434,11 +434,9 @@ namespace SlnMesnac.WPF private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { - string errorMsg = $"[实例 {_instanceId}] 发生未处理异常: {e.Exception.Message}"; - MessageBox.Show(errorMsg, "应用程序错误", MessageBoxButton.OK, MessageBoxImage.Error); - + string errorMsg = $"[实例 {_instanceId}] 发生未处理异常: {e.Exception.Message}, {e.Exception.StackTrace}"; Log.Error(e.Exception, $"实例 {_instanceId} 全局异常: {e.Exception.Message}"); - + MessageBox.Show(errorMsg, "应用程序错误", MessageBoxButton.OK, MessageBoxImage.Error); // 保存异常信息到实例日志目录 try { diff --git a/SlnMesnac.WPF/MainWindow.xaml b/SlnMesnac.WPF/MainWindow.xaml index 99ef4a9..b0e89c3 100644 --- a/SlnMesnac.WPF/MainWindow.xaml +++ b/SlnMesnac.WPF/MainWindow.xaml @@ -54,7 +54,7 @@