jace.tang
2022-06-17 9bc010fc2b51ff02d1f7d09accc2847e3d925777
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
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms.VisualStyles;
using HalconDotNet;
using System.Data;
using System.Data.SqlClient;
 
using DownloadDLL;
using System.Reflection;
 
namespace M423project
{
 
 
    //数据上传
    public class DetectionDataUpload
    {
        #region 数据上传相关私有成员
        private DetectionData detectionData;
        private Thread uploadDataProcesser = null;
        //private RunCard runCard;
        private ConfigStruct config;
        #endregion
        /// <summary>
        /// 上料检类构造函数
        /// </summary>
 
        public DetectionDataUpload(DetectionData _detectionData, ConfigStruct _config)
        {
            detectionData = _detectionData;
            config = _config;
            //runCard = new RunCard();
        }
 
        /// <summary>
        /// 启动检测线程
        /// </summary>
        public void Start()
        {
            if (uploadDataProcesser == null)
            {
                uploadDataProcesser = new Thread(Execute);
                uploadDataProcesser.IsBackground = true;
                uploadDataProcesser.Start();
            }
        }
 
        public void Stop()
        {
            //if (uploadDataProcesser != null && uploadDataProcesser.IsAlive)
            //{
            //    uploadDataProcesser.Abort();
            //    uploadDataProcesser.Join();
            //}
            //uploadDataProcesser = null;
        }
 
        //static object uploadLock = new object();
        public void UploadData()
        {
            lock (CommonUtil.UploadDataLock)
            {
                if (!CommonUtil.UploadDataEnable)
                    return;
 
                DataTable dtUpload = detectionData.QueryUnloadData();
                if (dtUpload.Rows.Count <= 0)
                    return;
 
                if (dtUpload.Rows.Count < 20)
                {
                    Thread.Sleep(2000);
                    dtUpload = detectionData.QueryUnloadData();
                }
 
                CommonUtil.UploadDoneHandle.Reset();
 
                //    Type runCardClass = asm.GetType("AutoRunCard.RunCard");
                //    object runCardObj = Activator.CreateInstance(runCardClass);
 
                //    MethodInfo mi = runCardClass.GetMethod("Data_Collection_Auto", new Type[] {
                //typeof(System.String), typeof(System.String), typeof(System.DateTime), typeof(System.String), typeof(System.String), typeof(System.String),
                //typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String[]),
                //typeof(System.String).MakeByRefType()});
 
 
                CommonUtil.WriteLog(LogType.Inf, string.Format("开始上传检测记录..."));
                string productNo, productStatus, LengthStatus, WidthStatus, HeightStatus, BarcodeStatus, testResult;
                double productLength = 0.0, productWidth = 0.0, productHeight = 0.0;
                DateTime detectTime;
                int plateID, uploadCount = 0;
                string retStr = string.Empty;
                string[] arryParams = new string[4];
                string autoidList = string.Empty;
                int autoID = 0;
                string carrierBarcode = "";
                int isSample = 0;
 
                foreach (DataRow dr in dtUpload.Rows)
                {
                    isSample = dr.IsNull("IsSample") ? 0 : (int)dr["IsSample"];
                    autoID = dr.IsNull("AutoID") ? 0 : (int)dr["AutoID"];
                    plateID = dr.IsNull("PlateID") ? 0 : (int)dr["PlateID"];
                    productNo = dr.IsNull("ProductNo") ? "" : (string)dr["ProductNo"];
                    detectTime = dr.IsNull("DetectTime") ? DateTime.Now : (DateTime)dr["DetectTime"];
                    //
                    productStatus = dr.IsNull("ProductStatus") ? "NG" : (string)dr["ProductStatus"];
                    LengthStatus = dr.IsNull("ProductLengthStatus") ? "NG" : (string)dr["ProductLengthStatus"];
                    WidthStatus = dr.IsNull("ProductWidthStatus") ? "NG" : (string)dr["ProductWidthStatus"];
                    HeightStatus = dr.IsNull("ProductHeightStatus") ? "NG" : (string)dr["ProductHeightStatus"];
                    BarcodeStatus = dr.IsNull("ProductNoStatus") ? "NG" : (string)dr["ProductNoStatus"];
                    carrierBarcode = dr.IsNull("CarrierBarcode") ? "NA" : (string)dr["CarrierBarcode"];
 
                    testResult = "";
                    if (productStatus != "OK")
                    {
                        if (BarcodeStatus != "OK")
                            testResult += "Barcode-NG,";
                        if (LengthStatus != "OK")
                            testResult += "X-NG,";
                        if (WidthStatus != "OK")
                            testResult += "Y-NG,";
                        if (HeightStatus != "OK")
                            testResult += "Z-NG,";
 
                        if (testResult != "")
                            testResult = testResult.Substring(0, testResult.Length - 1);
                    }
 
                    //
                    productLength = dr.IsNull("ProductLength") ? 0.0 : (double)dr["ProductLength"];
                    productWidth = dr.IsNull("ProductWidth") ? 0.0 : (double)dr["ProductWidth"];
                    productHeight = dr.IsNull("ProductHeight") ? 0.0 : (double)dr["ProductHeight"];
 
                    //第一个参数设置为抽检标志
                    arryParams[0] = isSample.ToString();
                    arryParams[1] = productLength.ToString();
                    arryParams[2] = productWidth.ToString();
                    arryParams[3] = productHeight.ToString();
                    object[] paramArr = new object[] { "DA06", "APMT", detectTime, testResult, plateID.ToString(), productStatus, carrierBarcode, productNo, productNo, "", "", arryParams, retStr };
                    try
                    {
                        if ((bool)mi.Invoke(runCardObj, paramArr))
                        {
                            CommonUtil.WriteLog(LogType.Err, string.Format("数据上传成功:{0};TestResult为:{1}", productNo, string.IsNullOrEmpty(testResult) ? "OK" : testResult));
                            autoidList += autoID.ToString() + ",";
                            uploadCount++;
                        }
                        else
                        {
                            CommonUtil.WriteLog(LogType.Err, string.Format("数据上传失败:{0};TestResult为:{1};原因:{2}", productNo, string.IsNullOrEmpty(testResult) ? "OK" : testResult, (string)paramArr[12]));
                            autoidList += autoID.ToString() + ",";
                            Thread.Sleep(50);
                        }
                        Thread.Sleep(20);
                    }
                    catch (Exception e)
                    {
                        autoidList += autoID.ToString() + ",";
                        CommonUtil.WriteLog(LogType.Inf, string.Format("处理数据上传过程中发生异常:{0}", e.Message + " 条码号:" + productNo));
                        continue;
                    }
 
                    int n = detectionData.SetUploadDataStatus(autoID.ToString());
                    CommonUtil.WriteLog(LogType.Inf, string.Format("已标记成功上传检测记录:{0}笔", n));
 
                }
                //if (autoidList != "")
                //{
                //    int n = detectionData.SetUploadDataStatus(autoidList);
                //    CommonUtil.WriteLog(LogType.Inf, string.Format("已标记成功上传检测记录:{0}笔", n));
                //}
 
                CommonUtil.WriteLog(LogType.Inf, string.Format("检测记录上传结束,共有{0}笔待上传数据,成功上传{1}笔", dtUpload.Rows.Count, uploadCount));
                CommonUtil.UploadDoneHandle.Set();
            }
        }
 
        public void DownLoadDll()
        {
            try
            {
                string strErrorMessage;
                Sync sync = new Sync();
                if (sync.SyncDll(out strErrorMessage) == false)
                {
                    CommonUtil.WriteLog(LogType.Inf, string.Format("下载程序集返回错误信息:{0}", strErrorMessage));
                }
                else
                {
                    CommonUtil.WriteLog(LogType.Inf, string.Format("已完成dll下载"));
                }
            }
            catch (Exception e)
            {
                CommonUtil.WriteLog(LogType.Inf, string.Format("下载程序集出现异常:{0}", e.Message));
            }
        }
 
        public Assembly LoadAutoRunCardAssemble()
        {
 
            Assembly asm = null;
            try
            {
                asm = Assembly.LoadFile(@"C:\MES_API\AutoRunCard.dll");
                return asm;
            }
            catch (Exception e)
            {
                CommonUtil.WriteLog(LogType.Inf, string.Format("加载程序集AutoRunCard.dll出现异常:{0}", e.Message));
                return null;
            }
        }
 
        Assembly asm = null;
        object runCardObj = null;
        MethodInfo mi = null;
 
        public void Execute()
        {
            //Assembly asm = null;
            try
            {
                DownLoadDll();
 
                asm = LoadAutoRunCardAssemble();
                if (asm == null)
                    return;
 
                Type runCardClass = asm.GetType("AutoRunCard.RunCard");
                runCardObj = Activator.CreateInstance(runCardClass);
 
                mi = runCardClass.GetMethod("Data_Collection_Auto", new Type[] {
            typeof(System.String), typeof(System.String), typeof(System.DateTime), typeof(System.String), typeof(System.String), typeof(System.String),
            typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String), typeof(System.String[]),
            typeof(System.String).MakeByRefType()});
 
                int times = 1200 * 5;
                while (uploadDataProcesser.IsAlive)
                {
                    //if (!CommonUtil.UploadDataEnable)
                    //{
                    //    Thread.Sleep(20);
                    //    continue;
                    //}
 
                    //UploadData(asm);
                    UploadData();
 
                    while (times > 0 && uploadDataProcesser.IsAlive)
                    {
                        Thread.Sleep(10);
                        times--;
                    }
                    times = 1200 * 5;
                }
 
            }
            catch (ThreadAbortException)
            {
                ;
            }
            catch (Exception e)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("上传数据过程发生异常:{0}", e.Message));
                Thread.Sleep(100);
            }
            finally
            {
 
            }
 
        }
    }
}