ReservationProfileHandler.cs 5.6 KB

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