CheckCardBalance.cs 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using SinochemCloudClient.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Wayne.Lib.StateEngine;
  8. using Wayne.Lib.StateEngine.Generic;
  9. namespace SinochemInternetPlusApp.States.ICCardMode
  10. {
  11. public class CheckCardBalance : TimeoutState<FuelingPoint>
  12. {
  13. protected override void Enter(StateEntry stateEntry, ref Transition transition)
  14. {
  15. base.Enter(stateEntry, ref transition);
  16. var tid = Main.OnlineVerificationRequest.TID;
  17. var encryptedPin = Main.OnlineVerificationRequest.EncryptedPIN;
  18. var cardNo = Main.OnlineVerificationRequest.CardNo.TrimStart('0');
  19. Main.SendBalanceQueryToCloudAsync(cardNo, encryptedPin, tid, Main.AssociatedNozzles.First());
  20. //transition = new Transition(this, TransitionType.Done);
  21. }
  22. protected override void HandleNonTimeoutEvent(StateEngineEvent stateEngineEvent, ref Transition transition)
  23. {
  24. if (stateEngineEvent.Type is EventType)
  25. {
  26. switch ((EventType)stateEngineEvent.Type)
  27. {
  28. case EventType.CloudBalanceOk:
  29. var genericEvent = stateEngineEvent as GenericEvent<BalanceInquiryResponse>;
  30. if (genericEvent != null)
  31. {
  32. var tid = Main.OnlineVerificationRequest.TID;
  33. var cardNo = Main.OnlineVerificationRequest.CardNo;
  34. if (Main.CurrentEpsTrx != null)
  35. {
  36. Main.DebugLogger.Add("ICCardMode, CurrentEpsTrx not null!");
  37. //Main.CurrentEpsTrx.Model.id
  38. Main.CurrentEpsTrx.Model.card_no = cardNo;
  39. Main.CurrentEpsTrx.Model.EnryptedPIN = Main.OnlineVerificationRequest.EncryptedPIN;
  40. Main.CurrentEpsTrx.Model.ttc = genericEvent.EventArgs.result.ttc;
  41. Main.CurrentEpsTrx.Model.balance_before_trx = genericEvent.EventArgs.result.amount;
  42. Main.CurrentEpsTrx.Model.token = genericEvent.EventArgs.result.token;
  43. Main.CurrentEpsTrx.Model.tid = tid;
  44. Main.CurrentEpsTrx.Model.xf_date = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd"));
  45. Main.CurrentEpsTrx.SaveToDb();
  46. }
  47. else
  48. {
  49. Main.DebugLogger.Add("ICCardMode, CurrentEpsTrx is null!");
  50. Main.CreateEpsTransaction(EpsTransactionMode.ICCardMode);
  51. //Main.CurrentEpsTrx.Model.car_number = genericEvent.EventArgs.result.car_Number;
  52. Main.CurrentEpsTrx.Model.card_no = cardNo;
  53. //Not in db
  54. Main.CurrentEpsTrx.Model.EnryptedPIN = Main.OnlineVerificationRequest.EncryptedPIN;
  55. Main.CurrentEpsTrx.Model.ttc = genericEvent.EventArgs.result.ttc;
  56. Main.CurrentEpsTrx.Model.balance_before_trx = genericEvent.EventArgs.result.amount;
  57. Main.CurrentEpsTrx.Model.token = genericEvent.EventArgs.result.token;
  58. Main.CurrentEpsTrx.Model.tid = tid;
  59. Main.CurrentEpsTrx.Model.xf_date = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd"));
  60. Main.CurrentEpsTrx.SaveToDb();
  61. }
  62. transition = new Transition(this, TransitionType.CloudBalanceOk);
  63. }
  64. else
  65. {
  66. transition = HandleCloudBalanceFailed();
  67. }
  68. stateEngineEvent.Handled = true;
  69. break;
  70. case EventType.CloudBalanceFailed:
  71. transition = HandleCloudBalanceFailed();
  72. stateEngineEvent.Handled = true;
  73. break;
  74. }
  75. }
  76. }
  77. protected override void Timeout(ref Transition transition)
  78. {
  79. transition = HandleCloudBalanceFailed();
  80. }
  81. private Transition HandleCloudBalanceFailed()
  82. {
  83. return new Transition(this, TransitionType.CloudBalanceFailed);
  84. }
  85. protected override int TimeoutInterval =>
  86. TimeoutValues.GetValueInMilliSec(TimeoutValues.FuelingPoint.ICCardMode_CheckCardBalance, 10);
  87. }
  88. }