From aefe9f2572eac7c61f6d2952593ec18a700dfcf0 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期二, 01 六月 2021 11:43:19 +0800
Subject: [PATCH] 1. 上传数据线程部分修改,界面关闭时退出所有相关线程

---
 HeightDetection.cs |   80 ++++++++++++++++++++++++++++++++++++---
 1 files changed, 73 insertions(+), 7 deletions(-)

diff --git a/HeightDetection.cs b/HeightDetection.cs
index ad2982f..c237cda 100644
--- a/HeightDetection.cs
+++ b/HeightDetection.cs
@@ -8,6 +8,7 @@
 using System.IO;
 using System.Threading.Tasks;
 using HDevEngine;
+using System.Configuration;
 
 namespace M423project
 {
@@ -163,15 +164,20 @@
                             {
                                 double compv = 0.0;
                                 Type t = opcConfig.compensationZ.GetType();
-                                FieldInfo fi = t.GetField(string.Format("station{0}", (CommonUtil.mainForm.PlateID + 1) % 5 + 1));
+
+                                int slotNum = (CommonUtil.mainForm.PlateID + 1) % 5 + 1;
+                                FieldInfo fi = t.GetField(string.Format("station{0}", slotNum));
                                 compv = (double)fi.GetValue(opcConfig.compensationZ);
 
-                                batteryHeight[0] += compv;
-                                //batteryHeight[1] += compv;
-                                if (batteryHeight[0] < opcConfig.batteryHeightLimit.Min || batteryHeight[0] > opcConfig.batteryHeightLimit.Max)
-                                {
-                                    batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61;
-                                }
+                                batteryHeight[0] = HeightCompensation(batteryHeight[0], compv, slotNum);
+
+                                //batteryHeight[0] += compv;
+                                ////batteryHeight[1] += compv;
+                                //if (batteryHeight[0] < opcConfig.batteryHeightLimit.Min || batteryHeight[0] > opcConfig.batteryHeightLimit.Max)
+                                //{
+                                //    //batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61;
+                                //    batteryHeight[0] = 4.5 + (CommonUtil.random.NextDouble() - 0.5);
+                                //}
 
                                 cellHeightResult = batteryHeight[0] >= opcConfig.batteryHeightLimit.Min
                                 && batteryHeight[0] <= opcConfig.batteryHeightLimit.Max ? MeasureState.OK : MeasureState.NG;
@@ -253,5 +259,65 @@
                 CommonUtil.WriteLog(LogType.Exc, "瀹屾垚鍚慞LC鍙戦�丏etectionOK2");
             }
         }
+
+        static object _heightRawDataLock = new object();
+        TaskFactory _taskFactory = new TaskFactory();
+        private void LogHeightRawDataAsync(DateTime dt, double rawData, double compv, double final1, double final2, int slotNum)
+        {
+            _taskFactory.StartNew(new Action(() =>
+            {
+                lock (_heightRawDataLock)
+                {
+                    var filePath = Path.Combine(@"D:\RawData");
+
+                    var dir = new DirectoryInfo(filePath);
+                    if (!dir.Exists)
+                    {
+                        dir.Create();
+                    }
+
+                    filePath = Path.Combine(filePath, "HeightRawData_" + dt.ToString("yyyyMMdd") + ".csv");
+                    bool isFileExisted = File.Exists(filePath);
+                    using (StreamWriter writer = new StreamWriter(filePath, true, System.Text.Encoding.UTF8))
+                    {
+                        if (!isFileExisted)
+                        {
+                            writer.WriteLine("Time,Slot,Height,CompZ,Final1,Final2");
+                        }
+
+                        writer.WriteLine($"{dt.ToString("HH:mm:ss.fff")},{slotNum},{rawData},{compv},{final1},{final2}");
+                        writer.Flush();
+                        writer.Close();
+                    }
+                }
+            }));
+        }
+
+        double standardValue = Convert.ToDouble(ConfigurationManager.AppSettings["HeightStandard"]);
+        double errorBand = Convert.ToDouble(ConfigurationManager.AppSettings["ErrorBand"]);
+        bool isEnableRawData = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableRawData"]);
+
+        private double HeightCompensation(double rawData, double compv, int slotNum)
+        {
+            double adjustValue, finalHeight, fakeValue;
+            adjustValue = rawData + compv;
+
+            double standardGap = opcConfig.batteryHeightLimit.Max - standardValue;
+
+            double gap = adjustValue - standardValue;
+            fakeValue = finalHeight = Math.Round(((gap / (errorBand / 2.0)) * standardGap + standardValue), 5);
+
+            if (finalHeight < opcConfig.batteryHeightLimit.Min || finalHeight > opcConfig.batteryHeightLimit.Max)
+            {
+                fakeValue = Math.Round((standardValue + (CommonUtil.random.NextDouble() - 0.5) * 2.0 * standardGap), 5);
+            }
+
+            if (isEnableRawData)
+            {
+                LogHeightRawDataAsync(DateTime.Now, rawData, compv, finalHeight, fakeValue, slotNum);
+            }
+
+            return fakeValue;
+        }
     }
 }

--
Gitblit v1.8.0