OuterBusinessService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. private string CustomerId = string.Empty;
  40. public OuterBusinessService(string customerId)
  41. {
  42. CustomerId = customerId;
  43. signMaterial = GetSign(customerId);
  44. }
  45. async public Task<IdTokenInfo> Authorize(string chargeBoxId, string idTag)
  46. {
  47. await Task.Delay(10);
  48. IdTokenInfo result = new IdTokenInfo() { IdTagInfo = new IdTagInfo() { status = AuthorizationStatus.Invalid } };
  49. try
  50. {
  51. string requestParams = string.Format("charging_auth?ChargeBoxId={0}&IdTag={1}", chargeBoxId, idTag);
  52. // if (CustomerId.ToLower() == "9e6bfdcc-09fb-4dab-a428-43fe507600a3")
  53. {
  54. logger.Info(chargeBoxId + " Charging Monitor======================================>");
  55. logger.Info(signMaterial.APIUrl + requestParams);
  56. }
  57. var response = await httpClient.Post(signMaterial.APIUrl + requestParams, new Dictionary<string, string>()
  58. {
  59. { "PartnerId",signMaterial.Id}
  60. }, null, signMaterial.SaltKey).ConfigureAwait(false);
  61. // if (CustomerId.ToLower() == "9e6bfdcc-09fb-4dab-a428-43fe507600a3")
  62. {
  63. logger.Info(JsonConvert.SerializeObject(response));
  64. }
  65. if (response.Success)
  66. {
  67. Console.WriteLine(response.Response);
  68. var _httpResult = JsonConvert.DeserializeObject<CPOOuterResponse>(response.Response);
  69. JObject jo = JObject.Parse(_httpResult.Data);
  70. if (jo.ContainsKey("ExpiryDate"))
  71. {
  72. DateTime dt = jo["ExpiryDate"].Value<DateTime>();
  73. result.IdTagInfo.expiryDate = dt;
  74. }
  75. if (jo.ContainsKey("ParentIdTag"))
  76. {
  77. string _Message = jo["ParentIdTag"].Value<string>();
  78. result.IdTagInfo.parentIdTag = _Message;
  79. }
  80. if (jo.ContainsKey("ChargePointFee"))
  81. {
  82. result.ChargePointFee = new List<ChargePointFee>();
  83. for(int i=0;i< jo["ChargePointFee"].Count();i++)
  84. {
  85. result.ChargePointFee.Add(jo["ChargePointFee"][i].ToObject<ChargePointFee>());
  86. }
  87. }
  88. if (jo.ContainsKey("ChargepointFee"))
  89. {
  90. result.ChargePointFee = new List<ChargePointFee>();
  91. for (int i = 0; i < jo["ChargepointFee"].Count(); i++)
  92. {
  93. result.ChargePointFee.Add(jo["ChargepointFee"][i].ToObject<ChargePointFee>());
  94. }
  95. }
  96. if (jo.ContainsKey("AccountBalance"))
  97. {
  98. decimal accountBalance = jo["AccountBalance"].Value<decimal>();
  99. result.AccountBalance = accountBalance;
  100. }
  101. if (jo.ContainsKey("Status"))
  102. {
  103. string _Message = jo["Status"].Value<string>();
  104. result.IdTagInfo.status = (AuthorizationStatus)Enum.Parse(typeof(AuthorizationStatus), _Message);
  105. }
  106. }
  107. else
  108. {
  109. logger.Error(chargeBoxId + " OuterBusinessService.Authorize Fail: " + response.Response);
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. result.IdTagInfo.status = AuthorizationStatus.Invalid;
  115. logger.Error(chargeBoxId + " OuterBusinessService.Authorize Ex: " + ex.ToString());
  116. }
  117. return result;
  118. }
  119. async public Task NotifyFaultStatus(ErrorDetails details)
  120. {
  121. try
  122. {
  123. if (signMaterial.CallsThirdParty)
  124. {
  125. logger.Info(" NotifyFaultStatus======================================>");
  126. logger.Info(JsonConvert.SerializeObject(details));
  127. var response = await httpClient.Post(signMaterial.APIUrl + "connectorfault", new Dictionary<string, string>()
  128. {
  129. { "PartnerId",signMaterial.Id}
  130. }, details, signMaterial.SaltKey).ConfigureAwait(false);
  131. logger.Info(JsonConvert.SerializeObject(response));
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. logger.Error(details.ChargeBoxId + " OuterBusinessService.NotifyFaultStatus Ex: " + ex.ToString());
  137. }
  138. }
  139. async public Task NotifyConnectorUnplugged(string chargeBoxId, string data)
  140. {
  141. try
  142. {
  143. JObject jo = JObject.Parse(data);
  144. var details = new { ChargeBoxId = chargeBoxId, SessionId = jo["idTx"].Value<Int32>(), Timestamp = jo["timestamp"].Value<DateTime>() };
  145. if (signMaterial.CallsThirdParty)
  146. {
  147. var response = await httpClient.Post(signMaterial.APIUrl + "connectorunplugged", new Dictionary<string, string>()
  148. {
  149. { "PartnerId",signMaterial.Id}
  150. }, details, signMaterial.SaltKey).ConfigureAwait(false);
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. logger.Error(chargeBoxId + " OuterBusinessService.NotifyConnectorUnplugged Ex: " + ex.ToString());
  156. }
  157. }
  158. private CustomerSignMaterial GetSign(string customerId)
  159. {
  160. Guid Id = new Guid(customerId);
  161. CustomerSignMaterial _customer = new CustomerSignMaterial();
  162. using (var db = new MainDBContext())
  163. {
  164. _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();
  165. }
  166. return _customer;
  167. }
  168. public Task NotifyConnectorUnplugged(string data)
  169. {
  170. throw new NotImplementedException();
  171. }
  172. }
  173. }