OuterBusinessService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Packet.Messages.SubTypes;
  3. using EVCB_OCPP.WSServer.Dto;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using NLog;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace EVCB_OCPP.WSServer.Service
  12. {
  13. internal class CPOOuterResponse
  14. {
  15. public CPOOuterResponse()
  16. {
  17. StatusCode = 0;
  18. }
  19. public int StatusCode { set; get; }
  20. public string StatusMessage { set; get; }
  21. public string Data { set; get; }
  22. [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  23. public string SerialNo { set; get; }
  24. [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  25. public string ErrorDetail { set; get; }
  26. }
  27. internal class CustomerSignMaterial
  28. {
  29. internal bool CallsThirdParty { set; get; }
  30. internal string Id { set; get; }
  31. internal string APIUrl { set; get; }
  32. internal string SaltKey { set; get; }
  33. }
  34. public class OuterBusinessService : IBusinessService
  35. {
  36. static private ILogger logger = NLog.LogManager.GetCurrentClassLogger();
  37. private OuterHttpClient httpClient = new OuterHttpClient();
  38. private CustomerSignMaterial signMaterial = null;
  39. public OuterBusinessService(string customerId)
  40. {
  41. signMaterial = GetSign(customerId);
  42. }
  43. async public Task<IdTagInfo> Authorize(string chargeBoxId, string idTag)
  44. {
  45. IdTagInfo result = new IdTagInfo() { status = AuthorizationStatus.Invalid };
  46. try
  47. {
  48. string requestParams = string.Format("charging_auth?ChargeBoxId={0}&IdTag={1}", chargeBoxId, idTag);
  49. var response = await httpClient.Post(signMaterial.APIUrl + requestParams, new Dictionary<string, string>()
  50. {
  51. { "PartnerId",signMaterial.Id}
  52. }, null, signMaterial.SaltKey).ConfigureAwait(false);
  53. if (response.Success)
  54. {
  55. Console.WriteLine(response.Response);
  56. var _httpResult = JsonConvert.DeserializeObject<CPOOuterResponse>(response.Response);
  57. JObject jo = JObject.Parse(_httpResult.Data);
  58. if (jo.ContainsKey("ExpiryDate"))
  59. {
  60. DateTime dt = jo["ExpiryDate"].Value<DateTime>();
  61. result.expiryDate = dt;
  62. }
  63. if (jo.ContainsKey("ParentIdTag"))
  64. {
  65. string _Message = jo["ParentIdTag"].Value<string>();
  66. result.parentIdTag = _Message;
  67. }
  68. if (jo.ContainsKey("Status"))
  69. {
  70. string _Message = jo["Status"].Value<string>();
  71. result.status = (AuthorizationStatus)Enum.Parse(typeof(AuthorizationStatus), _Message);
  72. }
  73. }
  74. else
  75. {
  76. logger.Error(chargeBoxId + " OuterBusinessService.Authorize Fail: " + response.Response);
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. result.status = AuthorizationStatus.Invalid;
  82. logger.Error(chargeBoxId + " OuterBusinessService.Authorize Ex: " + ex.ToString());
  83. }
  84. return result;
  85. }
  86. async public Task NotifyFaultStatus(ErrorDetails details)
  87. {
  88. try
  89. {
  90. if (signMaterial.CallsThirdParty)
  91. {
  92. var response = await httpClient.Post(signMaterial.APIUrl + "connectorfault", new Dictionary<string, string>()
  93. {
  94. { "PartnerId",signMaterial.Id}
  95. }, JsonConvert.SerializeObject(details, GlobalConfig.JSONSERIALIZER_FORMAT), signMaterial.SaltKey).ConfigureAwait(false);
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. logger.Error(details.ChargeBoxId + " OuterBusinessService.NotifyFaultStatus Ex: " + ex.ToString());
  101. }
  102. }
  103. async public Task NotifyConnectorUnplugged(string chargeBoxId, string data)
  104. {
  105. try
  106. {
  107. JObject jo = JObject.Parse(data);
  108. var details = new { ChargeBoxId = chargeBoxId, SessionId = jo["idTx"].Value<Int32>(), Timestamp = jo["timestamp"].Value<DateTime>() };
  109. if (signMaterial.CallsThirdParty)
  110. {
  111. var response = await httpClient.Post(signMaterial.APIUrl + "connectorunplugged", new Dictionary<string, string>()
  112. {
  113. { "PartnerId",signMaterial.Id}
  114. }, JsonConvert.SerializeObject(details, new JsonSerializerSettings()
  115. {
  116. DateTimeZoneHandling = DateTimeZoneHandling.Utc,
  117. NullValueHandling = NullValueHandling.Ignore,
  118. Formatting = Formatting.None
  119. }), signMaterial.SaltKey).ConfigureAwait(false);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. logger.Error(chargeBoxId + " OuterBusinessService.NotifyConnectorUnplugged Ex: " + ex.ToString());
  125. }
  126. }
  127. private CustomerSignMaterial GetSign(string customerId)
  128. {
  129. Guid Id = new Guid(customerId);
  130. CustomerSignMaterial _customer = new CustomerSignMaterial();
  131. using (var db = new MainDBContext())
  132. {
  133. _customer = db.Customer.Where(x => x.Id == Id).Select(x => new CustomerSignMaterial() { Id = x.Id.ToString(), APIUrl = x.ApiUrl, SaltKey = x.ApiKey, CallsThirdParty = x.CallPartnerApiOnSchedule }).FirstOrDefault();
  134. }
  135. return _customer;
  136. }
  137. public Task NotifyConnectorUnplugged(string data)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. }
  142. }