|
@@ -2,14 +2,11 @@
|
|
|
using EVCB_OCPP.Domain;
|
|
|
using EVCB_OCPP.Domain.Models.Database;
|
|
|
using EVCB_OCPP.WSServer.Helper;
|
|
|
-using log4net.Core;
|
|
|
-using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
-using MongoDB.Driver.Core.Connections;
|
|
|
using Newtonsoft.Json;
|
|
|
using OCPPPackage.Profiles;
|
|
|
using System.Data;
|
|
@@ -39,6 +36,7 @@ public interface IMainDbService
|
|
|
Task<int> AddNewTransactionRecord(TransactionRecord newTransaction);
|
|
|
Task<TransactionRecord> GetTransactionForStopTransaction(int transactionId, string chargeBoxId);
|
|
|
Task UpdateTransaction(int transactionId, int meterStop, DateTime stopTime, int stopReasonId, string stopReason, string stopIdTag, string receipt, int cost);
|
|
|
+ Task<bool> UpdateHeartBeats(IEnumerable<Machine> heartBeatsData);
|
|
|
}
|
|
|
|
|
|
public class MainDbService : IMainDbService
|
|
@@ -48,12 +46,14 @@ public class MainDbService : IMainDbService
|
|
|
SqlConnectionFactory<MainDBContext> sqlConnectionFactory,
|
|
|
IMemoryCache memoryCache,
|
|
|
IConfiguration configuration,
|
|
|
- ILoggerFactory loggerFactory)
|
|
|
+ ILoggerFactory loggerFactory,
|
|
|
+ ILogger<MainDbService> logger)
|
|
|
{
|
|
|
this.contextFactory = contextFactory;
|
|
|
this.sqlConnectionFactory = sqlConnectionFactory;
|
|
|
this.memoryCache = memoryCache;
|
|
|
this.loggerFactory = loggerFactory;
|
|
|
+ this.logger = logger;
|
|
|
var startupLimit = GetStartupLimit(configuration);
|
|
|
//this.connectionString = configuration.GetConnectionString("MainDBContext");
|
|
|
this.startupSemaphore = new(startupLimit);
|
|
@@ -66,10 +66,15 @@ public class MainDbService : IMainDbService
|
|
|
InitAddServerMessageHandler();
|
|
|
}
|
|
|
|
|
|
+ private const string CustomerMemCacheKeyFromat = "Customer_{0}";
|
|
|
+ private const string ChargeBoxConnectorIdMemCacheKeyFromat = "Connector_{0}{1}";
|
|
|
+
|
|
|
private readonly IDbContextFactory<MainDBContext> contextFactory;
|
|
|
private readonly SqlConnectionFactory<MainDBContext> sqlConnectionFactory;
|
|
|
private readonly IMemoryCache memoryCache;
|
|
|
private readonly ILoggerFactory loggerFactory;
|
|
|
+ private readonly ILogger<MainDbService> logger;
|
|
|
+
|
|
|
//private string connectionString;
|
|
|
private readonly QueueSemaphore startupSemaphore;
|
|
|
private readonly SemaphoreSlim opSemaphore;
|
|
@@ -81,6 +86,7 @@ public class MainDbService : IMainDbService
|
|
|
{
|
|
|
using var semaphoreWrapper = await startupSemaphore.GetToken();
|
|
|
using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+
|
|
|
var machine = await db.Machine.Where(x => x.ChargeBoxId == ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefaultAsync();
|
|
|
if (machine == null)
|
|
|
{
|
|
@@ -101,42 +107,25 @@ public class MainDbService : IMainDbService
|
|
|
.Select(x => x.ConfigureSetting).FirstOrDefaultAsync();
|
|
|
}
|
|
|
|
|
|
- public async Task<string> GetMachineSecurityProfile(string ChargeBoxId)
|
|
|
+ public Task<string> GetMachineSecurityProfile(string ChargeBoxId)
|
|
|
{
|
|
|
- return await GetMachineConfiguration(ChargeBoxId, StandardConfiguration.SecurityProfile);
|
|
|
+ return GetMachineConfiguration(ChargeBoxId, StandardConfiguration.SecurityProfile);
|
|
|
}
|
|
|
|
|
|
- public async Task<string> GetMachineAuthorizationKey(string ChargeBoxId)
|
|
|
+ public Task<string> GetMachineAuthorizationKey(string ChargeBoxId)
|
|
|
{
|
|
|
- return await GetMachineConfiguration(ChargeBoxId, StandardConfiguration.AuthorizationKey);
|
|
|
+ return GetMachineConfiguration(ChargeBoxId, StandardConfiguration.AuthorizationKey);
|
|
|
}
|
|
|
|
|
|
- public async Task<string> GetMachineHeartbeatInterval(string ChargeBoxId)
|
|
|
+ public Task<string> GetMachineHeartbeatInterval(string ChargeBoxId)
|
|
|
{
|
|
|
- return await GetMachineConfiguration(ChargeBoxId, StandardConfiguration.HeartbeatInterval);
|
|
|
+ return GetMachineConfiguration(ChargeBoxId, StandardConfiguration.HeartbeatInterval);
|
|
|
}
|
|
|
|
|
|
- public async Task UpdateMachineBasicInfo(string ChargeBoxId, Machine machine)
|
|
|
+ public Task UpdateMachineBasicInfo(string ChargeBoxId, Machine machine)
|
|
|
{
|
|
|
- //using var semaphoreWrapper = await startupSemaphore.GetToken();
|
|
|
- //using var db = await contextFactory.CreateDbContextAsync();
|
|
|
-
|
|
|
- //var _machine = await db.Machine.FirstOrDefaultAsync(x => x.ChargeBoxId == ChargeBoxId);
|
|
|
- //_machine.ChargeBoxSerialNumber = machine.ChargeBoxSerialNumber;
|
|
|
- //_machine.ChargePointSerialNumber = machine.ChargePointSerialNumber;
|
|
|
- //_machine.ChargePointModel = machine.ChargePointModel;
|
|
|
- //_machine.ChargePointVendor = machine.ChargePointVendor;
|
|
|
- //_machine.FW_CurrentVersion = machine.FW_CurrentVersion;
|
|
|
- //_machine.Iccid = DateTime.UtcNow.ToString("yy-MM-dd HH:mm");
|
|
|
- //_machine.Imsi = machine.Imsi;
|
|
|
- //_machine.MeterSerialNumber = machine.MeterSerialNumber;
|
|
|
- //_machine.MeterType = machine.MeterType;
|
|
|
-
|
|
|
- //await db.SaveChangesAsync();
|
|
|
-
|
|
|
- //using var semaphoreWrapper = await startupSemaphore.GetToken();
|
|
|
-
|
|
|
- await updateMachineBasicInfoHandler.HandleAsync(new UpdateMachineBasicInfoParam(ChargeBoxId, machine));
|
|
|
+ //return UpdateMachineBasicInfoEF(ChargeBoxId, machine);
|
|
|
+ return updateMachineBasicInfoHandler.HandleAsync(new UpdateMachineBasicInfoParam(ChargeBoxId, machine));
|
|
|
}
|
|
|
|
|
|
public async Task AddOCMF(OCMF oCMF)
|
|
@@ -167,16 +156,19 @@ public class MainDbService : IMainDbService
|
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
- memoryCache.Set($"{ChargeBoxId}{ConnectorId}", _currentStatus, TimeSpan.FromHours(12));
|
|
|
+ memoryCache.Set(
|
|
|
+ string.Format(ChargeBoxConnectorIdMemCacheKeyFromat, ChargeBoxId, ConnectorId)
|
|
|
+ , _currentStatus, TimeSpan.FromHours(12));
|
|
|
}
|
|
|
|
|
|
public async ValueTask<ConnectorStatus> GetConnectorStatus(string ChargeBoxId, int ConnectorId)
|
|
|
{
|
|
|
- var key = $"{ChargeBoxId}{ConnectorId}";
|
|
|
+ var key = string.Format(ChargeBoxConnectorIdMemCacheKeyFromat, ChargeBoxId, ConnectorId);
|
|
|
if (memoryCache.TryGetValue<ConnectorStatus>(key, out var status))
|
|
|
{
|
|
|
return status;
|
|
|
}
|
|
|
+
|
|
|
using var db = await contextFactory.CreateDbContextAsync();
|
|
|
var statusFromDb = await db.ConnectorStatus.Where(x => x.ChargeBoxId == ChargeBoxId
|
|
|
&& x.ConnectorId == ConnectorId).AsNoTracking().FirstOrDefaultAsync();
|
|
@@ -191,143 +183,29 @@ public class MainDbService : IMainDbService
|
|
|
//await UpdateConnectorStatusEF(Id, Status);
|
|
|
await UpdateConnectorStatusDapper(Id, Status);
|
|
|
|
|
|
- var key = $"{Status.ChargeBoxId}{Status.ConnectorId}";
|
|
|
+ var key = string.Format(ChargeBoxConnectorIdMemCacheKeyFromat, Status.ChargeBoxId, Status.ConnectorId);
|
|
|
memoryCache.Set(key, Status, TimeSpan.FromHours(12));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- public async Task<Guid> GetCustomerIdByChargeBoxId(string chargeboxId)
|
|
|
+ public Task<Guid> GetCustomerIdByChargeBoxId(string chargeboxId)
|
|
|
{
|
|
|
- //using var db = await contextFactory.CreateDbContextAsync();
|
|
|
- //var _CustomerId = await db.Machine.Where(x => x.ChargeBoxId == chargeboxId).Select(x => x.CustomerId).FirstOrDefaultAsync();
|
|
|
- //return _CustomerId;
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeboxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
-
|
|
|
- using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
- var _existedTx = await conn.QueryFirstOrDefaultAsync<Guid>("""
|
|
|
- select CustomerId
|
|
|
- from dbo.Machine
|
|
|
- where
|
|
|
- ChargeBoxId = @ChargeBoxId
|
|
|
- """, parameters);
|
|
|
-
|
|
|
- return _existedTx;
|
|
|
+ //return GetCustomerIdByChargeBoxIdEF(chargeboxId);
|
|
|
+ return GetCustomerIdByChargeBoxIdDapper(chargeboxId);
|
|
|
}
|
|
|
|
|
|
- public async Task<int?> TryGetDuplicatedTransactionId(string chargeBoxId, Guid customerId, int connectorId, DateTime timestamp)
|
|
|
+ public Task<int?> TryGetDuplicatedTransactionId(string chargeBoxId, Guid customerId, int connectorId, DateTime timestamp)
|
|
|
{
|
|
|
- //var _existedTx = await db.TransactionRecord.Where(x => x.CustomerId == customerId && x.ChargeBoxId == chargeBoxId
|
|
|
- // && x.ConnectorId == connectorId && x.StartTime == timestamp).Select(x=>x.Id).fi();
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
- parameters.Add("@CustomerId", customerId, DbType.Guid, ParameterDirection.Input);
|
|
|
- parameters.Add("@ConnectorId", connectorId, DbType.Int16, ParameterDirection.Input);
|
|
|
- parameters.Add("@TimeStamp", timestamp, DbType.DateTime, ParameterDirection.Input);
|
|
|
-
|
|
|
- using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
- var _existedTx = await conn.QueryFirstOrDefaultAsync<int?>("""
|
|
|
- SELECT Id
|
|
|
- FROM dbo.TransactionRecord
|
|
|
- WHERE
|
|
|
- ChargeBoxId = @ChargeBoxId and
|
|
|
- CustomerId = @CustomerId and
|
|
|
- ConnectorId = @ConnectorId and
|
|
|
- StartTime = @TimeStamp
|
|
|
- """, parameters);
|
|
|
-
|
|
|
- return _existedTx;
|
|
|
+ //return TryGetDuplicatedTransactionIdEF(chargeBoxId, customerId, connectorId, timestamp);
|
|
|
+ return TryGetDuplicatedTransactionIdDapper(chargeBoxId, customerId, connectorId, timestamp);
|
|
|
}
|
|
|
|
|
|
- private async Task UpdateConnectorStatusEF(string Id, ConnectorStatus Status)
|
|
|
- {
|
|
|
- using var db = await contextFactory.CreateDbContextAsync();
|
|
|
|
|
|
- ConnectorStatus status = new() { Id = Id };
|
|
|
-
|
|
|
- db.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
- db.ConnectorStatus.Attach(status);
|
|
|
-
|
|
|
-
|
|
|
- status.CreatedOn = Status.CreatedOn;
|
|
|
- status.Status = Status.Status;
|
|
|
- status.ChargePointErrorCodeId = Status.ChargePointErrorCodeId;
|
|
|
- status.ErrorInfo = Status.ErrorInfo;
|
|
|
- status.VendorId = Status.VendorId;
|
|
|
- status.VendorErrorCode = Status.VendorErrorCode;
|
|
|
-
|
|
|
-
|
|
|
- db.Entry(status).Property(x => x.CreatedOn).IsModified = true;
|
|
|
- db.Entry(status).Property(x => x.Status).IsModified = true;
|
|
|
- db.Entry(status).Property(x => x.ChargePointErrorCodeId).IsModified = true;
|
|
|
- db.Entry(status).Property(x => x.ErrorInfo).IsModified = true;
|
|
|
- db.Entry(status).Property(x => x.VendorId).IsModified = true;
|
|
|
- db.Entry(status).Property(x => x.VendorErrorCode).IsModified = true;
|
|
|
-
|
|
|
- await db.SaveChangesAsync();
|
|
|
- }
|
|
|
-
|
|
|
- private async Task UpdateConnectorStatusDapper(string Id, ConnectorStatus Status)
|
|
|
+ public ValueTask AddMachineError(byte ConnectorId, DateTime CreatedOn, int Status, string ChargeBoxId,
|
|
|
+ int ErrorCodeId, string ErrorInfo, int PreStatus, string VendorErrorCode, string VendorId)
|
|
|
{
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@Id", Id, DbType.String, ParameterDirection.Input, 36);
|
|
|
- parameters.Add("@CreatedOn", Status.CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@Status", Status.Status, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@ChargePointErrorCodeId", Status.ChargePointErrorCodeId, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@ErrorInfo", Status.ErrorInfo, DbType.String, ParameterDirection.Input, 50);
|
|
|
- parameters.Add("@VendorId", Status.VendorId, DbType.String, ParameterDirection.Input, 255);
|
|
|
- parameters.Add("@VendorErrorCode", Status.VendorErrorCode, DbType.String, ParameterDirection.Input, 100);
|
|
|
-
|
|
|
- using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
- await conn.ExecuteAsync("""
|
|
|
- update ConnectorStatus
|
|
|
- set
|
|
|
- CreatedOn = @CreatedOn,
|
|
|
- Status = @Status,
|
|
|
- ChargePointErrorCodeId = @ChargePointErrorCodeId,
|
|
|
- ErrorInfo = @ErrorInfo,
|
|
|
- VendorId = @VendorId,
|
|
|
- VendorErrorCode = @VendorErrorCode
|
|
|
- where Id = @Id
|
|
|
- """, parameters );
|
|
|
- }
|
|
|
-
|
|
|
- public async ValueTask AddMachineError(byte ConnectorId, DateTime CreatedOn, int Status, string ChargeBoxId,
|
|
|
- int ErrorCodeId, string ErrorInfo, int PreStatus, string VendorErrorCode, string VendorId)
|
|
|
- {
|
|
|
- //using var db = await contextFactory.CreateDbContextAsync();
|
|
|
- //await db.MachineError.AddAsync(new MachineError()
|
|
|
- //{
|
|
|
- // ConnectorId = ConnectorId,
|
|
|
- // CreatedOn = CreatedOn,
|
|
|
- // Status = Status,
|
|
|
- // ChargeBoxId = ChargeBoxId,
|
|
|
- // ErrorCodeId = ErrorCodeId,
|
|
|
- // ErrorInfo = ErrorInfo,
|
|
|
- // PreStatus = PreStatus,
|
|
|
- // VendorErrorCode = VendorErrorCode,
|
|
|
- // VendorId = VendorId
|
|
|
- //});
|
|
|
-
|
|
|
- //await db.SaveChangesAsync();
|
|
|
-
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ConnectorId", ConnectorId, DbType.Int16, ParameterDirection.Input);
|
|
|
- parameters.Add("@PreStatus", PreStatus, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@Status", Status, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@ErrorInfo", ErrorInfo, DbType.String, ParameterDirection.Input, 50);
|
|
|
- parameters.Add("@VendorId", VendorId, DbType.String, ParameterDirection.Input, 255);
|
|
|
- parameters.Add("@CreatedOn", CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@ErrorCodeId", ErrorCodeId, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@VendorErrorCode", VendorErrorCode, DbType.String, ParameterDirection.Input, 100);
|
|
|
- parameters.Add("@ChargeBoxId", ChargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
-
|
|
|
- using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
- await conn.ExecuteAsync("""
|
|
|
- INSERT INTO MachineError
|
|
|
- (ConnectorId, PreStatus, Status, ErrorInfo, VendorId, CreatedOn, ErrorCodeId, VendorErrorCode, ChargeBoxId)
|
|
|
- VALUES (@ConnectorId, @PreStatus, @Status, @ErrorInfo, @VendorId, @CreatedOn, @ErrorCodeId, @VendorErrorCode, @ChargeBoxId)
|
|
|
- """, parameters);
|
|
|
+ //return AddMachineErrorEF(ConnectorId, CreatedOn, Status, ChargeBoxId, ErrorCodeId, ErrorInfo, PreStatus, VendorErrorCode, VendorId);
|
|
|
+ return AddMachineErrorDapper(ConnectorId, CreatedOn, Status, ChargeBoxId, ErrorCodeId, ErrorInfo, PreStatus, VendorErrorCode, VendorId);
|
|
|
}
|
|
|
|
|
|
public Task AddServerMessage(string ChargeBoxId, string OutAction, object OutRequest, string CreatedBy, DateTime? CreatedOn = null, string SerialNo = "", string InMessage = "")
|
|
@@ -355,7 +233,7 @@ public class MainDbService : IMainDbService
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- return AddServerMessage(new ServerMessage()
|
|
|
+ var data = new ServerMessage()
|
|
|
{
|
|
|
ChargeBoxId = ChargeBoxId,
|
|
|
CreatedBy = CreatedBy,
|
|
@@ -364,20 +242,24 @@ public class MainDbService : IMainDbService
|
|
|
OutRequest = _OutRequest,
|
|
|
SerialNo = SerialNo,
|
|
|
InMessage = InMessage
|
|
|
- });
|
|
|
+ };
|
|
|
+
|
|
|
+ return AddServerMessage(data);
|
|
|
}
|
|
|
|
|
|
public Task AddServerMessage(ServerMessage message)
|
|
|
{
|
|
|
//return AddServerMessageEF(message);
|
|
|
- return addServerMessageHandler.HandleAsync(message);
|
|
|
+ //return addServerMessageHandler.HandleAsync(message);
|
|
|
+ return AddServerMessageDapper(message);
|
|
|
}
|
|
|
|
|
|
public ValueTask<Customer> GetCustomer(string id)
|
|
|
=> GetCustomer(new Guid(id));
|
|
|
+
|
|
|
public async ValueTask<Customer> GetCustomer(Guid id)
|
|
|
{
|
|
|
- var key = $"Customer{id}";
|
|
|
+ var key = string.Format(CustomerMemCacheKeyFromat, id);
|
|
|
if (memoryCache.TryGetValue<Customer>(key, out var customer))
|
|
|
{
|
|
|
return customer;
|
|
@@ -397,62 +279,44 @@ public class MainDbService : IMainDbService
|
|
|
return toReturn;
|
|
|
}
|
|
|
|
|
|
- public async Task<int> AddNewTransactionRecord(TransactionRecord newTransaction)
|
|
|
+ public Task<int> AddNewTransactionRecord(TransactionRecord newTransaction)
|
|
|
{
|
|
|
- //using (var db = await contextFactory.CreateDbContextAsync())
|
|
|
- //{
|
|
|
- // await db.TransactionRecord.AddAsync(newTransaction);
|
|
|
-
|
|
|
- // await db.SaveChangesAsync();
|
|
|
-
|
|
|
- // return newTransaction.Id;
|
|
|
- //}
|
|
|
+ //return AddNewTransactionRecordEF(newTransaction);
|
|
|
+ return AddNewTransactionRecordDapper(newTransaction);
|
|
|
+ }
|
|
|
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", newTransaction.ChargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
- parameters.Add("@ConnectorId", newTransaction.ConnectorId, DbType.Int16, ParameterDirection.Input);
|
|
|
- parameters.Add("@CreatedOn", newTransaction.CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@UpdatedOn", newTransaction.UpdatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@StartTransactionReportedOn", newTransaction.StartTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@StopTransactionReportedOn", newTransaction.StopTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@StartIdTag", newTransaction.StartIdTag, DbType.String, ParameterDirection.Input, 20);
|
|
|
- parameters.Add("@MeterStart", newTransaction.MeterStart, DbType.Decimal, ParameterDirection.Input, precision:18, scale:2);
|
|
|
- parameters.Add("@MeterStop", newTransaction.MeterStop, DbType.Decimal, ParameterDirection.Input, precision: 18, scale: 2);
|
|
|
- parameters.Add("@CustomerId", newTransaction.CustomerId, DbType.Guid, ParameterDirection.Input);
|
|
|
- parameters.Add("@StartTime", newTransaction.StartTime, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@StopTime", newTransaction.StopTime, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@ReservationId", newTransaction.ReservationId, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@RetryStartTransactionTimes", newTransaction.RetryStartTransactionTimes, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@RetryStopTransactionTimes", newTransaction.RetryStopTransactionTimes, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@Fee", newTransaction.Fee, DbType.String, ParameterDirection.Input, 1500);
|
|
|
+ public Task<TransactionRecord> GetTransactionForStopTransaction(int transactionId, string chargeBoxId)
|
|
|
+ {
|
|
|
+ //return GetTransactionForStopTransactionEF(transactionId, chargeBoxId);
|
|
|
+ return GetTransactionForStopTransactionDapper(transactionId, chargeBoxId);
|
|
|
+ }
|
|
|
|
|
|
- using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
- var id = await conn.QuerySingleAsync<int>("""
|
|
|
- INSERT INTO TransactionRecord
|
|
|
- (ChargeBoxId, ConnectorId, CreatedOn, UpdatedOn, StartTransactionReportedOn, StopTransactionReportedOn,
|
|
|
- StartIdTag, MeterStart, MeterStop, CustomerId, StartTime, StopTime, ReservationId, RetryStartTransactionTimes, RetryStopTransactionTimes, Fee)
|
|
|
- OUTPUT INSERTED.Id
|
|
|
- VALUES (@ChargeBoxId, @ConnectorId, @CreatedOn, @UpdatedOn, @StartTransactionReportedOn, @StopTransactionReportedOn,
|
|
|
- @StartIdTag, @MeterStart, @MeterStop, @CustomerId, @StartTime, @StopTime, @ReservationId, @RetryStartTransactionTimes, @RetryStopTransactionTimes, @Fee)
|
|
|
- """, parameters);
|
|
|
- return id;
|
|
|
+ public Task UpdateTransaction(int transactionId, int meterStop, DateTime stopTime, int stopReasonId, string stopReason, string stopIdTag, string receipt, int cost)
|
|
|
+ {
|
|
|
+ //return UpdateTransactionEF(transactionId, meterStop, stopTime, stopReasonId, stopReason, stopIdTag, receipt, cost);
|
|
|
+ return UpdateTransactionDapper(transactionId, meterStop, stopTime, stopReasonId, stopReason, stopIdTag, receipt, cost);
|
|
|
}
|
|
|
|
|
|
- public async Task<TransactionRecord> GetTransactionForStopTransaction(int transactionId, string chargeBoxId)
|
|
|
+ private async Task UpdateTransactionEF(int transactionId, int meterStop, DateTime stopTime, int stopReasonId, string stopReason, string stopIdTag, string receipt, int cost)
|
|
|
{
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@TransactionId", transactionId, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
|
|
|
- using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
- var record = await conn.QuerySingleAsync<TransactionRecord>("""
|
|
|
- SELECT ConnectorId, MeterStop, MeterStart, StopTime FROM TransactionRecord
|
|
|
- WHERE Id = @TransactionId and ChargeBoxId = @ChargeBoxId
|
|
|
- """, parameters);
|
|
|
- return record;
|
|
|
+ var _transaction = db.TransactionRecord.Where(x => x.Id == transactionId //&& x.ChargeBoxId == session.ChargeBoxId
|
|
|
+ ).FirstOrDefault();
|
|
|
+
|
|
|
+ _transaction.MeterStop = meterStop;
|
|
|
+ _transaction.StopTime = stopTime;
|
|
|
+ _transaction.StopReasonId = stopReasonId;
|
|
|
+ _transaction.StopReason = stopReason;
|
|
|
+ _transaction.StopIdTag = stopIdTag;
|
|
|
+ _transaction.Receipt = receipt;
|
|
|
+ _transaction.Cost = cost;
|
|
|
+
|
|
|
+ //await db.SaveChangesAsync();
|
|
|
+ await db.SaveChangesAsync();
|
|
|
}
|
|
|
|
|
|
- public async Task UpdateTransaction(int transactionId, int meterStop, DateTime stopTime, int stopReasonId, string stopReason, string stopIdTag, string receipt, int cost)
|
|
|
+ private async Task UpdateTransactionDapper(int transactionId, int meterStop, DateTime stopTime, int stopReasonId, string stopReason, string stopIdTag, string receipt, int cost)
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
parameters.Add("@TransactionId", transactionId, DbType.Int32, ParameterDirection.Input);
|
|
@@ -478,6 +342,35 @@ public class MainDbService : IMainDbService
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ public Task<bool> UpdateHeartBeats(IEnumerable<Machine> heartBeatsData)
|
|
|
+ {
|
|
|
+ //return UpdateHeartBeatsEF(heartBeatsData);
|
|
|
+ return UpdateHeartBeatsDapper(heartBeatsData);
|
|
|
+ }
|
|
|
+ private void InitUpdateConnectorStatusHandler()
|
|
|
+ {
|
|
|
+ if (statusNotificationHandler is not null)
|
|
|
+ {
|
|
|
+ throw new Exception($"{nameof(InitUpdateConnectorStatusHandler)} should only called once");
|
|
|
+ }
|
|
|
+
|
|
|
+ statusNotificationHandler = new GroupSingleHandler<StatusNotificationParam>(
|
|
|
+ handleFunc: BundleUpdateConnectorStatusDapper,
|
|
|
+ logger: loggerFactory.CreateLogger("StatusNotificationHandler"),
|
|
|
+ workerCnt: 1);
|
|
|
+ }
|
|
|
+ private void InitAddServerMessageHandler()
|
|
|
+ {
|
|
|
+ if (addServerMessageHandler is not null)
|
|
|
+ {
|
|
|
+ throw new Exception($"{nameof(InitAddServerMessageHandler)} should only called once");
|
|
|
+ }
|
|
|
+
|
|
|
+ addServerMessageHandler = new GroupSingleHandler<ServerMessage>(
|
|
|
+ handleFunc: BundleAddServerMessage,
|
|
|
+ logger: loggerFactory.CreateLogger("AddServerMessageHandler"));
|
|
|
+ }
|
|
|
+
|
|
|
private void InitUpdateMachineBasicInfoHandler()
|
|
|
{
|
|
|
if (updateMachineBasicInfoHandler is not null)
|
|
@@ -491,6 +384,27 @@ public class MainDbService : IMainDbService
|
|
|
workerCnt: 10);
|
|
|
}
|
|
|
|
|
|
+ private async Task UpdateMachineBasicInfoEF(string chargeBoxId, Machine machine)
|
|
|
+ {
|
|
|
+ using var semaphoreWrapper = await startupSemaphore.GetToken();
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+
|
|
|
+ var _machine = await db.Machine.FirstOrDefaultAsync(x => x.ChargeBoxId == chargeBoxId);
|
|
|
+ _machine.ChargeBoxSerialNumber = machine.ChargeBoxSerialNumber;
|
|
|
+ _machine.ChargePointSerialNumber = machine.ChargePointSerialNumber;
|
|
|
+ _machine.ChargePointModel = machine.ChargePointModel;
|
|
|
+ _machine.ChargePointVendor = machine.ChargePointVendor;
|
|
|
+ _machine.FW_CurrentVersion = machine.FW_CurrentVersion;
|
|
|
+ _machine.Iccid = DateTime.UtcNow.ToString("yy-MM-dd HH:mm");
|
|
|
+ _machine.Imsi = machine.Imsi;
|
|
|
+ _machine.MeterSerialNumber = machine.MeterSerialNumber;
|
|
|
+ _machine.MeterType = machine.MeterType;
|
|
|
+
|
|
|
+ await db.SaveChangesAsync();
|
|
|
+
|
|
|
+ //using var semaphoreWrapper = await startupSemaphore.GetToken();
|
|
|
+ }
|
|
|
+
|
|
|
private async Task BundelUpdateMachineBasicInfo(IEnumerable<UpdateMachineBasicInfoParam> pams)
|
|
|
{
|
|
|
using var db = await contextFactory.CreateDbContextAsync();
|
|
@@ -513,20 +427,153 @@ public class MainDbService : IMainDbService
|
|
|
}
|
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
- trans.Commit();
|
|
|
+ await trans.CommitAsync();
|
|
|
}
|
|
|
|
|
|
- private void InitUpdateConnectorStatusHandler()
|
|
|
+ private async Task UpdateConnectorStatusEF(string Id, ConnectorStatus Status)
|
|
|
{
|
|
|
- if (statusNotificationHandler is not null)
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+
|
|
|
+ ConnectorStatus status = new() { Id = Id };
|
|
|
+
|
|
|
+ db.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
+ db.ConnectorStatus.Attach(status);
|
|
|
+
|
|
|
+
|
|
|
+ status.CreatedOn = Status.CreatedOn;
|
|
|
+ status.Status = Status.Status;
|
|
|
+ status.ChargePointErrorCodeId = Status.ChargePointErrorCodeId;
|
|
|
+ status.ErrorInfo = Status.ErrorInfo;
|
|
|
+ status.VendorId = Status.VendorId;
|
|
|
+ status.VendorErrorCode = Status.VendorErrorCode;
|
|
|
+
|
|
|
+
|
|
|
+ db.Entry(status).Property(x => x.CreatedOn).IsModified = true;
|
|
|
+ db.Entry(status).Property(x => x.Status).IsModified = true;
|
|
|
+ db.Entry(status).Property(x => x.ChargePointErrorCodeId).IsModified = true;
|
|
|
+ db.Entry(status).Property(x => x.ErrorInfo).IsModified = true;
|
|
|
+ db.Entry(status).Property(x => x.VendorId).IsModified = true;
|
|
|
+ db.Entry(status).Property(x => x.VendorErrorCode).IsModified = true;
|
|
|
+
|
|
|
+ await db.SaveChangesAsync();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task UpdateConnectorStatusDapper(string Id, ConnectorStatus Status)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@Id", Id, DbType.String, ParameterDirection.Input, 36);
|
|
|
+ parameters.Add("@CreatedOn", Status.CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@Status", Status.Status, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargePointErrorCodeId", Status.ChargePointErrorCodeId, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ErrorInfo", Status.ErrorInfo, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@VendorId", Status.VendorId, DbType.String, ParameterDirection.Input, 255);
|
|
|
+ parameters.Add("@VendorErrorCode", Status.VendorErrorCode, DbType.String, ParameterDirection.Input, 100);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ await conn.ExecuteAsync("""
|
|
|
+ update ConnectorStatus
|
|
|
+ set
|
|
|
+ CreatedOn = @CreatedOn,
|
|
|
+ Status = @Status,
|
|
|
+ ChargePointErrorCodeId = @ChargePointErrorCodeId,
|
|
|
+ ErrorInfo = @ErrorInfo,
|
|
|
+ VendorId = @VendorId,
|
|
|
+ VendorErrorCode = @VendorErrorCode
|
|
|
+ where Id = @Id
|
|
|
+ """, parameters);
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<Guid> GetCustomerIdByChargeBoxIdEF(string chargeboxId)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ var _CustomerId = await db.Machine.Where(x => x.ChargeBoxId == chargeboxId).Select(x => x.CustomerId).FirstOrDefaultAsync();
|
|
|
+ return _CustomerId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<Guid> GetCustomerIdByChargeBoxIdDapper(string chargeboxId)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@ChargeBoxId", chargeboxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ var _existedTx = await conn.QueryFirstOrDefaultAsync<Guid>("""
|
|
|
+ select CustomerId
|
|
|
+ from dbo.Machine
|
|
|
+ where
|
|
|
+ ChargeBoxId = @ChargeBoxId
|
|
|
+ """, parameters);
|
|
|
+
|
|
|
+ return _existedTx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<int?> TryGetDuplicatedTransactionIdEF(string chargeBoxId, Guid customerId, int connectorId, DateTime timestamp)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ var _existedTx = await db.TransactionRecord.Where(x => x.CustomerId == customerId && x.ChargeBoxId == chargeBoxId
|
|
|
+ && x.ConnectorId == connectorId && x.StartTime == timestamp).Select(x => x.Id).FirstOrDefaultAsync();
|
|
|
+ return _existedTx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<int?> TryGetDuplicatedTransactionIdDapper(string chargeBoxId, Guid customerId, int connectorId, DateTime timestamp)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@CustomerId", customerId, DbType.Guid, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ConnectorId", connectorId, DbType.Int16, ParameterDirection.Input);
|
|
|
+ parameters.Add("@TimeStamp", timestamp, DbType.DateTime, ParameterDirection.Input);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ var _existedTx = await conn.QueryFirstOrDefaultAsync<int?>("""
|
|
|
+ SELECT Id
|
|
|
+ FROM dbo.TransactionRecord
|
|
|
+ WHERE
|
|
|
+ ChargeBoxId = @ChargeBoxId and
|
|
|
+ CustomerId = @CustomerId and
|
|
|
+ ConnectorId = @ConnectorId and
|
|
|
+ StartTime = @TimeStamp
|
|
|
+ """, parameters);
|
|
|
+
|
|
|
+ return _existedTx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async ValueTask AddMachineErrorEF(byte connectorId, DateTime createdOn, int status, string chargeBoxId, int errorCodeId, string errorInfo, int preStatus, string vendorErrorCode, string vendorId)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ await db.MachineError.AddAsync(new MachineError()
|
|
|
{
|
|
|
- throw new Exception($"{nameof(InitUpdateConnectorStatusHandler)} should only called once");
|
|
|
- }
|
|
|
+ ConnectorId = connectorId,
|
|
|
+ CreatedOn = createdOn,
|
|
|
+ Status = status,
|
|
|
+ ChargeBoxId = chargeBoxId,
|
|
|
+ ErrorCodeId = errorCodeId,
|
|
|
+ ErrorInfo = errorInfo,
|
|
|
+ PreStatus = preStatus,
|
|
|
+ VendorErrorCode = vendorErrorCode,
|
|
|
+ VendorId = vendorId
|
|
|
+ });
|
|
|
|
|
|
- statusNotificationHandler = new GroupSingleHandler<StatusNotificationParam>(
|
|
|
- handleFunc: BundleUpdateConnectorStatusDapper,
|
|
|
- logger: loggerFactory.CreateLogger("StatusNotificationHandler"),
|
|
|
- workerCnt: 1);
|
|
|
+ await db.SaveChangesAsync();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async ValueTask AddMachineErrorDapper(byte connectorId, DateTime createdOn, int status, string chargeBoxId, int errorCodeId, string errorInfo, int preStatus, string vendorErrorCode, string vendorId)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@ConnectorId", connectorId, DbType.Int16, ParameterDirection.Input);
|
|
|
+ parameters.Add("@PreStatus", preStatus, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@Status", status, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ErrorInfo", errorInfo, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@VendorId", vendorId, DbType.String, ParameterDirection.Input, 255);
|
|
|
+ parameters.Add("@CreatedOn", createdOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ErrorCodeId", errorCodeId, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@VendorErrorCode", vendorErrorCode, DbType.String, ParameterDirection.Input, 100);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ await conn.ExecuteAsync("""
|
|
|
+ INSERT INTO MachineError
|
|
|
+ (ConnectorId, PreStatus, Status, ErrorInfo, VendorId, CreatedOn, ErrorCodeId, VendorErrorCode, ChargeBoxId)
|
|
|
+ VALUES (@ConnectorId, @PreStatus, @Status, @ErrorInfo, @VendorId, @CreatedOn, @ErrorCodeId, @VendorErrorCode, @ChargeBoxId)
|
|
|
+ """, parameters);
|
|
|
}
|
|
|
|
|
|
private async Task BundleUpdateConnectorStatus(IEnumerable<StatusNotificationParam> statusNotifications)
|
|
@@ -564,7 +611,7 @@ public class MainDbService : IMainDbService
|
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
await trans.CommitAsync();
|
|
|
- db.ChangeTracker.Clear();
|
|
|
+ //db.ChangeTracker.Clear();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -600,18 +647,6 @@ public class MainDbService : IMainDbService
|
|
|
return Task.CompletedTask;
|
|
|
}
|
|
|
|
|
|
- private void InitAddServerMessageHandler()
|
|
|
- {
|
|
|
- if (addServerMessageHandler is not null)
|
|
|
- {
|
|
|
- throw new Exception($"{nameof(InitAddServerMessageHandler)} should only called once");
|
|
|
- }
|
|
|
-
|
|
|
- addServerMessageHandler = new GroupSingleHandler<ServerMessage>(
|
|
|
- handleFunc: BundleAddServerMessage,
|
|
|
- logger: loggerFactory.CreateLogger("AddServerMessageHandler"));
|
|
|
- }
|
|
|
-
|
|
|
private async Task BundleAddServerMessage(IEnumerable<ServerMessage> messages)
|
|
|
{
|
|
|
using var db = await contextFactory.CreateDbContextAsync();
|
|
@@ -624,7 +659,7 @@ public class MainDbService : IMainDbService
|
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
await trans.CommitAsync();
|
|
|
- db.ChangeTracker.Clear();
|
|
|
+ //db.ChangeTracker.Clear();
|
|
|
}
|
|
|
|
|
|
private async Task AddServerMessageEF(ServerMessage message)
|
|
@@ -636,7 +671,97 @@ public class MainDbService : IMainDbService
|
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
await trans.CommitAsync();
|
|
|
- db.ChangeTracker.Clear();
|
|
|
+ //db.ChangeTracker.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task AddServerMessageDapper(ServerMessage message)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@SerialNo", message.SerialNo, DbType.String, ParameterDirection.Input, 36);
|
|
|
+ parameters.Add("@OutAction", message.OutAction, DbType.String, ParameterDirection.Input, 30);
|
|
|
+ parameters.Add("@OutRequest", message.OutRequest, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@InMessage", message.InMessage, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@CreatedOn", message.CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@CreatedBy", message.CreatedBy, DbType.String, ParameterDirection.Input, 36);
|
|
|
+ parameters.Add("@ReceivedOn", message.ReceivedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", message.ChargeBoxId, DbType.String, ParameterDirection.Input, 30);
|
|
|
+ parameters.Add("@UpdatedOn", message.UpdatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ var resultCnt = await conn.ExecuteAsync("""
|
|
|
+ INSERT INTO ServerMessage
|
|
|
+ (SerialNo, OutAction, OutRequest, InMessage, CreatedOn, CreatedBy, ReceivedOn, ChargeBoxId, UpdatedOn)
|
|
|
+ VALUES (@SerialNo, @OutAction, @OutRequest, @InMessage, @CreatedOn, @CreatedBy, @ReceivedOn, @ChargeBoxId, @UpdatedOn)
|
|
|
+ """, parameters);
|
|
|
+ if (resultCnt != 1)
|
|
|
+ {
|
|
|
+ throw new Exception("Insert failed");
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<int> AddNewTransactionRecordEF(TransactionRecord newTransaction)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+
|
|
|
+ await db.TransactionRecord.AddAsync(newTransaction);
|
|
|
+
|
|
|
+ await db.SaveChangesAsync();
|
|
|
+
|
|
|
+ return newTransaction.Id;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<int> AddNewTransactionRecordDapper(TransactionRecord newTransaction)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@ChargeBoxId", newTransaction.ChargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@ConnectorId", newTransaction.ConnectorId, DbType.Int16, ParameterDirection.Input);
|
|
|
+ parameters.Add("@CreatedOn", newTransaction.CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@UpdatedOn", newTransaction.UpdatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StartTransactionReportedOn", newTransaction.StartTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StopTransactionReportedOn", newTransaction.StopTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StartIdTag", newTransaction.StartIdTag, DbType.String, ParameterDirection.Input, 20);
|
|
|
+ parameters.Add("@MeterStart", newTransaction.MeterStart, DbType.Decimal, ParameterDirection.Input, precision: 18, scale: 2);
|
|
|
+ parameters.Add("@MeterStop", newTransaction.MeterStop, DbType.Decimal, ParameterDirection.Input, precision: 18, scale: 2);
|
|
|
+ parameters.Add("@CustomerId", newTransaction.CustomerId, DbType.Guid, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StartTime", newTransaction.StartTime, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StopTime", newTransaction.StopTime, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ReservationId", newTransaction.ReservationId, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@RetryStartTransactionTimes", newTransaction.RetryStartTransactionTimes, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@RetryStopTransactionTimes", newTransaction.RetryStopTransactionTimes, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@Fee", newTransaction.Fee, DbType.String, ParameterDirection.Input, 1500);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ var id = await conn.QuerySingleAsync<int>("""
|
|
|
+ INSERT INTO TransactionRecord
|
|
|
+ (ChargeBoxId, ConnectorId, CreatedOn, UpdatedOn, StartTransactionReportedOn, StopTransactionReportedOn,
|
|
|
+ StartIdTag, MeterStart, MeterStop, CustomerId, StartTime, StopTime, ReservationId, RetryStartTransactionTimes, RetryStopTransactionTimes, Fee)
|
|
|
+ OUTPUT INSERTED.Id
|
|
|
+ VALUES (@ChargeBoxId, @ConnectorId, @CreatedOn, @UpdatedOn, @StartTransactionReportedOn, @StopTransactionReportedOn,
|
|
|
+ @StartIdTag, @MeterStart, @MeterStop, @CustomerId, @StartTime, @StopTime, @ReservationId, @RetryStartTransactionTimes, @RetryStopTransactionTimes, @Fee)
|
|
|
+ """, parameters);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<TransactionRecord> GetTransactionForStopTransactionEF(int transactionId, string chargeBoxId)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ return await db.TransactionRecord.Where(x => x.Id == transactionId
|
|
|
+ && x.ChargeBoxId == chargeBoxId).FirstOrDefaultAsync();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<TransactionRecord> GetTransactionForStopTransactionDapper(int transactionId, string chargeBoxId)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@TransactionId", transactionId, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ var record = await conn.QuerySingleAsync<TransactionRecord>("""
|
|
|
+ SELECT ConnectorId, MeterStop, MeterStart, StopTime FROM TransactionRecord
|
|
|
+ WHERE Id = @TransactionId and ChargeBoxId = @ChargeBoxId
|
|
|
+ """, parameters);
|
|
|
+ return record;
|
|
|
}
|
|
|
|
|
|
private Task BulkInsertServerMessage(IEnumerable<ServerMessage> messages)
|
|
@@ -707,6 +832,76 @@ public class MainDbService : IMainDbService
|
|
|
}
|
|
|
return limit;
|
|
|
}
|
|
|
+
|
|
|
+ private async Task<bool> UpdateHeartBeatsDapper(IEnumerable<Machine> heartBeatsData)
|
|
|
+ {
|
|
|
+ using var conn = await sqlConnectionFactory.CreateAsync();
|
|
|
+ using var trans = await conn.BeginTransactionAsync();
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ foreach (var data in heartBeatsData)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@Id", data.Id, DbType.String, ParameterDirection.Input, 36);
|
|
|
+ parameters.Add("@HeartbeatUpdatedOn", data.HeartbeatUpdatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ConnectionType", data.ConnectionType, DbType.Int32, ParameterDirection.Input);
|
|
|
+
|
|
|
+ var resultCnt = await conn.ExecuteAsync("""
|
|
|
+ UPDATE Machine
|
|
|
+ SET HeartbeatUpdatedOn = @HeartbeatUpdatedOn, ConnectionType = @ConnectionType
|
|
|
+ WHERE Id = @Id
|
|
|
+ """, parameters, trans);
|
|
|
+ if (resultCnt != 1)
|
|
|
+ {
|
|
|
+ throw new Exception("Update over one columes");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await trans.CommitAsync();
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ logger.LogCritical("HeartBeatCheckTrigger update fail, roll back");
|
|
|
+ await trans.RollbackAsync();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<bool> UpdateHeartBeatsEF(IEnumerable<Machine> heartBeatsData)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ using var transaction = await db.Database.BeginTransactionAsync();
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ foreach (var data in heartBeatsData)
|
|
|
+ {
|
|
|
+ var machine = new Machine() { Id = data.Id };
|
|
|
+ if (machine != null)
|
|
|
+ {
|
|
|
+ db.Machine.Attach(machine);
|
|
|
+ machine.HeartbeatUpdatedOn = DateTime.UtcNow;
|
|
|
+ machine.ConnectionType = data.ConnectionType;
|
|
|
+ db.Entry(machine).Property(x => x.HeartbeatUpdatedOn).IsModified = true;
|
|
|
+ db.Entry(machine).Property(x => x.ConnectionType).IsModified = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ await db.SaveChangesAsync();
|
|
|
+ await transaction.CommitAsync();
|
|
|
+ db.ChangeTracker.Clear();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogCritical(ex, "HeartBeatCheckTrigger update fail, roll back");
|
|
|
+ transaction.Rollback();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public record MachineAndCustomerInfo (string MachineId, Guid CustomerId, string CustomerName);
|