BusinessServiceFactory.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Packet.Messages.SubTypes;
  3. using EVCB_OCPP.WSServer.Dto;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using System;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace EVCB_OCPP.WSServer.Service;
  10. public interface IBusinessService
  11. {
  12. Task<IdTokenInfo> Authorize(string chargeBoxId, string idTag, int? connectorId = null);
  13. Task NotifyFaultStatus(ErrorDetails details);
  14. Task NotifyConnectorUnplugged(string chargeBoxId,string data);
  15. }
  16. public static class BusinessServiceFactoryRegistration
  17. {
  18. public static void AddBusinessServiceFactory(this IServiceCollection services)
  19. {
  20. services.AddSingleton<HttpClientService>();
  21. services.AddSingleton<OuterHttpClient>();
  22. services.AddTransient<OuterBusinessService>();
  23. services.AddTransient<LocalBusinessService>();
  24. services.AddSingleton<IBusinessServiceFactory, BusinessServiceFactory>();
  25. }
  26. }
  27. public class BusinessServiceFactory : IBusinessServiceFactory
  28. {
  29. public BusinessServiceFactory(
  30. IServiceProvider serviceProvider,
  31. IMainDbService mainDbService
  32. //IDbContextFactory<MainDBContext> mainDBContextFactory
  33. )
  34. {
  35. this.serviceProvider = serviceProvider;
  36. this.mainDbService = mainDbService;
  37. //this.mainDBContextFactory = mainDBContextFactory;
  38. }
  39. private readonly IServiceProvider serviceProvider;
  40. private readonly IMainDbService mainDbService;
  41. //private readonly IDbContextFactory<MainDBContext> mainDBContextFactory;
  42. public async Task<IBusinessService> CreateBusinessService(string customerId)
  43. {
  44. bool isCallOut = false;
  45. //using (var db = this.mainDBContextFactory.CreateDbContextAsync())
  46. //{
  47. // isCallOut = await db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => x.CallPartnerApiOnSchedule).FirstOrDefaultAsync();
  48. //}
  49. CustomerSignMaterial _customer = null;
  50. //using (var db = this.mainDBContextFactory.CreateDbContextAsync())
  51. //{
  52. // _customer = await db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => new CustomerSignMaterial() { Id = x.Id.ToString(), APIUrl = x.ApiUrl, SaltKey = x.ApiKey, CallsThirdParty = x.CallPartnerApiOnSchedule }).FirstOrDefaultAsync();
  53. //}
  54. var _customerDb = await mainDbService.GetCustomer(customerId);
  55. if (_customerDb is not null)
  56. {
  57. _customer = new CustomerSignMaterial() {
  58. Id = _customerDb.Id.ToString(),
  59. APIUrl = _customerDb.ApiUrl,
  60. SaltKey = _customerDb.ApiKey,
  61. CallsThirdParty = _customerDb.CallPartnerApiOnSchedule
  62. };
  63. }
  64. isCallOut = _customer != null && _customer.CallsThirdParty;
  65. //return isCallOut ? new OuterBusinessService(customerId) : new LocalBusinessService(customerId);
  66. if (isCallOut)
  67. {
  68. OuterBusinessService outerBusinessService = serviceProvider.GetService<OuterBusinessService>();
  69. //outerBusinessService.CustomerId = customerId;
  70. outerBusinessService.CustomerSignMaterial = _customer;
  71. return outerBusinessService;
  72. }
  73. LocalBusinessService toReturn = serviceProvider.GetService<LocalBusinessService>();
  74. toReturn.CustomerId = customerId;
  75. return toReturn;
  76. //return isCallOut ? new OuterBusinessService(customerId) :
  77. }
  78. }