BusinessServiceFactory.cs 3.9 KB

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