|
@@ -1,10 +1,12 @@
|
|
|
using EVCB_OCPP.Domain;
|
|
|
using EVCB_OCPP.Domain.Models.Database;
|
|
|
+using EVCB_OCPP.Packet.Messages.Core;
|
|
|
using EVCB_OCPP.WSServer.Helper;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
+using Newtonsoft.Json;
|
|
|
using OCPPPackage.Profiles;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
@@ -26,6 +28,8 @@ public interface IMainDbService
|
|
|
Task AddOCMF(OCMF oCMF);
|
|
|
Task<ConnectorStatus> GetConnectorStatus(string ChargeBoxId, int ConnectorId);
|
|
|
Task UpdateConnectorStatus(string Id, ConnectorStatus connectorStatus);
|
|
|
+ Task AddServerMessage(ServerMessage message);
|
|
|
+ Task AddServerMessage(string ChargeBoxId, string OutAction, object OutRequest, string CreatedBy = "", DateTime? CreatedOn = null, string SerialNo = "", string InMessage = "");
|
|
|
}
|
|
|
|
|
|
public class MainDbService : IMainDbService
|
|
@@ -41,7 +45,8 @@ public class MainDbService : IMainDbService
|
|
|
this.opSemaphore = new SemaphoreSlim(opLimit);
|
|
|
|
|
|
InitUpdateConnectorStatusHandler();
|
|
|
- //InitUpdateMachineBasicInfoHandler();
|
|
|
+ InitUpdateMachineBasicInfoHandler();
|
|
|
+ InitAddServerMessageHandler();
|
|
|
}
|
|
|
|
|
|
private readonly IDbContextFactory<MainDBContext> contextFactory;
|
|
@@ -49,7 +54,8 @@ public class MainDbService : IMainDbService
|
|
|
private readonly QueueSemaphore startupSemaphore;
|
|
|
private readonly SemaphoreSlim opSemaphore;
|
|
|
private GroupSingleHandler<StatusNotificationParam> statusNotificationHandler;
|
|
|
- //private GroupSingleHandler<UpdateMachineBasicInfoParam> updateMachineBasicInfoHandler;
|
|
|
+ private GroupSingleHandler<UpdateMachineBasicInfoParam> updateMachineBasicInfoHandler;
|
|
|
+ private GroupSingleHandler<ServerMessage> addServerMessageHandler;
|
|
|
|
|
|
public async Task<MachineAndCustomerInfo> GetMachineIdAndCustomerInfo(string ChargeBoxId)
|
|
|
{
|
|
@@ -90,75 +96,149 @@ public class MainDbService : IMainDbService
|
|
|
|
|
|
public async Task UpdateMachineBasicInfo(string ChargeBoxId, Machine machine)
|
|
|
{
|
|
|
- using var semaphoreWrapper = await startupSemaphore.GetToken();
|
|
|
- using var db = await contextFactory.CreateDbContextAsync();
|
|
|
-
|
|
|
- var _machine = db.Machine.FirstOrDefault(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));
|
|
|
- }
|
|
|
-
|
|
|
- //private void InitUpdateMachineBasicInfoHandler()
|
|
|
- //{
|
|
|
- // if (updateMachineBasicInfoHandler is not null)
|
|
|
- // {
|
|
|
- // throw new Exception($"{nameof(InitUpdateMachineBasicInfoHandler)} should only called once");
|
|
|
- // }
|
|
|
-
|
|
|
- // updateMachineBasicInfoHandler = new GroupSingleHandler<UpdateMachineBasicInfoParam>(async (pams) => {
|
|
|
-
|
|
|
- // using var db = await contextFactory.CreateDbContextAsync();
|
|
|
- // using var trans = await db.Database.BeginTransactionAsync();
|
|
|
-
|
|
|
- // foreach (var pam in pams)
|
|
|
- // {
|
|
|
- // var _machine = db.Machine.FirstOrDefault(x => x.ChargeBoxId == pam.ChargeBoxId);
|
|
|
- // _machine.ChargeBoxSerialNumber = pam.machine.ChargeBoxSerialNumber;
|
|
|
- // _machine.ChargePointSerialNumber = pam.machine.ChargePointSerialNumber;
|
|
|
- // _machine.ChargePointModel = pam.machine.ChargePointModel;
|
|
|
- // _machine.ChargePointVendor = pam.machine.ChargePointVendor;
|
|
|
- // _machine.FW_CurrentVersion = pam.machine.FW_CurrentVersion;
|
|
|
- // _machine.Iccid = DateTime.UtcNow.ToString("yy-MM-dd HH:mm");
|
|
|
- // _machine.Imsi = pam.machine.Imsi;
|
|
|
- // _machine.MeterSerialNumber = pam.machine.MeterSerialNumber;
|
|
|
- // _machine.MeterType = pam.machine.MeterType;
|
|
|
-
|
|
|
- // await db.SaveChangesAsync();
|
|
|
- // }
|
|
|
-
|
|
|
- // await trans.CommitAsync();
|
|
|
- // },
|
|
|
- // loggerFactory.CreateLogger("UpdateMachineBasicInfoHandler"));
|
|
|
- //}
|
|
|
+ //using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+
|
|
|
+ //var _machine = db.Machine.FirstOrDefault(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));
|
|
|
+ }
|
|
|
|
|
|
public async Task AddOCMF(OCMF oCMF)
|
|
|
{
|
|
|
- using var db = contextFactory.CreateDbContext() ;
|
|
|
+ using var db = contextFactory.CreateDbContext();
|
|
|
db.OCMF.Add(oCMF);
|
|
|
await db.SaveChangesAsync();
|
|
|
}
|
|
|
|
|
|
- public async Task<ConnectorStatus> GetConnectorStatus(string ChargeBoxId,int ConnectorId)
|
|
|
+ public async Task<ConnectorStatus> GetConnectorStatus(string ChargeBoxId, int ConnectorId)
|
|
|
{
|
|
|
using var db = contextFactory.CreateDbContext();
|
|
|
return await db.ConnectorStatus.Where(x => x.ChargeBoxId == ChargeBoxId
|
|
|
&& x.ConnectorId == ConnectorId).AsNoTracking().FirstOrDefaultAsync();
|
|
|
}
|
|
|
|
|
|
- public Task UpdateConnectorStatus(string Id, ConnectorStatus connectorStatus)
|
|
|
+ public async Task UpdateConnectorStatus(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();
|
|
|
+ //await statusNotificationHandler.HandleAsync(new StatusNotificationParam(Id, Status));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task AddServerMessage(string ChargeBoxId, string OutAction, object OutRequest, string CreatedBy, DateTime? CreatedOn = null, string SerialNo = "", string InMessage = "")
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(CreatedBy))
|
|
|
+ {
|
|
|
+ CreatedBy = "Server";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(SerialNo))
|
|
|
+ {
|
|
|
+ SerialNo = Guid.NewGuid().ToString();
|
|
|
+ }
|
|
|
+ var _CreatedOn = CreatedOn ?? DateTime.UtcNow;
|
|
|
+
|
|
|
+ string _OutRequest = "";
|
|
|
+ if (OutRequest is not null)
|
|
|
+ {
|
|
|
+ _OutRequest = JsonConvert.SerializeObject(
|
|
|
+ OutRequest,
|
|
|
+ new JsonSerializerSettings()
|
|
|
+ {
|
|
|
+ NullValueHandling = NullValueHandling.Ignore,
|
|
|
+ Formatting = Formatting.None
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return AddServerMessage(new ServerMessage()
|
|
|
+ {
|
|
|
+ ChargeBoxId = ChargeBoxId,
|
|
|
+ CreatedBy = CreatedBy,
|
|
|
+ CreatedOn = _CreatedOn,
|
|
|
+ OutAction = OutAction,
|
|
|
+ OutRequest = _OutRequest,
|
|
|
+ SerialNo = SerialNo,
|
|
|
+ InMessage = InMessage
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task AddServerMessage(ServerMessage message)
|
|
|
+ {
|
|
|
+ return addServerMessageHandler.HandleAsync(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void InitUpdateMachineBasicInfoHandler()
|
|
|
{
|
|
|
- return statusNotificationHandler.HandleAsync(new StatusNotificationParam(Id, connectorStatus));
|
|
|
+ if (updateMachineBasicInfoHandler is not null)
|
|
|
+ {
|
|
|
+ throw new Exception($"{nameof(InitUpdateMachineBasicInfoHandler)} should only called once");
|
|
|
+ }
|
|
|
+
|
|
|
+ updateMachineBasicInfoHandler = new GroupSingleHandler<UpdateMachineBasicInfoParam>(
|
|
|
+ handleFunc: BundelUpdateMachineBasicInfo,
|
|
|
+ logger: loggerFactory.CreateLogger("UpdateMachineBasicInfoHandler"))
|
|
|
+ {
|
|
|
+ WorkerCnt = 10
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task BundelUpdateMachineBasicInfo(IEnumerable<UpdateMachineBasicInfoParam> pams)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ using var trans = await db.Database.BeginTransactionAsync();
|
|
|
+
|
|
|
+ pams = pams.DistinctBy(x => x.ChargeBoxId);
|
|
|
+
|
|
|
+ foreach (var pam in pams)
|
|
|
+ {
|
|
|
+ var _machine = db.Machine.FirstOrDefault(x => x.ChargeBoxId == pam.ChargeBoxId);
|
|
|
+ _machine.ChargeBoxSerialNumber = pam.machine.ChargeBoxSerialNumber;
|
|
|
+ _machine.ChargePointSerialNumber = pam.machine.ChargePointSerialNumber;
|
|
|
+ _machine.ChargePointModel = pam.machine.ChargePointModel;
|
|
|
+ _machine.ChargePointVendor = pam.machine.ChargePointVendor;
|
|
|
+ _machine.FW_CurrentVersion = pam.machine.FW_CurrentVersion;
|
|
|
+ _machine.Iccid = DateTime.UtcNow.ToString("yy-MM-dd HH:mm");
|
|
|
+ _machine.Imsi = pam.machine.Imsi;
|
|
|
+ _machine.MeterSerialNumber = pam.machine.MeterSerialNumber;
|
|
|
+ _machine.MeterType = pam.machine.MeterType;
|
|
|
+ }
|
|
|
+
|
|
|
+ db.SaveChanges();
|
|
|
+ trans.Commit();
|
|
|
}
|
|
|
|
|
|
private void InitUpdateConnectorStatusHandler()
|
|
@@ -168,41 +248,81 @@ public class MainDbService : IMainDbService
|
|
|
throw new Exception($"{nameof(InitUpdateConnectorStatusHandler)} should only called once");
|
|
|
}
|
|
|
|
|
|
- statusNotificationHandler = new GroupSingleHandler<StatusNotificationParam>(async (paramCollection) => {
|
|
|
- using var db = await contextFactory.CreateDbContextAsync();
|
|
|
- using var trans = await db.Database.BeginTransactionAsync();
|
|
|
+ statusNotificationHandler = new GroupSingleHandler<StatusNotificationParam>(
|
|
|
+ handleFunc: BundleUpdateConnectorStatus,
|
|
|
+ logger: loggerFactory.CreateLogger("StatusNotificationHandler"))
|
|
|
+ {
|
|
|
+ WorkerCnt = 10
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- foreach (var param in paramCollection)
|
|
|
- {
|
|
|
- ConnectorStatus status = new() { Id = param.Id };
|
|
|
+ private async Task BundleUpdateConnectorStatus(IEnumerable<StatusNotificationParam> statusNotifications)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ using var trans = await db.Database.BeginTransactionAsync();
|
|
|
|
|
|
- db.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
- db.ConnectorStatus.Attach(status);
|
|
|
+ statusNotifications = statusNotifications.DistinctBy(x => x.Id);
|
|
|
+
|
|
|
+ foreach (var param in statusNotifications)
|
|
|
+ {
|
|
|
+ ConnectorStatus status = new() { Id = param.Id };
|
|
|
+
|
|
|
+ //db.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
+ db.ConnectorStatus.Attach(status);
|
|
|
|
|
|
|
|
|
- status.CreatedOn = param.Status.CreatedOn;
|
|
|
- status.Status = param.Status.Status;
|
|
|
- status.ChargePointErrorCodeId = param.Status.ChargePointErrorCodeId;
|
|
|
- status.ErrorInfo = param.Status.ErrorInfo;
|
|
|
- status.VendorId = param.Status.VendorId;
|
|
|
- status.VendorErrorCode = param.Status.VendorErrorCode;
|
|
|
+ status.CreatedOn = param.Status.CreatedOn;
|
|
|
+ status.Status = param.Status.Status;
|
|
|
+ status.ChargePointErrorCodeId = param.Status.ChargePointErrorCodeId;
|
|
|
+ status.ErrorInfo = param.Status.ErrorInfo;
|
|
|
+ status.VendorId = param.Status.VendorId;
|
|
|
+ status.VendorErrorCode = param.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;
|
|
|
+ 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;
|
|
|
+
|
|
|
+ //db.SaveChanges();
|
|
|
+ }
|
|
|
|
|
|
- //db.SaveChanges();
|
|
|
- await db.SaveChangesAsync();
|
|
|
- }
|
|
|
- await trans.CommitAsync();
|
|
|
+ db.SaveChanges();
|
|
|
+ trans.Commit();
|
|
|
+ db.ChangeTracker.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void InitAddServerMessageHandler()
|
|
|
+ {
|
|
|
+ if (addServerMessageHandler is not null)
|
|
|
+ {
|
|
|
+ throw new Exception($"{nameof(InitAddServerMessageHandler)} should only called once");
|
|
|
}
|
|
|
- , loggerFactory.CreateLogger("StatusNotificationHandler"));
|
|
|
+
|
|
|
+ addServerMessageHandler = new GroupSingleHandler<ServerMessage>(
|
|
|
+ handleFunc: BundleAddServerMessage,
|
|
|
+ logger: loggerFactory.CreateLogger("AddServerMessageHandler"))
|
|
|
+ {
|
|
|
+ WorkerCnt = 1
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
+ private async Task BundleAddServerMessage(IEnumerable<ServerMessage> messages)
|
|
|
+ {
|
|
|
+ using var db = await contextFactory.CreateDbContextAsync();
|
|
|
+ using var trans = await db.Database.BeginTransactionAsync();
|
|
|
+
|
|
|
+ foreach (var message in messages)
|
|
|
+ {
|
|
|
+ db.ServerMessage.Add(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ db.SaveChanges();
|
|
|
+ trans.Commit();
|
|
|
+ db.ChangeTracker.Clear();
|
|
|
+ }
|
|
|
|
|
|
private int GetStartupLimit(IConfiguration configuration)
|
|
|
{
|