PrintReceipt.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. DebugLog("nfe 2");
  29. if (Main.CurrentEpsTrx?.Model.trx_status != EpsTrxStatus.PaymentOk)
  30. {
  31. DebugLog("Do not print receipt as the trx was failed.");
  32. transition = new Transition(this, TransitionType.Done);
  33. return;
  34. }
  35. DebugLog("nfe 3");
  36. if (Main.GetPrinter() || printerReserved)
  37. {
  38. DebugLog("nfe 4");
  39. printerReserved = true;
  40. if (Main.CurrentReceipt == null)
  41. {
  42. DebugLog("nfe 5");
  43. Main.BuildReceipt();
  44. lineIndex = 0;
  45. }
  46. DebugLog("nfe 6");
  47. Main.SendCommand(CreateReceiptLine(lineIndex), out sqNo);
  48. lineIndex++;
  49. }
  50. else
  51. {
  52. if (Main.GetPrinterCount < 50)
  53. {
  54. //Printer is busy now, wait
  55. transition = new Transition(this, TransitionType.Abort);
  56. }
  57. else
  58. {
  59. DebugLog("why cannot i get the printer for such a long time???");
  60. transition = new Transition(this, TransitionType.Done);
  61. }
  62. }
  63. }
  64. protected override void HandleNonTimeoutEvent(StateEngineEvent stateEngineEvent, ref Transition transition)
  65. {
  66. GenericEvent<CardReaderAckEventArgs> e = stateEngineEvent as GenericEvent<CardReaderAckEventArgs>;
  67. if (e != null && e.EventArgs != null)
  68. {
  69. if (e.EventArgs.Ack.MessageSeqNumber == sqNo)
  70. {
  71. e.Handled = true;
  72. if (currentLine[0] == 0x02)
  73. {
  74. Main.ReleasePrinter();
  75. Main.CurrentReceipt.Clear();
  76. Main.CurrentReceipt = null;
  77. lineIndex = 0;
  78. printerReserved = false;
  79. transition = new Transition(this, TransitionType.Done);
  80. }
  81. else
  82. {
  83. transition = new Transition(this, TransitionType.Next);
  84. }
  85. }
  86. }
  87. }
  88. protected override void Timeout(ref Transition transition)
  89. {
  90. Main.ReleasePrinter();
  91. transition = new Transition(this, TransitionType.Timeout);
  92. }
  93. protected override int TimeoutInterval => 10 * 1000;
  94. private PrintReceiptRequest CreateReceiptLine(int i)
  95. {
  96. currentLine = Main.GetNextReceiptLine(i);
  97. return new PrintReceiptRequest(currentLine);
  98. }
  99. }
  100. }