quanzhou
2025-09-11 31de09d1ba504cb036ae29a98a9efbb0c2d61914
src/Bro.M141.Process/M141Process.cs
@@ -25,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;
@@ -80,6 +83,8 @@
                ThHeartPlc.IsBackground = true;
                ThHeartPlc.Start();
            }
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument_Print);
        }
        public override void ProcessRunStateChanged()
@@ -122,6 +127,8 @@
            }
            _errorSpec = M141Config.SpecCollection.FirstOrDefault(u => u.Code == M141Config.CheckErrorSpecCode) as Spec;
            //InitialProductList();
            NetWarmUp();
@@ -138,8 +145,12 @@
                mqtt = new RabbitMQHelper(M141Config.zIP, M141Config.zport, M141Config.zuser, M141Config.zpassword);
                mqtt.Connect(M141Config.MESchannel);
            }
            else
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭上传mes");
            }
            InitialContinuousNGAlarm();
                InitialContinuousNGAlarm();
        }
@@ -191,6 +202,7 @@
            Dictionary<string, List<int>> StateDIC = new Dictionary<string, List<int>>();
            List<string> AlarmTypeList = new List<string>();
            int numplc = 0;
            while (true)
@@ -235,18 +247,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)
                {
@@ -290,9 +302,7 @@
                                var alrams = item1.AlarmDetails;
                                var plcdev = DeviceCollection.FirstOrDefault(u => u.Id == item1.plcname) as PLCBase;
                                foreach (var item in alrams.GroupBy(u => u.address))
                                {
                                    int add = item.Key;
@@ -333,40 +343,32 @@
                                    }
                                    //if (!StateDIC.ContainsKey(plcdev.Name))
                                    //{
                                    //       StateDIC[plcdev.Name] = new List<int>();
                                    //}
                                }
                                List<int> list = new List<int>();
                                Dictionary<string, AlarmTypeData> AlarmType = new Dictionary<string, AlarmTypeData>();
                                List<int> list = new List<int>();
                                foreach (var item in alrams)
                                {
                                    if (!AlarmType.ContainsKey(item.alarmtype))
                                    string key = item.alarmtype;
                                    if (!AlarmType.TryGetValue(key, out var alarmData))
                                    {
                                        AlarmType.Add(item.alarmtype, new AlarmTypeData()
                                        // 不存在,创建新的
                                        alarmData = new AlarmTypeData()
                                        {
                                            CSVhead = csvhead,
                                            CSVdata = csvdata,
                                        });
                                        };
                                        AlarmType.Add(item.alarmtype, alarmData);
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"报警{item.alarmname}类别{key}作为key加入AlarmType字典");
                                    }
                                    if (AlarmType.ContainsKey(item.alarmtype))
                                    {
                                        AlarmType[item.alarmtype].CSVhead += $",{item.alarmname}";
                                    alarmData.CSVhead += $",{item.alarmname}";
                                    alarmData.CSVdata += $",{(item.value == 1 ? "1" : "")}";
                                    alarmData.AlarmTypeValue.Add(item.value);
                                        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}加入字典:AlarmType");
                                    }
                                }
                                foreach(var item in AlarmType.Keys)
                                {
@@ -377,13 +379,13 @@
                                    if (!StateDIC[key].SequenceEqual(list))
                                    {
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{plcdev.Name}报警类型:{item}的信号发生变化");
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{plcdev.Name}报警类型:{item}的信号发生变化,保存的数据为{AlarmType[item].CSVdata}");
                                        StateDIC[key].Clear();
                                        StateDIC[key].AddRange(list);
                                        CSVRecordAsync($"PLCstate_{key}.csv", AlarmType[item].CSVhead, AlarmType[item].CSVdata);
                                        CSVRecordAsync($"PLCstate_{DateTime.Now.ToString("yyyyMMdd")}_{key}.csv", AlarmType[item].CSVdata, AlarmType[item].CSVhead);
                                        var showdata = alrams.Where(u => u.value == 1).Select(u => u.alarmname).ToList();
@@ -404,9 +406,9 @@
                                }                                
                            }
                            catch
                            catch(Exception EXC)
                            {
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"PLC报警存储记录发生异常 {EXC.ToString()}");
                            }
                        }
                    }
@@ -442,7 +444,7 @@
        public class AlarmTypeData
        {
            public List <int> AlarmTypeValue;
            public List <int> AlarmTypeValue = new List<int>();
            public string CSVhead;
@@ -1099,7 +1101,7 @@
            CheckPositionDoneAsync_plc(measureBinds[0].WorkPosition, inputSequence, config, cameraIds);
            return msg;
        }
@@ -1237,13 +1239,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);
@@ -1456,6 +1458,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
                                    {
@@ -1609,7 +1619,10 @@
                            }
                        }
                        else
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭mes连接或者数据上传接口");
                        }
                    }
                    //ReplyPlcData(positionName, config.TriggerValue);
@@ -1882,6 +1895,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);//正文文字
            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 = 60,
                    Height = 60,
                    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