From aa6b32cb313c914e5f8eed0a347551f8fc65ffbd Mon Sep 17 00:00:00 2001
From: kingno <30263@KINGNO>
Date: 星期三, 27 八月 2025 16:51:18 +0800
Subject: [PATCH] 合并

---
 src/Bro.M141.Process/M141Process.cs |  262 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 220 insertions(+), 42 deletions(-)

diff --git a/src/Bro.M141.Process/M141Process.cs b/src/Bro.M141.Process/M141Process.cs
index be2c952..1a63702 100644
--- a/src/Bro.M141.Process/M141Process.cs
+++ b/src/Bro.M141.Process/M141Process.cs
@@ -13,6 +13,7 @@
 using MySql.Data.MySqlClient;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
+using NPOI.SS.Formula.Function;
 using NPOI.SS.Formula.Functions;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Ocsp;
@@ -24,11 +25,14 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Drawing.Imaging;
+using System.Drawing.Printing;
 using System.IO;
 using System.Linq;
 using System.Text;
 using Windows.ApplicationModel.Appointments;
+using Windows.Devices.PointOfService;
 using ZXing;
+using ZXing.QrCode;
 using static Bro.Common.Helper.EnumHelper;
 using static Bro.Process.ProcessControl;
 using static Org.BouncyCastle.Math.EC.ECCurve;
@@ -79,6 +83,8 @@
                 ThHeartPlc.IsBackground = true;
                 ThHeartPlc.Start();
             }
+
+            printDocument1.PrintPage += new PrintPageEventHandler(printDocument_Print);
         }
 
         public override void ProcessRunStateChanged()
@@ -120,6 +126,8 @@
                 LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"鏈缃畃lc");
             }
             _errorSpec = M141Config.SpecCollection.FirstOrDefault(u => u.Code == M141Config.CheckErrorSpecCode) as Spec;
+
+           
 
             //InitialProductList();
 
@@ -189,6 +197,8 @@
 
 
             Dictionary<string, List<int>> StateDIC = new Dictionary<string, List<int>>();
+            List<string> AlarmTypeList = new List<string>();
+
             int numplc = 0;
             while (true)
             {
@@ -232,18 +242,18 @@
                     //        numsum = 0;
 
 
-                        //        using (FileStream fileStream = new FileStream(_statisticFilePath, FileMode.OpenOrCreate, FileAccess.Write))
-                        //        {
-                        //            fileStream.Seek(0L, SeekOrigin.Begin);
-                        //            string s = JsonConvert.SerializeObject(StatisticRecordsFull);
-                        //            byte[] bytes = Encoding.UTF8.GetBytes(s);
-                        //            fileStream.Write(bytes, 0, bytes.Length);
-                        //            fileStream.SetLength(bytes.Length);
-                        //            fileStream.Flush();
-                        //            fileStream.Close();
-                        //        }
-                        //    }
-                        //}
+                    //        using (FileStream fileStream = new FileStream(_statisticFilePath, FileMode.OpenOrCreate, FileAccess.Write))
+                    //        {
+                    //            fileStream.Seek(0L, SeekOrigin.Begin);
+                    //            string s = JsonConvert.SerializeObject(StatisticRecordsFull);
+                    //            byte[] bytes = Encoding.UTF8.GetBytes(s);
+                    //            fileStream.Write(bytes, 0, bytes.Length);
+                    //            fileStream.SetLength(bytes.Length);
+                    //            fileStream.Flush();
+                    //            fileStream.Close();
+                    //        }
+                    //    }
+                    //}
                 }
                 catch (Exception ex)
                 {
@@ -285,16 +295,15 @@
                                 }
 
                                 var alrams = item1.AlarmDetails;
+
                                 var plcdev = DeviceCollection.FirstOrDefault(u => u.Id == item1.plcname) as PLCBase;
 
-                                if (!StateDIC.ContainsKey(plcdev.Name))
-                                {
-                                    StateDIC[plcdev.Name] = new List<int>();
-                                }
-
+                                  
+                                
                                 foreach (var item in alrams.GroupBy(u => u.address))
                                 {
                                     int add = item.Key;
+
                                     var readres = plcdev.Read(add, 1, out _)[0];
 
                                     var Allbin = Convert.ToString(readres, 2).PadLeft(16, '0').Select(c => c - '0').ToArray();
@@ -304,39 +313,103 @@
                                     foreach (var item2 in item)
                                     {
                                         item2.value = Allbin[item2.address2];
+
+                                        if (!AlarmTypeList.Contains(item2.alarmtype))
+                                        {
+
+                                            AlarmTypeList.Add(item2.alarmtype);
+
+                                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"鎶ヨ绫诲埆{item2.alarmtype}鍔犲叆闃熷垪,闃熷垪涓暟涓簕AlarmTypeList.Count}");
+                                        }
                                     }
                                 }
+
+                                string StateDICKey = "";
+
+                                foreach (var item in AlarmTypeList)
+                                {
+
+                                    StateDICKey = plcdev.Name +"_"+ item;
+
+                                    if (!StateDIC.ContainsKey(StateDICKey))
+                                    {
+
+                                        StateDIC[StateDICKey] = new List<int>();
+
+                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"鎶ヨ绫诲埆{StateDICKey}鍔犲叆瀛楀吀锛歋tateDIC");
+
+                                    }
+
+                                    //if (!StateDIC.ContainsKey(plcdev.Name))
+                                    //{
+
+                                    //       StateDIC[plcdev.Name] = new List<int>();
+                                    //}
+                                }
+
+                                Dictionary<string, AlarmTypeData> AlarmType = new Dictionary<string, AlarmTypeData>();
+
                                 List<int> list = new List<int>();
+                             
                                 foreach (var item in alrams)
                                 {
-                                    csvhead += $",{item.alarmname}";
-                                    csvdata += $",{(item.value == 1 ? "1" : "")}";
-                                    list.Add(item.value);
-                                }
+                                    if (!AlarmType.ContainsKey(item.alarmtype))
+                                    {
+                                        AlarmType.Add(item.alarmtype, new AlarmTypeData()
+                                        {
+                                            CSVhead = csvhead,
+                                            CSVdata = csvdata,
+                                        });
 
-                                if (!StateDIC[plcdev.Name].SequenceEqual(list))
+                                    }
+
+                                    if (AlarmType.ContainsKey(item.alarmtype))
+                                    {
+                                        AlarmType[item.alarmtype].CSVhead += $",{item.alarmname}";
+
+                                        AlarmType[item.alarmtype].CSVdata += $",{(item.value == 1 ? "1" : "")}";
+
+                                        AlarmType[item.alarmtype].AlarmTypeValue.Add(item.value);
+
+                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"鎶ヨ绫诲埆{item.alarmtype}锛涙暟鍊納item.value}鍔犲叆瀛楀吀锛欰larmType");
+                                    }
+                                    
+                                }
+                                foreach(var item in AlarmType.Keys)
                                 {
-                                    StateDIC[plcdev.Name].Clear();
-                                    StateDIC[plcdev.Name].AddRange(list);
 
-                                    CSVRecordAsync($"PLCstate_{plcdev.Name}.csv", csvdata, csvhead);
+                                    list = AlarmType[item].AlarmTypeValue;
 
-                                    var showdata = alrams.Where(u => u.value == 1).Select(u => u.alarmname).ToList();
+                                    string key = plcdev.Name+"_"+item;
 
-                                    if (showdata == null)
+                                    if (!StateDIC[key].SequenceEqual(list))
                                     {
-                                        showdata = new List<string>();
+                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{plcdev.Name}鎶ヨ绫诲瀷锛歿item}鐨勪俊鍙峰彂鐢熷彉鍖�");
+
+                                        StateDIC[key].Clear();
+
+                                        StateDIC[key].AddRange(list);
+
+                                        CSVRecordAsync($"PLCstate_{key}.csv", AlarmType[item].CSVhead, AlarmType[item].CSVdata);
+
+                                        var showdata = alrams.Where(u => u.value == 1).Select(u => u.alarmname).ToList();
+
+                                        if (showdata == null)
+                                        {
+                                            showdata = new List<string>();
+                                        }
+
+                                        if (showdata.Count > 0)
+                                        {
+                                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{plcdev.Name}鎶ヨ 涓暟:{showdata.Count}  {string.Join(',', showdata)}");
+                                        }
+                                        else
+                                        {
+                                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{plcdev.Name}鎶ヨ 涓暟:0");
+                                        }
                                     }
 
-                                    if (showdata.Count > 0)
-                                    {
-                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{plcdev.Name}鎶ヨ 涓暟:{showdata.Count}  {string.Join(',', showdata)}");
-                                    }
-                                    else
-                                    {
-                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{plcdev.Name}鎶ヨ 涓暟:0");
-                                    }
-                                }
+                                }                                
                             }
                             catch
                             {
@@ -350,8 +423,6 @@
                 {
 
                 }
-
-
 
                 try
                 {
@@ -375,6 +446,18 @@
                 Thread.Sleep(1000);
             }
         }
+
+        public class AlarmTypeData
+        {
+            public List <int> AlarmTypeValue;
+
+            public string CSVhead;
+
+            public string CSVdata;
+
+        }
+
+
 
         /// <summary>
         /// 缃戠粶棰勭儹
@@ -1023,7 +1106,7 @@
 
             CheckPositionDoneAsync_plc(measureBinds[0].WorkPosition, inputSequence, config, cameraIds);
 
-         
+
             return msg;
 
         }
@@ -1161,13 +1244,13 @@
 
         public async Task<List<ProductModel>> CheckPositionDoneAsync_plc(string positionName, string inputSequence, IOperationConfig config, List<string> cameraIds)
         {
-           
+
             string triggerSource = config.TriggerSource;
             return await _taskFactory.StartNew(() =>
             {
                 try
                 {
-                    
+
                     string index = config.TriggerStr.Split(',')[1];
                     var positionSet = M141Config.WorkPositionCollection.FirstOrDefault(u => u.PositionName == positionName);
 
@@ -1380,6 +1463,14 @@
                                     if (obj.zstatus == "200")
                                     {
                                         LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"浜у搧{pList[0].PID}鏁版嵁涓婁紶MES鎴愬姛  {Msgreceice}");
+
+                                        if (M141Config.Isprint && !string.IsNullOrEmpty(obj.tary_label))
+                                        {
+                                            StartPrint(obj.tary_label);
+                                        }
+
+
+
                                     }
                                     else
                                     {
@@ -1806,6 +1897,93 @@
 
             return new ResponseMessage();
         }
+
+
+
+
+
+
+        #region  鎵撳嵃鏈虹浉鍏�
+        PrintDocument printDocument1 = new PrintDocument();
+        string Printmessage = "";
+
+        public void StartPrint(string str)
+        {
+            try
+            {
+                int ttwith = 320;
+                int ttheigh = 160;
+                Printmessage = str;
+
+                this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)";
+                this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh);
+
+                this.printDocument1.PrintController = new System.Drawing.Printing.StandardPrintController();
+                this.printDocument1.Print();
+
+            }
+            catch
+            {
+
+            }
+
+        }
+
+
+        //C229M2508250500002
+        private void printDocument_Print(object sender, PrintPageEventArgs e)
+        {
+            Font fntTxt = new Font("榛戜綋", 15, System.Drawing.FontStyle.Bold);//姝f枃鏂囧瓧               
+            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//鐢诲埛           
+            try
+            {
+                var g = e.Graphics;
+                g.Clear(Color.White);
+                BarcodeWriter writer = new BarcodeWriter();
+                writer.Format = BarcodeFormat.QR_CODE;
+                QrCodeEncodingOptions options = new QrCodeEncodingOptions()
+                {
+                    DisableECI = true,//璁剧疆鍐呭缂栫爜
+                    CharacterSet = "UTF-8",  //璁剧疆浜岀淮鐮佺殑瀹藉害鍜岄珮搴�
+                    Width = 123,
+                    Height = 123,
+                    Margin = 1//璁剧疆浜岀淮鐮佺殑杈硅窛,鍗曚綅涓嶆槸鍥哄畾鍍忕礌
+                };
+
+                writer.Options = options;
+                Bitmap map = writer.Write(Printmessage);
+                e.Graphics.DrawImage(map, new System.Drawing.Point(90, 10));
+
+                // 鍦ㄤ簩缁寸爜涓嬫柟鐢诲唴瀹规枃瀛�
+                using (Font font = new Font("Arial", 10))
+                using (Brush brush3 = new SolidBrush(Color.Black))
+                {
+                    // 璁╂枃瀛楀眳涓埌浜岀淮鐮佷笅鏂�
+                    SizeF textSize = e.Graphics.MeasureString(Printmessage, font);
+                    float textX = 90 + (map.Width - textSize.Width) / 2;
+                    float textY = 10 + map.Height;
+
+                    e.Graphics.DrawString(Printmessage, font, brush3, textX, textY);
+                }
+
+            }
+            catch (Exception ee)
+            {
+     
+                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, ee.ToString());
+            }
+        }
+
+
+
+
+        #endregion
+
+
+
+
+
+
     }
 
     public class DefectNGRecord

--
Gitblit v1.8.0