ReservationProfileHandler.cs 5.5 KB

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