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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
 
namespace Bro.M141.Process
{
    public partial class M141Process
    {
        public InspectionConfig InspectionConfig { get; set; } = new InspectionConfig();
 
        private bool isInspectionDoing = false;
 
        volatile bool _isSpotCheckValid = true;
 
        public bool FistStart = true;
        const string SPOTALARMMSG = $"点检超时,请执行点检";
        List<InspectionShift> _alertInspectionShifts = new List<InspectionShift>();
        public bool IsInspectionDoing
        {
            get => isInspectionDoing;
            set
            {
                if (isInspectionDoing != value)
                {
                    isInspectionDoing = value;
 
                    if (!isInspectionDoing)
                    {
                        _alertInspectionShifts.Clear();
                    }
                }
            }
        }
 
 
        public bool SaveInspectionConfig(out string error)
        {
            error = "";
            try
            {
                using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Inspection.json"), false, System.Text.Encoding.UTF8))
                {
                    sw.Write(JsonConvert.SerializeObject(InspectionConfig));
                }
            }
            catch (Exception ex)
            {
                error = ex.GetExceptionMessage();
                return false;
            }
            return true;
        }
 
        volatile int _accumulativeSpotCheckTime = 0;
 
        private bool CheckInspectionInPeriodic(int heartBeatInterval)
        {
            //lock (_spotCheckLock)
            {
                if (FistStart)
                {
                    string StartPoint = ConfigurationManager.AppSettings["StartPoint"];
 
                    if (string.IsNullOrEmpty(StartPoint) || StartPoint.ToUpper().Equals("TRUE"))
                    {
 
                    }
                    else
                    {
                        FistStart = false;
                    }
                }
 
                if (_accumulativeSpotCheckTime == 0 || Interlocked.Add(ref _accumulativeSpotCheckTime, heartBeatInterval) >= 10000)
                {
                    Interlocked.Exchange(ref _accumulativeSpotCheckTime, 1);
                    CheckInspectionInPeriodicAsync();
                }
            }
 
            return _isSpotCheckValid;
        }
 
        public async void CheckInspectionInPeriodicAsync()
        {
            await Task.Run(() =>
            {
                DateTime dt = DateTime.Now;
                bool tempJudge = true;
                RaisedAlarm(SPOTALARMMSG, false);
                InspectionConfig.InspectionShifts.ForEach(s =>
                {
                    if (CheckInTimePeriod(s, dt))
                    {
                        if (!CheckInTimePeriod(s, InspectionConfig.LastInspectTime))
                        {
                            tempJudge = false;
                            AlertInspection(s);
                        }
                        else if (FistStart && tempJudge)
                        {
                            tempJudge = false;
                            AlertInspection(s);
                        }
                    }
                });
                _isSpotCheckValid = tempJudge;
 
            });
        }
 
        static object _alertInspectionShiftLock = new object();
 
        private void AlertInspection(InspectionShift s)
        {
            lock (_alertInspectionShiftLock)
            {
                if (!_alertInspectionShifts.Any(u => u.Id == s.Id))
                {
                    _alertInspectionShifts.Add(s);
 
                    Task.Run(() =>
                    {
                        if (!IsInspectionDoing)
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{s.ShiftName}{SPOTALARMMSG}");
 
                            RaisedAlarm(SPOTALARMMSG, true, WarningLevel.Hint);
                            MessageBox.Show($"{s.ShiftName}{SPOTALARMMSG}");
 
                            _alertInspectionShifts.RemoveAll(u => u.Id == s.Id);
                        }
                    });
                }
            }
        }
 
        private static bool CheckInTimePeriod(InspectionShift u, DateTime? dt)
        {
            if (dt == null)
                return false;
 
            var startTime = dt.Value.Date.Add(u.ShiftTime_Start.TimeOfDay);
            var endTime = startTime.AddHours(u.ValidHours);
 
            var isInTimes = dt >= startTime && dt < endTime;
            if (!isInTimes)
            {
                startTime = startTime.AddDays(-1);
                endTime = startTime.AddHours(u.ValidHours);
 
                isInTimes = dt >= startTime && dt < endTime;
            }
 
            return isInTimes;
        }
 
 
        public void InitialInspectionConfig()
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Inspection.json");
            if (File.Exists(filePath))
            {
                try
                {
                    using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8))
                    {
                        var dataStr = reader.ReadToEnd();
 
                        InspectionConfig = JsonConvert.DeserializeObject<InspectionConfig>(dataStr);
                        if (InspectionConfig == null)
                        {
                            InspectionConfig = new InspectionConfig();
                        }
                    }
                }
                catch (Exception ex)
                {
                    InspectionConfig = new InspectionConfig();
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"点检配置信息读取失败,初始化点检配置");
                }
            }
        }
 
 
 
 
    }
 
    public class InspectionConfig
    {
        [Browsable(false)]
        public List<DefectPositionSet> CheckDefectNames { get; set; } = new List<DefectPositionSet>();
 
        [Browsable(false)]
        public DateTime? LastInspectTime { get; set; } = null;
 
 
        [Browsable(false)]
        public string Excelpath { get; set; } = "";
 
        [ResCategory("点检时间")]
        [ResDescription("设置每次点检的开始时间")]
        [ResDisplayName("点检设置集合")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(ComplexCollectionEditor<InspectionShift>), typeof(UITypeEditor))]
        public List<InspectionShift> InspectionShifts { get; set; } = new List<InspectionShift>();
 
        [Category("尺寸点检")]
        [Description("尺寸点检误差百分比")]
        [DisplayName("尺寸点检误差百分比")]
 
        public int Check { get; set; }
    }
 
    public class InspectionShift : IComplexDisplay
    {
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [ResCategory("点检设置")]
        [ResDescription("点检名称设置")]
        [ResDisplayName("\t\t点检名称")]
        public string ShiftName { get; set; }
 
        [ResCategory("点检设置")]
        [ResDescription("点检开始时间设置")]
        [ResDisplayName("\t点检开始时间")]
        [TypeConverter(typeof(ShortTimeHHmmConverter))]
        [Editor(typeof(TimePickerEditor), typeof(UITypeEditor))]
        public DateTime ShiftTime_Start { get; set; }
 
        [ResCategory("点检设置")]
        [ResDescription("点检有效覆盖时间,单位hr")]
        [ResDisplayName("点检有效周期")]
        public int ValidHours { get; set; } = 12;
 
 
        public string GetDisplayText()
        {
            return $"{(string.IsNullOrWhiteSpace(ShiftName) ? "" : $"{ShiftName}:")}{ShiftTime_Start.ToString("HH:mm")}";
        }
    }
 
 
    public class DefectPositionSet
    {
 
        /// <summary>
        /// 工站
        /// </summary>
        public string PositionNum { get; set; } = "";
 
        /// <summary>
        ///  产品序号
        /// </summary>
        public string ProduceIndex { get; set; } = "";
 
        /// <summary>
        /// 测量项code
        /// </summary>
        public string Code { get; set; } = "";
 
        /// <summary>
        /// 标准值
        /// </summary>
        public string StandardValue { get; set; } = "";
 
        /// <summary>
        /// 最大值
        /// </summary>
        public string MaxValue { get; set; } = "";
 
        /// <summary>
        /// 最小值
        /// </summary>
        public string MinValue { get; set; } = "";
 
        /// <summary>
        /// excel录入值
        /// </summary>
        public string InsertValue { get; set; } = "";
 
        /// <summary>
        /// 测量值
        /// </summary>
        public string TestValue { get; set; } = "";
 
        /// <summary>
        /// 相关性差值
        /// </summary>
        public string Relevance { get; set; } = "";
 
        /// <summary>
        /// 相关性百分比
        /// </summary>
        public string Relevance2 { get; set; } = "";
 
        /// <summary>
        /// 是否为尺寸测量,true尺寸,false,外观
        /// </summary>
        public bool Ismeasure { get; set; } = false;
 
        /// <summary>
        /// 点检时间
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public DateTime? CheckTime { get; set; } = null;
 
 
        /// <summary>
        /// 点检结果
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool result { get; set; } = false;
 
    }
}