SmartChargingProfileHandler.cs 9.0 KB

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