BusinessServiceFactory.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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);
  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<OuterBusinessService>();
  22. services.AddSingleton<LocalBusinessService>();
  23. services.AddSingleton<IBusinessServiceFactory, BusinessServiceFactory>();
  24. }
  25. }
  26. public class BusinessServiceFactory : IBusinessServiceFactory
  27. {
  28. public BusinessServiceFactory(
  29. IServiceProvider serviceProvider,
  30. IMainDbService mainDbService
  31. //IDbContextFactory<MainDBContext> mainDBContextFactory
  32. )
  33. {
  34. this.serviceProvider = serviceProvider;
  35. this.mainDbService = mainDbService;
  36. //this.mainDBContextFactory = mainDBContextFactory;
  37. }
  38. private readonly IServiceProvider serviceProvider;
  39. private readonly IMainDbService mainDbService;
  40. //private readonly IDbContextFactory<MainDBContext> mainDBContextFactory;
  41. public async Task<IBusinessService> CreateBusinessService(string customerId)
  42. {
  43. bool isCallOut = false;
  44. //using (var db = this.mainDBContextFactory.CreateDbContext())
  45. //{
  46. // isCallOut = await db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => x.CallPartnerApiOnSchedule).FirstOrDefaultAsync();
  47. //}
  48. CustomerSignMaterial _customer = null;
  49. //using (var db = this.mainDBContextFactory.CreateDbContext())
  50. //{
  51. // _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();
  52. //}
  53. var _customerDb = await mainDbService.GetCustomer(customerId);
  54. if (_customerDb is not null)
  55. {
  56. _customer = new CustomerSignMaterial() {
  57. Id = _customerDb.Id.ToString(),
  58. APIUrl = _customerDb.ApiUrl,
  59. SaltKey = _customerDb.ApiKey,
  60. CallsThirdParty = _customerDb.CallPartnerApiOnSchedule
  61. };
  62. }
  63. isCallOut = _customer != null && _customer.CallsThirdParty;
  64. //return isCallOut ? new OuterBusinessService(customerId) : new LocalBusinessService(customerId);
  65. if (isCallOut)
  66. {
  67. OuterBusinessService outerBusinessService = serviceProvider.GetService<OuterBusinessService>();
  68. //outerBusinessService.CustomerId = customerId;
  69. outerBusinessService.CustomerSignMaterial = _customer;
  70. return outerBusinessService;
  71. }
  72. LocalBusinessService toReturn = serviceProvider.GetService<LocalBusinessService>();
  73. toReturn.CustomerId = customerId;
  74. return toReturn;
  75. //return isCallOut ? new OuterBusinessService(customerId) :
  76. }
  77. }