SmartChargingProfileHandler.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace EVCB_OCPP.WSServer.Message
  15. {
  16. internal partial class ProfileHandler
  17. {
  18. internal void SetChargingProfile(string chargeBoxId, decimal value, ChargingRateUnitType unit)
  19. {
  20. using (var db = new MainDBContext())
  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.Now,
  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 void ClearChargingProfile(string chargeBoxId)
  57. {
  58. using (var db = new MainDBContext())
  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.Now,
  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 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 = new MainDBContext())
  88. {
  89. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  90. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  91. if (operation != null)
  92. {
  93. operation.FinishedOn = DateTime.Now;
  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 = new MainDBContext())
  107. {
  108. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  109. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  110. if (operation != null)
  111. {
  112. operation.FinishedOn = DateTime.Now;
  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 = new MainDBContext())
  126. {
  127. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  128. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  129. if (operation != null)
  130. {
  131. operation.FinishedOn = DateTime.Now;
  132. operation.Status = 1;//電樁有回覆
  133. operation.EVSE_Status = (int)_confirm.status;//OK
  134. operation.EVSE_Value = JsonConvert.SerializeObject(_confirm.chargingSchedule, Formatting.None);
  135. db.SaveChanges();
  136. }
  137. }
  138. }
  139. break;
  140. default:
  141. {
  142. Console.WriteLine(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
  143. }
  144. break;
  145. }
  146. return result;
  147. }
  148. internal MessageResult ReceivedSmartChargingError(Actions action, string errorMsg, ClientData session, string requestId)
  149. {
  150. MessageResult result = new MessageResult() { Success = true };
  151. switch (action)
  152. {
  153. case Actions.ClearChargingProfile:
  154. case Actions.SetChargingProfile:
  155. case Actions.GetCompositeSchedule:
  156. {
  157. using (var db = new MainDBContext())
  158. {
  159. var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  160. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
  161. if (operation != null)
  162. {
  163. operation.FinishedOn = DateTime.Now;
  164. operation.Status = 1;//電樁有回覆
  165. operation.EVSE_Status = (int)255;//錯誤
  166. operation.EVSE_Value = errorMsg;
  167. db.SaveChanges();
  168. }
  169. }
  170. }
  171. break;
  172. default:
  173. {
  174. Console.WriteLine(string.Format("Not Implement {0} Logic", action));
  175. }
  176. break;
  177. }
  178. return result;
  179. }
  180. }
  181. }