PrintReceipt.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Wayne.Lib.StateEngine;
  7. using Wayne.Lib.StateEngine.Generic;
  8. using WayneChina_IcCardReader_SinoChem.MessageEntity;
  9. using WayneChina_IcCardReader_SinoChem.MessageEntity.Outgoing;
  10. namespace SinochemInternetPlusApp.States.Shared
  11. {
  12. internal class PrintReceipt : TimeoutState<FuelingPoint>
  13. {
  14. protected byte sqNo;
  15. byte[] currentLine;
  16. int lineIndex = 0;
  17. bool printerReserved = false;
  18. protected override void Enter(StateEntry stateEntry, ref Transition transition)
  19. {
  20. base.Enter(stateEntry, ref transition);
  21. DebugLog("nfe 1");
  22. if(!ConfigurationValues.PrintReceiptEnabled)
  23. {
  24. DebugLog("Print receipt is disabled.");
  25. transition = new Transition(this, TransitionType.Done);
  26. return;
  27. }
  28. if (Main.CurrentEpsTrx == null)
  29. {
  30. DebugLog("Do not print receipt when eps trx was not created for this trx.");
  31. transition = new Transition(this, TransitionType.Done);
  32. return;
  33. }
  34. else if (Main.CurrentEpsTrx != null && Main.CurrentEpsTrx.Model.trx_status != EpsTrxStatus.PaymentOk &&
  35. string.IsNullOrEmpty(Main.CurrentEpsTrx.Model.pay_url))
  36. {
  37. DebugLog("The trx not paid yet and pay_url is null, don't need print receipt");
  38. transition = new Transition(this, TransitionType.Done);
  39. return;
  40. }
  41. if (Main.GetPrinter() || printerReserved)
  42. {
  43. DebugLog("nfe 4");
  44. printerReserved = true;
  45. if (Main.CurrentReceipt == null)
  46. {
  47. DebugLog("nfe 5");
  48. Main.BuildReceipt();
  49. lineIndex = 0;
  50. }
  51. DebugLog("nfe 6");
  52. Main.SendCommand(CreateReceiptLine(lineIndex), out sqNo);
  53. lineIndex++;
  54. }
  55. else
  56. {
  57. if (Main.GetPrinterCount < 50)
  58. {
  59. //Printer is busy now, wait
  60. DebugLog("Printer is busy now");
  61. transition = new Transition(this, TransitionType.Abort);
  62. }
  63. else
  64. {
  65. DebugLog("why cannot i get the printer for such a long time???");
  66. transition = new Transition(this, TransitionType.Done);
  67. }
  68. }
  69. }
  70. protected override void HandleNonTimeoutEvent(StateEngineEvent stateEngineEvent, ref Transition transition)
  71. {
  72. GenericEvent<CardReaderAckEventArgs> e = stateEngineEvent as GenericEvent<CardReaderAckEventArgs>;
  73. if (e != null && e.EventArgs != null)
  74. {
  75. if (e.EventArgs.Ack.MessageSeqNumber == sqNo)
  76. {
  77. e.Handled = true;
  78. if (currentLine[0] == 0x02)
  79. {
  80. Main.ReleasePrinter();
  81. Main.CurrentReceipt.Clear();
  82. Main.CurrentReceipt = null;
  83. lineIndex = 0;
  84. printerReserved = false;
  85. transition = new Transition(this, TransitionType.Done);
  86. }
  87. else
  88. {
  89. transition = new Transition(this, TransitionType.Next);
  90. }
  91. }
  92. }
  93. }
  94. protected override void Timeout(ref Transition transition)
  95. {
  96. Main.ReleasePrinter();
  97. transition = new Transition(this, TransitionType.Timeout);
  98. }
  99. protected override int TimeoutInterval => 10 * 1000;
  100. private PrintReceiptRequest CreateReceiptLine(int i)
  101. {
  102. currentLine = Main.GetNextReceiptLine(i);
  103. return new PrintReceiptRequest(currentLine);
  104. }
  105. }
  106. }