From ef52fd6fd07508883dce4eb002acda59f0169e59 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期四, 23 四月 2026 10:19:33 +0800
Subject: [PATCH] 优化;

---
 Plugin.TestResult/Scanner.xaml.cs |  208 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 178 insertions(+), 30 deletions(-)

diff --git a/Plugin.TestResult/Scanner.xaml.cs b/Plugin.TestResult/Scanner.xaml.cs
index d8fa36e..48bf1c5 100644
--- a/Plugin.TestResult/Scanner.xaml.cs
+++ b/Plugin.TestResult/Scanner.xaml.cs
@@ -1,9 +1,13 @@
-锘縰sing HMI.Core.Util;
-using HMI.UI.MDI;
+锘縰sing HMI.Core.Manager;
+using HMI.Core.Setting;
+using HMI.Core.Util;
 using HMI.UI.Util;
 using HMI.Util;
 using Panuon.WPF.UI;
+using Plugin.TestResult.Lixun;
 using Plugin.TestResult.Model;
+using Plugin.TestResult.Util;
+using Plugin.TestResult_WPF.Model;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
@@ -18,16 +22,14 @@
         public string name { get; private set; }
         public string positionCode { get; private set; }
         public string snrCode { get; private set; }
-        public string oldPosition { get; private set; }
-        public int oldIndex { get; private set; }
+        public CellInfo lastInfo { get; private set; }
 
-        public BarcodeEventArg(string name, string positionCode, string snrCode, string oldPosition, int oldIndex)
+        public BarcodeEventArg(string name, string positionCode, string snrCode, CellInfo lastInfo)
         {
             this.name = name;
             this.positionCode = positionCode;
             this.snrCode = snrCode;
-            this.oldPosition = oldPosition;
-            this.oldIndex = oldIndex;
+            this.lastInfo = lastInfo;
         }
     }
 
@@ -47,14 +49,21 @@
         public event EventHandler<BarcodeEventArg> FinishBarcode;
         public event EventHandler<RoutedEventArgs> ComplateClick;
 
-        private static Dictionary<string, string> snrCache = new Dictionary<string, string>();
-        private static Dictionary<string, int> snrIndex = new Dictionary<string, int>();
-
-        private DataTable dataTable;
+        private static Dictionary<string, CellInfo> snrCache = new Dictionary<string, CellInfo>();
         SerialPort serialPort;
-        public Scanner(string title)
+
+        private string model;
+        private int columns;
+        private int rows;
+        private int ovenIndex;
+        private CellInfo targetCellInfo;
+        public Scanner(int index, string title, string model, int columns, int rows)
         {
+            this.ovenIndex = index;
             this.title = title;
+            this.model = model;
+            this.columns = columns;
+            this.rows = rows;
             this.DataContext = this;
             InitializeComponent();
             Loaded += Scanner_Loaded;
@@ -62,8 +71,16 @@
 
         private void Scanner_Loaded(object sender, RoutedEventArgs e)
         {
-            InitScanner();
-            Loaded -= Scanner_Loaded;
+            try
+            {
+                InitScanner();
+                Loaded -= Scanner_Loaded;
+            }
+            catch (Exception ex)
+            {
+                ViewControl.Logger.Error("Open Scanner Window Error.", ex);
+                MsgBoxHelper.Error(this, $"{ex.Message}");
+            }
         }
 
         /// <summary>
@@ -127,7 +144,7 @@
             try
             {
                 SerialPort sp = (SerialPort)sender;
-                string barcode = ReadDataFrom(sp);
+                string barcode = ReadDataFrom(sp).Trim();
 
                 //string barcode = 
                 Dispatcher.Invoke(() =>
@@ -139,10 +156,24 @@
                     }
                     else
                     {
-                        positionText.Text = barcode;
-                        //positionText.Focus();
-                        //snrText.Text = "";
-                        snrText.Focus();
+                        try
+                        {
+                            this.targetCellInfo = ScannerParser.GetCellInfoByCode(this.model, this.columns, this.rows, barcode);
+                            if (!TestResultView.nameTables.ContainsKey(targetCellInfo.OvenName))
+                            {
+                                MsgBoxHelper2.Error($"鑰佸寲鐐� [{targetCellInfo.OvenName}] 涓嶅瓨鍦紝璇锋鏌ラ厤缃�");
+                                return;
+                            }
+                            positionText.Text = barcode;
+                            //positionText.Focus();
+                            //snrText.Text = "";
+                            snrText.Focus();
+                        }
+                        catch (Exception ex)
+                        {
+                            ViewControl.Logger.Error("Parse Position Code Error.", ex);
+                            MsgBoxHelper2.Error($"绌翠綅鍙� [{barcode}] 瑙f瀽澶辫触: {ex.Message}");
+                        }
                     }
                 });
             }
@@ -153,31 +184,28 @@
             }
         }
 
-        public static void InsertNewCode(string snr, string position, int index)
+        public static void InsertNewCode(string snr, CellInfo info)
         {
-            snrCache[snr] = position;
-            snrIndex[snr] = index;
+            snrCache[snr] = info;
         }
 
         public static void RemoveSNR(string snr)
         {
             snrCache.Remove(snr);
-            snrIndex.Remove(snr);
         }
 
         private void SendCode(string tag, string position, string snr)
         {
             if (snrCache.ContainsKey(snr))
             {
-                var oldPosition = snrCache[snr];
+                string oldPosition = snrCache[snr].OriginCode;
                 if (MsgBoxHelper2.WarnChoose($"鏉$爜[{snr}]宸叉斁缃簬[{oldPosition}]涓紝鏄惁娓呴櫎锛�") == MessageBoxResult.OK)
                 {
                     try
                     {
-                        FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, null, oldPosition, snrIndex[snr]));
+                        FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, null, snrCache[snr]));
                         snrCache.Remove(snr);
-                        snrIndex.Remove(snr);
-                        FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null, -1));
+                        FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null));
                     }
                     catch (Exception ex)
                     {
@@ -187,7 +215,7 @@
             }
             else
             {
-                FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null, -1));
+                FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null));
             }
         }
 
@@ -196,6 +224,91 @@
             Dispatcher.Invoke(() =>
             {
                 string position = positionText.Text, snr = snrText.Text;
+                try
+                {
+                    var EnableMes = ParameterManager.GetCommonParameterByName<bool>("EnableMes", false);
+                    if (ParamSetting.CustomerCode == "LIXUN" && EnableMes)
+                    {
+                        var table = TestResultView.tables[this.ovenIndex];
+                        string currentPn = table.CbProduct.Text;
+
+                        /// 妫�鏌ユ牸寮�
+                        if (!SNValidator.ValidateSN(currentPn, snr, out string msg))
+                        {
+                            TestResultView.tables[this.ovenIndex].GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"妫�鏌ヤ骇鍝佹潯鐮佸紓甯革細{msg}");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+
+                        /// 鑾峰彇绔欏悕
+                        string stationName = ProductManager.GetParameterByProductAndName(currentPn, "MES绔欎綅")?.Value;
+                        if (string.IsNullOrEmpty(stationName))
+                        {
+                            TestResultView.tables[this.ovenIndex].GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"妫�鏌ヤ骇鍝佹潯鐮佸紓甯革紝褰撳墠浜у搧[{currentPn}]鏈缃弬鏁癕ES绔欎綅");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+
+                        /// 妫�鏌N
+                        if (PNUtil.GetPN(ParamSetting.MesAddress, stationName, snr, out string pn, out msg))
+                        {
+                            
+                            if (pn != currentPn)
+                            {
+                                TestResultView.tables[this.ovenIndex].GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                                MsgBoxHelper2.Error($"褰撳墠娴嬭瘯鏂欏彿涓� [{currentPn}]锛屾壂鐮佷骇鍝佹枡鍙蜂负 [{pn}] 涓嶅厑璁告祴璇�");
+                                snrText.Text = string.Empty;
+                                snrText.Focus();
+                                return;
+                            }
+                        }
+                        else
+                        {
+                            TestResultView.tables[this.ovenIndex].GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"妫�鏌ヤ骇鍝佹潯鐮佸紓甯革細{msg}");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+
+                        /// 妫�鏌ュ伐搴�
+                        if (PNUtil.CheckIn(ViewControl.Logger, ParamSetting.MesAddress, stationName, snr, out bool needCheckIn, out msg))
+                        {
+                            if (needCheckIn != true)
+                            {
+                                TestResultView.tables[this.ovenIndex].GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                                MsgBoxHelper2.Error($"鏉$爜[{snr}]宸ュ簭涓嶅湪鏈珯锛歿msg}");
+                                snrText.Text = string.Empty;
+                                snrText.Focus();
+                                return;
+                            }
+                        }
+                        else
+                        {
+                            TestResultView.tables[this.ovenIndex].GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"鏉$爜[{snr}]CheckIn澶辫触锛歿msg}");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+                    }
+                    //var _ = ScannerParser.GetCellInfoByCode(this.model, this.columns, this.rows, snr);
+                    //ToastHelper.Warning(this, "浜у搧鐮佹牸寮忓紓甯�");
+                    //snrText.Text = string.Empty;
+                    //snrText.Focus();
+                    //return;
+                }
+                catch (Exception ex)
+                {
+                    ViewControl.Logger.Error("Scanner Check Error.", ex);
+                    MsgBoxHelper2.Error($"鎵爜妫�鏌ュ紓甯革細{ex.Message}");
+                    return;
+                }
+
                 if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
                 {
                     ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
@@ -264,7 +377,8 @@
         {
             try
             {
-                serialPort.Close();
+                targetCellInfo = null;
+                serialPort.Dispose();
                 serialPort = null;
             }
             catch (Exception ex)
@@ -285,7 +399,41 @@
                     }
                     else
                     {
-                        snrText.Focus();
+                        if (ParamSetting.CustomerCode != "LIXUN")
+                        {
+                            snrText.Focus();
+                            return;
+                        }
+                        string barcode = positionText.Text;
+                        try
+                        {
+                            targetCellInfo = ScannerParser.GetCellInfoByCode(this.model, this.columns, this.rows, barcode);
+                            if (!TestResultView.nameTables.ContainsKey(targetCellInfo.OvenName))
+                            {
+                                MsgBoxHelper2.Error($"鑰佸寲鐐� [{targetCellInfo.OvenName}] 涓嶅瓨鍦紝璇锋鏌ラ厤缃�");
+                                positionText.Text = string.Empty;
+                                positionText.Focus();
+                                targetCellInfo = null;
+                                return;
+                            }
+                            var oven = TestResultView.nameTables[targetCellInfo.OvenName];
+                            if (oven.cells.Length < targetCellInfo.Index + 1)
+                            {
+                                MsgBoxHelper2.Error($"绌翠綅涓嬫爣 [{targetCellInfo.Index}] 瓒呭嚭绱㈠紩");
+                                positionText.Text = string.Empty;
+                                positionText.Focus();
+                                targetCellInfo = null;
+                                return;
+                            }
+                            snrText.Focus();
+                        }
+                        catch (Exception ex)
+                        {
+                            positionText.Text = string.Empty;
+                            positionText.Focus();
+                            ViewControl.Logger.Error("Parse Position Code Error.", ex);
+                            MsgBoxHelper2.Error($"绌翠綅鍙� [{barcode}] 瑙f瀽澶辫触: {ex.Message}");
+                        }
                     }
                 });
             }

--
Gitblit v1.9.1