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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bro.Process;
 
namespace Bro.M141.Process
{
    public partial class M141Process
    {
 
        static public System.Threading.Timer Timer;
        private void SetTaskAtFixedTime()
        {
            //System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(SaveDataByWorkShift(dateTime)), null, 1*60*1000);
            //Task task = new Task(() =>
            //{
            //    timer
            //});
            //task.Start();
 
            //Task.Factory.StartNew(() =>
            //{
            //    System.Threading.Thread.Sleep(1*60*1000);
            //    SaveDataByWorkShift(dateTime);
            //});
 
            Timer = new System.Threading.Timer(SaveDataByWorkShift, 1, 0, Timeout.Infinite);
        }
 
        public void SaveDataByWorkShift(object obj)
        {
            try
            {
                string dateTime = DateTime.Now.ToString("HH:mm");
                var workShiftSetting = M141Config.WorkShiftList;
                if (workShiftSetting.Count == 0)
                {
                    return;
                }
 
                var curProductAmount = base.StatisticRecordsFull.CurRecord.DefectSummary;
                if (curProductAmount != null)
                {
                    workShiftSetting.ForEach(w =>
                    {
                        //如果当前时间为该班次的起始时间,且需要清除数据
                        if (dateTime == w.ShiftTime_Start.ToString("HH:mm") && w.IsClearProductSummary)
                        {
                            LogAsync(DateTime.Now, Common.Helper.EnumHelper.LogLevel.Action, $"班次{w.ShiftName}开始时进行生产数据清零");
                            curProductAmount.Reset();
                        }
                        //如果当前时间为该班次的结束时间,且需要保存数据
                        else if (dateTime == w.ShiftTime_End.ToString("HH:mm") && w.IsRecordProductSummary)
                        {
                            LogAsync(DateTime.Now, Common.Helper.EnumHelper.LogLevel.Action, $"班次{w.ShiftName}结束时进行生产数据保存");
 
                            string savePath = Path.Combine(M141Config.LogPath, "班次记录");
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
 
                            string fileName = $"{w.ShiftName}_{DateTime.Now.ToString("yyyyMMdd")}_#{w.ShiftTime_Start.ToString("HHmm")}-{w.ShiftTime_End.ToString("HHmm")}#.csv", pHead = "检测项,数量,占比", pData = "";
 
                            curProductAmount.RecordsList.ForEach(e =>
                            {
                                pData += $"{e.ClassDesc},{e.Amount},{e.PercentStr},\n";
                            });
 
                            CSVRecordAsync(Path.Combine(savePath, fileName), pData, pHead);
                        }
                    });
                }
            }
            catch (Exception)
            {
                Timer.Dispose();
            }
            finally
            {
                Timer.Change(1 * 60 * 1000, Timeout.Infinite);
            }
        }
    }
}