SmartChargingProfileHandler.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Domain.Models.Database;
  3. using EVCB_OCPP.Packet.Features;
  4. using EVCB_OCPP.Packet.Messages;
  5. using EVCB_OCPP.Packet.Messages.SmartCharging;
  6. using EVCB_OCPP.Packet.Messages.SubTypes;
  7. using Microsoft.EntityFrameworkCore;
  8. using Newtonsoft.Json;
  9. using OCPPServer.Protocol;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. namespace EVCB_OCPP.WSServer.Message
  15. {
  16. internal partial class ProfileHandler
  17. {
  18. internal async void SetChargingProfile(string chargeBoxId, decimal value, ChargingRateUnitType unit)
  19. {
  20. var _setProfileRequest = new SetChargingProfileRequest()
  21. {
  22. connectorId = 0,
  23. csChargingProfiles = new Packet.Messages.SubTypes.csChargingProfiles()
  24. {
  25. chargingProfileId = 1,
  26. chargingProfileKind = Packet.Messages.SubTypes.ChargingProfileKindType.Recurring,
  27. chargingProfilePurpose = Packet.Messages.SubTypes.ChargingProfilePurposeType.ChargePointMaxProfile,
  28. chargingSchedule = new Packet.Messages.SubTypes.ChargingSchedule()
  29. {
  30. chargingRateUnit = unit,
  31. chargingSchedulePeriod = new List<Packet.Messages.SubTypes.ChargingSchedulePeriod>()
  32. {
  33. new Packet.Messages.SubTypes.ChargingSchedulePeriod(){ startPeriod=0, limit=value}
  34. },
  35. duration = 86400,
  36. },
  37. recurrencyKind = Packet.Messages.SubTypes.RecurrencyKindType.Daily,
  38. stackLevel = 1,
  39. }
  40. };
  41. await mainDbService.AddServerMessage(
  42. ChargeBoxId: chargeBoxId,
  43. OutAction: _setProfileRequest.Action.ToString(),
  44. OutRequest: _setProfileRequest
  45. );
  46. }
  47. internal async void ClearChargingProfile(string chargeBoxId)
  48. {
  49. var _clearProfileRequest = new ClearChargingProfileRequest()
  50. {
  51. connectorId = 0,
  52. chargingProfilePurpose = ChargingProfilePurposeType.ChargePointMaxProfile,
  53. };
  54. await mainDbService.AddServerMessage(new ServerMessage()
  55. {
  56. ChargeBoxId = chargeBoxId,
  57. OutAction = _clearProfileRequest.Action.ToString(),
  58. OutRequest = JsonConvert.SerializeObject(_clearProfileRequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None })
  59. });
  60. }
  61. internal async Task<MessageResult> ExecuteSmartChargingConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
  62. {
  63. MessageResult result = new MessageResult() { Success = true };
  64. switch (action)
  65. {
  66. case Actions.ClearChargingProfile:
  67. {
  68. ClearChargingProfileConfirmation _confirm = confirm as ClearChargingProfileConfirmation;
  69. ClearChargingProfileRequest _request = _confirm.GetRequest() as ClearChargingProfileRequest;
  70. using (var db = await maindbContextFactory.CreateDbContextAsync())
  71. {
  72. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  73. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  74. if (operation != null)
  75. {
  76. operation.FinishedOn = DateTime.UtcNow;
  77. operation.Status = 1;//電樁有回覆
  78. operation.EVSE_Status = (int)_confirm.status;//OK
  79. operation.EVSE_Value = _confirm.status.ToString();
  80. await db.SaveChangesAsync();
  81. }
  82. }
  83. }
  84. break;
  85. case Actions.SetChargingProfile:
  86. {
  87. SetChargingProfileConfirmation _confirm = confirm as SetChargingProfileConfirmation;
  88. SetChargingProfileRequest _request = _confirm.GetRequest() as SetChargingProfileRequest;
  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)_confirm.status;//OK
  98. operation.EVSE_Value = _confirm.status.ToString();
  99. await db.SaveChangesAsync();
  100. }
  101. }
  102. }
  103. break;
  104. case Actions.GetCompositeSchedule:
  105. {
  106. GetCompositeScheduleConfirmation _confirm = confirm as GetCompositeScheduleConfirmation;
  107. GetCompositeScheduleRequest _request = _confirm.GetRequest() as GetCompositeScheduleRequest;
  108. using (var db = await maindbContextFactory.CreateDbContextAsync())
  109. {
  110. var operation = await db.MachineOperateRecord
  111. .Where(x => x.SerialNo == requestId && x.ChargeBoxId == session.ChargeBoxId && x.Status == 0)
  112. .FirstOrDefaultAsync();
  113. if (operation != null)
  114. {
  115. operation.FinishedOn = DateTime.UtcNow;
  116. operation.Status = 1;//電樁有回覆
  117. operation.EVSE_Status = (int)_confirm.status;//OK
  118. operation.EVSE_Value = JsonConvert.SerializeObject(_confirm.chargingSchedule, Formatting.None);
  119. await db.SaveChangesAsync();
  120. }
  121. }
  122. }
  123. break;
  124. default:
  125. {
  126. Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
  127. }
  128. break;
  129. }
  130. return result;
  131. }
  132. internal async Task<MessageResult> ReceivedSmartChargingError(Actions action, string errorMsg, ClientData session, string requestId)
  133. {
  134. MessageResult result = new MessageResult() { Success = true };
  135. switch (action)
  136. {
  137. case Actions.ClearChargingProfile:
  138. case Actions.SetChargingProfile:
  139. case Actions.GetCompositeSchedule:
  140. {
  141. using (var db = await maindbContextFactory.CreateDbContextAsync())
  142. {
  143. var operation = await db.MachineOperateRecord
  144. .Where(x => x.SerialNo == requestId && x.ChargeBoxId == session.ChargeBoxId && x.Status == 0)
  145. .FirstOrDefaultAsync();
  146. if (operation != null)
  147. {
  148. operation.FinishedOn = DateTime.UtcNow;
  149. operation.Status = 1;//電樁有回覆
  150. operation.EVSE_Status = (int)255;//錯誤
  151. operation.EVSE_Value = errorMsg;
  152. await db.SaveChangesAsync();
  153. }
  154. }
  155. }
  156. break;
  157. default:
  158. {
  159. Console.WriteLine(string.Format("Not Implement {0} Logic", action));
  160. }
  161. break;
  162. }
  163. return result;
  164. }
  165. }
  166. }