|
@@ -75,7 +75,9 @@ internal partial class ProfileHandler
|
|
|
private readonly ILogger logger;
|
|
|
private readonly string webConnectionString;// = ConfigurationManager.ConnectionStrings[].ConnectionString;
|
|
|
private readonly IDbContextFactory<MainDBContext> maindbContextFactory;
|
|
|
- private readonly IDbContextFactory<MeterValueDBContext> metervaluedbContextFactory;
|
|
|
+ private readonly MeterValueDbService meterValueDbService;
|
|
|
+
|
|
|
+ //private readonly IDbContextFactory<MeterValueDBContext> metervaluedbContextFactory;
|
|
|
private readonly IBusinessServiceFactory businessServiceFactory;
|
|
|
private readonly IMainDbService mainDbService;
|
|
|
private OuterHttpClient httpClient = new OuterHttpClient();
|
|
@@ -83,7 +85,8 @@ internal partial class ProfileHandler
|
|
|
public ProfileHandler(
|
|
|
IConfiguration configuration,
|
|
|
IDbContextFactory<MainDBContext> maindbContextFactory,
|
|
|
- IDbContextFactory<MeterValueDBContext> metervaluedbContextFactory,
|
|
|
+ //IDbContextFactory<MeterValueDBContext> metervaluedbContextFactory,
|
|
|
+ MeterValueDbService meterValueDbService,
|
|
|
IBusinessServiceFactory businessServiceFactory,
|
|
|
IMainDbService mainDbService,
|
|
|
ILogger<ProfileHandler> logger)
|
|
@@ -92,8 +95,9 @@ internal partial class ProfileHandler
|
|
|
|
|
|
this.logger = logger;
|
|
|
this.maindbContextFactory = maindbContextFactory;
|
|
|
+ this.meterValueDbService = meterValueDbService;
|
|
|
this.mainDbService = mainDbService;
|
|
|
- this.metervaluedbContextFactory = metervaluedbContextFactory;
|
|
|
+ //this.metervaluedbContextFactory = metervaluedbContextFactory;
|
|
|
this.businessServiceFactory = businessServiceFactory;
|
|
|
}
|
|
|
|
|
@@ -366,70 +370,64 @@ internal partial class ProfileHandler
|
|
|
if (_request.meterValue.Count > 0)
|
|
|
{
|
|
|
|
|
|
-
|
|
|
- using (var db = await metervaluedbContextFactory.CreateDbContextAsync())
|
|
|
+ foreach (var item in _request.meterValue)
|
|
|
{
|
|
|
- foreach (var item in _request.meterValue)
|
|
|
+ if (_request.transactionId.HasValue)
|
|
|
{
|
|
|
- if (_request.transactionId.HasValue)
|
|
|
- {
|
|
|
- decimal meterStart = 0;
|
|
|
- var energy_Register = item.sampledValue.Where(x => x.measurand == Measurand.Energy_Active_Import_Register).FirstOrDefault();
|
|
|
-
|
|
|
- if (energy_Register != null)
|
|
|
- {
|
|
|
- decimal energyRegister = decimal.Parse(energy_Register.value);
|
|
|
- energyRegister = energy_Register.unit.Value == UnitOfMeasure.kWh ? decimal.Multiply(energyRegister, 1000) : energyRegister;
|
|
|
+ decimal meterStart = 0;
|
|
|
+ var energy_Register = item.sampledValue.Where(x => x.measurand == Measurand.Energy_Active_Import_Register).FirstOrDefault();
|
|
|
|
|
|
+ if (energy_Register != null)
|
|
|
+ {
|
|
|
+ decimal energyRegister = decimal.Parse(energy_Register.value);
|
|
|
+ energyRegister = energy_Register.unit.Value == UnitOfMeasure.kWh ? decimal.Multiply(energyRegister, 1000) : energyRegister;
|
|
|
|
|
|
- using (var maindb = maindbContextFactory.CreateDbContext())
|
|
|
- {
|
|
|
- meterStart = await maindb.TransactionRecord
|
|
|
- .Where(x => x.Id == _request.transactionId.Value).Select(x => x.MeterStart)
|
|
|
- .FirstOrDefaultAsync();
|
|
|
- }
|
|
|
|
|
|
- item.sampledValue.Add(new SampledValue()
|
|
|
- {
|
|
|
- context = ReadingContext.Sample_Periodic,
|
|
|
- format = ValueFormat.Raw,
|
|
|
- location = Location.Outlet,
|
|
|
- phase = item.sampledValue.Where(x => x.measurand == Measurand.Energy_Active_Import_Register).Select(x => x.phase).FirstOrDefault(),
|
|
|
- unit = UnitOfMeasure.Wh,
|
|
|
- measurand = Measurand.TotalEnergy,
|
|
|
- value = decimal.Subtract(energyRegister, meterStart).ToString()
|
|
|
- });
|
|
|
+ using (var maindb = maindbContextFactory.CreateDbContext())
|
|
|
+ {
|
|
|
+ meterStart = await maindb.TransactionRecord
|
|
|
+ .Where(x => x.Id == _request.transactionId.Value).Select(x => x.MeterStart)
|
|
|
+ .FirstOrDefaultAsync();
|
|
|
}
|
|
|
|
|
|
+ item.sampledValue.Add(new SampledValue()
|
|
|
+ {
|
|
|
+ context = ReadingContext.Sample_Periodic,
|
|
|
+ format = ValueFormat.Raw,
|
|
|
+ location = Location.Outlet,
|
|
|
+ phase = item.sampledValue.Where(x => x.measurand == Measurand.Energy_Active_Import_Register).Select(x => x.phase).FirstOrDefault(),
|
|
|
+ unit = UnitOfMeasure.Wh,
|
|
|
+ measurand = Measurand.TotalEnergy,
|
|
|
+ value = decimal.Subtract(energyRegister, meterStart).ToString()
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- foreach (var sampleVaule in item.sampledValue)
|
|
|
- {
|
|
|
-
|
|
|
- decimal value = Convert.ToDecimal(sampleVaule.value);
|
|
|
-
|
|
|
- string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId," +
|
|
|
- "@ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId";
|
|
|
+ }
|
|
|
|
|
|
- List<SqlParameter> parameter = new List<SqlParameter>();
|
|
|
- parameter.AddInsertMeterValueRecordSqlParameters(
|
|
|
- chargeBoxId: session.ChargeBoxId
|
|
|
- ,connectorId: (byte)_request.connectorId
|
|
|
- ,value: value
|
|
|
- ,createdOn: item.timestamp
|
|
|
- ,contextId: sampleVaule.context.HasValue ? (int)sampleVaule.context : 0
|
|
|
- ,formatId: sampleVaule.format.HasValue ? (int)sampleVaule.format : 0
|
|
|
- ,measurandId: sampleVaule.measurand.HasValue ? (int)sampleVaule.measurand : 0
|
|
|
- ,phaseId: sampleVaule.phase.HasValue ? (int)sampleVaule.phase : 0
|
|
|
- ,locationId: sampleVaule.location.HasValue ? (int)sampleVaule.location : 0
|
|
|
- ,unitId: sampleVaule.unit.HasValue ? (int)sampleVaule.unit : 0
|
|
|
- ,transactionId: _request.transactionId.HasValue ? _request.transactionId.Value : -1);
|
|
|
-
|
|
|
- await db.Database.ExecuteSqlRawAsync(sp, parameter.ToArray());
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
+ List<Task> insertTasks = new();
|
|
|
+ foreach (var item in _request.meterValue)
|
|
|
+ {
|
|
|
+ foreach (var sampleVaule in item.sampledValue)
|
|
|
+ {
|
|
|
+ decimal value = Convert.ToDecimal(sampleVaule.value);
|
|
|
+ var task = meterValueDbService.InsertAsync(
|
|
|
+ chargeBoxId: session.ChargeBoxId
|
|
|
+ , connectorId: (byte)_request.connectorId
|
|
|
+ , value: value
|
|
|
+ , createdOn: item.timestamp
|
|
|
+ , contextId: sampleVaule.context.HasValue ? (int)sampleVaule.context : 0
|
|
|
+ , formatId: sampleVaule.format.HasValue ? (int)sampleVaule.format : 0
|
|
|
+ , measurandId: sampleVaule.measurand.HasValue ? (int)sampleVaule.measurand : 0
|
|
|
+ , phaseId: sampleVaule.phase.HasValue ? (int)sampleVaule.phase : 0
|
|
|
+ , locationId: sampleVaule.location.HasValue ? (int)sampleVaule.location : 0
|
|
|
+ , unitId: sampleVaule.unit.HasValue ? (int)sampleVaule.unit : 0
|
|
|
+ , transactionId: _request.transactionId.HasValue ? _request.transactionId.Value : -1);
|
|
|
+ insertTasks.Add(task);
|
|
|
}
|
|
|
}
|
|
|
+ await Task.WhenAll(insertTasks);
|
|
|
}
|
|
|
|
|
|
// if (energy_kwh > 0)
|
|
@@ -739,20 +737,14 @@ internal partial class ProfileHandler
|
|
|
if (_request.transactionData != null &&
|
|
|
_request.transactionData.Count > 0)
|
|
|
{
|
|
|
- using (var _meterDb = await metervaluedbContextFactory.CreateDbContextAsync())
|
|
|
+ List<Task> insertTasks = new();
|
|
|
+ foreach (var item in _request.transactionData)
|
|
|
{
|
|
|
- foreach (var item in _request.transactionData)
|
|
|
- foreach (var sampleVaule in item.sampledValue)
|
|
|
- {
|
|
|
- decimal value = Convert.ToDecimal(sampleVaule.value);
|
|
|
-
|
|
|
-
|
|
|
- string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId," +
|
|
|
- "@ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId";
|
|
|
-
|
|
|
- List<SqlParameter> parameter = new List<SqlParameter>();
|
|
|
- parameter.AddInsertMeterValueRecordSqlParameters(
|
|
|
- chargeBoxId: session.ChargeBoxId
|
|
|
+ foreach (var sampleVaule in item.sampledValue)
|
|
|
+ {
|
|
|
+ decimal value = Convert.ToDecimal(sampleVaule.value);
|
|
|
+ var task = meterValueDbService.InsertAsync(
|
|
|
+ chargeBoxId: session.ChargeBoxId
|
|
|
, connectorId: (byte)_ConnectorId
|
|
|
, value: value
|
|
|
, createdOn: item.timestamp
|
|
@@ -762,15 +754,11 @@ internal partial class ProfileHandler
|
|
|
, phaseId: sampleVaule.phase.HasValue ? (int)sampleVaule.phase : 0
|
|
|
, locationId: sampleVaule.location.HasValue ? (int)sampleVaule.location : 0
|
|
|
, unitId: sampleVaule.unit.HasValue ? (int)sampleVaule.unit : 0
|
|
|
- , transactionId: _request.transactionId
|
|
|
- );
|
|
|
-
|
|
|
- _meterDb.Database.ExecuteSqlRaw(sp, parameter.ToArray());
|
|
|
- }
|
|
|
-
|
|
|
+ , transactionId: _request.transactionId);
|
|
|
+ insertTasks.Add(task);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ await Task.WhenAll(insertTasks);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -866,11 +854,11 @@ internal partial class ProfileHandler
|
|
|
|
|
|
//if (action == Actions.Heartbeat)
|
|
|
//{
|
|
|
- watch.Stop();
|
|
|
- if (watch.ElapsedMilliseconds / 1000 > 3)
|
|
|
- {
|
|
|
- logger.LogError("Processing " + action.ToString() + " costs " + watch.ElapsedMilliseconds / 1000 + " seconds"); ;
|
|
|
- }
|
|
|
+ watch.Stop();
|
|
|
+ if (watch.ElapsedMilliseconds / 1000 > 3)
|
|
|
+ {
|
|
|
+ logger.LogError("Processing " + action.ToString() + " costs " + watch.ElapsedMilliseconds / 1000 + " seconds"); ;
|
|
|
+ }
|
|
|
//}
|
|
|
return result;
|
|
|
}
|
|
@@ -1178,28 +1166,18 @@ internal partial class ProfileHandler
|
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
|
- using (var meterdb = await metervaluedbContextFactory.CreateDbContextAsync())
|
|
|
- {
|
|
|
- string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId," +
|
|
|
- "@ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId";
|
|
|
-
|
|
|
- List<SqlParameter> parameter = new List<SqlParameter>();
|
|
|
- parameter.AddInsertMeterValueRecordSqlParameters(
|
|
|
- chargeBoxId: session.ChargeBoxId,
|
|
|
- connectorId: feedto.ConnectorId,
|
|
|
- value: chargingCost,
|
|
|
- createdOn: DateTime.UtcNow,
|
|
|
- contextId: (int)ReadingContext.Sample_Periodic,
|
|
|
- formatId: (int)ValueFormat.Raw,
|
|
|
- measurandId: (int)Measurand.TotalCost,
|
|
|
- phaseId: -1,
|
|
|
- locationId: -1,
|
|
|
- unitId: -1,
|
|
|
- transactionId: feedto.Id
|
|
|
- );
|
|
|
-
|
|
|
- meterdb.Database.ExecuteSqlRaw(sp, parameter.ToArray());
|
|
|
- }
|
|
|
+ await meterValueDbService.InsertAsync(
|
|
|
+ chargeBoxId: session.ChargeBoxId,
|
|
|
+ connectorId: feedto.ConnectorId,
|
|
|
+ value: chargingCost,
|
|
|
+ createdOn: DateTime.UtcNow,
|
|
|
+ contextId: (int)ReadingContext.Sample_Periodic,
|
|
|
+ formatId: (int)ValueFormat.Raw,
|
|
|
+ measurandId: (int)Measurand.TotalCost,
|
|
|
+ phaseId: -1,
|
|
|
+ locationId: -1,
|
|
|
+ unitId: -1,
|
|
|
+ transactionId: feedto.Id);
|
|
|
|
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
|
{
|
|
@@ -1263,32 +1241,20 @@ internal partial class ProfileHandler
|
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
|
- using (var meterdb = await metervaluedbContextFactory.CreateDbContextAsync())
|
|
|
- {
|
|
|
- string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId," +
|
|
|
- "@ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId";
|
|
|
-
|
|
|
- List<SqlParameter> parameter = new List<SqlParameter>();
|
|
|
- parameter.AddInsertMeterValueRecordSqlParameters(
|
|
|
- chargeBoxId: session.ChargeBoxId,
|
|
|
- connectorId: (byte)feedto.ConnectorId,
|
|
|
- value: chargingCost,
|
|
|
- createdOn: DateTime.UtcNow,
|
|
|
- contextId: (int)ReadingContext.Sample_Periodic,
|
|
|
- formatId: (int)ValueFormat.Raw,
|
|
|
- measurandId: (int)Measurand.ChargingCost,
|
|
|
- phaseId: -1,
|
|
|
- locationId: -1,
|
|
|
- unitId: -1,
|
|
|
- transactionId: feedto.Id
|
|
|
- );
|
|
|
-
|
|
|
- meterdb.Database.ExecuteSqlRaw(sp, parameter.ToArray());
|
|
|
- }
|
|
|
-
|
|
|
+ await meterValueDbService.InsertAsync(
|
|
|
+ chargeBoxId: session.ChargeBoxId,
|
|
|
+ connectorId: (byte)feedto.ConnectorId,
|
|
|
+ value: chargingCost,
|
|
|
+ createdOn: DateTime.UtcNow,
|
|
|
+ contextId: (int)ReadingContext.Sample_Periodic,
|
|
|
+ formatId: (int)ValueFormat.Raw,
|
|
|
+ measurandId: (int)Measurand.ChargingCost,
|
|
|
+ phaseId: -1,
|
|
|
+ locationId: -1,
|
|
|
+ unitId: -1,
|
|
|
+ transactionId: feedto.Id
|
|
|
+ );
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
}
|