BusinessServiceFactory.cs 3.5 KB

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