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; //private RunCard runCard; private ConfigStruct config; #endregion /// /// 上料检类构造函数 /// public DetectionDataUpload(DetectionData _detectionData, ConfigStruct _config) { detectionData = _detectionData; config = _config; //runCard = new RunCard(); } /// /// 启动检测线程 /// public void Start() { uploadDataProcesser = new Thread(Execute); 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 (uploadLock) { DataTable dtUpload = detectionData.QueryUnloadData(); if (dtUpload.Rows.Count <= 0) return; if (dtUpload.Rows.Count < 20) { Thread.Sleep(2000); dtUpload = detectionData.QueryUnloadData(); } // 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)); } } 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 { } } } }