|
@@ -6,6 +6,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.Tasks;
|
|
|
|
|
|
namespace EVCB_OCPP.WSServer.Service
|
|
namespace EVCB_OCPP.WSServer.Service
|
|
{
|
|
{
|
|
@@ -46,9 +47,9 @@ namespace EVCB_OCPP.WSServer.Service
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public bool IsNeedtoCancelSetting(int stationId, string machineId, string chargeBoxId)
|
|
|
|
|
|
+ async public Task<bool> IsNeedtoCancelSetting(int stationId, string machineId, string chargeBoxId)
|
|
{
|
|
{
|
|
- var setting = GetLoadBalance(stationId);
|
|
|
|
|
|
+ var setting = await GetLoadBalance(stationId);
|
|
if (setting == null) return false;
|
|
if (setting == null) return false;
|
|
|
|
|
|
lock (GetLock(stationId))
|
|
lock (GetLock(stationId))
|
|
@@ -56,14 +57,14 @@ namespace EVCB_OCPP.WSServer.Service
|
|
if (setting.LBMode > 0 && setting.LBMode < 3 && !IsStillInTransactions(chargeBoxId))
|
|
if (setting.LBMode > 0 && setting.LBMode < 3 && !IsStillInTransactions(chargeBoxId))
|
|
{
|
|
{
|
|
// renew table
|
|
// renew table
|
|
- UpdateLoadbalanceRecord(stationId, machineId, 0, DateTime.UtcNow);
|
|
|
|
|
|
+ // UpdateLoadbalanceRecord(stationId, machineId, 0, DateTime.UtcNow);
|
|
return true;
|
|
return true;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if (setting.LBMode >= 3 || setting.LBMode < 1)
|
|
if (setting.LBMode >= 3 || setting.LBMode < 1)
|
|
{
|
|
{
|
|
- CloseLoadbalanceRecord(stationId);
|
|
|
|
|
|
+ // CloseLoadbalanceRecord(stationId);
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -174,65 +175,12 @@ namespace EVCB_OCPP.WSServer.Service
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public Dictionary<string, decimal?> GetRerangeSettingPower(int stationId)
|
|
|
|
- {
|
|
|
|
- Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
|
|
|
|
- var setting = GetLoadBalance(stationId);
|
|
|
|
- if (setting == null) return null;
|
|
|
|
-
|
|
|
|
- lock (GetLock(stationId))
|
|
|
|
- {
|
|
|
|
- if (setting != null && setting.LBMode == 2)
|
|
|
|
- {
|
|
|
|
- string machineId = string.Empty;
|
|
|
|
- decimal ratedPower = GetRatedPower(machineId);
|
|
|
|
- //找站內最早要充電的交易 下發充電Power & 填寫新給的Power
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
|
- {
|
|
|
|
- var parameters = new DynamicParameters();
|
|
|
|
- parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
|
|
- string strSql = "Select M.Id from [dbo].[LoadingBalance] LB, [dbo].[Machine] M where LB.StationId=@StationId and LB.MachineId=M.Id and LB.Power < M.RatedPower and LB.FinishedOn='1991/01/01' order by LB.Id asc; ";
|
|
|
|
- machineId = conn.ExecuteScalar<string>(strSql, parameters);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!string.IsNullOrEmpty(machineId))
|
|
|
|
- {
|
|
|
|
- decimal estimatedPwerValue = GetFCFSPower(stationId, machineId, setting.LBCurrent);
|
|
|
|
-
|
|
|
|
- // renew table
|
|
|
|
- UpdateLoadbalanceRecord(stationId, machineId, estimatedPwerValue, null, true);
|
|
|
|
- // UpdateLoadbalanceRecord(stationId, machineId, estimatedPwerValue, null);
|
|
|
|
-
|
|
|
|
- dic.Add(machineId, estimatedPwerValue);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (setting != null && setting.LBMode == 1)
|
|
|
|
- {
|
|
|
|
- dic = GetAveragePower(stationId, setting.LBCurrent);
|
|
|
|
- foreach (var kv in dic)
|
|
|
|
- {
|
|
|
|
- if (kv.Value.HasValue)
|
|
|
|
- {
|
|
|
|
- UpdateLoadbalanceRecord(stationId, kv.Key, 0, DateTime.UtcNow);
|
|
|
|
- UpdateLoadbalanceRecord(stationId, kv.Key, kv.Value.Value, null);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
- return dic;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Dictionary<string, decimal?> GetSettingPower(int stationId, string machineId)
|
|
|
|
|
|
+ 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 = GetLoadBalance(stationId);
|
|
|
|
|
|
+ var setting = await GetLoadBalance(stationId);
|
|
if (setting == null) return null;
|
|
if (setting == null) return null;
|
|
|
|
|
|
lock (GetLock(stationId))
|
|
lock (GetLock(stationId))
|
|
@@ -242,31 +190,9 @@ namespace EVCB_OCPP.WSServer.Service
|
|
{
|
|
{
|
|
if (setting.LBMode == 1)
|
|
if (setting.LBMode == 1)
|
|
{
|
|
{
|
|
- dic = GetAveragePower(stationId, setting.LBCurrent, machineId);
|
|
|
|
- foreach (var kv in dic)
|
|
|
|
- {
|
|
|
|
- if (kv.Value.HasValue)
|
|
|
|
- {
|
|
|
|
- UpdateLoadbalanceRecord(stationId, kv.Key, 0, DateTime.UtcNow);
|
|
|
|
- UpdateLoadbalanceRecord(stationId, kv.Key, kv.Value.Value, null);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+ dic = GetAveragePower(stationId, setting.LBCurrent).Result;
|
|
}
|
|
}
|
|
- else if (setting.LBMode == 2)
|
|
|
|
- {
|
|
|
|
- dic.Add(machineId, GetFCFSPower(stationId, machineId, setting.LBCurrent));
|
|
|
|
-
|
|
|
|
- UpdateLoadbalanceRecord(stationId, machineId, 0, DateTime.UtcNow);
|
|
|
|
- UpdateLoadbalanceRecord(stationId, machineId, dic[machineId].Value, null);
|
|
|
|
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- // 把LB TABLE 關閉
|
|
|
|
- CloseLoadbalanceRecord(stationId);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -275,7 +201,7 @@ namespace EVCB_OCPP.WSServer.Service
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public LoadBalanceSetting GetLoadBalance(int stationId)
|
|
|
|
|
|
+ async public Task<LoadBalanceSetting> GetLoadBalance(int stationId)
|
|
{
|
|
{
|
|
LoadBalanceSetting setting = null;
|
|
LoadBalanceSetting setting = null;
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
@@ -284,12 +210,13 @@ 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 ; ";
|
|
- setting = conn.Query<LoadBalanceSetting>(strSql, parameters).FirstOrDefault();
|
|
|
|
|
|
+ var result = await conn.QueryAsync<LoadBalanceSetting>(strSql, parameters);
|
|
|
|
+ setting = result.FirstOrDefault();
|
|
}
|
|
}
|
|
return setting;
|
|
return setting;
|
|
}
|
|
}
|
|
|
|
|
|
- private List<string> GetIdsbyStationId(int stationId)
|
|
|
|
|
|
+ async private Task<List<string>> GetIdsbyStationId(int stationId)
|
|
{
|
|
{
|
|
List<string> machineIds = new List<string>();
|
|
List<string> machineIds = new List<string>();
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
@@ -297,97 +224,127 @@ namespace EVCB_OCPP.WSServer.Service
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
parameters.Add("@StationId", stationId, DbType.Int16, ParameterDirection.Input);
|
|
parameters.Add("@StationId", stationId, DbType.Int16, ParameterDirection.Input);
|
|
string strSql = "Select MachineId from [dbo].[StationMachine] where StationId=@StationId; ";
|
|
string strSql = "Select MachineId from [dbo].[StationMachine] where StationId=@StationId; ";
|
|
- machineIds = conn.Query<String>(strSql, parameters).ToList();
|
|
|
|
|
|
+ var result = await conn.QueryAsync<String>(strSql, parameters);
|
|
|
|
+ machineIds = result.ToList();
|
|
}
|
|
}
|
|
return machineIds;
|
|
return machineIds;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- private Dictionary<string, decimal?> GetAveragePower(int stationId, int availableCapacity, string machineId = "")
|
|
|
|
|
|
+ async private Task<Dictionary<string, decimal?>> GetAveragePower(int stationId, int availableCapacity)
|
|
{
|
|
{
|
|
Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
|
|
Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>();
|
|
- //總量 * 該樁的額定功率/該站充電中樁的總額定功率
|
|
|
|
- List<string> _MachineIds = new List<string>();
|
|
|
|
- int skipCount = 0;
|
|
|
|
- int size = 200;
|
|
|
|
- int takeCount = 0;
|
|
|
|
- int totalRatePower = 0;
|
|
|
|
-
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
|
|
|
+ availableCapacity = (int)(availableCapacity * 1000 / 1.05M);
|
|
|
|
+ int keepPower = 0;
|
|
|
|
+ //讀取上一次斷線但還沒充完電的分配量
|
|
|
|
+ var offlineCPs = GetChargeBoxIdbyOfflineCharging(stationId, out keepPower);
|
|
|
|
+ //扣除Keep充電功率 = 分配充電量
|
|
|
|
+ var totalPower = availableCapacity - keepPower;
|
|
|
|
+
|
|
|
|
+ if (totalPower > 0)
|
|
{
|
|
{
|
|
- var parameters = new DynamicParameters();
|
|
|
|
- parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
|
|
- string strSql = "Select MachineId from [dbo].[LoadingBalance] where StationId=@StationId and FinishedOn='1991/01/01'; ";
|
|
|
|
- _MachineIds = conn.Query<string>(strSql, parameters).ToList();
|
|
|
|
- }
|
|
|
|
|
|
+ //總量 * 該樁的額定功率/該站充電中樁的總額定功率
|
|
|
|
+ var onlineChargingCPs = await GetOnlineChargerwithCharging(stationId);
|
|
|
|
+ if (onlineChargingCPs.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ int singlePower = (int)Decimal.Divide(totalPower, onlineChargingCPs.Count);
|
|
|
|
+
|
|
|
|
+ foreach (var id in onlineChargingCPs)
|
|
|
|
+ {
|
|
|
|
+ dic.Add(id, singlePower);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- if (!string.IsNullOrEmpty(machineId) && !_MachineIds.Contains(machineId))
|
|
|
|
- {
|
|
|
|
- _MachineIds.Add(machineId);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- while (skipCount < _MachineIds.Count())
|
|
|
|
- {
|
|
|
|
- takeCount = _MachineIds.Count() - skipCount > size ? size : _MachineIds.Count() - skipCount;
|
|
|
|
|
|
+ return dic;
|
|
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async private Task<List<string>> GetOnlineChargerwithCharging(int stationId)
|
|
|
|
+ {
|
|
|
|
+ List<string> results = new List<string>();
|
|
|
|
+ List<string> machineIds = await GetIdsbyStationId(stationId);
|
|
|
|
+ List<string> chargeboxids = new List<string>();
|
|
|
|
+ using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
|
+ {
|
|
|
|
+ string onlineChargerSql = "Select ChargeBoxId from [dbo].[Machine] where Id in @machineIds and [Online]=1; ";
|
|
|
|
+ var onlineResult = await conn.QueryAsync<string>(onlineChargerSql, new { machineIds = machineIds.ToArray() });
|
|
|
|
+ chargeboxids = onlineResult.ToList();
|
|
|
|
+ foreach (var chargeboxid in chargeboxids)
|
|
{
|
|
{
|
|
- string strSql = "Select Sum(RatedPower) from [dbo].[Machine] where Id in @machineIds and [Online]=1; ";
|
|
|
|
- totalRatePower += conn.ExecuteScalar<Int32>(strSql, new { machineIds = _MachineIds.ToArray() });
|
|
|
|
- skipCount += takeCount;
|
|
|
|
|
|
+ string txSql = "SELECT TOP(1) [Id] from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and StopTime = '1991-01-01 00:00:00.000'; ";
|
|
|
|
+ var txId = await conn.ExecuteScalarAsync<Int64>(txSql, new { ChargeBoxId = chargeboxid });
|
|
|
|
+ if (txId > 0)
|
|
|
|
+ {
|
|
|
|
+ results.Add(chargeboxid);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- foreach (var id in _MachineIds)
|
|
|
|
- {
|
|
|
|
- int singleRatePower = (int)GetRatedPower(id);
|
|
|
|
- var value = totalRatePower == 0 ? 0 : availableCapacity * singleRatePower / totalRatePower;
|
|
|
|
- dic.Add(id, value);
|
|
|
|
}
|
|
}
|
|
|
|
+ return results;
|
|
|
|
+ }
|
|
|
|
|
|
- return dic;
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 取得斷線樁號
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="stationId">站點代號</param>
|
|
|
|
+ /// <param name="ratedPowers">總額定功率</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ private List<string> GetChargeBoxIdbyOfflineCharging(int stationId, out int ratedPowers)
|
|
|
|
+ {
|
|
|
|
+ List<string> machineIds = GetIdsbyStationId(stationId).Result;
|
|
|
|
+ List<string> result = new List<string>();
|
|
|
|
+ ratedPowers = 0;
|
|
|
|
+ using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
|
+ {
|
|
|
|
|
|
|
|
+ string offlineChargerSql = "Select ChargeBoxId from [dbo].[Machine] where Id in @machineIds and [Online]=0; ";
|
|
|
|
+ result = conn.Query<string>(offlineChargerSql, new { machineIds = machineIds }).ToList();
|
|
|
|
+ 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'; ";
|
|
|
|
+ var txId = conn.ExecuteScalar<Int64>(txSql, new { ChargeBoxId = charger });
|
|
|
|
+ if (txId > 0)
|
|
|
|
+ {
|
|
|
|
+ string ratedPowerSql = "Select Sum(RatedPower) from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and [Online]=0; ";
|
|
|
|
+ ratedPowers += conn.ExecuteScalar<int>(ratedPowerSql, new { ChargeBoxId = charger });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ratedPowers *= 1000;
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
- private decimal GetRatedPower(string machineId)
|
|
|
|
|
|
+ private decimal GetRatedPowerbyChargeBoxId(string chargeBoxId)
|
|
{
|
|
{
|
|
decimal ratedPower = 0;
|
|
decimal ratedPower = 0;
|
|
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", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
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);
|
|
}
|
|
}
|
|
return ratedPower;
|
|
return ratedPower;
|
|
}
|
|
}
|
|
|
|
|
|
- private decimal GetFCFSPower(int stationId, string machineId, int availableCapacity)
|
|
|
|
|
|
+ private decimal GetRatedPowerbyId(string machineId)
|
|
{
|
|
{
|
|
-
|
|
|
|
- decimal ongoingPower = 0;
|
|
|
|
- decimal singleRatePower = GetRatedPower(machineId);
|
|
|
|
-
|
|
|
|
- //先找LB 裡面目前下發的Power
|
|
|
|
- decimal? currentPower = GetCurrentSetting(machineId);
|
|
|
|
-
|
|
|
|
- if (!currentPower.HasValue) currentPower = 0;
|
|
|
|
-
|
|
|
|
- //總量 - 所有正在進行的Power
|
|
|
|
|
|
+ decimal ratedPower = 0;
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
{
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
var parameters = new DynamicParameters();
|
|
- parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
|
|
|
|
- string strSql = "Select Sum(Power) from [dbo].[LoadingBalance] where StationId=@StationId and FinishedOn='1991/01/01'; ";
|
|
|
|
- ongoingPower = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
|
|
|
|
+ parameters.Add("@machineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
|
+ string strSql = "Select RatedPower from [dbo].[Machine] where Id=@machineId; ";
|
|
|
|
+ ratedPower = conn.ExecuteScalar<Int32>(strSql, parameters);
|
|
}
|
|
}
|
|
-
|
|
|
|
- return availableCapacity - (ongoingPower - currentPower.Value) > singleRatePower ? singleRatePower :
|
|
|
|
- (availableCapacity - (ongoingPower - currentPower.Value) > 0 ? availableCapacity - (ongoingPower - currentPower.Value) : 0);
|
|
|
|
|
|
+ return ratedPower;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|