|
@@ -1,5 +1,6 @@
|
|
|
|
|
|
using EVCB_OCPP.Domain;
|
|
|
+using EVCB_OCPP.WSServer.Service;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
@@ -26,6 +27,7 @@ namespace OCPPServer.Protocol
|
|
|
static private ILogger logger;
|
|
|
private readonly IConfiguration configuration;
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
+ private readonly MainDbService mainDbService;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 可允許連線Clinet數
|
|
@@ -41,12 +43,13 @@ namespace OCPPServer.Protocol
|
|
|
/// Initializes a new instance of the <see cref="WebSocketServer"/> class.
|
|
|
/// </summary>
|
|
|
/// <param name="subProtocols">The sub protocols.</param>
|
|
|
- public OCPPWSServer(IEnumerable<ISubProtocol<ClientData>> subProtocols, IServiceProvider serviceProvider)
|
|
|
+ public OCPPWSServer(IEnumerable<ISubProtocol<ClientData>> subProtocols, IServiceProvider serviceProvider
|
|
|
+ ,MainDbService mainDbService)
|
|
|
: base(subProtocols)
|
|
|
{
|
|
|
this.configuration = serviceProvider.GetService<IConfiguration>();
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
-
|
|
|
+ this.mainDbService = mainDbService;
|
|
|
logger = serviceProvider.GetService<ILogger<OCPPWSServer>>();
|
|
|
}
|
|
|
|
|
@@ -107,40 +110,45 @@ namespace OCPPServer.Protocol
|
|
|
logger.LogInformation(string.Format("ValidateHandshake: {0}", session.Path));
|
|
|
bool isExistedSN = false;
|
|
|
bool authorizated = false;
|
|
|
- using (var db = serviceProvider.GetService<IDbContextFactory<MainDBContext>>().CreateDbContext())
|
|
|
- {
|
|
|
- var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefault();
|
|
|
- session.CustomerName = machine == null ? "Unknown" : db.Customer.Where(x => x.Id == machine.CustomerId).Select(x => x.Name).FirstOrDefault();
|
|
|
- session.CustomerId = machine == null ? Guid.Empty : machine.CustomerId;
|
|
|
- session.MachineId = machine == null ? String.Empty : machine.Id;
|
|
|
- isExistedSN = machine == null ? false : true;
|
|
|
|
|
|
- if (!isExistedSN)
|
|
|
- {
|
|
|
- StringBuilder responseBuilder = new StringBuilder();
|
|
|
+ var info = mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId).Result;
|
|
|
+ //var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefault();
|
|
|
+ //session.CustomerName = machine == null ? "Unknown" : db.Customer.Where(x => x.Id == machine.CustomerId).Select(x => x.Name).FirstOrDefault();
|
|
|
+ //session.CustomerId = machine == null ? Guid.Empty : machine.CustomerId;
|
|
|
+ //session.MachineId = machine == null ? String.Empty : machine.Id;
|
|
|
+ //isExistedSN = machine == null ? false : true;
|
|
|
+ session.CustomerName = info.CustomerName;
|
|
|
+ session.CustomerId = info.CustomerId;
|
|
|
+ session.MachineId = info.MachineId;
|
|
|
+ isExistedSN = !string.IsNullOrEmpty(info.MachineId);// machine == null ? false : true;
|
|
|
+
|
|
|
+ if (!isExistedSN)
|
|
|
+ {
|
|
|
+ StringBuilder responseBuilder = new StringBuilder();
|
|
|
|
|
|
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
- (int)HttpStatusCode.NotFound, @"Not Found");
|
|
|
+ responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
+ (int)HttpStatusCode.NotFound, @"Not Found");
|
|
|
|
|
|
- responseBuilder.AppendWithCrCf();
|
|
|
- string sb = responseBuilder.ToString();
|
|
|
- byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
- ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
+ responseBuilder.AppendWithCrCf();
|
|
|
+ string sb = responseBuilder.ToString();
|
|
|
+ byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
+ ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
|
|
|
- logger.LogInformation(sb);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ logger.LogInformation(sb);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- var configVaule = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
|
|
|
- .Select(x => x.ConfigureSetting).FirstOrDefault();
|
|
|
- int.TryParse(configVaule, out securityProfile);
|
|
|
+ //var configVaule = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
|
|
|
+ // .Select(x => x.ConfigureSetting).FirstOrDefault();
|
|
|
+ var configVaule = mainDbService.GetMachineSecurityProfile(session.ChargeBoxId).Result;
|
|
|
+ int.TryParse(configVaule, out securityProfile);
|
|
|
|
|
|
- if (session.ISOCPP20)
|
|
|
- {
|
|
|
- // 1.6 server only support change server function
|
|
|
- securityProfile = 0;
|
|
|
- }
|
|
|
+ if (session.ISOCPP20)
|
|
|
+ {
|
|
|
+ // 1.6 server only support change server function
|
|
|
+ securityProfile = 0;
|
|
|
}
|
|
|
+
|
|
|
if (securityProfile == 3 && session.UriScheme == "ws")
|
|
|
{
|
|
|
StringBuilder responseBuilder = new StringBuilder();
|
|
@@ -166,20 +174,16 @@ namespace OCPPServer.Protocol
|
|
|
|
|
|
if (session.Items.ContainsKey("Authorization") || session.Items.ContainsKey("authorization"))
|
|
|
{
|
|
|
+ //authorizationKey = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
|
|
|
+ // .Select(x => x.ConfigureSetting).FirstOrDefault();
|
|
|
+ authorizationKey = mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId).Result;
|
|
|
|
|
|
- using (var db = serviceProvider.GetService<IDbContextFactory<MainDBContext>>().CreateDbContext())
|
|
|
+ if (session.ISOCPP20)
|
|
|
{
|
|
|
-
|
|
|
- authorizationKey = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
|
|
|
- .Select(x => x.ConfigureSetting).FirstOrDefault();
|
|
|
-
|
|
|
-
|
|
|
- if (session.ISOCPP20)
|
|
|
- {
|
|
|
- // 1.6 server only support change server function
|
|
|
- securityProfile = 0;
|
|
|
- }
|
|
|
+ // 1.6 server only support change server function
|
|
|
+ securityProfile = 0;
|
|
|
}
|
|
|
+
|
|
|
logger.LogInformation("***********Authorization ");
|
|
|
|
|
|
if (!string.IsNullOrEmpty(authorizationKey))
|