|
@@ -38,6 +38,7 @@ using Microsoft.AspNetCore.Builder;
|
|
|
using NLog.Extensions.Logging;
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
using System.Collections.ObjectModel;
|
|
|
+using System.Collections.Concurrent;
|
|
|
|
|
|
namespace EVCB_OCPP.WSServer
|
|
|
{
|
|
@@ -82,8 +83,8 @@ namespace EVCB_OCPP.WSServer
|
|
|
|
|
|
private OuterHttpClient httpClient = new OuterHttpClient();
|
|
|
private DateTime lastcheckdt = DateTime.UtcNow.AddSeconds(-20);
|
|
|
- private Dictionary<string, ClientData> clientDic = new Dictionary<string, ClientData>();
|
|
|
- private readonly Object _lockClientDic = new object();
|
|
|
+ private ConcurrentDictionary<string, ClientData> clientDic = new ConcurrentDictionary<string, ClientData>();
|
|
|
+ //private readonly Object _lockClientDic = new object();
|
|
|
private readonly Object _lockConfirmPacketList = new object();
|
|
|
private readonly IConfiguration configuration;
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
@@ -139,11 +140,8 @@ namespace EVCB_OCPP.WSServer
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
- Dictionary<string, ClientData> toReturn = null ;
|
|
|
- lock (_lockClientDic)
|
|
|
- {
|
|
|
- toReturn = new Dictionary<string, ClientData>(clientDic);
|
|
|
- }
|
|
|
+ Dictionary<string, ClientData> toReturn = null;
|
|
|
+ toReturn = new Dictionary<string, ClientData>(clientDic);
|
|
|
return toReturn;
|
|
|
}
|
|
|
}
|
|
@@ -237,11 +235,7 @@ namespace EVCB_OCPP.WSServer
|
|
|
{
|
|
|
Console.WriteLine("Command List Clients");
|
|
|
Dictionary<string, ClientData> _copyClientDic = null;
|
|
|
- lock (_lockClientDic)
|
|
|
- {
|
|
|
- _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
-
|
|
|
- }
|
|
|
+ _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
var list = _copyClientDic.Select(c => c.Value).ToList();
|
|
|
int i = 1;
|
|
|
foreach (var c in list)
|
|
@@ -255,11 +249,7 @@ namespace EVCB_OCPP.WSServer
|
|
|
{
|
|
|
Console.WriteLine("Command List Customer Name");
|
|
|
Dictionary<string, ClientData> _copyClientDic = null;
|
|
|
- lock (_lockClientDic)
|
|
|
- {
|
|
|
- _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
-
|
|
|
- }
|
|
|
+ _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
var lcn = clientDic.Select(c => c.Value.CustomerName).Distinct().ToList();
|
|
|
int iLcn = 1;
|
|
|
foreach (var c in lcn)
|
|
@@ -377,11 +367,7 @@ namespace EVCB_OCPP.WSServer
|
|
|
app.MapPost("/lc", () => {
|
|
|
List<string> toReturn = new List<string>() { "Command List Clients" };
|
|
|
Dictionary<string, ClientData> _copyClientDic = null;
|
|
|
- lock (_lockClientDic)
|
|
|
- {
|
|
|
- _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
-
|
|
|
- }
|
|
|
+ _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
var list = _copyClientDic.Select(c => c.Value).ToList();
|
|
|
var locations = _copyClientDic.Where(x => !string.IsNullOrEmpty(x.Value.StationLocation)).Distinct().Select(x => x.Value.StationLocation).ToList();
|
|
|
int i = 1;
|
|
@@ -402,11 +388,7 @@ namespace EVCB_OCPP.WSServer
|
|
|
app.MapPost("/lcn", () => {
|
|
|
List<string> toReturn = new List<string> { "Command List Customer Name" };
|
|
|
Dictionary<string, ClientData> _copyClientDic = null;
|
|
|
- lock (_lockClientDic)
|
|
|
- {
|
|
|
- _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
-
|
|
|
- }
|
|
|
+ _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
var lcn = clientDic.Select(c => c.Value.CustomerName).Distinct().ToList();
|
|
|
int iLcn = 1;
|
|
|
foreach (var c in lcn)
|
|
@@ -560,40 +542,39 @@ namespace EVCB_OCPP.WSServer
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- lock (_lockClientDic)
|
|
|
+ bool isNotSupported = session.SecWebSocketProtocol.Contains("ocpp1.6") ? false : session.SecWebSocketProtocol.Contains("ocpp2.0") ? false : true;
|
|
|
+ if (isNotSupported)
|
|
|
{
|
|
|
- bool isNotSupported = session.SecWebSocketProtocol.Contains("ocpp1.6") ? false : session.SecWebSocketProtocol.Contains("ocpp2.0") ? false : true;
|
|
|
- if (isNotSupported)
|
|
|
- {
|
|
|
- //logger.LogDebug(string.Format("ChargeBoxId:{0} SecWebSocketProtocol:{1} NotSupported", session.ChargeBoxId, session.SecWebSocketProtocol));
|
|
|
- WriteMachineLog(session, string.Format("SecWebSocketProtocol:{0} NotSupported", session.SecWebSocketProtocol), "Connection", "");
|
|
|
- return;
|
|
|
- }
|
|
|
- ClientData _removeClient = null;
|
|
|
+ //logger.LogDebug(string.Format("ChargeBoxId:{0} SecWebSocketProtocol:{1} NotSupported", session.ChargeBoxId, session.SecWebSocketProtocol));
|
|
|
+ WriteMachineLog(session, string.Format("SecWebSocketProtocol:{0} NotSupported", session.SecWebSocketProtocol), "Connection", "");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //ClientData _removeClient = null;
|
|
|
|
|
|
+ var addedClient = clientDic.GetOrAdd(session.ChargeBoxId, session);
|
|
|
|
|
|
- clientDic.TryGetValue(session.ChargeBoxId, out _removeClient);
|
|
|
- if (_removeClient != null)
|
|
|
- {
|
|
|
- WriteMachineLog(_removeClient, "Duplicate Logins", "Connection", "");
|
|
|
- _removeClient.Close(CloseReason.ServerShutdown);
|
|
|
- RemoveClient(_removeClient);
|
|
|
- }
|
|
|
+ //clientDic.TryGetValue(session.ChargeBoxId, out _removeClient);
|
|
|
+ if (addedClient != session)
|
|
|
+ {
|
|
|
+ WriteMachineLog(addedClient, "Duplicate Logins", "Connection", "");
|
|
|
+ addedClient.Close(CloseReason.ServerShutdown);
|
|
|
+ RemoveClient(addedClient);
|
|
|
+ }
|
|
|
|
|
|
- clientDic.Add(session.ChargeBoxId, session);
|
|
|
- session.m_ReceiveData += new ClientData.OCPPClientDataEventHandler<ClientData, String>(ReceivedMessage);
|
|
|
- // logger.LogDebug("------------New " + (session == null ? "Oops" : session.ChargeBoxId));
|
|
|
- WriteMachineLog(session, "NewSessionConnected", "Connection", "");
|
|
|
+ //clientDic.add.Add(session.ChargeBoxId, session);
|
|
|
+ session.m_ReceiveData += new ClientData.OCPPClientDataEventHandler<ClientData, String>(ReceivedMessage);
|
|
|
+ // logger.LogDebug("------------New " + (session == null ? "Oops" : session.ChargeBoxId));
|
|
|
+ WriteMachineLog(session, "NewSessionConnected", "Connection", "");
|
|
|
|
|
|
- using (var db = maindbContextFactory.CreateDbContext())
|
|
|
+ using (var db = maindbContextFactory.CreateDbContext())
|
|
|
+ {
|
|
|
+ var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
|
|
|
+ if (machine != null)
|
|
|
{
|
|
|
- var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
|
|
|
- if (machine != null)
|
|
|
- {
|
|
|
- machine.ConnectionType = session.Origin.Contains("https") ? 2 : 1;
|
|
|
- db.SaveChanges();
|
|
|
- }
|
|
|
+ machine.ConnectionType = session.Origin.Contains("https") ? 2 : 1;
|
|
|
+ db.SaveChanges();
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -1397,25 +1378,22 @@ namespace EVCB_OCPP.WSServer
|
|
|
|
|
|
private void RemoveClientDic(ClientData session)
|
|
|
{
|
|
|
- if (!string.IsNullOrEmpty(session.ChargeBoxId))
|
|
|
+ if (string.IsNullOrEmpty(session.ChargeBoxId))
|
|
|
{
|
|
|
- lock (_lockClientDic)
|
|
|
- {
|
|
|
-
|
|
|
- if (clientDic.ContainsKey(session.ChargeBoxId))
|
|
|
- {
|
|
|
- if (clientDic[session.ChargeBoxId].SessionID == session.SessionID)
|
|
|
- {
|
|
|
- logger.Debug(String.Format("ChargeBoxId:{0} Remove SessionId:{1} Removed SessionId:{2}", session.ChargeBoxId, session.SessionID, clientDic[session.ChargeBoxId].SessionID));
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- clientDic.Remove(session.ChargeBoxId);
|
|
|
- logger.Trace("RemoveClient ContainsKey " + session.ChargeBoxId);
|
|
|
- }
|
|
|
+ if (clientDic.ContainsKey(session.ChargeBoxId))
|
|
|
+ {
|
|
|
+ if (clientDic[session.ChargeBoxId].SessionID == session.SessionID)
|
|
|
+ {
|
|
|
+ logger.Debug(String.Format("ChargeBoxId:{0} Remove SessionId:{1} Removed SessionId:{2}", session.ChargeBoxId, session.SessionID, clientDic[session.ChargeBoxId].SessionID));
|
|
|
|
|
|
- }
|
|
|
+ clientDic.Remove(session.ChargeBoxId, out _);
|
|
|
+ logger.Trace("RemoveClient ContainsKey " + session.ChargeBoxId);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private async void WarmUpLog()
|