using EVCB_OCPP.Domain; using EVCB_OCPP.Domain.Models.Database; using EVCB_OCPP.Packet.Messages.SubTypes; using EVCB_OCPP.WSServer.Dto; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading.Tasks; namespace EVCB_OCPP.WSServer.Service; public interface IBusinessService { Task Authorize(string chargeBoxId, string idTag); Task NotifyFaultStatus(ErrorDetails details); Task NotifyConnectorUnplugged(string chargeBoxId,string data); } public static class BusinessServiceFactoryRegistration { public static void AddBusinessServiceFactory(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); } } public class BusinessServiceFactory : IBusinessServiceFactory { public BusinessServiceFactory( IServiceProvider serviceProvider, IDbContextFactory mainDBContextFactory) { this.serviceProvider = serviceProvider; this.mainDBContextFactory = mainDBContextFactory; } private readonly IServiceProvider serviceProvider; private readonly IDbContextFactory mainDBContextFactory; public async Task CreateBusinessService(string customerId) { bool isCallOut = false; //using (var db = this.mainDBContextFactory.CreateDbContext()) //{ // isCallOut = await db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => x.CallPartnerApiOnSchedule).FirstOrDefaultAsync(); //} CustomerSignMaterial _customer; using (var db = this.mainDBContextFactory.CreateDbContext()) { _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(); } isCallOut = _customer != null && _customer.CallsThirdParty; //return isCallOut ? new OuterBusinessService(customerId) : new LocalBusinessService(customerId); if (isCallOut) { OuterBusinessService outerBusinessService = serviceProvider.GetService(); //outerBusinessService.CustomerId = customerId; outerBusinessService.CustomerSignMaterial = _customer; return outerBusinessService; } LocalBusinessService toReturn = serviceProvider.GetService(); toReturn.CustomerId = customerId; return toReturn; //return isCallOut ? new OuterBusinessService(customerId) : } }