123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using EVCB_OCPP.Domain;
- using EVCB_OCPP.Packet.Messages.SubTypes;
- using EVCB_OCPP.WSServer.Dto;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using NLog;
- 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
- {
- static private ILogger logger = NLog.LogManager.GetCurrentClassLogger();
- private OuterHttpClient httpClient = new OuterHttpClient();
- private CustomerSignMaterial signMaterial = null;
- public OuterBusinessService(string customerId)
- {
- signMaterial = GetSign(customerId);
- }
-
- async public Task<IdTagInfo> Authorize(string chargeBoxId, string idTag)
- {
- IdTagInfo result = new IdTagInfo() { status = AuthorizationStatus.Invalid };
- try
- {
- string requestParams = string.Format("charging_auth?chargeBoxId={0}&IdTag={1}", chargeBoxId, idTag);
- var response = await httpClient.Post(signMaterial.APIUrl + requestParams, new Dictionary<string, string>()
- {
- { "PartnerId",signMaterial.Id}
- }, null, signMaterial.SaltKey);
- if (response.Success)
- {
- var _httpResult = JsonConvert.DeserializeObject<CPOOuterResponse>(response.Response);
- JObject jo = JObject.Parse(_httpResult.Data);
- if (jo.ContainsKey("ExpiryDate"))
- {
- DateTime dt = jo["ExpiryDate"].Value<DateTime>();
- result.expiryDate = dt;
- }
- if (jo.ContainsKey("ParentIdTag"))
- {
- string _Message = jo["ParentIdTag"].Value<string>();
- result.parentIdTag = _Message;
- }
- if (jo.ContainsKey("Status"))
- {
- string _Message = jo["Status"].Value<string>();
- result.status = (AuthorizationStatus)Enum.Parse(typeof(AuthorizationStatus), _Message);
- }
- }
- }
- catch (Exception ex)
- {
- result.status = AuthorizationStatus.Invalid;
- logger.Error(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}
- }, JsonConvert.SerializeObject(details, GlobalConfig.JSONSERIALIZER_FORMAT), signMaterial.SaltKey);
- }
- }
- catch (Exception ex)
- {
- logger.Error(details.ChargeBoxId + " OuterBusinessService.NotifyFaultStatus Ex: " + ex.ToString());
- }
- }
- private CustomerSignMaterial GetSign(string customerId)
- {
- Guid Id = new Guid(customerId);
- CustomerSignMaterial _customer = new CustomerSignMaterial();
- using (var db = new MainDBContext())
- {
- _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();
- }
- return _customer;
- }
- }
- }
|