patrick.xu
2021-06-01 aefe9f2572eac7c61f6d2952593ec18a700dfcf0
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Threading;
 
namespace M423project
{
    /// <summary>
    /// 日志记录类型
    /// </summary>
    public enum LogType { Err, Inf, Exc}
 
    /// <summary>
    /// 检测作业类型
    /// </summary>
    public enum DetectionType
    {
        Normal = 0, //正常測試
        Repeat = 1, //重複性測試
        Offline, //離線測試
        LastProduct,//最後一盤料測試
        Manual, //手動測試
        BIAS //BIAS
    }
 
    public enum DetectionOption
    { doProduct,doStandardBlock}
 
    /// <summary>
    /// 公用静态类
    /// </summary>
    public static class CommonUtil
    {
        public const int ProductNoLength = 17;
 
        public static StepContol StepControl;
 
        public static Random random;
        /// <summary>
        /// 线程锁对象
        /// </summary>
        private static object lockObj = new object();
        private static object logObj = new object();
 
        //private static bool uploadDataEnable = true;
 
        //public static bool UploadDataEnable
        //{
        //    get
        //    {
        //        lock (lockObj) { return uploadDataEnable; }
        //    }
        //    set
        //    {
        //        lock (lockObj)
        //        {
        //            uploadDataEnable = value;
        //        }
        //    } 
        //}
 
        private static DetectionOption detectionOption = DetectionOption.doProduct;
 
        public static DetectionOption DetectionOption
        {
            get{return detectionOption;}
            set{detectionOption = value;}
        } 
     
        private static string logFile;
 
        public const double InvalidValue = 999.999;
        /// <summary>
        /// 系统状态
        /// </summary>
        public static bool IsRunning { get; set; }
        /// <summary>
        /// 程序运行目录
        /// </summary>
        public static string CurrentDir { get; set; } 
 
        public static string FileSaveDir { get; set; }
 
        /// <summary>
        /// 返回当天图象目录 .\日期\Images
        /// </summary>
        public static string ImageDir { get { return FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\Image"; } }
        public static string ProductNoImageDir { get { return FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\Image\ProductNo"; } }
        public static string ProductHeightImageDir { get { return FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\Image\ProductHeight"; } }
        public static string ProductSizeImageDir { get { return FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\Image\ProductSize"; } }
        public static string ScreenshotsDir { get { return FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\Image\Screenshots"; } }     
        /// <summary>
        /// 返回当天截图目录 .\日期\Screenshots
        /// </summary>
 
        /// <summary>
        /// 返回当天工作目录 .\日期
        /// </summary>
        public static string DayDir { get { return FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd"); } }
 
        public static string DailyReport { get { return @"E:\DailyReport"; } }
 
        /// <summary>
        /// 返回配置文件目录 .\config
        /// </summary>
        public static string ConfigDir { get { return CurrentDir + @"\config"; } }
 
        /// <summary>
        /// 返回模板文件目录 .\model
        /// </summary>
        public static string ModelDir { get { return CurrentDir + @"\Model"; } }
 
        /// <summary>
        /// 主窗体引用
        /// </summary>
        public static MainForm mainForm { get; set; }
 
        public static ManualResetEvent UploadDoneHandle = new ManualResetEvent(true);
        public static object UploadDataLock = new object();
        public static bool UploadDataEnable = true;
 
        static CommonUtil()
        {
            CurrentDir = Directory.GetCurrentDirectory();
            Directory.CreateDirectory(CurrentDir + @"\config");
            Directory.CreateDirectory(CurrentDir + @"\Model");
            DriveInfo di = new DriveInfo("E");
 
            if (di.IsReady && di.DriveType == DriveType.Fixed)
            {
                FileSaveDir = @"E:\DetectFile";
                Directory.CreateDirectory(FileSaveDir);
                Directory.CreateDirectory(@"E:\DailyReport");
                Directory.CreateDirectory(@"E:\CheckReport");
            }
            else
            {
                FileSaveDir = CurrentDir;
                Directory.CreateDirectory(FileSaveDir + @"\DailyReport");
            }
 
            //Directory.CreateDirectory(FileSaveDir + @"\DailyReport");
 
            DateTime D = DateTime.Now;
            DateTime EndDay = D.AddDays(7);
 
            while (D <= EndDay)
            {
                string dayDir = FileSaveDir + @"\" + D.ToString("yyyyMMdd");
                Directory.CreateDirectory(dayDir);
                Directory.CreateDirectory(dayDir + @"\log");
                Directory.CreateDirectory(dayDir + @"\Image");
                Directory.CreateDirectory(dayDir + @"\Image\ProductNo");
                Directory.CreateDirectory(dayDir + @"\Image\ProductHeight");
                Directory.CreateDirectory(dayDir + @"\Image\ProductSize");
                Directory.CreateDirectory(dayDir + @"\Image\Screenshots");
 
                Directory.CreateDirectory(dayDir + @"\Image\ProductNo_NG");
                Directory.CreateDirectory(dayDir + @"\Image\ProductHeight_NG");
                Directory.CreateDirectory(dayDir + @"\Image\ProductSize_NG");
                Directory.CreateDirectory(dayDir + @"\Image\Screenshots_NG");
                D = D.AddDays(1);
            }
 
 
            StepControl = new StepContol();
            random = new Random();
            IsRunning = false;
        }
 
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="_logType"></param>
        /// <param name="_logInfo"></param>
        public static void WriteLog(LogType _logType, string _logInfo)
        {
            MessageHandler.MessageHandlerInstance.NewMessage(_logInfo, true);
            /*
            lock (logObj)
            {
                try
                {
                    string dayDir = FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd");
 
                    logFile = dayDir + @"\log\" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                    using (StreamWriter sw = new StreamWriter(logFile, true))
                    {
                        string infoString = string.Format("{0}[{1}] {2}", DateTime.Now.ToString("HH:mm:ss"), _logType.ToString(), _logInfo);
                        sw.WriteLine(infoString);
                        mainForm.Invoke(new Action(() => { 
                            mainForm.InfoInspect.AppendText(infoString + "\r\n");
                            mainForm.InfoInspect.ScrollToCaret();
                        }));
                        sw.Close();
                    }
                }
                catch (Exception)
                {
                    ;
                }
                
            }*/
        }
 
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="_logType"></param>
        /// <param name="_logInfo"></param>
        public static void WriteLog(string _logInfo)
        {
            lock (logObj)
            {
                try
                {
                    string dayDir = FileSaveDir + @"\" + DateTime.Now.ToString("yyyyMMdd");
 
                    logFile = dayDir + @"\log\" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                    using (StreamWriter sw = new StreamWriter(logFile, true))
                    {
                        string infoString = string.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), _logInfo);
                        sw.WriteLine(infoString);
                      
                        sw.Close();
                    }
                }
                catch (Exception)
                {
                    ;
                }
 
            }
        }
    }
}