SmartChargingProfileHandler.cs 8.5 KB

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