From 33214ad7fa872e174ba46b6e9d6d7c4a52d727d3 Mon Sep 17 00:00:00 2001 From: Nguyen Dinh Thuan Date: Mon, 16 Sep 2024 23:33:30 +0700 Subject: [PATCH] =?UTF-8?q?AppData=20-->=20l=E1=BA=A5y=20tr=E1=BA=A1ng=20t?= =?UTF-8?q?h=C3=A1i=20g=C3=A1n=20th=E1=BB=9Di=20gian=20APIModels-->=20B?= =?UTF-8?q?=E1=BB=8F=20g=C3=A1n=20th=E1=BB=9Di=20gian=20DepositSMUCv2-->?= =?UTF-8?q?=20B=C3=A1o=20=C4=91ang=20trong=20giao=20d=E1=BB=8Bch=20ch?= =?UTF-8?q?=E1=BB=89=20khi=20=C4=91=C4=83ng=20nh=E1=BA=ADp=20th=C3=A0nh=20?= =?UTF-8?q?c=C3=B4ng?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API/APIModels.cs | 4 ++-- AppData.cs | 53 +++++++++++++++++++++++++++++++------------ DepositSMUCv2.xaml.cs | 47 +++++++++++++++++++++++++++++++++++--- SafeBagDE70.csproj | 1 - 4 files changed, 85 insertions(+), 20 deletions(-) diff --git a/API/APIModels.cs b/API/APIModels.cs index 930c5e4..a6b3746 100644 --- a/API/APIModels.cs +++ b/API/APIModels.cs @@ -64,7 +64,7 @@ namespace SafeBagDE70.API { var json = JsonConvert.SerializeObject(deviceStatus); var _deviceStatus = JsonConvert.DeserializeObject(json); - _deviceStatus.DeviceTime=DateTime.Now; + //_deviceStatus.DeviceTime=DateTime.Now; HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }; @@ -97,7 +97,7 @@ namespace SafeBagDE70.API { if (deviceStatus == null) return; DeviceEvent deviceEvent = InsertEventToDB(deviceStatus); - deviceStatus.DeviceTime = DateTime.Now; + //deviceStatus.DeviceTime = DateTime.Now; deviceStatus.TransactionCode = AppData.GetTransactionCode(); TaskSendEvent(deviceStatus).ContinueWith(t => { diff --git a/AppData.cs b/AppData.cs index 1bacbe7..f8b06b3 100644 --- a/AppData.cs +++ b/AppData.cs @@ -36,25 +36,34 @@ namespace SafeBagDE70 public static int MaxNotes = 300; public static string BaseURL = ""; public static int TenantId = 0; - public static DeviceStatus? DeviceStatus = null; + public static DeviceStatus? _DeviceStatus = null; + public static DeviceStatus? DeviceStatus + { + set { _DeviceStatus = value; } + get + { + if (_DeviceStatus == null) + { + InitDeviceStatus(); + } + else + { + _DeviceStatus.DeviceTime = DateTime.Now; + } + return _DeviceStatus; + } + } public static bool bInitUser = false; + static AppData() + { + CDMSTATE = new CDMSTATE(); + //InitDeviceStatus(); + } public static void Init() { try { - CDMSTATE = new CDMSTATE(); - DeviceStatus = new DeviceStatus - { - Device = Environment.MachineName, - TenantId = AppData.TenantId, - Status = 0, - CDMStatus = 0, - PrinterStatus = 0, - SensorStatus = 0, - Notes = "", - OtherError = 0, - }; - bInitUser=InitUsers(); + bInitUser = InitUsers(); } catch (Exception ex) { @@ -68,6 +77,21 @@ namespace SafeBagDE70 } + public static void InitDeviceStatus() + { + DeviceStatus = new DeviceStatus + { + Device = Environment.MachineName, + TenantId = AppData.TenantId, + Status = 0, + CDMStatus = 0, + PrinterStatus = 0, + SensorStatus = 0, + Notes = "", + OtherError = 0, + }; + } + public static bool InitUsers() { int retryCount = 0; @@ -101,6 +125,7 @@ namespace SafeBagDE70 } catch (Exception ex) { + Log.Information($"Retry InitUser: {retryCount}/{maxRetries}"); retryCount++; if (retryCount >= maxRetries) diff --git a/DepositSMUCv2.xaml.cs b/DepositSMUCv2.xaml.cs index 3b5c4c9..0be225d 100644 --- a/DepositSMUCv2.xaml.cs +++ b/DepositSMUCv2.xaml.cs @@ -100,8 +100,8 @@ namespace SafeBagDE70 { transactionCode = AppData.GetTransactionCode(); lblUser.Content = $"Xin chào khách hàng: {AppData.CurrentCustomerAccount.Name} (Gian hàng {AppData.CurrentCustomerAccount.CustomerId})"; - lblTransactionCode.Content = $"Mã GD: {transactionCode}"; + AppData.IsInTransaction = true; } cCmdRsp = new CCmdRsp(serialPortDE70); AppData.cCmdRsp = cCmdRsp; @@ -114,7 +114,7 @@ namespace SafeBagDE70 notesInSession = new NotesInSession(); spNotesInSession.DataContext = notesInSession; - AppData.IsInTransaction = true; + ResetButtons(); btnStart.IsEnabled = true; btnLogout.IsEnabled = true; @@ -359,7 +359,7 @@ namespace SafeBagDE70 sensorTimer.IsEnabled = false; try { - countSensorTimer++; + if (countSensorTimer % 10 == 0) { @@ -392,7 +392,48 @@ namespace SafeBagDE70 AppData.DeviceStatus.DeviceTime = DateTime.Now; APIModels.SendHealthCheck(AppData.DeviceStatus); } + if (countSensorTimer % 1800 == 0) + { + if (!AppData.IsInTransaction) + { + Task.Run(async () => + { + try + { + using (var context = new CDMDbContext()) + { + var startTime = DateTime.Now.Date.AddDays(-2).Date; + var unsentHist = context.Histories + .Where(x => x.DepositTime >= startTime) + .Where(x => ((x.IsSent == null || x.IsSent == false))) + .ToList(); + foreach(var item in unsentHist) + { + var data2Send = JsonConvert.DeserializeObject(item.Data); + var bResult = await APIModels.SendCountingData(data2Send); + if (bResult) + { + item.IsSent = true; + context.Update(item); + context.SaveChanges(); + Log.Information("Resend: " + data2Send + " successfully"); + } + else + { + Log.Error("Resend: " + data2Send + " failed"); + } + } + } + } + catch (Exception ex) + { + Log.Fatal(ex,"Resend failed"); + } + }); + } + } + countSensorTimer++; sensorTimer.IsEnabled = false; GetSensorValue(); sensorTimer.IsEnabled = true; diff --git a/SafeBagDE70.csproj b/SafeBagDE70.csproj index 849de47..b0ea94c 100644 --- a/SafeBagDE70.csproj +++ b/SafeBagDE70.csproj @@ -69,7 +69,6 @@ -