SmartChargingProfileHandler.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. using (var db = maindbContextFactory.CreateDbContext())
  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*1000}
  36. },
  37. duration = 60,
  38. },
  39. recurrencyKind = Packet.Messages.SubTypes.RecurrencyKindType.Daily,
  40. stackLevel = 1,
  41. }
  42. };
  43. db.ServerMessage.Add(new ServerMessage()
  44. {
  45. ChargeBoxId = chargeBoxId,
  46. CreatedBy = "Server",
  47. CreatedOn = DateTime.UtcNow,
  48. OutAction = _setProfileRequest.Action.ToString(),
  49. OutRequest = JsonConvert.SerializeObject(_setProfileRequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  50. SerialNo = Guid.Empty.ToString(),
  51. InMessage = string.Empty
  52. });
  53. db.SaveChanges();
  54. }
  55. }
  56. internal async void ClearChargingProfile(string chargeBoxId)
  57. {
  58. using (var db = maindbContextFactory.CreateDbContext())
  59. {
  60. var _clearProfileRequest = new ClearChargingProfileRequest()
  61. {
  62. connectorId = 0,
  63. chargingProfilePurpose = ChargingProfilePurposeType.ChargePointMaxProfile,
  64. };
  65. db.ServerMessage.Add(new ServerMessage()
  66. {
  67. ChargeBoxId = chargeBoxId,
  68. CreatedBy = "Server",
  69. CreatedOn = DateTime.UtcNow,
  70. OutAction = _clearProfileRequest.Action.ToString(),
  71. OutRequest = JsonConvert.SerializeObject(_clearProfileRequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  72. SerialNo = Guid.Empty.ToString(),
  73. InMessage = string.Empty
  74. });
  75. db.SaveChanges();
  76. }
  77. }
  78. internal async Task<MessageResult> ExecuteSmartChargingConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
  79. {
  80. MessageResult result = new MessageResult() { Success = true };
  81. switch (action)
  82. {
  83. case Actions.ClearChargingProfile:
  84. {
  85. ClearChargingProfileConfirmation _confirm = confirm as ClearChargingProfileConfirmation;
  86. ClearChargingProfileRequest _request = _confirm.GetRequest() as ClearChargingProfileRequest;
  87. using (var db = maindbContextFactory.CreateDbContext())
  88. {
  89. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  90. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  91. if (operation != null)
  92. {
  93. operation.FinishedOn = DateTime.UtcNow;
  94. operation.Status = 1;//電樁有回覆
  95. operation.EVSE_Status = (int)_confirm.status;//OK
  96. operation.EVSE_Value = _confirm.status.ToString();
  97. db.SaveChanges();
  98. }
  99. }
  100. }
  101. break;
  102. case Actions.SetChargingProfile:
  103. {
  104. SetChargingProfileConfirmation _confirm = confirm as SetChargingProfileConfirmation;
  105. SetChargingProfileRequest _request = _confirm.GetRequest() as SetChargingProfileRequest;
  106. using (var db = maindbContextFactory.CreateDbContext())
  107. {
  108. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  109. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  110. if (operation != null)
  111. {
  112. operation.FinishedOn = DateTime.UtcNow;
  113. operation.Status = 1;//電樁有回覆
  114. operation.EVSE_Status = (int)_confirm.status;//OK
  115. operation.EVSE_Value = _confirm.status.ToString();
  116. db.SaveChanges();
  117. }
  118. }
  119. }
  120. break;
  121. case Actions.GetCompositeSchedule:
  122. {
  123. GetCompositeScheduleConfirmation _confirm = confirm as GetCompositeScheduleConfirmation;
  124. GetCompositeScheduleRequest _request = _confirm.GetRequest() as GetCompositeScheduleRequest;
  125. using (var db = maindbContextFactory.CreateDbContext())
  126. {
  127. var operation = await db.MachineOperateRecord
  128. .Where(x => x.SerialNo == requestId && x.ChargeBoxId == session.ChargeBoxId && x.Status == 0)
  129. .FirstOrDefaultAsync();
  130. if (operation != null)
  131. {
  132. operation.FinishedOn = DateTime.UtcNow;
  133. operation.Status = 1;//電樁有回覆
  134. operation.EVSE_Status = (int)_confirm.status;//OK
  135. operation.EVSE_Value = JsonConvert.SerializeObject(_confirm.chargingSchedule, Formatting.None);
  136. db.SaveChanges();
  137. }
  138. }
  139. }
  140. break;
  141. default:
  142. {
  143. Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
  144. }
  145. break;
  146. }
  147. return result;
  148. }
  149. internal async Task<MessageResult> ReceivedSmartChargingError(Actions action, string errorMsg, ClientData session, string requestId)
  150. {
  151. MessageResult result = new MessageResult() { Success = true };
  152. switch (action)
  153. {
  154. case Actions.ClearChargingProfile:
  155. case Actions.SetChargingProfile:
  156. case Actions.GetCompositeSchedule:
  157. {
  158. using (var db = maindbContextFactory.CreateDbContext())
  159. {
  160. var operation = await db.MachineOperateRecord
  161. .Where(x => x.SerialNo == requestId && x.ChargeBoxId == session.ChargeBoxId && x.Status == 0)
  162. .FirstOrDefaultAsync();
  163. if (operation != null)
  164. {
  165. operation.FinishedOn = DateTime.UtcNow;
  166. operation.Status = 1;//電樁有回覆
  167. operation.EVSE_Status = (int)255;//錯誤
  168. operation.EVSE_Value = errorMsg;
  169. db.SaveChanges();
  170. }
  171. }
  172. }
  173. break;
  174. default:
  175. {
  176. Console.WriteLine(string.Format("Not Implement {0} Logic", action));
  177. }
  178. break;
  179. }
  180. return result;
  181. }
  182. }
  183. }