M182轴承端盖外观缺陷AOI
kingno
2025-05-26 5a405c7dce20d8c79a733c9c786cc42eb59fe81c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using Bro.Common.Helper;
using Bro.M135.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.M141.Process
{
    public partial class M141Process
    {
        List<DefectNGRecord> DefectNGRecordList = new List<DefectNGRecord>();
 
 
        public void InitialContinuousNGAlarm()
        {
            DefectNGRecordList = M141Config.ContinuousNGAlarmColletion.Where(u => u.IsEnabled).Select(u =>
            {
                DefectNGRecord record = new DefectNGRecord();
                record.DefectName = u.DefectType;
                record.AlarmSetting = u;
                return record;
            }).ToList();
 
 
        }
 
 
 
 
 
        public async void CheckContinuousNGAlarmAsync(ProductModel product)
        {
            await Task.Run(() =>
            {
                if (!M141Config.IsEnableContinuousNGAlarm)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"连续NG报警总开关已关闭");
                    return;
                }
 
                if (DefectNGRecordList.Count == 0)
                    return;
 
                //var allDefects = product.DETAILS.SelectMany(u => u.DetectResults).ToList().GetDefectDescList();
 
                List<string> allDefects = new List<string>();
                foreach (var v in product.Details)
                {
                    allDefects.AddRange(v.ResultList.GetDefectDescList());
                    allDefects.AddRange(v.DefectList);
                }
                allDefects = allDefects.Distinct().ToList();
 
                string allMsg = "";
                bool isAlarmRaised = false;
                int alarmType = 0;
                string ngItem = "";
 
                DefectNGRecordList.ForEach(d =>
                {
                    string alarmMsg = "";
 
                    if (d.CheckIsAlarmRaised(!allDefects.Contains(d.DefectName), out alarmMsg, out int alarmTypeTemp))
                    {
                        allMsg += $"{alarmMsg}\r\n";
                        isAlarmRaised = true;
                        ngItem = d.DefectName;
                        alarmType = alarmTypeTemp;
                    }
 
                });
 
                if (isAlarmRaised)
                {
                    var state = Plc1.Read(3027, 1, out _)[0];
                    if (state == 0)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"连续NG监控报警,{allMsg}");
                        if (!Plc1.WriteSingleAddress(3006, 1, out _))
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"连续NG监控通知PLC报警失败");
                        }
                        else
                        {
                            DefectNGRecordList.ForEach(d =>
                            {
                                d.ResetAlarm();
                            });
                        }
                    }
                    else
                    {
 
                        DefectNGRecordList.ForEach(d =>
                        {
                            d.ResetAlarm();
                        });
 
                    }
 
 
                }
            });
        }
    }
 
    public class DefectNGRecord
    {
        public string DefectName { get; set; }
        public List<DateTime> NGRecords { get; set; } = new List<DateTime>();
        public int ContinuousNGNum { get; set; } = 0;
 
        public ContinuousNGAlarm AlarmSetting { get; set; }
 
        private object _lockObj = new object();
 
        public bool IsAlarmRaised = false;
 
        bool _isContinuousAlarm = false;
        bool _timeAlarm = false;
 
        public bool CheckIsAlarmRaised(bool isOK, out string alarmMsg, out int alarmType)
        {
            alarmType = 0;
            alarmMsg = "";
            bool isAlarmRasied = false;
 
            if (IsAlarmRaised)
                return false;
 
            lock (_lockObj)
            {
                if (IsAlarmRaised)
                    return false;
 
                if (isOK)
                {
                    ContinuousNGNum = 0;
                }
                else
                {
                    if (AlarmSetting.ContinuousNumThreshold > 0)
                    {
                        ContinuousNGNum++;
                    }
 
                    if (AlarmSetting.TimePeriodNumThresold > 0)
                    {
                        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);
                    if (timeInMinute <= AlarmSetting.TimePeriod)
                    {
                        isAlarmRasied = true;
                        alarmMsg += $"{DefectName}{timeInMinute}分钟内NG{NGRecords.Count}个 ";
                        alarmType = AlarmSetting.TimePeriodAlarmType;
 
                        _timeAlarm = true;
                    }
                }
 
                if (ContinuousNGNum >= AlarmSetting.ContinuousNumThreshold)
                {
                    isAlarmRasied = true;
                    alarmMsg += $"{DefectName}连续NG{ContinuousNGNum}个 ";
                    alarmType = AlarmSetting.ContinuousAlarmType;
 
                    _isContinuousAlarm = true;
                }
 
                IsAlarmRaised = isAlarmRasied;
 
                return isAlarmRasied;
            }
        }
 
        public void ResetAlarm()
        {
            string msg = "";
            lock (_lockObj)
            {
                IsAlarmRaised = false;
 
                if (_isContinuousAlarm)
                {
                    ContinuousNGNum = 0;
                    _isContinuousAlarm = false;
 
                    msg += "连续NG报警 ";
                }
 
                if (_timeAlarm)
                {
                    NGRecords.Clear();
                    _timeAlarm = false;
 
                    msg += "时段内NG报警 ";
                }
            }
 
            CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{DefectName}{msg}已重置");
        }
    }
}