AuthorizePump.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using Wayne.Lib;
  4. using Wayne.Lib.StateEngine;
  5. using Wayne.Lib.StateEngine.Generic;
  6. namespace SinochemInternetPlusApp.States.Shared
  7. {
  8. class AuthorizePump : TimeoutState<FuelingPoint>
  9. {
  10. protected override void Enter(StateEntry stateEntry, ref Transition transition)
  11. {
  12. base.Enter(stateEntry, ref transition);
  13. ClearExpiredLastTrx();
  14. Main.AuthorizePumpAsync(0); // auth unlimited amount
  15. }
  16. protected override void HandleNonTimeoutEvent(StateEngineEvent stateEngineEvent, ref Transition transition)
  17. {
  18. if (stateEngineEvent.Type is EventType)
  19. {
  20. switch ((EventType)stateEngineEvent.Type)
  21. {
  22. case EventType.PumpAuthOk:
  23. var genericEvent = stateEngineEvent as GenericEvent<GenericEventArg<string>>;
  24. if (genericEvent != null && Main.CurrentEpsTrx != null)
  25. {
  26. Main.DebugLogger.Add($"Current jihao: {Main.CurrentEpsTrx.Model.jihao}");
  27. Main.CurrentEpsTrx.Model.auth_time = genericEvent.EventArgs.Arg;
  28. Main.CurrentEpsTrx.Model.trx_status = EpsTrxStatus.Fueling;
  29. Main.CurrentEpsTrx.SaveToDb();
  30. }
  31. //}
  32. transition = new Transition(this, TransitionType.PumpAuthOk);
  33. stateEngineEvent.Handled = true;
  34. break;
  35. case EventType.PumpAuthFailed:
  36. transition = new Transition(this, TransitionType.PumpAuthFailed);
  37. stateEngineEvent.Handled = true;
  38. break;
  39. }
  40. }
  41. }
  42. protected override void Timeout(ref Transition transition)
  43. {
  44. transition = new Transition(this, TransitionType.Timeout);
  45. }
  46. protected override int TimeoutInterval =>
  47. TimeoutValues.GetValueInMilliSec(TimeoutValues.FuelingPoint.Shared_AuthorizePump, 10);
  48. private int expiredTrxInterval = TimeoutValues.GetValueInSec(TimeoutValues.FuelingPoint.__FP_Shared_AuthorizePump_ClearLastTrx, 30 * 60);
  49. private void ClearExpiredLastTrx()
  50. {
  51. if (Main.CurrentEpsTrx != null)
  52. {
  53. Main.DebugLogger.Add($"expired interval value is: {expiredTrxInterval}");
  54. DateTime oldEnoughDatetime = DateTime.Now.AddSeconds(-1 * expiredTrxInterval);
  55. Main.DebugLogger.Add($" ClearExpiredLastTrx Main.CurrentEpsTrx.Model.created_time: {Main.CurrentEpsTrx.Model.created_time}, " +
  56. $"oldEnoughDatetime: {oldEnoughDatetime}");
  57. if (Main.CurrentEpsTrx.Model.created_time < oldEnoughDatetime)
  58. {
  59. Main.DebugLogger.Add("Set CurrentEpsTrx to null");
  60. Main.CurrentEpsTrx = null;
  61. }
  62. }
  63. }
  64. }
  65. }