ReservationProfileHandler.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Packet.Features;
  3. using EVCB_OCPP.Packet.Messages;
  4. using EVCB_OCPP.Packet.Messages.Reservation;
  5. using OCPPServer.Protocol;
  6. using System;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace EVCB_OCPP.WSServer.Message
  10. {
  11. internal partial class ProfileHandler
  12. {
  13. internal async Task<MessageResult> ExecuteReservationConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
  14. {
  15. MessageResult result = new MessageResult() { Success = true };
  16. switch (action)
  17. {
  18. case Actions.ReserveNow:
  19. {
  20. ReserveNowConfirmation _confirm = confirm as ReserveNowConfirmation;
  21. ReserveNowRequest _request = _confirm.GetRequest() as ReserveNowRequest;
  22. using (var db = await maindbContextFactory.CreateDbContextAsync())
  23. {
  24. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  25. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  26. if (operation != null)
  27. {
  28. operation.FinishedOn = DateTime.UtcNow;
  29. operation.Status = 1;//電樁有回覆
  30. operation.EVSE_Status = (int)_confirm.status;//OK
  31. operation.EVSE_Value = _confirm.status.ToString();
  32. await db.SaveChangesAsync();
  33. }
  34. }
  35. }
  36. break;
  37. case Actions.CancelReservation:
  38. {
  39. CancelReservationConfirmation _confirm = confirm as CancelReservationConfirmation;
  40. CancelReservationRequest _request = _confirm.GetRequest() as CancelReservationRequest;
  41. using (var db = await maindbContextFactory.CreateDbContextAsync())
  42. {
  43. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  44. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  45. if (operation != null)
  46. {
  47. operation.FinishedOn = DateTime.UtcNow;
  48. operation.Status = 1;//電樁有回覆
  49. operation.EVSE_Status = (int)_confirm.status;//OK
  50. operation.EVSE_Value = _confirm.status.ToString();
  51. await db.SaveChangesAsync();
  52. }
  53. }
  54. }
  55. break;
  56. default:
  57. {
  58. Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
  59. }
  60. break;
  61. }
  62. return result;
  63. }
  64. internal async Task<MessageResult> ExecuteReservationError(Actions action, string errorMsg, ClientData session, string requestId)
  65. {
  66. MessageResult result = new MessageResult() { Success = true };
  67. switch (action)
  68. {
  69. case Actions.ReserveNow:
  70. {
  71. using (var db = await maindbContextFactory.CreateDbContextAsync())
  72. {
  73. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  74. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  75. if (operation != null)
  76. {
  77. operation.FinishedOn = DateTime.UtcNow;
  78. operation.Status = 1;//電樁有回覆
  79. operation.EVSE_Status = (int)255;//錯誤
  80. operation.EVSE_Value = errorMsg;
  81. await db.SaveChangesAsync();
  82. }
  83. }
  84. }
  85. break;
  86. case Actions.CancelReservation:
  87. {
  88. using (var db = await maindbContextFactory.CreateDbContextAsync())
  89. {
  90. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  91. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  92. if (operation != null)
  93. {
  94. operation.FinishedOn = DateTime.UtcNow;
  95. operation.Status = 1;//電樁有回覆
  96. operation.EVSE_Status = (int)255;//錯誤
  97. operation.EVSE_Value = errorMsg;
  98. await db.SaveChangesAsync();
  99. }
  100. }
  101. }
  102. break;
  103. default:
  104. {
  105. Console.WriteLine(string.Format("Not Implement {0} Logic", action));
  106. }
  107. break;
  108. }
  109. return result;
  110. }
  111. }
  112. }