using EVCB_OCPP.Domain; using EVCB_OCPP.Packet.Messages.SubTypes; using EVCB_OCPP.WSServer.Helper; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EVCB_OCPP.WSServer.Service; public class MeterValueDbService { private readonly IDbContextFactory meterValueDbContextFactory; private readonly ILoggerFactory loggerFactory; //private GroupSingleHandler insertMeterValueHandler; public MeterValueDbService(IDbContextFactory meterValueDbContextFactory, ILoggerFactory loggerFactory) { this.meterValueDbContextFactory = meterValueDbContextFactory; this.loggerFactory = loggerFactory; //InitInsertMeterValueHandler(); } public async Task InsertAsync(string chargeBoxId, byte connectorId, decimal value, DateTime createdOn , int contextId, int formatId, int measurandId, int phaseId , int locationId, int unitId, int transactionId) { using var db = await meterValueDbContextFactory.CreateDbContextAsync(); string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId, @ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId"; var param = new InsertMeterValueParam(chargeBoxId, connectorId, value, createdOn, contextId, formatId, measurandId, phaseId, locationId, unitId, transactionId); List parameter = new List(); parameter.AddInsertMeterValueRecordSqlParameters( chargeBoxId: param.chargeBoxId , connectorId: (byte)param.connectorId , value: param.value , createdOn: param.createdOn , contextId: param.contextId , formatId: param.formatId , measurandId: param.measurandId , phaseId: param.phaseId , locationId: param.locationId , unitId: param.unitId , transactionId: param.transactionId); await db.Database.ExecuteSqlRawAsync(sp, parameter.ToArray()); //return insertMeterValueHandler.HandleAsync(new InsertMeterValueParam(chargeBoxId, connectorId, value, createdOn, contextId, formatId, measurandId, phaseId, locationId, unitId, transactionId)); } // private void InitInsertMeterValueHandler() // { // if (insertMeterValueHandler is not null) // { // throw new Exception($"{nameof(InitInsertMeterValueHandler)} should only called once"); // } // insertMeterValueHandler = new GroupSingleHandler(async (parms) => { // using var db = await meterValueDbContextFactory.CreateDbContextAsync(); // using var trans = await db.Database.BeginTransactionAsync(); // string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId," + //"@ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId"; // foreach (var param in parms) // { // List parameter = new List(); // parameter.AddInsertMeterValueRecordSqlParameters( // chargeBoxId: param.chargeBoxId // , connectorId: (byte)param.connectorId // , value: param.value // , createdOn: param.createdOn // , contextId: param.contextId // , formatId: param.formatId // , measurandId: param.measurandId // , phaseId: param.phaseId // , locationId: param.locationId // , unitId: param.unitId // , transactionId: param.transactionId); // await db.Database.ExecuteSqlRawAsync(sp, parameter.ToArray()); // } // await trans.CommitAsync(); // } // , loggerFactory.CreateLogger("InsertMeterValueHandler")); // } } public record InsertMeterValueParam(string chargeBoxId, byte connectorId, decimal value, DateTime createdOn , int contextId, int formatId, int measurandId, int phaseId , int locationId, int unitId, int transactionId);