WaitForFueling.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Wayne.Lib;
  5. using Wayne.Lib.StateEngine;
  6. using Wayne.Lib.StateEngine.Generic;
  7. namespace SinochemInternetPlusApp.States.CarPlateMode
  8. {
  9. public class WaitForFueling : TimeoutState<FuelingPoint>
  10. {
  11. private byte sqNo;
  12. private bool cardReaderBackToIdle = false;
  13. private bool nozzleLifted = false;
  14. protected override void Enter(StateEntry stateEntry, ref Transition transition)
  15. {
  16. base.Enter(stateEntry, ref transition);
  17. cardReaderBackToIdle = false;
  18. nozzleLifted = false;
  19. Main.SetICCardReaderToCarPlateIdle(out sqNo);
  20. DebugLog($"Start to set card reader back to CarPlateIdle, SqNo={sqNo}\n Waiting for Ack");
  21. }
  22. protected override void HandleNonTimeoutEvent(StateEngineEvent stateEngineEvent, ref Transition transition)
  23. {
  24. if (stateEngineEvent.Type.Equals(EventType.NozzleLifted))
  25. {
  26. var nlEvent = stateEngineEvent as GenericEvent<NozzleLiftedEventArgs>;
  27. if (nlEvent.EventArgs.NozzleId == Main.CurrentNozzleId)
  28. {
  29. DebugLog("Nozzle correctly lifted");
  30. nozzleLifted = true;
  31. }
  32. else
  33. {
  34. DebugLog("Nozzle lifted, but the nozzle was wrong: " + nlEvent.EventArgs.NozzleId);
  35. }
  36. stateEngineEvent.Handled = true;
  37. }
  38. if (stateEngineEvent.Type.Equals(EventType.NozzleReplaced))
  39. {
  40. var nrEvent = stateEngineEvent as GenericEvent<NozzleReplacedEventArgs>;
  41. if(nrEvent.EventArgs.NozzleId != Main.CurrentNozzleId)
  42. {
  43. DebugLog("Nozzle replaced, but the nozzle was wrong: " + nrEvent.EventArgs.NozzleId);
  44. stateEngineEvent.Handled = true;
  45. }
  46. }
  47. GenericEvent<CardReaderAckEventArgs> ackEvent = stateEngineEvent as GenericEvent<CardReaderAckEventArgs>;
  48. if (ackEvent != null && ackEvent.EventArgs != null)
  49. {
  50. if (ackEvent.EventArgs.Ack.MessageSeqNumber == sqNo)
  51. {
  52. DebugLog($" Set CardReader to CarPlateIdle ack'ed, SqNo={sqNo}\n");
  53. ackEvent.Handled = true;
  54. cardReaderBackToIdle = true;
  55. }
  56. else
  57. {
  58. DebugLog($"SqNo not matched, Sent: {sqNo}, Received: {ackEvent.EventArgs.Ack.MessageSeqNumber}");
  59. ackEvent.Handled = true;
  60. cardReaderBackToIdle = true;
  61. }
  62. }
  63. if (stateEngineEvent.Type.Equals(EventType.TrxOpRequest))
  64. {
  65. GenericEvent<TransactionOperationEventArgs> trxOpEvent = stateEngineEvent as GenericEvent<TransactionOperationEventArgs>;
  66. if (trxOpEvent != null && trxOpEvent.EventArgs != null && Main.ActiveTrx != null)
  67. {
  68. trxOpEvent.Handled = true;
  69. if (trxOpEvent.EventArgs.TrxOp.OpType == OpType.Delete)
  70. {
  71. int trxId = trxOpEvent.EventArgs.TrxOp.TrxId;
  72. foreach (var trx in Main.ActiveTrx)
  73. {
  74. if ((trx.id == trxId) && (Main.AssociatedNozzles.Contains(trx.jihao)))
  75. {
  76. EpsTransaction.RestroeEpsTrxFrom(trx).UpdateTrxStatusToDb(EpsTrxStatus.Removed);
  77. Main.CurrentEpsTrx = null;
  78. transition = new Transition(this, TransitionType.FuelingRemoved);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. if (nozzleLifted && cardReaderBackToIdle)
  85. {
  86. transition = new Transition(this, TransitionType.NozzleLifted);
  87. }
  88. }
  89. protected override void Timeout(ref Transition transition)
  90. {
  91. transition = new Transition(this, TransitionType.Timeout);
  92. }
  93. protected override int TimeoutInterval =>
  94. TimeoutValues.GetValueInMilliSec(TimeoutValues.FuelingPoint.Shared_WaitForFueling, 30 * 60);
  95. }
  96. }