From ff7cab72419729ce2c5adc46350ef45d89a5d1e5 Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期六, 24 七月 2021 10:31:41 +0800 Subject: [PATCH] MES动作添加异常捕捉和重试机制 --- src/Bro.UI.Config/MenuForms/FrmStatistic.cs | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 103 insertions(+), 17 deletions(-) diff --git a/src/Bro.UI.Config/MenuForms/FrmStatistic.cs b/src/Bro.UI.Config/MenuForms/FrmStatistic.cs index 080514a..0b63795 100644 --- a/src/Bro.UI.Config/MenuForms/FrmStatistic.cs +++ b/src/Bro.UI.Config/MenuForms/FrmStatistic.cs @@ -5,6 +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; @@ -349,6 +352,25 @@ { _idleThreshold = 30; } + + this.FormClosing += (s, e) => + { + _checkIdleTimer?.Change(-1, -1); + _checkIdleTimer?.Dispose(); + _checkIdleTimer = null; + + _idleTimer?.Change(-1, -1); + _idleTimer?.Dispose(); + _idleTimer = null; + + _downTimer?.Change(-1, -1); + _downTimer?.Dispose(); + _downTimer = null; + + _availableTimer?.Change(-1, -1); + _availableTimer?.Dispose(); + _availableTimer = null; + }; } public override void OnProcessUpdated() @@ -402,10 +424,10 @@ CurrentState = RunState.Stop; } - if (state == EnumHelper.DeviceState.DSClose) - { - SaveNumRecord(); - } + //if (state == EnumHelper.DeviceState.DSClose) + //{ + // SaveNumRecord(); + //} } private void CheckIdle(object state) @@ -432,6 +454,11 @@ } Qty_OEE++; + + Task.Run(() => + { + SaveNumRecord(Qty_OK, Qty_NG); + }); } private void UpdateCT(float ctTime) @@ -499,22 +526,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()}"); } } } @@ -529,21 +586,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; } + } } -- Gitblit v1.8.0