SmartChargingProfileHandler.cs 8.5 KB

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