diff --git a/AppData.cs b/AppData.cs index 95bed3d..eeec314 100644 --- a/AppData.cs +++ b/AppData.cs @@ -19,6 +19,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Forms.VisualStyles; using System.Windows.Threading; namespace SafeBagDE70 @@ -44,7 +45,8 @@ namespace SafeBagDE70 static Task? resendTaskHistories = null; static Task? resendTaskEvents = null; static Task? commandTask = null; - public static DeviceStatus? _DeviceStatus = null; + private static DeviceStatus? _DeviceStatus = null; + public static DeviceStatusFile? DeviceStatusFile = null; public static DeviceStatus? DeviceStatus { set { _DeviceStatus = value; } @@ -67,6 +69,7 @@ namespace SafeBagDE70 CDMSTATE = new CDMSTATE(); ADMINDEVICESTATUS = new ADMINDEVICESTATUS(); customerAccounts = new ObservableCollection(); + GetDeviceStatusFromFile(); //InitDeviceStatus(); } public static void Init() @@ -474,6 +477,108 @@ namespace SafeBagDE70 Log.Error("GetandExecuteCommands error: "+str); } } + static string deviceStatusFileName = "device_status.txt"; + public static void GetDeviceStatusFromFile() + { + try + { + lock(deviceStatusFileName) + { + if (File.Exists(deviceStatusFileName)) + { + var fileContent = File.ReadAllText(deviceStatusFileName); + DeviceStatusFile = JsonConvert.DeserializeObject(fileContent); + } + else + { + DeviceStatusFile = new DeviceStatusFile(); + UpdateDeviceStatusToFile(); + } + } + Log.Information("GetPrinterStatusFromFile success"); + } + catch(Exception ex) + { + Log.Fatal(ex, "GetPrinterStatusFromFile"); + } + } + public static void UpdatePrinterStatus(bool IsPrinterPaperOut) + { + try + { + if (DeviceStatusFile!=null) + { + DeviceStatusFile.IsPrinterNearPaperOut = IsPrinterPaperOut; + if (!IsPrinterPaperOut) + { + DeviceStatusFile.PrintingCountFromNPO = 0; + } + } + }catch(Exception ex) + { + Log.Fatal(ex, "UpdatePrinterStatus"); + } + } + public static void UpdatePrintingCountToStatusFile() + { + try + { + Log.Information("UpdateTransactionCountToStatusFile"); + + if (DeviceStatusFile!=null) + { + if (DeviceStatusFile.IsPrinterNearPaperOut) + { + DeviceStatusFile.PrintingCountFromNPO++; + } + else + { + DeviceStatusFile.PrintingCountFromNPO = 0; + } + UpdateDeviceStatusToFile(); + + Log.Information("UpdateTransactionCountToStatusFile success"); + } + } + catch (Exception ex) + { + Log.Fatal(ex, "UpdateTransactionCountToStatusFile"); + } + } + public static void UpdateDeviceStatusToFile() + { + try + { + lock (deviceStatusFileName) + { + if (File.Exists(deviceStatusFileName)) + { + var fileContent = JsonConvert.SerializeObject(DeviceStatusFile); + File.WriteAllText(deviceStatusFileName, fileContent); + } + } + Log.Information("UpdatePrinterStatusFromFile success"); + } + catch (Exception ex) + { + Log.Fatal(ex, "UpdatePrinterStatusFromFile"); + } + } + public static bool IsStopPrinting() + { + try + { + if (DeviceStatusFile != null) + return DeviceStatusFile.IsStopPrinting(); + else + return false; + } + catch(Exception ex) + { + return false; + } + } + public static async Task UpdateCustomers() { try @@ -867,6 +972,19 @@ namespace SafeBagDE70 public string? TransactionCode { get; set; } public string Description { get; set; } } + public class DeviceStatusFile + { + public bool IsPrinterNearPaperOut = false; + public int PrintingCountFromNPO = 0;//Transaction Count From Near Paper Out + public bool IsStopPrinting() + { + if (IsPrinterNearPaperOut && PrintingCountFromNPO >= 20) + { + return true; + } + return false; + } + } public enum DeviceStatusCode { NORMAL=0, ERROR=1 diff --git a/UserControl/DepositSMUCv2.xaml.cs b/UserControl/DepositSMUCv2.xaml.cs index 8335bb8..7b77ce2 100644 --- a/UserControl/DepositSMUCv2.xaml.cs +++ b/UserControl/DepositSMUCv2.xaml.cs @@ -451,7 +451,7 @@ namespace SafeBagDE70 var strPrinterMessage = ""; if (status != null) { - if (status.StsNOPAPER) + if (status.StsNOPAPER|| AppData.IsStopPrinting()) { AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.PAPEROUT; StopTimers(); @@ -461,6 +461,7 @@ namespace SafeBagDE70 } else if (status.StsNEARENDPAP) { + AppData.UpdatePrinterStatus(true); strPrinterMessage = "MÁY IN: GẦN HẾT GIẤY"; AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.NEARPAPEROUT; AppData.ADMINDEVICESTATUS.PrinterStatus = strPrinterMessage; @@ -473,6 +474,8 @@ namespace SafeBagDE70 } else { + AppData.UpdatePrinterStatus(false); + strPrinterMessage = "MÁY IN: OK"; AppData.DeviceStatus.PrinterStatus = (int)PrinterStatusCode.NORMAL; AppData.ADMINDEVICESTATUS.PrinterStatus = strPrinterMessage; @@ -1471,8 +1474,7 @@ namespace SafeBagDE70 // or for more complex scenarios, directly manipulate the byte stream sent to the printer. }; printDocument.Print(); - - + AppData.UpdatePrintingCountToStatusFile(); } private void UserControl_Unloaded(object sender, RoutedEventArgs e)