From 2b4a509e3a4617920ab39e4f8ddcc8c0bd7287b6 Mon Sep 17 00:00:00 2001 From: Nguyen Dinh Thuan Date: Mon, 16 Sep 2024 15:08:10 +0700 Subject: [PATCH] =?UTF-8?q?try=20catch=20Init=20printer=20=C4=90=E1=BA=A3o?= =?UTF-8?q?=20th=E1=BB=AD=20t=E1=BB=B1=20InitUsers=20Ki=E1=BB=83m=20tra=20?= =?UTF-8?q?=C4=91=E1=BA=A7u=20=C4=91=E1=BB=8Dc=20QR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AppData.cs | 47 +++++++++++++++++++++-------- DepositSMUCv2.xaml.cs | 51 ++++++++++++++++++------------- Services/PrinterManager.cs | 18 ++++++++--- WelcomeUC.xaml.cs | 61 +++++++++++++++++--------------------- 4 files changed, 105 insertions(+), 72 deletions(-) diff --git a/AppData.cs b/AppData.cs index 2887c43..2dd4aaa 100644 --- a/AppData.cs +++ b/AppData.cs @@ -1,5 +1,6 @@ using DE70_CMDv2; using Newtonsoft.Json.Linq; +using SafeBagDE70.API; using SafeBagDE70.Model; using Serilog; using System; @@ -39,7 +40,6 @@ namespace SafeBagDE70 { try { - InitUsers(); CDMSTATE = new CDMSTATE(); DeviceStatus = new DeviceStatus { @@ -52,33 +52,54 @@ namespace SafeBagDE70 Notes = "", OtherError = 0, }; + InitUsers(); + + } catch (Exception ex) { Log.Fatal(ex, "AppInit: "+ex.Message); + if(DeviceStatus != null) + { + DeviceStatus.Description = "Exception in AppInit: "+ex.Message; + APIModels.SendEvent(DeviceStatus); + } } } private static void InitUsers() { - customerAccounts = new List(); - using (var context = new CDMDbContext()) + try { - var customers = context.Customers; - foreach (var customer in customers) + customerAccounts = new List(); + using (var context = new CDMDbContext()) { - var ca = new CustomerAccount + var customers = context.Customers; + foreach (var customer in customers) { - Name = customer.TenantName, - CustomerId = customer.AccountNumber, - QRCode = customer.AccountNumber, - PINCode = customer.PinCode - }; - customerAccounts.Add(ca); + var ca = new CustomerAccount + { + Name = customer.TenantName, + CustomerId = customer.AccountNumber, + QRCode = customer.AccountNumber, + PINCode = customer.PinCode + }; + customerAccounts.Add(ca); + } + } + CurrentCustomerAccount = customerAccounts[0]; + } + catch(Exception ex) + { + Log.Fatal(ex, "InitUsers: " + ex.Message); + if (DeviceStatus != null) + { + DeviceStatus.Description = "Exception in InitUsers: " + ex.Message; + APIModels.SendEvent(DeviceStatus); } } - CurrentCustomerAccount = customerAccounts[0]; + } public static CDMSTATE? CDMSTATE = null; diff --git a/DepositSMUCv2.xaml.cs b/DepositSMUCv2.xaml.cs index 301ef5b..10b032f 100644 --- a/DepositSMUCv2.xaml.cs +++ b/DepositSMUCv2.xaml.cs @@ -75,7 +75,7 @@ namespace SafeBagDE70 int countTimer = 0; CCmdRsp cCmdRsp = null; bool bInitialized = false; - bool bOpenPort = true; + bool bOpenPort = false; bool bIsMaintain = false; MainWindow mainWindow = null; Thread thread = null; @@ -117,6 +117,7 @@ namespace SafeBagDE70 AppData.IsInTransaction = true; ResetButtons(); btnStart.IsEnabled = true; + btnLogout.IsEnabled = true; ResetTimers(); var state=GetCDMState(); if(state.GetSR2()==0x1C) @@ -135,10 +136,14 @@ namespace SafeBagDE70 } catch (Exception ex) { - AppData.DeviceStatus.CDMStatus = (int)CDMStatusCode.DISCONNECT_SERIALPORT; - APIModels.SendEvent(AppData.DeviceStatus); - Log.Fatal(ex, "LỖI CỔNG KẾT NỐI"); - ShowError("LỖI CỔNG KẾT NỐI, VUI LÒNG KIỂM TRA LẠI CỔNG KẾT NỐI"); + if(AppData.DeviceStatus!=null) + { + AppData.DeviceStatus.CDMStatus = (int)CDMStatusCode.DISCONNECT_SERIALPORT; + APIModels.SendEvent(AppData.DeviceStatus); + Log.Fatal(ex, "LỖI CỔNG KẾT NỐI"); + ShowError("LỖI CỔNG KẾT NỐI, VUI LÒNG KIỂM TRA LẠI CỔNG KẾT NỐI"); + } + } } @@ -361,24 +366,28 @@ namespace SafeBagDE70 var status = printerManager.GetStatus(); var strPrinterMessage = ""; - if(status.StsNOPAPER) + if(status!=null) { - AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.PAPEROUT; - StopTimers(); - strPrinterMessage = "MÁY IN HẾT GIẤY! ĐỀ NGHỊ THAY GIẤY"; - ShowError(strPrinterMessage); - } - else if(status.StsNEARENDPAP) - { - strPrinterMessage = "MÁY IN GẦN HẾT GIẤY"; - AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.NEARPAPEROUT; - } - if(AppData.DeviceStatus.PrinterStatus!= prevPrinterStatus) - { - AppData.DeviceStatus.Description = strPrinterMessage; - APIModels.SendEvent(AppData.DeviceStatus); - prevPrinterStatus = AppData.DeviceStatus.PrinterStatus; + if (status.StsNOPAPER) + { + AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.PAPEROUT; + StopTimers(); + strPrinterMessage = "MÁY IN HẾT GIẤY! ĐỀ NGHỊ THAY GIẤY"; + ShowError(strPrinterMessage); + } + else if (status.StsNEARENDPAP) + { + strPrinterMessage = "MÁY IN GẦN HẾT GIẤY"; + AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.NEARPAPEROUT; + } + if (AppData.DeviceStatus.PrinterStatus != prevPrinterStatus) + { + AppData.DeviceStatus.Description = strPrinterMessage; + APIModels.SendEvent(AppData.DeviceStatus); + prevPrinterStatus = AppData.DeviceStatus.PrinterStatus; + } } + AppData.DeviceStatus.Description = AppData.CDMSTATE.CDMStatus; AppData.DeviceStatus.DeviceTime = DateTime.Now; APIModels.SendHealthCheck(AppData.DeviceStatus); diff --git a/Services/PrinterManager.cs b/Services/PrinterManager.cs index 0213ba9..ff21eb7 100644 --- a/Services/PrinterManager.cs +++ b/Services/PrinterManager.cs @@ -1,5 +1,6 @@ using Custom.CuCustomWndAPI; using HandyControl.Data; +using SafeBagDE70.API; using Serilog; using System; using System.Collections.Generic; @@ -23,18 +24,27 @@ namespace SafeBagDE70.Services customWndAPIWrap.InitLibrary(); USBDevice[] usbArray = customWndAPIWrap.EnumUSBDevices(); dev = customWndAPIWrap.OpenPrinterUSB(usbArray[0]); - Log.Information("[PRINTER] Init Printer Successfully"); + Log.Information("[PRINTER] Khởi tạo thành công"); } catch (Exception ex) { - // System.Windows.MessageBox.Show(ex.ToString()); + Log.Information("[PRINTER] Không thể khởi tạo máy in"); + + AppData.DeviceStatus.PrinterStatus = 3; + AppData.DeviceStatus.Description = "[PRINTER] Không thể khởi tạo máy in"; + APIModels.SendEvent(AppData.DeviceStatus); } } public PrinterStatus? GetStatus() { - var status = dev.GetPrinterFullStatus(); - return status; + if(dev!=null) + { + var status = dev.GetPrinterFullStatus(); + return status; + } + return null; + } public bool IsNearPaperOut() { diff --git a/WelcomeUC.xaml.cs b/WelcomeUC.xaml.cs index 35b4834..9479867 100644 --- a/WelcomeUC.xaml.cs +++ b/WelcomeUC.xaml.cs @@ -1,4 +1,6 @@ using CoreScanner; +using SafeBagDE70.API; +using Serilog; using System; using System.Collections.Generic; using System.Diagnostics; @@ -16,6 +18,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Xml; +using System.Xml.Linq; namespace SafeBagDE70 { @@ -103,31 +106,11 @@ namespace SafeBagDE70 } private async void PinCodeButton_Click(object sender, RoutedEventArgs e) { - //var onScreenKeyboardProcess = new ProcessStartInfo("osk.exe") - //{ - // UseShellExecute = true - //}; try { - //var progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink"; - //var keyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe"); - //Process.Start(keyboardPath); - // AutomationElement.FromHandle(-1); - - // string keyboardPath = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe"; - // Process.Start(keyboardPath); - //await Launcher.LaunchUriAsync(new Uri("ms-inputapp://")); - - //var onScreenKeyboardProcess = new ProcessStartInfo(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe") - //{ - // UseShellExecute = true - //}; - //Process.Start(onScreenKeyboardProcess); - QrCodePanel.Visibility = Visibility.Collapsed; PinCodePanel.Visibility = Visibility.Visible; - cCoreScannerClass.BarcodeEvent -= new _ICoreScannerEvents_BarcodeEventEventHandler(OnBarcodeEvent); } catch { @@ -157,21 +140,31 @@ namespace SafeBagDE70 int status; // Extended API return code cCoreScannerClass.Open(0, scannerTypes, numberOfScannerTypes, out status); + short numberOfScanners; // Number of scanners expect to be used + int[] connectedScannerIDList = new int[255]; // List of scanner IDs to be returned + string outXML; + cCoreScannerClass.GetScanners(out numberOfScanners, connectedScannerIDList, out outXML, out status); + if (numberOfScanners > 0) + { + // Let's subscribe for events + int opcode = 1001; // Method for Subscribe events + //string outXML; // XML Output + string inXML = "" + + "" + + "1" + // Number of events you want to subscribe + "1" + // Comma separated event IDs + "" + + ""; - // Subscribe for barcode events in cCoreScannerClass - - - // Let's subscribe for events - int opcode = 1001; // Method for Subscribe events - string outXML; // XML Output - string inXML = "" + - "" + - "1" + // Number of events you want to subscribe - "1" + // Comma separated event IDs - "" + - ""; - - cCoreScannerClass.ExecCommand(opcode, ref inXML, out outXML, out status); + cCoreScannerClass.ExecCommand(opcode, ref inXML, out outXML, out status); + } + else + { + Log.Information("Không tìm thấy đầu đọc QR"); + AppData.DeviceStatus.OtherError = 1; + AppData.DeviceStatus.Description = "Không tìm thấy đầu đọc QR"; + APIModels.SendEvent(AppData.DeviceStatus); + } } catch (Exception exp) {