quanzhou
2025-08-25 c469200a8f46df6ee518c3cb7226e8a8ad6217af
给mes功能添加日志记录,优化Statistic.json的读写功能
2个文件已修改
157 ■■■■ 已修改文件
src/Bro.M141.Process/M141Process.cs 137 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/MyMQTT.cs 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/M141Process.cs
@@ -22,6 +22,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
@@ -137,10 +138,10 @@
                mqtt.Connect(M141Config.MESchannel);
            }
            InitialContinuousNGAlarm();
            InitialContinuousNGAlarm();
        }
        public override void Close()
        {
            devicestate = false;
@@ -219,24 +220,30 @@
                            }
                        }
                    }
                    lock (StatisticRecordsFull)
                    if (numsum > 20)
                    {
                        if (numsum > 20)
                        {
                            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();
                            }
                        }
                        numsum = 0;
                        StatisticRecordsFull.SaveSummaryRecord();
                    }
                    //lock (StatisticRecordsFull)
                    //{
                    //    if (numsum > 20)
                    //    {
                    //        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();
                        //        }
                        //    }
                        //}
                }
                catch (Exception ex)
                {
@@ -942,6 +949,7 @@
        #region plc
        public ResponseMessage RunImageCheck_plc(IOperationConfig config)
        {
            ResponseMessage msg = new ResponseMessage();
            msg.Result = 1;
            List<MeasureBind> measureBinds = new List<MeasureBind>();
@@ -1014,7 +1022,10 @@
            }
            CheckPositionDoneAsync_plc(measureBinds[0].WorkPosition, inputSequence, config, cameraIds);
            return msg;
        }
@@ -1150,11 +1161,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);
@@ -1242,19 +1255,17 @@
                        mysqlhelper.UpdateProduct(p);
                        if (positionSet.IsLastPosition)
                        {
                            //班次统计时间划分
                            if (M141Config.WorkShiftList.Count == 0)
                            {
                                //生成一个报表
                                string name = $"ProductRecord_{DateTime.Now.ToString("yyyyMMdd")}.csv";
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"创建{name}数据报表");
                                UpdateProductResultAsync(p, name);
                                UpdateProductResultAsync(p, name);
                            }
                            else
                            {
@@ -1274,7 +1285,7 @@
                                    }
                                    else
                                    {
                                        if (now.TimeOfDay >= item.ShiftTime_Start.TimeOfDay )
                                        if (now.TimeOfDay >= item.ShiftTime_Start.TimeOfDay)
                                        {
                                            //生成一个报表
                                            string name = $"ProductRecord_{DateTime.Now.ToString("yyyyMMdd")}_{item.ShiftName}.csv";
@@ -1291,16 +1302,12 @@
                                    }
                                }
                            }
                            //UpdateProductResultAsync(p);
                            mysqlhelper.NewForAll(p, M141Config.StationCode, M141Config.defectname);
                            if (M141Config.IsfinDevice)
                            {
                                SummaryAllprodata(p);
                            }
                        }
                    });
@@ -1460,7 +1467,7 @@
                                            newp.BasketCode = plist[0].BasketCode;
                                            newp.Result = "NG";
                                            newp.SN = plist[0].SN;
                                            if(M141Config.IsfinDevice)
                                            if (M141Config.IsfinDevice)
                                            {
                                                SummaryAllprodata(newp);
                                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{newp.PID}提前NG加入汇总数据报表统计");
@@ -1557,7 +1564,7 @@
        public void SummaryAllprodata(ProductModel p)
        {
            _taskFactory.StartNew(() =>
            {
                try
@@ -1572,7 +1579,7 @@
                    Thread.Sleep(500);
                    var plist = mysqlhelper.GetProductList(p.SEQUENCE);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品{p.PID}检测结果为{p.Result}从数据库中获取的数据为产品{plist[0].PID}检测结果为{plist[0].Result}");
                    if (plist != null)
                    {
@@ -1594,7 +1601,7 @@
                    string data = "";
                    string Head = "时间,PID,栏具码,物料码,条码,汇总检测结果,S2_P1检测明细,S2_P2检测明细,S2_P3检测明细,S3_P1检测明细,S3_P2检测明细,S4_P1检测明细,S4_P2检测明细,S5_P1检测明细,S5_P2检测明细,S5_P3检测明细,Barcode,SPC_A,SPC_BF_A1,SPC_BF_A2,SPC_BF_A3,SPC_BF_A4,SPC_BF_A5,SPC_BF_A6,SPC_BF_A7,SPC_BF_A8,SPC_BF_A9,SPC_BS_A1,SPC_BS_A2,SPC_BS_A3,SPC_BS_A4,SPC_BS_A5,SPC_BS_A6,SPC_BS_A7,SPC_BS_A8,SPC_BS_A9,SPC_BT_A100RES,SPC_BT_A101RES,SPC_BT_A102RES,SPC_BT_A103RES,SPC_BT_A104RES,SPC_BT_A105RES,SPC_BT_A106RES,SPC_BT_A107RES,SPC_BT_A108RES,SPC_BT_A109RES,SPC_BT_A10RES,SPC_BT_A110RES,SPC_BT_A111RES,SPC_BT_A112RES,SPC_BT_A113RES,SPC_BT_A11RES,SPC_BT_A12RES,SPC_BT_A13RES,SPC_BT_A14RES,SPC_BT_A15RES,SPC_BT_A16RES,SPC_BT_A17RES,SPC_BT_A18RES,SPC_BT_A19RES,SPC_BT_A1RES,SPC_BT_A20RES,SPC_BT_A21RES,SPC_BT_A22RES,SPC_BT_A23RES,SPC_BT_A24RES,SPC_BT_A25RES,SPC_BT_A26RES,SPC_BT_A27RES,SPC_BT_A28RES,SPC_BT_A29RES,SPC_BT_A2RES,SPC_BT_A30RES,SPC_BT_A31RES,SPC_BT_A32RES,SPC_BT_A33RES,SPC_BT_A34RES,SPC_BT_A35RES,SPC_BT_A36RES,SPC_BT_A37RES,SPC_BT_A38RES,SPC_BT_A39RES,SPC_BT_A3RES,SPC_BT_A40RES,SPC_BT_A41RES,SPC_BT_A42RES,SPC_BT_A43RES,SPC_BT_A44RES,SPC_BT_A45RES,SPC_BT_A46RES,SPC_BT_A47RES,SPC_BT_A48RES,SPC_BT_A49RES,SPC_BT_A4RES,SPC_BT_A50RES,SPC_BT_A51RES,SPC_BT_A52RES,SPC_BT_A53RES,SPC_BT_A54RES,SPC_BT_A55RES,SPC_BT_A56RES,SPC_BT_A57RES,SPC_BT_A58RES,SPC_BT_A59RES,SPC_BT_A5RES,SPC_BT_A60RES,SPC_BT_A61RES,SPC_BT_A62RES,SPC_BT_A63RES,SPC_BT_A64RES,SPC_BT_A65RES,SPC_BT_A66RES,SPC_BT_A67RES,SPC_BT_A68RES,SPC_BT_A69RES,SPC_BT_A6RES,SPC_BT_A70RES,SPC_BT_A71RES,SPC_BT_A72RES,SPC_BT_A73RES,SPC_BT_A74RES,SPC_BT_A75RES,SPC_BT_A76RES,SPC_BT_A77RES,SPC_BT_A78RES,SPC_BT_A79RES,SPC_BT_A7RES,SPC_BT_A80RES,SPC_BT_A81RES,SPC_BT_A82RES,SPC_BT_A83RES,SPC_BT_A84RES,SPC_BT_A85RES,SPC_BT_A86RES,SPC_BT_A87RES,SPC_BT_A88RES,SPC_BT_A89RES,SPC_BT_A8RES,SPC_BT_A90RES,SPC_BT_A91RES,SPC_BT_A92RES,SPC_BT_A93RES,SPC_BT_A94RES,SPC_BT_A95RES,SPC_BT_A96RES,SPC_BT_A97RES,SPC_BT_A98RES,SPC_BT_A99RES,SPC_BT_A9RES,SPC_C";
                    for (int i = 0; i < Head.Split(",").ToList().Count(); i++)
                    {
                        int index = csvHeadforall.Split(",").ToList().IndexOf(Head.Split(",").ToList()[i]);
@@ -1665,35 +1672,35 @@
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"连续NG数据记录");
                    DefectNGRecordList.ForEach(d =>
                    {
                            string alarmMsg = "";
                            int alarmTypeTemp = 0;
                            if (product.Result == "OK")
                        string alarmMsg = "";
                        int alarmTypeTemp = 0;
                        if (product.Result == "OK")
                        {
                            if (d.CheckIsAlarmRaised(product.Result == "OK", out alarmMsg, out alarmTypeTemp))
                            {
                                if (d.CheckIsAlarmRaised(product.Result == "OK", out alarmMsg, out alarmTypeTemp))
                                allMsg += $"{alarmMsg}\r\n";
                                isAlarmRaised = true;
                                ngItem = "产品结果";
                                alarmType = alarmTypeTemp;
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{product.PID}数据结果为{product.Result}参与连续NG统计");
                            }
                        }
                        else
                        {
                            if (product.Result.Contains(d.DefectName))
                            {
                                if (d.CheckIsAlarmRaised(!product.Result.Contains(d.DefectName), out alarmMsg, out alarmTypeTemp))
                                {
                                    allMsg += $"{alarmMsg}\r\n";
                                    isAlarmRaised = true;
                                    ngItem = "产品结果";
                                    alarmType = alarmTypeTemp;
                                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{product.PID}数据结果为{product.Result}参与连续NG统计");
                                }
                            }
                            else
                            {
                                if (product.Result.Contains(d.DefectName))
                                {
                                    if (d.CheckIsAlarmRaised(!product.Result.Contains(d.DefectName), out alarmMsg, out alarmTypeTemp))
                                    {
                                        allMsg += $"{alarmMsg}\r\n";
                                        isAlarmRaised = true;
                                        ngItem = "产品结果";
                                        alarmType = alarmTypeTemp;
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{product.PID}数据结果为{product.Result}参与连续NG统计");
                                    }
                                }
                            }
                        }
                    });
                }
                catch (Exception ex)
@@ -1701,7 +1708,7 @@
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Warning, $"连续NG数据记录失败");
                }
                if (isAlarmRaised)
                {
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Warning, $"连续NG监控报警,{allMsg}");
                    if (M141Config.IsOperatorReset)
                    {
@@ -1718,7 +1725,7 @@
                    {
                        Task.Delay(50).Wait();
                        ResetContinuousNGAlarm();
                    }
                    }
                }
            });
        }
@@ -1741,14 +1748,14 @@
                                _continuousNGAlarmFrm = new FrmContinuousNGAlarm();
                                _continuousNGAlarmFrm.TopMost = true;
                                _continuousNGAlarmFrm.FormClosed += _continuousNGAlarmFrm_FormClosed;
                                Task.Run(() =>
                                {
                                    _continuousNGAlarmFrm.ShowDialog();
                                });
                                });
                            }
                        }
                    }
@@ -1761,7 +1768,7 @@
                    {
                        _continuousNGAlarmFrm.Close();
                    }
                }
            });
@@ -1770,7 +1777,7 @@
        private void _continuousNGAlarmFrm_FormClosed(object? sender, FormClosedEventArgs e)
        {
            ResetContinuousNGAlarm();
            LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"连续NG报警已复位");
            LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"连续NG报警已复位");
            _continuousNGAlarmFrm = null;
        }
@@ -1785,7 +1792,7 @@
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"连续NG监控通知PLC重置报警失败,{error}");
                }
            }
        }
        [ProcessMethod("", "ContinuousNGAlarmTest", "连续NG报警测试", InvokeType.TestInvoke)]
@@ -1848,10 +1855,10 @@
                        NGRecords.Add(DateTime.Now);
                    }
                }
                if (NGRecords.Count >= AlarmSetting.TimePeriodNumThresold && NGRecords.Count > 0)
                {
                    NGRecords = NGRecords.Skip(NGRecords.Count - AlarmSetting.TimePeriodNumThresold).OrderBy(u => u).ToList();
                    int timeInMinute = (int)Math.Ceiling((NGRecords[NGRecords.Count - 1] - NGRecords[0]).TotalMinutes);
@@ -1882,9 +1889,9 @@
            string msg = "";
            lock (_lockObj)
            {
                IsAlarmRaised = false;
                IsAlarmRaised = false;
                if (_isContinuousAlarm)
                {
                {
                    _isContinuousAlarm = false;
                    ContinuousNGNum = 0;
                    msg += "连续NG报警 ";
@@ -1892,7 +1899,7 @@
                if (_timeAlarm)
                {
                    _timeAlarm = false;
                    NGRecords.Clear();
                    msg += "时段内NG报警 ";
src/Bro.M141.Process/MyMQTT.cs
@@ -8,7 +8,6 @@
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using SourceGrid.Cells.Editors;
using Sunny.UI.Win32;
using System;
using System.Collections.Concurrent;
@@ -81,8 +80,6 @@
                InitConsumer(_receiveChannel2, _queue2, HandleQueue2Message);
            }
            CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, "RabbitMQ 连接成功");
        }
@@ -111,16 +108,19 @@
                }
                else
                {
                    CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ 篮具 收到未匹配 zbguid:{obj?.zbguid}");
                    if (MSGClasses.Any(u => u.zguid == obj?.zbguid))
                    {
                        var tem = MSGClasses.FirstOrDefault(u => u.zguid == obj?.zbguid);
                        if (!string.IsNullOrEmpty(tem.receive))
                        {
                            tem.receive = message;
                            CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ 篮具有超时动作,截留下匹配信息:{message}");
                        }
                    }
                    CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ 篮具 收到未匹配 zbguid:{obj?.zbguid}");
                }
            }
            catch (Exception ex)
@@ -164,21 +164,23 @@
                {
                    if (!string.IsNullOrEmpty(tem.receive))
                    {
                        CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ {keystr}二次接收 返回值正常 {tem.receive}");
                        return tem.receive;
                    }
                    Thread.Sleep(1000);
                }
                MSGClasses.RemoveAll(u => u.key == keystr);
                return tem.receive;
                CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ {keystr}二次接收 返回值为空");
                return null;
            }
            var guid = Guid.NewGuid().ToString();
            var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
            _pendingTask1[guid] = tcs;
            var msgObj = new AutoLineMacQueue
            {