|
@@ -7,6 +7,7 @@ using System.Configuration;
|
|
using System.Data;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
|
+using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace EVCB_OCPP.WSServer.Service
|
|
namespace EVCB_OCPP.WSServer.Service
|
|
@@ -23,7 +24,8 @@ namespace EVCB_OCPP.WSServer.Service
|
|
|
|
|
|
public class LoadingBalanceService
|
|
public class LoadingBalanceService
|
|
{
|
|
{
|
|
- ConcurrentDictionary<int, object> _lockDic = new ConcurrentDictionary<int, object>();
|
|
|
|
|
|
+ //ConcurrentDictionary<int, object> _lockDic = new ConcurrentDictionary<int, object>();
|
|
|
|
+ ConcurrentDictionary<int, SemaphoreSlim> _semaphoreDic = new ConcurrentDictionary<int, SemaphoreSlim>();
|
|
private readonly string mainConnectionString;
|
|
private readonly string mainConnectionString;
|
|
private readonly string webConnectionString;
|
|
private readonly string webConnectionString;
|
|
|
|
|
|
@@ -33,15 +35,15 @@ namespace EVCB_OCPP.WSServer.Service
|
|
webConnectionString = configuration.GetConnectionString("WebDBContext");
|
|
webConnectionString = configuration.GetConnectionString("WebDBContext");
|
|
}
|
|
}
|
|
|
|
|
|
- public int GetStationIdByMachineId(string machineId)
|
|
|
|
|
|
+ public async Task<int> GetStationIdByMachineId(string machineId)
|
|
{
|
|
{
|
|
int stationId = 0;
|
|
int stationId = 0;
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
string strSql = "Select StationId from [dbo].[StationMachine] where MachineId=@MachineId ; ";
|
|
string strSql = "Select StationId from [dbo].[StationMachine] where MachineId=@MachineId ; ";
|
|
- stationId = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
|
|
|
|
+ stationId = await conn.ExecuteScalarAsync<Int32>(strSql, parameters);
|
|
}
|
|
}
|
|
return stationId;
|
|
return stationId;
|
|
}
|
|
}
|
|
@@ -52,38 +54,51 @@ namespace EVCB_OCPP.WSServer.Service
|
|
var setting = await GetLoadBalance(stationId);
|
|
var setting = await GetLoadBalance(stationId);
|
|
if (setting == null) return false;
|
|
if (setting == null) return false;
|
|
|
|
|
|
- lock (GetLock(stationId))
|
|
|
|
|
|
+ var semaphore = GetSemaphore(stationId);
|
|
|
|
+ await semaphore.WaitAsync();
|
|
|
|
+ if (setting.LBMode > 0 && setting.LBMode < 3 && !await IsStillInTransactions(chargeBoxId))
|
|
{
|
|
{
|
|
- if (setting.LBMode > 0 && setting.LBMode < 3 && !IsStillInTransactions(chargeBoxId))
|
|
|
|
- {
|
|
|
|
- // renew table
|
|
|
|
- // UpdateLoadbalanceRecord(stationId, machineId, 0, DateTime.UtcNow);
|
|
|
|
- return true;
|
|
|
|
|
|
+ // renew table
|
|
|
|
+ // UpdateLoadbalanceRecord(stationId, machineId, 0, DateTime.UtcNow);
|
|
|
|
+ return true;
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- if (setting.LBMode >= 3 || setting.LBMode < 1)
|
|
|
|
- {
|
|
|
|
- // CloseLoadbalanceRecord(stationId);
|
|
|
|
|
|
+ if (setting.LBMode >= 3 || setting.LBMode < 1)
|
|
|
|
+ {
|
|
|
|
+ // CloseLoadbalanceRecord(stationId);
|
|
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
+ semaphore.Release();
|
|
|
|
|
|
return false;
|
|
return false;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private object GetLock(int stationId)
|
|
|
|
|
|
+ //private object GetLock(int stationId)
|
|
|
|
+ //{
|
|
|
|
+
|
|
|
|
+ // if (!_lockDic.ContainsKey(stationId))
|
|
|
|
+ // {
|
|
|
|
+ // _lockDic.TryAdd(stationId, new object());
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ // return _lockDic[stationId];
|
|
|
|
+
|
|
|
|
+ //}
|
|
|
|
+
|
|
|
|
+ private SemaphoreSlim GetSemaphore(int stationId)
|
|
{
|
|
{
|
|
|
|
|
|
- if (!_lockDic.ContainsKey(stationId))
|
|
|
|
|
|
+ if (!_semaphoreDic.ContainsKey(stationId))
|
|
{
|
|
{
|
|
- _lockDic.TryAdd(stationId, new object());
|
|
|
|
|
|
+ _semaphoreDic.TryAdd(stationId, new SemaphoreSlim(1));
|
|
}
|
|
}
|
|
|
|
|
|
- return _lockDic[stationId];
|
|
|
|
|
|
+ return _semaphoreDic[stationId];
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
private void CloseLoadbalanceRecord(int stationId)
|
|
private void CloseLoadbalanceRecord(int stationId)
|
|
{
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
@@ -106,7 +121,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
if (finishedOn.HasValue)
|
|
if (finishedOn.HasValue)
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
parameters.Add("@FinishedOn", finishedOn.Value, DbType.DateTime, ParameterDirection.Input);
|
|
parameters.Add("@FinishedOn", finishedOn.Value, DbType.DateTime, ParameterDirection.Input);
|
|
string strSql = "Update [dbo].[LoadingBalance] SET FinishedOn=@FinishedOn where MachineId=@MachineId and FinishedOn='1991/01/01'; ";
|
|
string strSql = "Update [dbo].[LoadingBalance] SET FinishedOn=@FinishedOn where MachineId=@MachineId and FinishedOn='1991/01/01'; ";
|
|
conn.Execute(strSql, parameters);
|
|
conn.Execute(strSql, parameters);
|
|
@@ -116,7 +131,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
if (keepgoing)
|
|
if (keepgoing)
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
parameters.Add("@Power", power, DbType.Decimal, ParameterDirection.Input);
|
|
parameters.Add("@Power", power, DbType.Decimal, ParameterDirection.Input);
|
|
string strSql = "Update [dbo].[LoadingBalance] SET Power=@Power where MachineId=@MachineId and FinishedOn='1991/01/01'; ";
|
|
string strSql = "Update [dbo].[LoadingBalance] SET Power=@Power where MachineId=@MachineId and FinishedOn='1991/01/01'; ";
|
|
conn.Execute(strSql, parameters);
|
|
conn.Execute(strSql, parameters);
|
|
@@ -125,7 +140,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
- parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
parameters.Add("@Power", power, DbType.Decimal, ParameterDirection.Input);
|
|
parameters.Add("@Power", power, DbType.Decimal, ParameterDirection.Input);
|
|
parameters.Add("@CreatedOn", DateTime.UtcNow, DbType.DateTime, ParameterDirection.Input);
|
|
parameters.Add("@CreatedOn", DateTime.UtcNow, DbType.DateTime, ParameterDirection.Input);
|
|
parameters.Add("@FinishedOn", new DateTime(1991, 1, 1, 0, 0, 0, DateTimeKind.Utc), DbType.DateTime, ParameterDirection.Input);
|
|
parameters.Add("@FinishedOn", new DateTime(1991, 1, 1, 0, 0, 0, DateTimeKind.Utc), DbType.DateTime, ParameterDirection.Input);
|
|
@@ -143,16 +158,16 @@ namespace EVCB_OCPP.WSServer.Service
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private bool IsStillInTransactions(string chargeBoxId)
|
|
|
|
|
|
+ private async Task<bool> IsStillInTransactions(string chargeBoxId)
|
|
{
|
|
{
|
|
bool result = false;
|
|
bool result = false;
|
|
|
|
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
string strSql = "Select count(*) from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and StopTime='1991/01/01'; ";
|
|
string strSql = "Select count(*) from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and StopTime='1991/01/01'; ";
|
|
- result = conn.ExecuteScalar<bool>(strSql, parameters);
|
|
|
|
|
|
+ result = await conn.ExecuteScalarAsync<bool>(strSql, parameters);
|
|
}
|
|
}
|
|
|
|
|
|
return result;
|
|
return result;
|
|
@@ -165,7 +180,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
string strSql = "Select Power from [dbo].[LoadingBalance] where MachineId=@MachineId and FinishedOn='1991/01/01'; ";
|
|
string strSql = "Select Power from [dbo].[LoadingBalance] where MachineId=@MachineId and FinishedOn='1991/01/01'; ";
|
|
result = conn.ExecuteScalar<decimal>(strSql, parameters);
|
|
result = conn.ExecuteScalar<decimal>(strSql, parameters);
|
|
}
|
|
}
|
|
@@ -173,29 +188,23 @@ namespace EVCB_OCPP.WSServer.Service
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
async public Task<Dictionary<string, decimal?>> GetSettingPower(int stationId)
|
|
async public Task<Dictionary<string, decimal?>> GetSettingPower(int stationId)
|
|
{
|
|
{
|
|
Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
|
|
Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
|
|
var setting = await GetLoadBalance(stationId);
|
|
var setting = await GetLoadBalance(stationId);
|
|
if (setting == null) return null;
|
|
if (setting == null) return null;
|
|
|
|
|
|
- lock (GetLock(stationId))
|
|
|
|
|
|
+ var semaphore = GetSemaphore(stationId);
|
|
|
|
+ await semaphore.WaitAsync();
|
|
|
|
+ if (setting != null)
|
|
{
|
|
{
|
|
-
|
|
|
|
- if (setting != null)
|
|
|
|
|
|
+ if (setting.LBMode == 1)
|
|
{
|
|
{
|
|
- if (setting.LBMode == 1)
|
|
|
|
- {
|
|
|
|
- dic = GetAveragePower(stationId, setting.LBCurrent).Result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ dic = await GetAveragePower(stationId, setting.LBCurrent);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+ semaphore.Release();
|
|
|
|
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
@@ -210,8 +219,8 @@ namespace EVCB_OCPP.WSServer.Service
|
|
parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
|
|
|
|
string strSql = "Select LBMode,LBCurrent from [dbo].[Station] where Id=@StationId ; ";
|
|
string strSql = "Select LBMode,LBCurrent from [dbo].[Station] where Id=@StationId ; ";
|
|
- var result = await conn.QueryAsync<LoadBalanceSetting>(strSql, parameters);
|
|
|
|
- setting = result.FirstOrDefault();
|
|
|
|
|
|
+ setting = await conn.QueryFirstOrDefaultAsync<LoadBalanceSetting>(strSql, parameters);
|
|
|
|
+ //etting = result.FirstOrDefaultAsync();
|
|
}
|
|
}
|
|
return setting;
|
|
return setting;
|
|
}
|
|
}
|
|
@@ -238,7 +247,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
availableCapacity = (int)(availableCapacity * 1000 / 1.05M);
|
|
availableCapacity = (int)(availableCapacity * 1000 / 1.05M);
|
|
int keepPower = 0;
|
|
int keepPower = 0;
|
|
//讀取上一次斷線但還沒充完電的分配量
|
|
//讀取上一次斷線但還沒充完電的分配量
|
|
- var offlineCPs = GetChargeBoxIdbyOfflineCharging(stationId, out keepPower);
|
|
|
|
|
|
+ (var offlineCPs, keepPower) = await GetChargeBoxIdbyOfflineCharging(stationId);
|
|
//扣除Keep充電功率 = 分配充電量
|
|
//扣除Keep充電功率 = 分配充電量
|
|
var totalPower = availableCapacity - keepPower;
|
|
var totalPower = availableCapacity - keepPower;
|
|
|
|
|
|
@@ -292,29 +301,29 @@ namespace EVCB_OCPP.WSServer.Service
|
|
/// <param name="stationId">站點代號</param>
|
|
/// <param name="stationId">站點代號</param>
|
|
/// <param name="ratedPowers">總額定功率</param>
|
|
/// <param name="ratedPowers">總額定功率</param>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- private List<string> GetChargeBoxIdbyOfflineCharging(int stationId, out int ratedPowers)
|
|
|
|
|
|
+ private async Task<(List<string>,int ratedPowers)> GetChargeBoxIdbyOfflineCharging(int stationId)
|
|
{
|
|
{
|
|
List<string> machineIds = GetIdsbyStationId(stationId).Result;
|
|
List<string> machineIds = GetIdsbyStationId(stationId).Result;
|
|
List<string> result = new List<string>();
|
|
List<string> result = new List<string>();
|
|
- ratedPowers = 0;
|
|
|
|
|
|
+ int ratedPowers = 0;
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
{
|
|
{
|
|
|
|
|
|
string offlineChargerSql = "Select ChargeBoxId from [dbo].[Machine] where Id in @machineIds and [Online]=0; ";
|
|
string offlineChargerSql = "Select ChargeBoxId from [dbo].[Machine] where Id in @machineIds and [Online]=0; ";
|
|
- result = conn.Query<string>(offlineChargerSql, new { machineIds = machineIds }).ToList();
|
|
|
|
|
|
+ result = (await conn.QueryAsync<string>(offlineChargerSql, new { machineIds = machineIds })).ToList();
|
|
foreach (var charger in result)
|
|
foreach (var charger in result)
|
|
{
|
|
{
|
|
string txSql = "SELECT TOP(1) [Id] from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and StopTime = '1991-01-01 00:00:00.000'; ";
|
|
string txSql = "SELECT TOP(1) [Id] from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and StopTime = '1991-01-01 00:00:00.000'; ";
|
|
- var txId = conn.ExecuteScalar<Int64>(txSql, new { ChargeBoxId = charger });
|
|
|
|
|
|
+ var txId = await conn.ExecuteScalarAsync<Int64>(txSql, new { ChargeBoxId = charger });
|
|
if (txId > 0)
|
|
if (txId > 0)
|
|
{
|
|
{
|
|
string ratedPowerSql = "Select Sum(RatedPower) from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and [Online]=0; ";
|
|
string ratedPowerSql = "Select Sum(RatedPower) from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and [Online]=0; ";
|
|
- ratedPowers += conn.ExecuteScalar<int>(ratedPowerSql, new { ChargeBoxId = charger });
|
|
|
|
|
|
+ ratedPowers += await conn.ExecuteScalarAsync<int>(ratedPowerSql, new { ChargeBoxId = charger });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ratedPowers *= 1000;
|
|
ratedPowers *= 1000;
|
|
- return result;
|
|
|
|
|
|
+ return (result, ratedPowers);
|
|
}
|
|
}
|
|
|
|
|
|
private decimal GetRatedPowerbyChargeBoxId(string chargeBoxId)
|
|
private decimal GetRatedPowerbyChargeBoxId(string chargeBoxId)
|
|
@@ -323,7 +332,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@machineId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@machineId", chargeBoxId, DbType.String, ParameterDirection.Input, 36);
|
|
string strSql = "Select RatedPower from [dbo].[Machine] where Id=@machineId; ";
|
|
string strSql = "Select RatedPower from [dbo].[Machine] where Id=@machineId; ";
|
|
ratedPower = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
ratedPower = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
}
|
|
}
|
|
@@ -336,7 +345,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@machineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
|
|
+ parameters.Add("@machineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
string strSql = "Select RatedPower from [dbo].[Machine] where Id=@machineId; ";
|
|
string strSql = "Select RatedPower from [dbo].[Machine] where Id=@machineId; ";
|
|
ratedPower = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
ratedPower = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
}
|
|
}
|