using Casic.Birmm.RbFreqStandMeasure.info; using Casic.Birmm.RbFreqStandMeasure.Properties; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Model; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service.Impl; using Casic.Birmm.RbFreqStandMeasure.Tools; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.IO.Ports; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Windows.Forms; namespace Casic.Birmm.RbFreqStandMeasure.tools { public class DetectionHelper { private readonly object obj = new object(); private bool isUsing = false; public static DetectionHelper detectionHelper; public List<Detection> detections; private bool isUpdate = false; public long detectionItemId = -1; private System.Threading.Timer timerAccuracy; delegate void TimerDelegate(string text); private long devIdAccuracy = -1; private SerialPort portAccuracy; private System.Threading.Timer timerStability1; private System.Threading.Timer timerStability10; private System.Threading.Timer timerStability20; private System.Threading.Timer timerStability100; private long devIdStability = -1; private SerialPort portStability; private List<string> resultStability1 = new List<string>(); private List<string> resultStability10 = new List<string>(); private List<string> resultStability20 = new List<string>(); private List<string> resultStability100 = new List<string>(); private System.Threading.Timer timerBootFeature; private long devIdBootFeature = -1; private SerialPort portBootFeature; private List<string> resultBootFeature = new List<string>(); private System.Threading.Timer timerAgeRate; private long devIdAgeRate = -1; private SerialPort portAgeRate; private List<string> resultAgeRate = new List<string>(); private DetectionService detectionService = new DetectionServiceImpl(); private DetectionItemService detectionItemService = new DetectionItemServiceImpl(); private DeviceService deviceService = new DeviceServiceImpl(); //1-STABILITY,2-ACCURACY,3-BOOT_FEATURE,4-AGE_RATE public DetectionHelper() { detectionHelper = this; } #region 稳定度 public void detecStability(long deviceId, string startTime, string interval, SerialPort port,bool isNow, bool isUpdates) { resultStability1 = new List<string>(); resultStability10 = new List<string>(); resultStability20 = new List<string>(); resultStability100 = new List<string>(); if (isUpdates) { isUpdate = true; } portStability = port; devIdStability = deviceId; int delay = 0; if (Convert.ToDateTime(startTime) > DateTime.Now) { TimeSpan secondSpan = new TimeSpan(Convert.ToDateTime(startTime).Ticks - DateTime.Now.Ticks); delay = secondSpan.Milliseconds; } string[] arr = interval.Split(','); foreach (string inter in arr) { if (inter.Equals("1")) timerStability1 = new System.Threading.Timer(exeStability1, null, delay + 100, 1000); if (inter.Equals("10")) timerStability10 = new System.Threading.Timer(exeStability10, null, delay + 100, 10 * 1000); if (inter.Equals("20")) timerStability20 = new System.Threading.Timer(exeStability20, null, delay + 100, 20 * 1000); if (inter.Equals("100")) timerStability100 = new System.Threading.Timer(exeStability100, null, delay + 100, 100 * 1000); } } private void exeStability1(Object State) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "获取稳定度1s"); try { lock (obj) { lock(resultStability1) { string result = ""; string fre = getFrequencyData(portStability); if (!fre.Equals("") && resultStability1.Count<101) { resultStability1.Add(fre); if (!isUpdate) detectionService.add(devIdStability, fre, "1-1"); } if (!isUpdate) { if (resultStability1.Count == 101) { timerStability1.Dispose(); result = FreMath.Stable(resultStability1) + ""; resultStability1.Clear(); LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "1s稳定度结果:" + result + ", devId =" + devIdStability); // 更新检测结果 detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", result, "", "", ""); LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "detectionId :" + detectionItemId); DetectionItem detectionItem = detectionItemService.searchById(detectionItemId); if (detectionItem != null) { //更新稳定度检测状态 if (!detectionItem.Stability1.Equals("-1") && !detectionItem.Stability10.Equals("-1") && !detectionItem.Stability20.Equals("-1") && !detectionItem.Stability100.Equals("-1")) { detectionItemService.updateDetecStatus(detectionItemId, "0", "", "", "", "", "", "", "", ""); } detectionItem = detectionItemService.searchById(detectionItemId); //更新整个仪器检测状态 if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1")) { deviceService.updateStatus(devIdStability, "3", ""); portStability.Close(); } HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); } } } else { if (resultStability1.Count == 101) { timerStability1.Dispose(); string result1 = FreMath.Stable(resultStability1) + ""; detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", result1, "", "", ""); updateView(resultStability1); stability.label_result.Text = result1; resultStability1.Clear(); } } } } } catch (Exception ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "稳定度检测错误:" + ex.Message); } } private void exeStability10(Object State) { lock (obj) { string result = ""; string fre = getFrequencyData(portStability); if (!fre.Equals("") && resultStability10.Count < 101) { resultStability10.Add(fre); if (!isUpdate) detectionService.add(devIdStability, fre, "1-10"); } LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "稳定度10s fre=" + fre + ", devId =" + devIdStability); if (!isUpdate) { if (resultStability10.Count == 101) { timerStability10.Dispose(); result = FreMath.Stable(resultStability10) + ""; LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "10s稳定度检测结果:" + result +", devId =" + devIdStability); detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", "", result, "", ""); DetectionItem detectionItem = detectionItemService.searchById(detectionItemId); if (detectionItem != null) { //更新稳定度检测状态 if (!detectionItem.Stability1.Equals("-1") && !detectionItem.Stability10.Equals("-1") && !detectionItem.Stability20.Equals("-1") && !detectionItem.Stability100.Equals("-1")) { detectionItemService.updateDetecStatus(detectionItemId, "0", "", "", "", "", "", "", "", ""); } detectionItem = detectionItemService.searchById(detectionItemId); //更新整个仪器检测状态 if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1")) { deviceService.updateStatus(devIdStability, "3", ""); portStability.Close(); } HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); } } } else { if (resultStability10.Count == 101) { timerStability10.Dispose(); string result1 = FreMath.Stable(resultStability10) + ""; detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", "", result1, "", ""); updateView(resultStability10); stability.label_result.Text = result1; } } } } private void exeStability20(Object State) { lock (obj) { string result = ""; string fre = getFrequencyData(portStability); if (!fre.Equals("") && resultStability20.Count < 101) { resultStability20.Add(fre); if (!isUpdate) detectionService.add(devIdStability, fre, "1-20"); } LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "稳定度20s fre=" + fre + ", devId =" + devIdStability); if (!isUpdate) { if (resultStability20.Count == 101) { timerStability20.Dispose(); result = FreMath.Stable(resultStability20) + ""; LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "20s稳定度检测结果:" + result + ", devId =" + devIdStability); detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", "", "", result, ""); DetectionItem detectionItem = detectionItemService.searchById(detectionItemId); if (detectionItem != null) { //更新稳定度检测状态 if (!detectionItem.Stability1.Equals("-1") && !detectionItem.Stability10.Equals("-1") && !detectionItem.Stability20.Equals("-1") && !detectionItem.Stability100.Equals("-1")) { detectionItemService.updateDetecStatus(detectionItemId, "0", "", "", "", "", "", "", "", ""); } detectionItem = detectionItemService.searchById(detectionItemId); //更新整个仪器检测状态 if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1")) { deviceService.updateStatus(devIdStability, "3", ""); portStability.Close(); } HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); } resultStability20.Clear(); } } else { if (resultStability20.Count == 101) { timerStability20.Dispose(); string result1 = FreMath.Stable(resultStability20) + ""; detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", "", "", result1, ""); updateView(resultStability20); stability.label_result.Text = result1; resultStability20.Clear(); } } } } private void exeStability100(Object State) { lock (obj) { string result = ""; string fre = getFrequencyData(portStability); if (!fre.Equals("") && resultStability100.Count < 101) { resultStability100.Add(fre); if (!isUpdate) detectionService.add(devIdStability, fre, "1-100"); } LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "稳定度100s fre=" + fre + ", devId =" + devIdStability); if (!isUpdate) { if (resultStability100.Count == 101) { timerStability100.Dispose(); result = FreMath.Stable(resultStability100) + ""; LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "100s稳定度检测结果:" + result + ", devId =" + devIdStability); detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", "", "", "", result); DetectionItem detectionItem = detectionItemService.searchById(detectionItemId); if (detectionItem != null) { //更新稳定度检测状态 if (!detectionItem.Stability1.Equals("-1") && !detectionItem.Stability10.Equals("-1") && !detectionItem.Stability20.Equals("-1") && !detectionItem.Stability100.Equals("-1")) { detectionItemService.updateDetecStatus(detectionItemId, "0", "", "", "", "", "", "", "", ""); } detectionItem = detectionItemService.searchById(detectionItemId); //更新整个仪器检测状态 if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1")) { deviceService.updateStatus(devIdStability, "3", ""); portStability.Close(); } HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); } resultStability100.Clear(); } } else { if (resultStability100.Count == 101) { timerStability100.Dispose(); string result1 = FreMath.Stable(resultStability100) + ""; detectionItemService.updateDetecStatus(detectionItemId, "", "", "", "", "", "", "", "", result1); updateView(resultStability100); stability.label_result.Text = result1; resultStability100.Clear(); } } } } #endregion #region 准确度 public void detecAccuracy(long deviceId, SerialPort port, string endTime, bool isStartNow, bool isUpdates) { portAccuracy = port; devIdAccuracy = deviceId; if (isUpdates) { isUpdate = true; LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重新检测准确度"); } int delay = 0; if (Convert.ToDateTime(endTime)>=DateTime.Now) { TimeSpan secondSpan = new TimeSpan(Convert.ToDateTime(endTime).Ticks - DateTime.Now.Ticks); delay = secondSpan.Milliseconds; } timerAccuracy = new System.Threading.Timer(exeAccuracy, null, delay + 200 ,5*60*1000); } private void exeAccuracy(Object State) { try { lock (obj) { if(isUpdate) LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重新检测准确度!!!!!!!开始"); string result = ""; double sum = 0.0; List<string> resultList = new List<string>(); for (int i = 0; i < 50; i++) { string fre = getFrequencyData(portAccuracy); resultList.Add(fre); if (fre.Equals("")) continue; if (!isUpdate) { detectionService.add(devIdAccuracy, fre, "2"); } if (resultList.Count == 3) { sum = sum + Convert.ToDouble(fre); break; } Thread.Sleep(500); } result = sum / 3 + ""; timerAccuracy.Dispose(); while (true) { if (detectionItemId != -1) { detectionItemService.updateDetecStatus(detectionItemId, "", result, "", "", "", "", "", "", ""); break; } } if (!isUpdate) { deviceService.updateStatus(devIdAccuracy, "3", ""); HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); } else { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "更新label_result:" + result); accuracy.label_result.Text = result; updateView(resultList); } portAccuracy.Close(); } } catch (Exception ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "计算准确度出错:"+ ex.Message); } } #endregion #region 开机特性 public void detecBootFeature(long deviceId, string startTime, SerialPort port, bool isStartNow) { resultBootFeature = new List<string>(); portBootFeature = port; devIdBootFeature = deviceId; int delay = 0; if (!isStartNow && Convert.ToDateTime(startTime) >= DateTime.Now) { TimeSpan secondSpan = new TimeSpan(Convert.ToDateTime(startTime).Ticks - DateTime.Now.Ticks); delay = secondSpan.Milliseconds; } timerBootFeature = new System.Threading.Timer(exeBootFeature, null, delay + 100, 60*60*1000); } private void exeBootFeature(Object State) { lock (obj) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "开始检测开机特性"); string result = ""; double sum = 0.0; int count = 0; for (int i = 0; i < 50; i++) { string fre = getFrequencyData(portBootFeature); if (!fre.Equals("")) { count++; sum = sum + Convert.ToDouble(fre); if (count == 3) break; } } detectionService.add(devIdBootFeature, sum / 3 + "", "3"); resultBootFeature.Add(sum / 3 + ""); if (resultBootFeature.Count == 8) { timerBootFeature.Dispose(); string max = "0.0"; string min = "0.0"; foreach (string fre in resultBootFeature) { if (Convert.ToDouble(fre) > Convert.ToDouble(max)) max = fre; if (Convert.ToDouble(fre) < Convert.ToDouble(min) || min.Equals("0.0")) min = fre; } result = (Convert.ToDouble(max) - Convert.ToDouble(min)) + ""; detectionItemService.updateDetecStatus(detectionItemId, "", "", result, "", "", "", "", "", ""); DetectionItem detectionItem = detectionItemService.searchById(detectionItemId); if (detectionItem != null) { if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1")) { deviceService.updateStatus(devIdBootFeature, "3", ""); portBootFeature.Close(); } } HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); resultBootFeature.Clear(); } } } #endregion #region 日老化率 public void detecAgeRate(long deviceId, string startTime, SerialPort port, bool isStartNow) { resultAgeRate = new List<string>(); portAgeRate = port; devIdAgeRate = deviceId; int delay = 0; if (!isStartNow && Convert.ToDateTime(startTime) > DateTime.Now) { TimeSpan secondSpan = new TimeSpan(Convert.ToDateTime(startTime).Ticks - DateTime.Now.Ticks); delay = secondSpan.Milliseconds; } timerAgeRate = new System.Threading.Timer(exeAgeRate, null, delay + 100, 12* 60* 60 * 1000); } private void exeAgeRate(Object State) { lock (obj) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "开始检测日老化率"); string result = ""; double sum = 0.0; int count = 0; for (int i = 0; i < 50; i++) { string fre = getFrequencyData(portAgeRate); if (!fre.Equals("")) { count++; sum = sum + Convert.ToDouble(fre); if (count == 3) break; } } detectionService.add(devIdAgeRate, sum / 3+"", "4"); resultAgeRate.Add(sum / 3 + ""); if (resultAgeRate.Count == 15) { timerAgeRate.Dispose(); double r = 0.0; double k = FreMath.DriftRate(resultAgeRate, out r); result = k + "," + r.ToString().Substring(0,5); LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "日老化率检测结果:" + result); detectionItemService.updateDetecStatus(detectionItemId, "", "", "", result, "", "", "", "", ""); DetectionItem detectionItem = detectionItemService.searchById(detectionItemId); if (detectionItem != null) { if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1")) { deviceService.updateStatus(devIdAgeRate, "3", ""); portAgeRate.Close(); } } HomeCtrlForm.homeCtrlForm.LoadChannelStatus(); resultAgeRate.Clear(); } } } #endregion // 获取检测数据 public string getFrequencyData(SerialPort port) { string received = ""; try { while (true) { if (!isUsing) { isUsing = true; if (port.IsOpen) LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "portname:" + port.PortName); else LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, port.PortName + " port closed"); if (ConfigHelper.GetAppConfig("deviceType").ToString().Equals("1")) { int count = 0; while (count < 5000) { Thread.Sleep(100); int readLen = port.BytesToRead; if (readLen > 0) { byte[] buffer = new byte[readLen]; port.Read(buffer, 0, readLen);// 接收数据到buffer里面 string data = Encoding.ASCII.GetString(buffer); string[] dataArray = data.Split(' '); if (data.StartsWith("$") && dataArray.Length == 2 && data.LastIndexOf('$') == 0) { if (received.Contains("-")) { received = (Convert.ToDouble(dataArray[0].Replace("$", "")) - Convert.ToDouble("0." + data.Split(' ')[1].Replace(".", "").Replace("-", "").TrimEnd('0'))) + ""; } else received = dataArray[0].Replace("$", "") + "." + data.Split(' ')[1].Replace(".", "").TrimEnd('0'); received = ((Convert.ToDouble(received) - 10000000) / 10000000) + ""; break; } } count++; } } else { int count = 0; while (count < 5000) { Thread.Sleep(100); int readLen = port.BytesToRead; if (readLen > 0) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, port.PortName + "2 readLen="+ readLen); byte[] buffer = new byte[readLen]; port.Read(buffer, 0, readLen);// 接收数据到buffer里面 string data = Encoding.ASCII.GetString(buffer); string[] dataArray = data.Split(' '); if (dataArray.Length >= 3) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, port.PortName + "2 data =" + data); foreach (string sss in dataArray) { if (sss.Contains("E")) { received = sss; break; } } if (!received.Equals("")) break; } } count++; } } if (received.Equals("")) { string name = ConfigHelper.getKeyByValue(port.PortName + " 115200").Replace("channel", "通道"); LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, name + "获取数据失败!" + DateTime.Now.ToLongTimeString()); } break; } } } catch (Exception ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "错误:" + ex.Message); return ""; } finally { isUsing = false; } return received; } private void updateView(List<string> result) { isUpdate = false; new Thread(() => { int index = 0; LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "DetailDlg.dataGridView.Rows:" + DetailDlg.dataGridView.Rows); foreach (DataGridViewRow row in DetailDlg.dataGridView.Rows) { if (index < result.Count) { row.Cells[2].Value = result[index].ToString(); detectionService.updateFrequency(Convert.ToInt64(row.Cells[3].Value), result[index].ToString()); } index++; } DetailDlg.label_redetecting.Visible = false; }).Start(); } } }