123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- using EVCB_OCPP.Domain;
- using EVCB_OCPP.Packet.Messages.SubTypes;
- using EVCB_OCPP.WSServer.Dto;
- using EVCB_OCPP.WSServer.Service.DbService;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using NLog;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace EVCB_OCPP.WSServer.Service
- {
- internal class CPOOuterResponse
- {
- public CPOOuterResponse()
- {
- StatusCode = 0;
- }
- public int StatusCode { set; get; }
- public string StatusMessage { set; get; }
- public string Data { set; get; }
- [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
- public string SerialNo { set; get; }
- [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
- public string ErrorDetail { set; get; }
- }
- internal class CustomerSignMaterial
- {
- internal bool CallsThirdParty { set; get; }
- internal string Id { set; get; }
- internal string APIUrl { set; get; }
- internal string SaltKey { set; get; }
- }
- public class OuterBusinessService : IBusinessService
- {
- private readonly ILogger<OuterBusinessService> logger;
- private readonly IDbContextFactory<MainDBContext> maindbContextFactory;
- private readonly IMainDbService mainDbService;
- private readonly OuterHttpClient httpClient;
- private string _CustomerId = string.Empty;
- private CustomerSignMaterial signMaterial = null;
- public string CustomerId
- {
- get => _CustomerId;
- set
- {
- _CustomerId = value;
- signMaterial = GetSign(_CustomerId).Result;
- }
- }
- internal CustomerSignMaterial CustomerSignMaterial
- {
- get => signMaterial;
- set
- {
- signMaterial = value;
- _CustomerId = signMaterial.Id;
- }
- }
- public OuterBusinessService(
- ILogger<OuterBusinessService> logger,
- IDbContextFactory<MainDBContext> maindbContextFactory,
- IMainDbService mainDbService,
- OuterHttpClient httpClient)
- {
- this.logger = logger;
- this.maindbContextFactory = maindbContextFactory;
- this.mainDbService = mainDbService;
- this.httpClient = httpClient;
- }
- async public Task<IdTokenInfo> Authorize(string chargeBoxId, string idTag, int? connectorId = null)
- {
- //return new IdTokenInfo() { IdTagInfo = new IdTagInfo()
- //{
- // expiryDate = DateTime.UtcNow.AddDays(1),
- // status = AuthorizationStatus.Accepted
- //} };
- //await Task.Delay(10);
- IdTokenInfo result = new IdTokenInfo() { IdTagInfo = new IdTagInfo() { status = AuthorizationStatus.Invalid } };
- try
- {
- logger.LogInformation(chargeBoxId + " Charging Monitor======================================>");
- string requestParams = idTag.StartsWith("vid:") ? await GetRequestParamsAsPnC(chargeBoxId, idTag, connectorId) : GetRequestParamsAsNormal(chargeBoxId, idTag);
- logger.LogInformation($"{chargeBoxId} Authorize : {signMaterial.APIUrl + requestParams}");
- HttpResult response = await httpClient.Post(signMaterial.APIUrl + requestParams, new Dictionary<string, string>()
- {
- { "PartnerId",signMaterial.Id}
- }, requestBody: null, saltkey: signMaterial.SaltKey).ConfigureAwait(false);
- logger.LogInformation($"{chargeBoxId} response : {JsonConvert.SerializeObject(response)}");
- if (response.Success)
- {
- //Console.WriteLine(response.Response);
- var _httpResult = JsonConvert.DeserializeObject<CPOOuterResponse>(response.Response);
- JObject jo = JObject.Parse(_httpResult.Data);
- if (jo.ContainsKey("ExpiryDate"))
- {
- DateTime dt = jo["ExpiryDate"].Value<DateTime>();
- result.IdTagInfo.expiryDate = dt;
- }
- if (jo.ContainsKey("ParentIdTag"))
- {
- string _Message = jo["ParentIdTag"].Value<string>();
- result.IdTagInfo.parentIdTag = _Message;
- }
- if (jo.ContainsKey("ChargePointFee"))
- {
-
- for(int i=0;i< jo["ChargePointFee"].Count();i++)
- {
- if(i==0)
- {
- result.ChargePointFee = new List<ChargePointFee>();
- }
- result.ChargePointFee.Add(jo["ChargePointFee"][i].ToObject<ChargePointFee>());
- }
-
- }
- if (jo.ContainsKey("ChargepointFee"))
- {
- for (int i = 0; i < jo["ChargepointFee"].Count(); i++)
- {
- if (i == 0)
- {
- result.ChargePointFee = new List<ChargePointFee>();
- }
- result.ChargePointFee.Add(jo["ChargepointFee"][i].ToObject<ChargePointFee>());
- }
- }
- if (jo.ContainsKey("AccountBalance"))
- {
- decimal accountBalance = jo["AccountBalance"].Value<decimal>();
- result.AccountBalance = accountBalance;
- }
- if (jo.ContainsKey("Status"))
- {
- string _Message = jo["Status"].Value<string>();
- result.IdTagInfo.status = (AuthorizationStatus)Enum.Parse(typeof(AuthorizationStatus), _Message);
- }
- }
- else
- {
- logger.LogError(chargeBoxId + " OuterBusinessService.Authorize Fail: " + response.Response);
- }
- }
- catch (Exception ex)
- {
- result.IdTagInfo.status = AuthorizationStatus.Invalid;
- logger.LogError(chargeBoxId + " OuterBusinessService.Authorize Ex: " + ex.ToString());
- }
- return result;
- }
- async public Task NotifyFaultStatus(ErrorDetails details)
- {
- try
- {
- if (signMaterial.CallsThirdParty)
- {
- var response = await httpClient.Post(signMaterial.APIUrl + "connectorfault", new Dictionary<string, string>()
- {
- { "PartnerId",signMaterial.Id}
- }, details, signMaterial.SaltKey).ConfigureAwait(false);
- }
- }
- catch (Exception ex)
- {
- logger.LogError(details.ChargeBoxId + " OuterBusinessService.NotifyFaultStatus Ex: " + ex.ToString());
- }
- }
- async public Task NotifyConnectorUnplugged(string chargeBoxId, string data)
- {
- try
- {
- JObject jo = JObject.Parse(data);
- var details = new { ChargeBoxId = chargeBoxId, SessionId = jo["idTx"].Value<Int32>(), Timestamp = jo["timestamp"].Value<DateTime>() };
- if (signMaterial.CallsThirdParty)
- {
- var response = await httpClient.Post(signMaterial.APIUrl + "connectorunplugged", new Dictionary<string, string>()
- {
- { "PartnerId",signMaterial.Id}
- }, details, signMaterial.SaltKey).ConfigureAwait(false);
- }
- }
- catch (Exception ex)
- {
- logger.LogError(chargeBoxId + " OuterBusinessService.NotifyConnectorUnplugged Ex: " + ex.ToString());
- }
- }
- private async Task<CustomerSignMaterial> GetSign(string customerId)
- {
- Guid Id = new Guid(customerId);
- CustomerSignMaterial _customer = new CustomerSignMaterial();
- //using (var db = new MainDBContext())
- //using (var db = maindbContextFactory.CreateDbContextAsync())
- //{
- // _customer = await db.Customer.Where(x => x.Id == Id).Select(x => new CustomerSignMaterial() { Id = x.Id.ToString(), APIUrl = x.ApiUrl, SaltKey = x.ApiKey, CallsThirdParty = x.CallPartnerApiOnSchedule }).FirstOrDefaultAsync();
- //}
- var _customerDb = await mainDbService.GetCustomer(Id);
- if (_customerDb is not null)
- {
- _customer.Id = _customerDb.Id.ToString();
- _customer.APIUrl = _customerDb.ApiUrl;
- _customer.SaltKey = _customerDb.ApiKey;
- _customer.CallsThirdParty = _customerDb.CallPartnerApiOnSchedule;
- }
- return _customer;
- }
- private async ValueTask<string> GetRequestParamsAsPnC(string chargeBoxId, string idTag, int? connectorId)
- {
- idTag = idTag.Replace("vid:", "");
- if (connectorId is null)
- {
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- var connectorStatuses = await db.ConnectorStatus.Where(x => x.ChargeBoxId == chargeBoxId).
- Select(x => new { x.ConnectorId, x.Status, x.CreatedOn }).ToListAsync();
- var connectorStatus = connectorStatuses.Where(x => x.Status == 2).OrderByDescending(x => x.CreatedOn).FirstOrDefault();
- if (connectorStatus != null)
- {
- connectorId = connectorStatus.ConnectorId;
- }
- }
- }
- return string.Format("charging_auth?ChargeBoxId={0}&ConnectorId={1}&IdTag={2}", chargeBoxId, connectorId, idTag);
- }
- private string GetRequestParamsAsNormal(string chargeBoxId, string idTag)
- {
- return string.Format("charging_auth?ChargeBoxId={0}&IdTag={1}", chargeBoxId, idTag);
- }
- public Task NotifyConnectorUnplugged(string data)
- {
- throw new NotImplementedException();
- }
- }
- }
|