Преглед изворни кода

增加相邻键数据判断功能

zly пре 5 месеци
родитељ
комит
e2965806e0

+ 284 - 53
TeamAAS-VM/Core/Management.cs

@@ -1,5 +1,6 @@
 using Cognex.VisionPro;
 using CSScripting;
+using NPOI.SS.Formula.Eval;
 using NPOI.Util;
 
 //using ICSharpCode.SharpZipLib.Zip;
@@ -7,6 +8,7 @@ using Prism.Events;
 using Prism.Ioc;
 using Prism.Mvvm;
 using Prism.Regions;
+using SqlSugar.SplitTableExtensions;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
@@ -78,6 +80,7 @@ namespace TeamAAS_VP.Core
         /// </summary>
         private bool _hasCompressedToday = false;
 
+
         #endregion
 
         #region 属性
@@ -95,7 +98,10 @@ namespace TeamAAS_VP.Core
         private string ScanValue = "";
         // 扫码完成事件
         private readonly ManualResetEventSlim _scanDoneEvent = new ManualResetEventSlim(false);
-
+        //键盘数据
+        public Dictionary<string, double> keyData;
+        //键盘差值
+        public bool isValueOk;
 
         private System.Windows.Media.Brush _StatusBackground;
 
@@ -264,6 +270,8 @@ namespace TeamAAS_VP.Core
             get { return _ProductList; }
             set { SetProperty(ref _ProductList, value); }
         }
+
+  
         #endregion
 
         #region 命令
@@ -2672,6 +2680,16 @@ namespace TeamAAS_VP.Core
                                     LogHelper.WriteLogMes($"【mes开始ADD】");
                                     SendTaskMessage($"mes开始ADD", MessageLevel.Info);
 
+                                    StringBuilder submitDataError = new StringBuilder();
+                                    foreach (var item in lockResultsAll)
+                                    {
+                                        if (item.PhotoPassed == "NG")
+                                        {
+                                            submitDataError.Append(item.PhotoName + "_Fail,");
+                                        }
+                                    }
+
+
                                     StringBuilder submitData = new StringBuilder();
                                     StringBuilder submitData2 = new StringBuilder();
                                     StringBuilder submitData3 = new StringBuilder();
@@ -2708,30 +2726,40 @@ namespace TeamAAS_VP.Core
 
                                     }
                                     bool allSuccess = true;
-                                    ShowMes_Result = resultIndex == 1 ? true : false;
+                                    bool ok = CheckKeyBoardValue(keyData, out string msg);
+                                    if (ok && resultIndex == 1 ? true : false && isValueOk)
+                                    {
+                                        ShowMes_Result = true;
+                                    }
+                                    else
+                                    {
+                                        ShowMes_Result = false;
+                                    }
+                                    // ShowMes_Result = resultIndex == 1 ? true : false;
+                                    
                                     //(bool isSuccess, string response) = await _mesService.SubmitGetAsync(ProductMainSN, "", submitData.ToString(), resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
                                     if (submitData.Length > 0)
                                     {
                                         string data1 = submitData.ToString().TrimEnd('&');
-                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(0,ProductMainSN, "", submitData.ToString(), resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
+                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(0,ProductMainSN, "", submitData.ToString(), ShowMes_Result, submitDataError.ToString(), _LastMesCheckTime[i], serverTimeResult.Now);
                                         allSuccess &= isSuccess;
                                     }
                                     if (submitData2.Length > 0)
                                     {
                                         string data2 = submitData2.ToString().TrimEnd('&');
-                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(1,ProductMainSN, "", submitData2.ToString(), resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
+                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(1, ProductMainSN, "", submitData2.ToString(), ShowMes_Result, submitDataError.ToString(), _LastMesCheckTime[i], serverTimeResult.Now);
                                         allSuccess &= isSuccess;
                                     }
                                     if (submitData3.Length > 0)
                                     {
                                         string data3 = submitData3.ToString().TrimEnd('&');
-                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(2,ProductMainSN, "", submitData3.ToString(), resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
+                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(2, ProductMainSN, "", submitData3.ToString(), ShowMes_Result   , submitDataError.ToString(), _LastMesCheckTime[i], serverTimeResult.Now);
                                         allSuccess &= isSuccess;
                                     }
                                     if (submitData4.Length > 0)
                                     {
                                         string data4 = submitData4.ToString().TrimEnd('&');
-                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(3,ProductMainSN, "", submitData4.ToString(), resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
+                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(3, ProductMainSN, "", submitData4.ToString(), ShowMes_Result, submitDataError.ToString(), _LastMesCheckTime[i], serverTimeResult.Now);
                                         allSuccess &= isSuccess;
                                     }
 
@@ -3165,15 +3193,92 @@ namespace TeamAAS_VP.Core
                                 //    }
 
                                 //}
-
-                                string[] strpoints1 = visionResult.Items[i].Split(',');
-                                string[] body1 = strpoints1[1].Split(";");
-                                if (body1.Length > 1)
+                                try
                                 {
-                                    for (int j = 1; j <= body1.Length; j++)
+                                    string[] strpoints1 = visionResult.Items[i].Split(',');
+                                    string[] body1 = strpoints1[1].Split(";");
+
+                                    string keyName = strpoints1[0].Split('-').Last().Trim();
+                                    List<double> list = new List<double>();
+
+                                    foreach (string v in body1)
+                                    {
+                                        if (double.TryParse(v.Trim(), out double num))
+                                        {
+                                            if (num == 999)
+                                            {
+                                                continue;
+                                            }
+                                            list.Add(num);
+                                        }
+                                    }
+                                    if (list.Count == 0)
+                                    {
+                                        continue;
+                                    }
+
+                                    double max = list.Max();
+                                    double min = list.Min();
+
+                                    if (max - min <= 0.3)
+                                    {
+                                        isValueOk = true;
+                                    }
+                                    else
+                                    {
+                                        SendTaskMessage($"键盘格{keyName}最大值{max}与最小值{min}相差大于0.3!",MessageLevel.Error);
+                                    }
+                                    double average = list.Average();
+                                    if (!keyData.ContainsKey(keyName))
+                                        keyData.Add(keyName, average);
+
+                                    if (body1.Length > 1)
+                                    {
+                                        for (int j = 1; j <= body1.Length; j++)
+                                        {
+                                            lockResult1.PhotoName = strpoints1[0] + "_" + j.ToString();
+                                            lockResult1.ResultData = body1[j - 1];
+                                            lockResult1.PhotoCode = ProductMainSN;
+                                            var globalParam = currentProduct.GlobalParamListCollection.FirstOrDefault(p => p.Name == strpoints1[0]);
+                                            if (globalParam != null)
+                                            {
+                                                lockResult1.UpLimit = globalParam.Param1;
+                                                lockResult1.DownLimit = globalParam.Param2;
+                                                lockResult1.MidLimit = globalParam.Param5;
+                                                string ResData = body1[j - 1];
+                                                if (ResData.Contains("NA"))
+                                                {
+                                                    ResData = "-1000";
+                                                }
+
+                                                bool resultflog = true;
+                                                if (Convert.ToDouble(ResData) >= Convert.ToDouble(globalParam.Param2) && Convert.ToDouble(ResData) <= Convert.ToDouble(globalParam.Param1))
+                                                {
+
+                                                }
+                                                else
+                                                {
+                                                    resultflog = false;
+                                                }
+                                                if (resultflog)
+                                                {
+                                                    lockResult1.PhotoPassed = "OK";
+                                                    results.Add(true);
+                                                }
+                                                else
+                                                {
+                                                    lockResult1.PhotoPassed = "NG";
+                                                    results.Add(false);
+                                                }
+
+                                            }
+                                            lockResultsAll.Add(lockResult1.Copy());
+                                        }
+                                    }
+                                    else
                                     {
-                                        lockResult1.PhotoName = strpoints1[0] + "_" + j.ToString();
-                                        lockResult1.ResultData = body1[j - 1];
+                                        lockResult1.PhotoName = strpoints1[0];
+                                        lockResult1.ResultData = strpoints1[1];
                                         lockResult1.PhotoCode = ProductMainSN;
                                         var globalParam = currentProduct.GlobalParamListCollection.FirstOrDefault(p => p.Name == strpoints1[0]);
                                         if (globalParam != null)
@@ -3181,7 +3286,7 @@ namespace TeamAAS_VP.Core
                                             lockResult1.UpLimit = globalParam.Param1;
                                             lockResult1.DownLimit = globalParam.Param2;
                                             lockResult1.MidLimit = globalParam.Param5;
-                                            string ResData = body1[j - 1];
+                                            string ResData = strpoints1[1];
                                             if (ResData.Contains("NA"))
                                             {
                                                 ResData = "-1000";
@@ -3206,51 +3311,15 @@ namespace TeamAAS_VP.Core
                                                 lockResult1.PhotoPassed = "NG";
                                                 results.Add(false);
                                             }
-
                                         }
-                                        lockResultsAll.Add(lockResult1.Copy());
+                                        lockResultsAll.Add(lockResult1);
                                     }
                                 }
-                                else
+                                catch (Exception ex) 
                                 {
-                                    lockResult1.PhotoName = strpoints1[0];
-                                    lockResult1.ResultData = strpoints1[1];
-                                    lockResult1.PhotoCode = ProductMainSN;
-                                    var globalParam = currentProduct.GlobalParamListCollection.FirstOrDefault(p => p.Name == strpoints1[0]);
-                                    if (globalParam != null)
-                                    {
-                                        lockResult1.UpLimit = globalParam.Param1;
-                                        lockResult1.DownLimit = globalParam.Param2;
-                                        lockResult1.MidLimit = globalParam.Param5;
-                                        string ResData = strpoints1[1];
-                                        if (ResData.Contains("NA"))
-                                        {
-                                            ResData = "-1000";
-                                        }
+                                    SendTaskMessage($"拆分视觉结果出错:{ex}",MessageLevel.Error);
 
-                                        bool resultflog = true;
-                                        if (Convert.ToDouble(ResData) >= Convert.ToDouble(globalParam.Param2) && Convert.ToDouble(ResData) <= Convert.ToDouble(globalParam.Param1))
-                                        {
-
-                                        }
-                                        else
-                                        {
-                                            resultflog = false;
-                                        }
-                                        if (resultflog)
-                                        {
-                                            lockResult1.PhotoPassed = "OK";
-                                            results.Add(true);
-                                        }
-                                        else
-                                        {
-                                            lockResult1.PhotoPassed = "NG";
-                                            results.Add(false);
-                                        }
-                                    }
-                                    lockResultsAll.Add(lockResult1);
                                 }
-
                                 App.Current.Dispatcher.Invoke(() =>
                                 {
                                     _eventAggregator.GetEvent<LockFinishNotification>().Publish(lockResult1);
@@ -3558,6 +3627,168 @@ namespace TeamAAS_VP.Core
             }
             concurrenty = true;
         }
+
+        /// <summary>
+        /// 校验键盘所有按键值,相邻差值不能大于 0.3
+        /// </summary>
+        public bool CheckKeyBoardValue(Dictionary<string, double> keyDic, out string errorMsg)
+        {
+            errorMsg = "";
+            try
+            {
+                // 2. 获取每个按键的【真实相邻按键】
+                Dictionary<string, List<string>> neighborMap = GetKeyboardNeighborMap();
+
+                // 3. 开始校验:每个按键 和 相邻按键 差值 ≤0.3
+                foreach (var item in keyDic)
+                {
+                    string key = item.Key.ToUpper();
+                    double val = item.Value;
+
+                    // 没有相邻关系就跳过
+                    if (!neighborMap.ContainsKey(key))
+                        continue;
+
+                    // 遍历所有相邻按键
+                    foreach (string neighbor in neighborMap[key])
+                    {
+                        string n = neighbor.ToUpper();
+                        if (!keyDic.ContainsKey(n))
+                            continue;
+
+                        double nVal = keyDic[n];
+                        double diff = Math.Abs(val - nVal);
+
+                        // 超过 0.3 直接报错
+                        if (diff > 0.3)
+                        {
+                            SendTaskMessage($"按键 {key}({val}) 与相邻按键 {n}({nVal}) 差值 {diff:F3} > 0.3,校验不通过", MessageLevel.Error);
+                            return false;
+                        }
+                    }
+                }
+                SendTaskMessage("所有键盘按键相邻差值校验通过", MessageLevel.Debug);
+                return true;
+            }
+            catch (Exception ex)
+            {
+                errorMsg = "校验异常:" + ex.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 键盘相邻关系
+        /// </summary>
+        private Dictionary<string, List<string>> GetKeyboardNeighborMap()
+        {
+            return new Dictionary<string, List<string>>
+            {
+                // ==============================================
+                // 第一行:ESC + F1~F12
+                // ==============================================
+                ["ESC"] = new List<string> { "F1", "`", "1" },
+                ["F1"] = new List<string> { "ESC", "F2", "`", "1", "2" },
+                ["F2"] = new List<string> { "F1", "F3", "1", "2", "3" },
+                ["F3"] = new List<string> { "F2", "F4", "2", "3", "4" },
+                ["F4"] = new List<string> { "F3", "F5", "3", "4", "5" },
+                ["F5"] = new List<string> { "F4", "F6", "4", "5", "6" },
+                ["F6"] = new List<string> { "F5", "F7", "5", "6", "7" },
+                ["F7"] = new List<string> { "F6", "F8", "6", "7", "8" },
+                ["F8"] = new List<string> { "F7", "F9", "7", "8", "9" },
+                ["F9"] = new List<string> { "F8", "F10", "8", "9", "0" },
+                ["F10"] = new List<string> { "F9", "F11", "9", "0", "-" },
+                ["F11"] = new List<string> { "F10", "F12", "0", "-", "=" },
+                ["F12"] = new List<string> { "F11", "-", "=", "Backspace" },
+
+                // ==============================================
+                // 第二行:` 1 2 3 4 5 6 7 8 9 0 - = Backspace
+                // ==============================================
+                ["`"] = new List<string> { "ESC", "F1", "1", "TAB", "Q" },
+                ["1"] = new List<string> { "ESC", "F1", "F2", "`", "2", "TAB", "Q" },
+                ["2"] = new List<string> { "F1", "F2", "F3", "1", "3", "Q", "W" },
+                ["3"] = new List<string> { "F2", "F3", "F4", "2", "4", "W", "E" },
+                ["4"] = new List<string> { "F3", "F4", "F5", "3", "5", "E", "R" },
+                ["5"] = new List<string> { "F4", "F5", "F6", "4", "6", "R", "T" },
+                ["6"] = new List<string> { "F5", "F6", "F7", "5", "7", "T", "Y" },
+                ["7"] = new List<string> { "F6", "F7", "F8", "6", "8", "Y", "U" },
+                ["8"] = new List<string> { "F7", "F8", "F9", "7", "9", "U", "I" },
+                ["9"] = new List<string> { "F8", "F9", "F10", "8", "0", "I", "O" },
+                ["0"] = new List<string> { "F9", "F10", "F11", "9", "-", "O", "P" },
+                ["-"] = new List<string> { "F10", "F11", "F12", "0", "=", "P", "[" },
+                ["="] = new List<string> { "F11", "F12", "-", "Backspace", "[", "]" },
+                ["Backspace"] = new List<string> { "F12", "=", "]", "\\" },
+
+                // ==============================================
+                // 第三行:TAB Q W E R T Y U I O P [ ] \
+                // ==============================================
+                ["TAB"] = new List<string> { "`", "1", "Q", "CapsLock" },
+                ["Q"] = new List<string> { "TAB", "`", "1", "2", "W", "A", "CapsLock" },
+                ["W"] = new List<string> { "Q", "1", "2", "3", "E", "A", "S" },
+                ["E"] = new List<string> { "W", "2", "3", "4", "R", "S", "D" },
+                ["R"] = new List<string> { "E", "3", "4", "5", "T", "D", "F" },
+                ["T"] = new List<string> { "R", "4", "5", "6", "Y", "F", "G" },
+                ["Y"] = new List<string> { "T", "5", "6", "7", "U", "G", "H" },
+                ["U"] = new List<string> { "Y", "6", "7", "8", "I", "H", "J" },
+                ["I"] = new List<string> { "U", "7", "8", "9", "O", "J", "K" },
+                ["O"] = new List<string> { "I", "8", "9", "0", "P", "K", "L" },
+                ["P"] = new List<string> { "O", "9", "0", "-", "[", "L", ";" }, 
+                ["["] = new List<string> { "P", "0", "-", "=", "]", ";", "'" },
+                ["]"] = new List<string> { "[", "-", "=", "Backspace", "\\", "'", "Enter" },
+                ["\\"] = new List<string> { "]", "=", "Backspace", "Enter" },
+
+                // ==============================================
+                // 第四行:CapsLock A S D F G H J K L ; ' Enter
+                // ==============================================
+                ["CapsLock"] = new List<string> { "TAB", "Q", "A", "LeftShift" },
+                ["A"] = new List<string> { "CapsLock", "Q", "W", "S", "LeftShift", "Z" },
+                ["S"] = new List<string> { "A", "W", "E", "D", "Z", "X", "LeftShift" },
+                ["D"] = new List<string> { "S", "E", "R", "F", "X", "C", "LeftShift" },
+                ["F"] = new List<string> { "D", "R", "T", "G", "C", "V" },
+                ["G"] = new List<string> { "F", "T", "Y", "H", "V", "B" }, 
+                ["H"] = new List<string> { "G", "Y", "U", "J", "B", "N" },
+                ["J"] = new List<string> { "H", "U", "I", "K", "N", "M" },
+                ["K"] = new List<string> { "J", "I", "O", "L", "M", "," },
+                ["L"] = new List<string> { "K", "O", "P", ";", ",", ".", "/" },
+                [";"] = new List<string> { "L", "P", "[", "'", ".", "/" },
+                ["'"] = new List<string> { ";", "[", "]", "Enter", "/" },
+                ["Enter"] = new List<string> { "'", "]", "\\", "RightShift" },
+
+                // ==============================================
+                // 第五行:LeftShift Z X C V B N M , . / RightShift
+                // ==============================================
+                ["LeftShift"] = new List<string> { "CapsLock", "A", "S", "D", "Z", "Ctrl", "Fn" },
+                ["Z"] = new List<string> { "LeftShift", "A", "S", "X" },
+                ["X"] = new List<string> { "Z", "S", "D", "C" },
+                ["C"] = new List<string> { "X", "D", "F", "V" },
+                ["V"] = new List<string> { "C", "F", "G", "B" },
+                ["B"] = new List<string> { "V", "G", "H", "N" },
+                ["N"] = new List<string> { "B", "H", "J", "M" },
+                ["M"] = new List<string> { "N", "J", "K", "," },
+                [","] = new List<string> { "M", "K", "L", "." },
+                ["."] = new List<string> { ",", "L", ";", "/" },
+                ["/"] = new List<string> { ".", ";", "'", "RightShift" },
+                ["RightShift"] = new List<string> { "/", "Enter" },
+
+                // ==============================================
+                // 第六行:Ctrl Fn Win Alt Space Alt Fn Ctrl ← ↓ →
+                // ==============================================
+                ["Ctrl"] = new List<string> { "Fn", "LeftShift" },
+                ["Fn"] = new List<string> { "Ctrl", "Win", "LeftShift" },
+                ["Win"] = new List<string> { "Fn", "Alt" },
+                ["Alt"] = new List<string> { "Win", "Space" },
+                ["Space"] = new List<string> { "Alt", "RightAlt" },
+                ["RightAlt"] = new List<string> { "Space", "RightFn" },
+                ["RightFn"] = new List<string> { "RightAlt", "RightCtrl" },
+                ["RightCtrl"] = new List<string> { "RightFn", "←", "↓", "→" },
+                ["←"] = new List<string> { "RightCtrl", "↓" },
+                ["↓"] = new List<string> { "←", "→", "RightCtrl" },
+                ["→"] = new List<string> { "↓", "RightCtrl" }
+            };
+        }
+
+
+
         private async void DoYieldTime(object state)
         {
             try

+ 1 - 1
TeamAAS-VM/Data/SystemDatabaseService.cs

@@ -430,10 +430,10 @@ namespace TeamAAS_VP.Data
         /// <returns></returns>
         public async Task<int> GetNGProductionAsync(string productName)
         {
-            //今天的起始时间
             var todayStart = DateTime.UtcNow.Date;
             //今天的结束时间
             var todayEnd = DateTime.UtcNow.Date;
+            todayEnd = todayEnd.AddDays(1);
             //按照产品名称和时间范围查询生产记录的总数
             var total = await _db.Queryable<ProductionRecord>()
                 .Where(r => r.ProductName == productName && r.Timestamp >= todayStart && r.Timestamp < todayEnd && r.Category=="NG")

+ 2 - 1
TeamAAS-VM/Interfaces/IMesService.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -104,7 +105,7 @@ namespace TeamAAS_VP.Interfaces
         /// - 若 <paramref name="start_time"/> 晚于 <paramref name="stop_time"/>,实现应记录并视情况返回失败或抛出异常。
         /// - 实现应尊重 <paramref name="cancellationToken"/>,并在取消时尽早中止请求。
         /// </remarks>
-        Task<(bool IsSuccess, string Response)> SubmitGetAsync(int deviceNo,string sn, string comp, string mesdata, bool isSuccess, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default);
+        Task<(bool IsSuccess, string Response)> SubmitGetAsync(int deviceNo,string sn, string comp, string mesdata, bool isSuccess, string submitDataError, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default);
 
         /// <summary>
         /// 获取服务器当前时间

+ 7 - 2
TeamAAS-VM/Services/MesService.cs

@@ -263,11 +263,11 @@ namespace TeamAAS_VP.Services
         /// - 捕获 <see cref="OperationCanceledException"/> 返回 "Canceled",捕获其它异常则返回异常消息。
         /// - 若实例已被释放则抛出 <see cref="ObjectDisposedException"/>。
         /// </remarks>
-        public async Task<(bool IsSuccess, string Response)> SubmitGetAsync(int deviceNo,string sn, string comp, string mesdata, bool isSuccess, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default)
+        public async Task<(bool IsSuccess, string Response)> SubmitGetAsync(int deviceNo,string sn, string comp, string mesdata, bool isSuccess,string submitDataError, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default)
         {
             // 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
             if (_disposed) throw new ObjectDisposedException(nameof(MesService));
-
+            string ErrorData = submitDataError;
             var device = _configService.GetDeviceInfo(deviceNo);
             if (device == null)
                 device = _configService.GetDeviceInfo();
@@ -286,6 +286,11 @@ namespace TeamAAS_VP.Services
             }
 
             string res = isSuccess ? "PASS" : "FAIL";
+            if (res == "FAIL")
+            {
+                res = ErrorData;
+                //res = "A1-gap-Ctrl2_4_Fail";
+            }
             string url = "";
             if (device.F == "PTL")
             {