OuterBusinessService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using EVCB_OCPP.Domain;
  7. using EVCB_OCPP.Packet.Messages.SubTypes;
  8. using EVCB_OCPP.WSServer.Dto;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using NLog;
  12. namespace EVCB_OCPP.WSServer.Service
  13. {
  14. internal class CPOOuterResponse
  15. {
  16. public CPOOuterResponse()
  17. {
  18. StatusCode = 0;
  19. }
  20. public int StatusCode { set; get; }
  21. public string StatusMessage { set; get; }
  22. public string Data { set; get; }
  23. [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  24. public string SerialNo { set; get; }
  25. [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  26. public string ErrorDetail { set; get; }
  27. }
  28. internal class CustomerSignMaterial
  29. {
  30. internal bool CallsThirdParty { set; get; }
  31. internal string Id { set; get; }
  32. internal string APIUrl { set; get; }
  33. internal string SaltKey { set; get; }
  34. }
  35. public class OuterBusinessService : IBusinessService
  36. {
  37. static private ILogger logger = NLog.LogManager.GetCurrentClassLogger();
  38. private OuterHttpClient httpClient = new OuterHttpClient();
  39. private CustomerSignMaterial signMaterial = null;
  40. public OuterBusinessService(string customerId)
  41. {
  42. signMaterial = GetSign(customerId);
  43. }
  44. async public Task<IdTagInfo> Authorize(string chargeBoxId, string idTag)
  45. {
  46. IdTagInfo result = new IdTagInfo() { status = AuthorizationStatus.Invalid };
  47. try
  48. {
  49. string requestParams = string.Format("charging_auth?chargeBoxId={0}&IdTag={1}", chargeBoxId, idTag);
  50. var response = await httpClient.Post(signMaterial.APIUrl + requestParams, new Dictionary<string, string>()
  51. {
  52. { "PartnerId",signMaterial.Id}
  53. }, null, signMaterial.SaltKey);
  54. if (response.Success)
  55. {
  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. }
  75. catch (Exception ex)
  76. {
  77. result.status = AuthorizationStatus.Invalid;
  78. logger.Error(chargeBoxId + " OuterBusinessService.Authorize Ex: " + ex.ToString());
  79. }
  80. return result;
  81. }
  82. async public Task NotifyFaultStatus(ErrorDetails details)
  83. {
  84. try
  85. {
  86. if (signMaterial.CallsThirdParty)
  87. {
  88. var response = await httpClient.Post(signMaterial.APIUrl + "connectorfault", new Dictionary<string, string>()
  89. {
  90. { "PartnerId",signMaterial.Id}
  91. }, JsonConvert.SerializeObject(details, GlobalConfig.JSONSERIALIZER_FORMAT), signMaterial.SaltKey);
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. logger.Error(details.ChargeBoxId + " OuterBusinessService.NotifyFaultStatus Ex: " + ex.ToString());
  97. }
  98. }
  99. private CustomerSignMaterial GetSign(string customerId)
  100. {
  101. Guid Id = new Guid(customerId);
  102. CustomerSignMaterial _customer = new CustomerSignMaterial();
  103. using (var db = new MainDBContext())
  104. {
  105. _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();
  106. }
  107. return _customer;
  108. }
  109. }
  110. }