From e69865a1a3739740201bac8782b721da35d49923 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期一, 25 一月 2021 18:45:28 +0800
Subject: [PATCH] 1. gocator添加重试和清理缓存操作 2. gts板卡调整监听频率 3. 记录产品数据使用filsstream避免程序崩溃影响

---
 src/Bro.UI.Config/MenuForms/FrmStatistic.Designer.cs |   23 +++++
 src/Bro.Device.GTSCard/GTSCardDriver.cs              |   23 ++++-
 src/Bro.UI.Config/MenuForms/FrmStatistic.cs          |   97 +++++++++++++++++++----
 src/Bro.M071.Process/UI/M071_MainForm.cs             |    2 
 src/Bro.M071.Process/M071Process.cs                  |    2 
 src/Bro.UI.Config/MenuForms/FrmStatistic.resx        |    3 
 src/Bro.Device.Gocator/GocatorDriver.cs              |   56 ++++++++++---
 src/Bro.UI.Config/Properties/AssemblyInfo.cs         |    4 
 src/Bro.Device.Gocator/Properties/AssemblyInfo.cs    |    4 
 src/Bro.Common.Device/Base/DeviceBase.cs             |    1 
 src/Bro.Device.GTSCard/Properties/AssemblyInfo.cs    |    4 
 11 files changed, 176 insertions(+), 43 deletions(-)

diff --git a/src/Bro.Common.Device/Base/DeviceBase.cs b/src/Bro.Common.Device/Base/DeviceBase.cs
index 382f53d..ba49b47 100644
--- a/src/Bro.Common.Device/Base/DeviceBase.cs
+++ b/src/Bro.Common.Device/Base/DeviceBase.cs
@@ -70,6 +70,7 @@
             }
             set
             {
+                LogAsync(DateTime.Now, $"璁惧鐘舵�佸垏鎹細{_currentState.ToString()}->{value.ToString()}", "");
                 _currentState = value;
 
                 if (value != EnumHelper.DeviceState.TBD)
diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index 25ec672..a1c7aa6 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -312,7 +312,6 @@
             }
         }
 
-
         public override bool AllAxisOn()
         {
             List<Task<bool>> taskList = new List<Task<bool>>(); ;
@@ -387,7 +386,7 @@
                 responseMessage.Result = resultList.All(u => u == true);
                 if (!responseMessage.Result)
                 {
-                    responseMessage.Message = $"鐐逛綅杩愬姩寮傚父";
+                    responseMessage.Message = $"鐐逛綅杩愬姩寮傚父,杩愬姩缁撴灉锛歿string.Join(" ", resultList.Select(u => u ? "1" : "0"))}";
                 }
             }
             return responseMessage;
@@ -770,7 +769,14 @@
                     repeatTime--;
                 }
 
-                return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+                if (repeatTime <= 0)
+                {
+                    LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move鏈仠姝�", "");
+                    return false;
+                }
+
+                //return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+                return true;
             }
             catch (Exception ex)
             {
@@ -1056,6 +1062,10 @@
                 OnExceptionOccured?.Invoke(DateTime.Now, ex);
                 return false;
             }
+            finally
+            { 
+            
+            }
         }
 
         /// <summary>
@@ -1235,7 +1245,9 @@
                             }
                         });
 
-                        _positionWait.SpinOnce();
+                        Task.Delay(100).Wait();
+
+                        //_positionWait.SpinOnce();
                     }
                     catch (Exception ex)
                     {
@@ -1272,7 +1284,8 @@
                             }
                         });
 
-                        _statusWait.SpinOnce();
+                        Thread.Sleep(10);
+                        //Task.Delay(10).Wait();
                     }
                     catch (Exception ex)
                     {
diff --git a/src/Bro.Device.GTSCard/Properties/AssemblyInfo.cs b/src/Bro.Device.GTSCard/Properties/AssemblyInfo.cs
index 507b541..f5ccf9a 100644
--- a/src/Bro.Device.GTSCard/Properties/AssemblyInfo.cs
+++ b/src/Bro.Device.GTSCard/Properties/AssemblyInfo.cs
@@ -32,5 +32,5 @@
 //鍙互鎸囧畾鎵�鏈夎繖浜涘�硷紝涔熷彲浠ヤ娇鐢ㄢ�滅敓鎴愬彿鈥濆拰鈥滀慨璁㈠彿鈥濈殑榛樿鍊�
 //閫氳繃浣跨敤 "*"锛屽涓嬫墍绀�:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.1.0")]
+[assembly: AssemblyFileVersion("1.0.1.0")]
diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs
index fb7e04f..52c32df 100644
--- a/src/Bro.Device.Gocator/GocatorDriver.cs
+++ b/src/Bro.Device.Gocator/GocatorDriver.cs
@@ -13,6 +13,7 @@
 using System.Drawing;
 using System.IO;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading;
@@ -218,6 +219,7 @@
             }
         }
 
+        [MethodImpl(MethodImplOptions.Synchronized)]
         public override IImageSet Snapshot(IOperationConfig config)
         {
             GocatorOperationConfig opConfig = config as GocatorOperationConfig;
@@ -234,7 +236,8 @@
                     {
                         try
                         {
-                            system.Start();
+                            sensor.Start();
+                            LogAsync(DateTime.Now, $"浼犳劅鍣ㄧ姸鎬侊細{sensor.State.ToString()}", "");
                             if (sensor.State != GoState.Running)
                             {
                                 Thread.Sleep(100);
@@ -247,6 +250,7 @@
                         catch (Exception ex)
                         {
                             LogAsync(DateTime.Now, $"寮�鍚紓甯革細{ex.GetExceptionMessage()}", "");
+                            Thread.Sleep(100);
                         }
                     } while (true);
 
@@ -284,17 +288,27 @@
                     return null;
                 }
 
-                try
+                int reTryTime = 3;
+                do
                 {
-                    dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
-                }
-                catch (Exception ex)
-                {
-                    LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚寮傚父", ex.GetExceptionMessage());
-                    imgSet.HImage = null;
-                    dataSet = null;
-                    //return imgSet;
-                }
+                    try
+                    {
+                        dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
+                        reTryTime = 0;
+                    }
+                    catch (Exception ex)
+                    {
+                        reTryTime--;
+                        LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚寮傚父", ex.GetExceptionMessage());
+
+                        if (reTryTime <= 0)
+                        {
+                            imgSet.HImage = null;
+                            dataSet = null;
+                            system.ClearData();
+                        }
+                    }
+                } while (reTryTime > 0);
             }
 
             if (!IIConfig.IsAsyncMode)
@@ -310,8 +324,9 @@
                     {
                         try
                         {
-                            system.Stop();
+                            sensor.Stop();
 
+                            LogAsync(DateTime.Now, $"浼犳劅鍣ㄧ姸鎬侊細{sensor.State.ToString()}", "");
                             if (sensor.State != GoState.Ready)
                             {
                                 Thread.Sleep(100);
@@ -324,6 +339,7 @@
                         catch (Exception ex)
                         {
                             LogAsync(DateTime.Now, $"鍏抽棴寮傚父锛歿ex.GetExceptionMessage()}", "");
+                            Thread.Sleep(100);
                         }
                     } while (true);
 
@@ -442,10 +458,22 @@
         {
             base.Start();
 
-            //system.Start();
+            if (sensor.State != GoState.Ready)
+            {
+                system.Start();
+            }
 
             if (IIConfig.IsAsyncMode)
-                system.Start();
+            {
+                sensor.Start();
+            }
+            else
+            {
+                //if (sensor.State == GoState.Ready)
+                {
+                    sensor.Stop();
+                }
+            }
         }
 
         protected override void Stop()
diff --git a/src/Bro.Device.Gocator/Properties/AssemblyInfo.cs b/src/Bro.Device.Gocator/Properties/AssemblyInfo.cs
index 43f4501..3407a97 100644
--- a/src/Bro.Device.Gocator/Properties/AssemblyInfo.cs
+++ b/src/Bro.Device.Gocator/Properties/AssemblyInfo.cs
@@ -32,5 +32,5 @@
 //鍙互鎸囧畾鎵�鏈夎繖浜涘�硷紝涔熷彲浠ヤ娇鐢ㄢ�滅敓鎴愬彿鈥濆拰鈥滀慨璁㈠彿鈥濈殑榛樿鍊�
 //閫氳繃浣跨敤 "*"锛屽涓嬫墍绀�:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.1.0")]
-[assembly: AssemblyFileVersion("1.0.1.0")]
+[assembly: AssemblyVersion("1.0.2.0")]
+[assembly: AssemblyFileVersion("1.0.2.0")]
diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs
index f4108cd..7165338 100644
--- a/src/Bro.M071.Process/M071Process.cs
+++ b/src/Bro.M071.Process/M071Process.cs
@@ -19,6 +19,7 @@
 using System.Drawing.Imaging;
 using System.IO;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
@@ -197,6 +198,7 @@
         List<ProductionMeasurement> productionList = new List<ProductionMeasurement>();
 
         [ProcessMethod("", "StartJob", "寮�濮嬫壂鎻�", InvokeType.TestInvoke)]
+        [MethodImpl(MethodImplOptions.Synchronized)]
         public ProcessResponse StartJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
         {
             if (!IsAllowedWork)
diff --git a/src/Bro.M071.Process/UI/M071_MainForm.cs b/src/Bro.M071.Process/UI/M071_MainForm.cs
index 8ef45d1..c92a0ec 100644
--- a/src/Bro.M071.Process/UI/M071_MainForm.cs
+++ b/src/Bro.M071.Process/UI/M071_MainForm.cs
@@ -7,8 +7,10 @@
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
+using System.IO;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using static Bro.Common.Helper.EnumHelper;
diff --git a/src/Bro.UI.Config/MenuForms/FrmStatistic.Designer.cs b/src/Bro.UI.Config/MenuForms/FrmStatistic.Designer.cs
index e4aa25e..236ea20 100644
--- a/src/Bro.UI.Config/MenuForms/FrmStatistic.Designer.cs
+++ b/src/Bro.UI.Config/MenuForms/FrmStatistic.Designer.cs
@@ -28,9 +28,12 @@
         /// </summary>
         private void InitializeComponent()
         {
+            this.components = new System.ComponentModel.Container();
             this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
             this.plStatistic = new System.Windows.Forms.Panel();
             this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
+            this.ctmsClearQty = new System.Windows.Forms.ContextMenuStrip(this.components);
+            this.tsmiClearQty = new System.Windows.Forms.ToolStripMenuItem();
             this.label1 = new System.Windows.Forms.Label();
             this.label2 = new System.Windows.Forms.Label();
             this.label3 = new System.Windows.Forms.Label();
@@ -71,6 +74,7 @@
             this.flowLayoutPanel1.SuspendLayout();
             this.plStatistic.SuspendLayout();
             this.tableLayoutPanel1.SuspendLayout();
+            this.ctmsClearQty.SuspendLayout();
             this.plCT.SuspendLayout();
             this.tableLayoutPanel2.SuspendLayout();
             this.plOEE.SuspendLayout();
@@ -104,6 +108,7 @@
             this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
             this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 35F));
             this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 35F));
+            this.tableLayoutPanel1.ContextMenuStrip = this.ctmsClearQty;
             this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
             this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1);
             this.tableLayoutPanel1.Controls.Add(this.label3, 0, 2);
@@ -122,6 +127,20 @@
             this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 33F));
             this.tableLayoutPanel1.Size = new System.Drawing.Size(215, 100);
             this.tableLayoutPanel1.TabIndex = 0;
+            // 
+            // ctmsClearQty
+            // 
+            this.ctmsClearQty.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
+            this.tsmiClearQty});
+            this.ctmsClearQty.Name = "ctmsClearQty";
+            this.ctmsClearQty.Size = new System.Drawing.Size(125, 26);
+            // 
+            // tsmiClearQty
+            // 
+            this.tsmiClearQty.Name = "tsmiClearQty";
+            this.tsmiClearQty.Size = new System.Drawing.Size(124, 22);
+            this.tsmiClearQty.Text = "璁℃暟娓呴浂";
+            this.tsmiClearQty.Click += new System.EventHandler(this.tsmiClearQty_Click);
             // 
             // label1
             // 
@@ -226,6 +245,7 @@
             this.btnClearStatistic.TabIndex = 8;
             this.btnClearStatistic.Text = "娓�  闆�";
             this.btnClearStatistic.UseVisualStyleBackColor = true;
+            this.btnClearStatistic.Visible = false;
             this.btnClearStatistic.Click += new System.EventHandler(this.btnClearStatistic_Click);
             // 
             // plCT
@@ -588,6 +608,7 @@
             this.plStatistic.ResumeLayout(false);
             this.tableLayoutPanel1.ResumeLayout(false);
             this.tableLayoutPanel1.PerformLayout();
+            this.ctmsClearQty.ResumeLayout(false);
             this.plCT.ResumeLayout(false);
             this.tableLayoutPanel2.ResumeLayout(false);
             this.tableLayoutPanel2.PerformLayout();
@@ -641,5 +662,7 @@
         private System.Windows.Forms.Button btnRefreshOEE;
         private System.Windows.Forms.Label lblCT;
         private System.Windows.Forms.Label lblUPH;
+        private System.Windows.Forms.ContextMenuStrip ctmsClearQty;
+        private System.Windows.Forms.ToolStripMenuItem tsmiClearQty;
     }
 }
\ No newline at end of file
diff --git a/src/Bro.UI.Config/MenuForms/FrmStatistic.cs b/src/Bro.UI.Config/MenuForms/FrmStatistic.cs
index 30b5409..d587aca 100644
--- a/src/Bro.UI.Config/MenuForms/FrmStatistic.cs
+++ b/src/Bro.UI.Config/MenuForms/FrmStatistic.cs
@@ -5,7 +5,9 @@
 using System;
 using System.Configuration;
 using System.IO;
+using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
+using System.Windows.Forms;
 using WeifenLuo.WinFormsUI.Docking;
 using static Bro.Common.Helper.EnumHelper;
 
@@ -403,10 +405,10 @@
                 CurrentState = RunState.Stop;
             }
 
-            if (state == EnumHelper.DeviceState.DSClose)
-            {
-                SaveNumRecord();
-            }
+            //if (state == EnumHelper.DeviceState.DSClose)
+            //{
+            //    SaveNumRecord();
+            //}
         }
 
         private void CheckIdle(object state)
@@ -436,7 +438,7 @@
 
             Task.Run(() =>
             {
-                SaveNumRecord();
+                SaveNumRecord(Qty_OK, Qty_NG);
             });
         }
 
@@ -505,22 +507,52 @@
             AvailableTime = AvailableTime.Add(new TimeSpan(0, 0, 1));
         }
 
-        private void OnClearQty()
-        {
-            Qty_OK = Qty_NG = 0;
-        }
+        //private void OnClearQty()
+        //{
+        //    Qty_OK = Qty_NG = 0;
+        //}
 
         string numRecordFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Statistic.json");
         static object numLock = new object();
-        private void SaveNumRecord()
+
+        [MethodImpl(MethodImplOptions.Synchronized)]
+        private void SaveNumRecord(int okNum, int ngNum)
         {
             lock (numLock)
             {
-                using (StreamWriter writer = new StreamWriter(numRecordFile, false, System.Text.Encoding.UTF8))
+                try
                 {
-                    string dataStr = JsonConvert.SerializeObject(new { Qty_OK, Qty_NG });
-                    writer.Write(dataStr);
-                    writer.Flush();
+                    //using (StreamWriter writer = new StreamWriter(numRecordFile, false, System.Text.Encoding.UTF8))
+                    //{
+                    //    if (okNum % 5 == 0)
+                    //    {
+                    //        Application.Exit();
+                    //        throw new AccessViolationException();
+                    //    }
+                    //    string dataStr = JsonConvert.SerializeObject(new StatisticModel() { Qty_OK = okNum, Qty_NG = ngNum });
+                    //    if (!string.IsNullOrWhiteSpace(dataStr))
+                    //    {
+                    //        writer.Write(dataStr);
+                    //        writer.Flush();
+                    //    }
+                    //}
+
+                    using (FileStream fs = new FileStream(numRecordFile, FileMode.OpenOrCreate, FileAccess.Write))
+                    {
+                        fs.Seek(0, SeekOrigin.Begin);
+
+                        string dataStr = JsonConvert.SerializeObject(new StatisticModel() { Qty_OK = okNum, Qty_NG = ngNum });
+
+                        byte[] dataBuf = System.Text.Encoding.UTF8.GetBytes(dataStr);
+                        fs.Write(dataBuf, 0, dataBuf.Length);
+                        fs.SetLength(dataBuf.Length);
+                        fs.Flush();
+                        fs.Close();
+                    }
+                }
+                catch (Exception ex)
+                {
+                    LogAsync(DateTime.Now, $"淇濆瓨缁熻鏁版嵁寮傚父锛寋ex.GetExceptionMessage()}");
                 }
             }
         }
@@ -535,21 +567,50 @@
                     {
                         string dataStr = reader.ReadToEnd();
 
-                        JObject data = JsonConvert.DeserializeObject<JObject>(dataStr);
+                        //JObject data = JsonConvert.DeserializeObject<JObject>(dataStr);
 
-                        if (data != null)
+                        //if (data != null)
+                        //{
+                        //    Qty_OK = data.Value<int>("Qty_OK");
+                        //    Qty_NG = data.Value<int>("Qty_NG");
+                        //}
+
+                        StatisticModel model = JsonConvert.DeserializeObject<StatisticModel>(dataStr);
+                        if (model != null)
                         {
-                            Qty_OK = data.Value<int>("Qty_OK");
-                            Qty_NG = data.Value<int>("Qty_NG");
+                            Qty_OK = model.Qty_OK;
+                            Qty_NG = model.Qty_NG;
+                        }
+                        else
+                        {
+                            //LogAsync(DateTime.Now, $"杞藉叆缁熻鏁版嵁閿欒", "");
+                            MessageBox.Show($"杞藉叆缁熻鏁版嵁閿欒");
                         }
                     }
                 }
+            }
+            else
+            {
+                //LogAsync(DateTime.Now, $"缁熻鏁版嵁鏂囦欢涓嶅瓨鍦�", "");
+                MessageBox.Show($"缁熻鏁版嵁鏂囦欢涓嶅瓨鍦�");
             }
         }
 
         private void btnClearStatistic_Click(object sender, EventArgs e)
         {
+            //Qty_OK = Qty_NG = 0;
+        }
+
+        private void tsmiClearQty_Click(object sender, EventArgs e)
+        {
             Qty_OK = Qty_NG = 0;
         }
     }
+
+    public class StatisticModel
+    {
+        public int Qty_OK { get; set; }
+
+        public int Qty_NG { get; set; }
+    }
 }
diff --git a/src/Bro.UI.Config/MenuForms/FrmStatistic.resx b/src/Bro.UI.Config/MenuForms/FrmStatistic.resx
index 1af7de1..7a151da 100644
--- a/src/Bro.UI.Config/MenuForms/FrmStatistic.resx
+++ b/src/Bro.UI.Config/MenuForms/FrmStatistic.resx
@@ -117,4 +117,7 @@
   <resheader name="writer">
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
+  <metadata name="ctmsClearQty.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>17, 17</value>
+  </metadata>
 </root>
\ No newline at end of file
diff --git a/src/Bro.UI.Config/Properties/AssemblyInfo.cs b/src/Bro.UI.Config/Properties/AssemblyInfo.cs
index d627c5c..18d26fe 100644
--- a/src/Bro.UI.Config/Properties/AssemblyInfo.cs
+++ b/src/Bro.UI.Config/Properties/AssemblyInfo.cs
@@ -31,5 +31,5 @@
 // 鍙互鎸囧畾鎵�鏈夊�硷紝涔熷彲浠ヤ娇鐢ㄤ互涓嬫墍绀虹殑 "*" 棰勭疆鐗堟湰鍙峰拰淇鍙�
 // 鏂规硶鏄寜濡備笅鎵�绀轰娇鐢ㄢ��*鈥�: :
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.1.0")]
+[assembly: AssemblyFileVersion("1.0.1.0")]

--
Gitblit v1.8.0