|
@@ -10,6 +10,9 @@ using System.Collections.Generic;
|
|
|
using System.Data;
|
|
|
using System.Linq;
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
+using EVCB_OCPP.Domain;
|
|
|
+using EVCB_OCPP.WEBAPI.Helpers;
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
namespace EVCB_OCPP.WEBAPI.Services
|
|
|
{
|
|
@@ -22,27 +25,39 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
|
|
|
public class ChargePointService : IChargePointService
|
|
|
{
|
|
|
- readonly string mainConnectionString;
|
|
|
- readonly string meterConnectionString;
|
|
|
+ //readonly string mainConnectionString;
|
|
|
+ //readonly string meterConnectionString;
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
+ private readonly SqlConnectionFactory<MainDBContext> mainDbConneciotnFactory;
|
|
|
+ private readonly SqlConnectionFactory<MeterValueDBContext> meterValueDbConnectionFactory;
|
|
|
|
|
|
- public ChargePointService(IConfiguration configuration, IServiceProvider serviceProvider)
|
|
|
+ public ChargePointService(
|
|
|
+ IServiceProvider serviceProvider,
|
|
|
+ SqlConnectionFactory<MainDBContext> mainDbConneciotnFactory,
|
|
|
+ SqlConnectionFactory<MeterValueDBContext> meterValueDbConnectionFactory)
|
|
|
{
|
|
|
- mainConnectionString = configuration.GetConnectionString("MainDBContext");
|
|
|
- meterConnectionString = configuration.GetConnectionString("MeterValueDBContext");
|
|
|
+ //mainConnectionString = configuration.GetConnectionString("MainDBContext");
|
|
|
+ //meterConnectionString = configuration.GetConnectionString("MeterValueDBContext");
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
+ this.mainDbConneciotnFactory = mainDbConneciotnFactory;
|
|
|
+ this.meterValueDbConnectionFactory = meterValueDbConnectionFactory;
|
|
|
}
|
|
|
|
|
|
public DateTime GetLastUpdatedTimebyMachineId(string machineId)
|
|
|
+ {
|
|
|
+ return GetLastUpdatedTimebyMachineIdAsync(machineId).Result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<DateTime> GetLastUpdatedTimebyMachineIdAsync(string machineId)
|
|
|
{
|
|
|
DateTime lastUpdatedOn = DateTime.UtcNow;
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = await mainDbConneciotnFactory.CreateAsync())
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
|
|
|
- parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input, 36);
|
|
|
parameters.Add("@LastUpdatedTime", lastUpdatedOn, DbType.DateTime, ParameterDirection.Output);
|
|
|
- conn.Execute("GetBasicInfoLastUpdatedTimeById", parameters, commandType: System.Data.CommandType.StoredProcedure);
|
|
|
+ await conn.ExecuteAsync("GetBasicInfoLastUpdatedTimeById", parameters, commandType: System.Data.CommandType.StoredProcedure);
|
|
|
DateTime? time = null;
|
|
|
time = parameters.Get<DateTime?>("@LastUpdatedTime");
|
|
|
|
|
@@ -54,16 +69,46 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public async Task<Dictionary<string, DateTime>> GetLastUpdatedTimebyMachineIdAsync(IEnumerable<string> machineIds)
|
|
|
+ {
|
|
|
+ string cmd = """
|
|
|
+ SELECT Id, MAX(LastUpdatedTime) as LastUpdatedTime
|
|
|
+ FROM
|
|
|
+ (
|
|
|
+ SELECT BasicMachine.Id,
|
|
|
+ CASE
|
|
|
+ WHEN cs.CreatedOn > BasicMachine.CreatedOn
|
|
|
+ THEN cs.CreatedOn
|
|
|
+ WHEN BasicMachine.CreatedOn > BasicMachine.OfflineOn
|
|
|
+ THEN BasicMachine.CreatedOn
|
|
|
+ ELSE BasicMachine.OfflineOn
|
|
|
+ END AS LastUpdatedTime
|
|
|
+ FROM
|
|
|
+ (SELECT Id, OfflineOn, CreatedOn, ChargeBoxId FROM [dbo].[Machine] WHERE Id IN @MachineIds) BasicMachine
|
|
|
+ LEFT JOIN [dbo].[ConnectorStatus] cs
|
|
|
+ On cs.ChargeBoxId = BasicMachine.ChargeBoxId
|
|
|
+ ) t
|
|
|
+ GROUP BY t.Id
|
|
|
+ """;
|
|
|
+
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@MachineIds", machineIds);
|
|
|
+
|
|
|
+ using SqlConnection conn = await mainDbConneciotnFactory.CreateAsync();
|
|
|
+ var tmp = await conn.QueryAsync<LastUpdatedTimeDto>(cmd, parameters);
|
|
|
+ //return tmp.Select(x => new KeyValuePair<string, DateTime>(x.Id, x.LastUpdatedTime));
|
|
|
+ return tmp.ToDictionary(x => x.Id, x => x.LastUpdatedTime);
|
|
|
+ }
|
|
|
+
|
|
|
public string GetMachineIdbyChargeBoxId(string chargeBoxId)
|
|
|
{
|
|
|
string machineId = string.Empty;
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
string strSql = "Select Id from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
|
|
|
- machineId = conn.Query<string>(strSql, parameters).FirstOrDefault();
|
|
|
-
|
|
|
+ machineId = conn.QueryFirstOrDefault<string>(strSql, parameters);
|
|
|
}
|
|
|
|
|
|
return machineId;
|
|
@@ -74,12 +119,12 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
public string GetVersionbyChargeBoxId(string chargeBoxId)
|
|
|
{
|
|
|
string version = string.Empty;
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
string strSql = "Select BoardVersions from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
|
|
|
- version = conn.Query<string>(strSql, parameters).FirstOrDefault();
|
|
|
+ version = conn.QueryFirstOrDefault<string>(strSql, parameters);
|
|
|
}
|
|
|
return version;
|
|
|
}
|
|
@@ -100,15 +145,20 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
|
|
|
|
|
|
public EVSE GetBasicInfobyId(string machineId)
|
|
|
+ {
|
|
|
+ return GetBasicInfobyIdAsync(machineId).Result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<EVSE> GetBasicInfobyIdAsync(string machineId)
|
|
|
{
|
|
|
EVSE cp = new EVSE();
|
|
|
Machine _machine = new Machine();
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = await mainDbConneciotnFactory.CreateAsync())
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
parameters.Add("@Id", machineId, DbType.String, ParameterDirection.Input);
|
|
|
string strSql = "Select ChargeBoxId,ChargePointSerialNumber,ChargePointModel,RatedPower,Online,OfflineOn ,GunAmt,Latitude,Longitude,ConnectorType from [dbo].[Machine] where Id=@Id and IsDelete=0; ";
|
|
|
- _machine = conn.Query<Machine>(strSql, parameters).FirstOrDefault();
|
|
|
+ _machine = await conn.QueryFirstOrDefaultAsync<Machine>(strSql, parameters);
|
|
|
}
|
|
|
if (_machine != null)
|
|
|
{
|
|
@@ -120,16 +170,16 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
cp.LastUpdated = _machine.CreatedOn > _machine.OfflineOn ? _machine.CreatedOn : _machine.OfflineOn;
|
|
|
cp.Coordinates = new GeoLocation() { Latitude = _machine.Latitude, Longitude = _machine.Longitude };
|
|
|
cp.Connectors = new List<Connector>();
|
|
|
- var _Connectors = GetConnectorStatus(_machine.ChargeBoxId);
|
|
|
+ var _Connectors = await GetConnectorStatusAsync(_machine.ChargeBoxId);
|
|
|
for (int i = 1; i <= _machine.GunAmt; i++)
|
|
|
{
|
|
|
var _RefConnector = _Connectors.Where(x => x.ConnectorId == i).FirstOrDefault();
|
|
|
cp.Connectors.Add(new Connector()
|
|
|
{
|
|
|
- ConnectorId = _RefConnector == null ? i : _RefConnector.ConnectorId,
|
|
|
- Status = cp.Status == Status.Offline ? Status.Offline : _RefConnector == null ? Status.Unknown : ConvertConnectorStatus(_RefConnector.Status),
|
|
|
+ ConnectorId = _RefConnector is null ? i : _RefConnector.ConnectorId,
|
|
|
+ Status = cp.Status == Status.Offline ? Status.Offline : _RefConnector is null ? Status.Unknown : ConvertConnectorStatus(_RefConnector.Status),
|
|
|
ConnectorType = ConvertConnectorType(int.Parse(_machine.ConnectorType.Split(',')[i - 1])),
|
|
|
- FaultMessage = ((ChargePointErrorCode)_RefConnector.ChargePointErrorCodeId).ToString() + (string.IsNullOrEmpty(_RefConnector.VendorErrorCode) ? "" : "-" + _RefConnector.VendorErrorCode)
|
|
|
+ FaultMessage = _RefConnector is null ? "Connector not found" : ((ChargePointErrorCode)_RefConnector.ChargePointErrorCodeId).ToString() + (string.IsNullOrEmpty(_RefConnector.VendorErrorCode) ? "" : "-" + _RefConnector.VendorErrorCode)
|
|
|
|
|
|
});
|
|
|
}
|
|
@@ -137,24 +187,121 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
return _machine == null ? null : cp;
|
|
|
}
|
|
|
|
|
|
+ public async Task<Dictionary<string, EVSE>> GetBasicInfobyIdAsync(IEnumerable<string> machineIds)
|
|
|
+ {
|
|
|
+ Dictionary<string, EVSE> cps = new();
|
|
|
+ Dictionary<string , Machine> _machines = new();
|
|
|
+
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@MachineIds", machineIds);
|
|
|
+ string strSql = """
|
|
|
+ SELECT Id,ChargeBoxId,ChargePointSerialNumber,ChargePointModel,RatedPower,Online,OfflineOn ,GunAmt,Latitude,Longitude,ConnectorType
|
|
|
+ FROM [dbo].[Machine]
|
|
|
+ WHERE Id IN @MachineIds and IsDelete=0;
|
|
|
+ """;
|
|
|
+
|
|
|
+ using (SqlConnection conn = await mainDbConneciotnFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ var result = await conn.QueryAsync<Machine>(strSql, parameters);
|
|
|
+ _machines = result.ToDictionary(x => x.Id, x => x);
|
|
|
+ }
|
|
|
+
|
|
|
+ var MachineConnectors = await GetConnectorStatusAsync(machineIds);
|
|
|
+
|
|
|
+ foreach (var machineId in machineIds)
|
|
|
+ {
|
|
|
+ if (!_machines.ContainsKey(machineId))
|
|
|
+ {
|
|
|
+ cps.Add(machineId, null);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ var _machine = _machines[machineId];
|
|
|
+ EVSE cp = new();
|
|
|
+
|
|
|
+ cp.ChargeBoxSystemID = _machine.ChargePointModel + _machine.ChargePointSerialNumber;
|
|
|
+ cp.ChargeBoxId = _machine.ChargeBoxId;
|
|
|
+ cp.NumberofConnectors = _machine.GunAmt;
|
|
|
+ cp.RatedPower = _machine.RatedPower;
|
|
|
+ cp.Status = _machine.Online ? Status.Available : Status.Offline;
|
|
|
+ cp.LastUpdated = _machine.CreatedOn > _machine.OfflineOn ? _machine.CreatedOn : _machine.OfflineOn;
|
|
|
+ cp.Coordinates = new GeoLocation() { Latitude = _machine.Latitude, Longitude = _machine.Longitude };
|
|
|
+ cp.Connectors = new List<Connector>();
|
|
|
+ //var _Connectors = await GetConnectorStatusAsync(_machine.ChargeBoxId);
|
|
|
+ var _Connectors = MachineConnectors[machineId];
|
|
|
+ for (int i = 1; i <= _machine.GunAmt; i++)
|
|
|
+ {
|
|
|
+ var _RefConnector = _Connectors.Where(x => x.ConnectorId == i).FirstOrDefault();
|
|
|
+ cp.Connectors.Add(new Connector()
|
|
|
+ {
|
|
|
+ ConnectorId = _RefConnector is null ? i : _RefConnector.ConnectorId,
|
|
|
+ Status = cp.Status == Status.Offline ? Status.Offline : _RefConnector is null ? Status.Unknown : ConvertConnectorStatus(_RefConnector.Status),
|
|
|
+ ConnectorType = ConvertConnectorType(int.Parse(_machine.ConnectorType.Split(',')[i - 1])),
|
|
|
+ FaultMessage = _RefConnector is null ? "Connector not found" : ((ChargePointErrorCode)_RefConnector.ChargePointErrorCodeId).ToString() + (string.IsNullOrEmpty(_RefConnector.VendorErrorCode) ? "" : "-" + _RefConnector.VendorErrorCode)
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
+ cps.Add(_machine.Id, cp);
|
|
|
+ }
|
|
|
+
|
|
|
+ return cps;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
public List<ConnectorStatus> GetConnectorStatus(string chargeBoxId)
|
|
|
+ {
|
|
|
+ return GetConnectorStatusAsync(chargeBoxId).Result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<List<ConnectorStatus>> GetConnectorStatusAsync(string chargeBoxId)
|
|
|
{
|
|
|
|
|
|
List<ConnectorStatus> _Connectors = new List<ConnectorStatus>();
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = await mainDbConneciotnFactory.CreateAsync())
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
string strSql = "Select ConnectorId,Status,ChargePointErrorCodeId,VendorErrorCode from [dbo].[ConnectorStatus] where ChargeBoxId=@ChargeBoxId order by ConnectorId; ";
|
|
|
- _Connectors = conn.Query<ConnectorStatus>(strSql, parameters).SkipWhile(x => x.ConnectorId == 0).ToList();
|
|
|
+ _Connectors = (await conn.QueryAsync<ConnectorStatus>(strSql, parameters)).SkipWhile(x => x.ConnectorId == 0).ToList();
|
|
|
}
|
|
|
|
|
|
+ return _Connectors;
|
|
|
+ }
|
|
|
|
|
|
+ public async Task<Dictionary<string,List<ConnectorStatus>>> GetConnectorStatusAsync(IEnumerable<string> chargeBoxIds)
|
|
|
+ {
|
|
|
+ Dictionary<string, List<ConnectorStatus>> toReturn = new();
|
|
|
+ IEnumerable<ConnectorStatus> _Connectors;// = new List<ConnectorStatus>();
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@ChargeBoxIds", chargeBoxIds);
|
|
|
+ string strSql = """
|
|
|
+ SELECT ChargeBoxId,ConnectorId,Status,ChargePointErrorCodeId,VendorErrorCode
|
|
|
+ FROM [dbo].[ConnectorStatus]
|
|
|
+ WHERE ChargeBoxId IN @ChargeBoxIds
|
|
|
+ ORDER BY ConnectorId;
|
|
|
+ """;
|
|
|
+
|
|
|
+ using (SqlConnection conn = await mainDbConneciotnFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ _Connectors = (await conn.QueryAsync<ConnectorStatus>(strSql, parameters)).SkipWhile(x => x.ConnectorId == 0);//.ToList();
|
|
|
+ }
|
|
|
|
|
|
- return _Connectors;
|
|
|
+ var tempDic = _Connectors.GroupBy(x => x.ChargeBoxId).ToDictionary(x => x.Key, x => x.ToList());
|
|
|
+ foreach (var chargeBoxId in chargeBoxIds)
|
|
|
+ {
|
|
|
+ if (tempDic.ContainsKey(chargeBoxId))
|
|
|
+ {
|
|
|
+ toReturn.Add(chargeBoxId, tempDic[chargeBoxId]);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ toReturn.Add(chargeBoxId, new List<ConnectorStatus>());
|
|
|
+ }
|
|
|
+
|
|
|
+ return toReturn;
|
|
|
+
|
|
|
+ //return _Connectors;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -166,8 +313,8 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
if (Exists(chargeBoxId))
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select GunAmt from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
|
|
|
count = conn.ExecuteScalarAsync<int>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout).Result;
|
|
@@ -183,8 +330,8 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
|
|
|
if (string.IsNullOrEmpty(chargeBoxId)) return exists;
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select count(*) from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
|
|
|
exists = conn.ExecuteScalar<bool>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout);
|
|
@@ -199,12 +346,12 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
if (string.IsNullOrEmpty(chargeBoxId)) return connectorId;
|
|
|
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
parameters.Add("@TransactionId", transactionId, DbType.Int32, ParameterDirection.Input);
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select ConnectorId from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and Id= @TransactionId and StopTime='1991/01/01'; ";
|
|
|
- connectorId = conn.Query<Int32>(strSql, parameters).FirstOrDefault();
|
|
|
+ connectorId = conn.QueryFirstOrDefault<Int32>(strSql, parameters);
|
|
|
}
|
|
|
|
|
|
return connectorId;
|
|
@@ -220,12 +367,12 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
if (string.IsNullOrEmpty(chargeBoxId)) return exists;
|
|
|
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
parameters.Add("@TransactionId", transactionId, DbType.Int32, ParameterDirection.Input);
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select Id from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and Id= @TransactionId and StopTime='1991/01/01'; ";
|
|
|
- int id = conn.Query<Int32>(strSql, parameters).FirstOrDefault();
|
|
|
+ int id = conn.QueryFirstOrDefault<Int32>(strSql, parameters);//.FirstOrDefault();
|
|
|
exists = id > 0 ? true : false;
|
|
|
}
|
|
|
return exists;
|
|
@@ -244,10 +391,10 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
if (string.IsNullOrEmpty(chargeBoxId)) return _status;
|
|
|
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
parameters.Add("@ConnectorId", connectorId, DbType.Int32, ParameterDirection.Input);
|
|
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select online from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
|
|
|
bool online = conn.ExecuteScalar<bool>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout);
|
|
@@ -256,7 +403,7 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
{
|
|
|
string connectorStrSql = "Select Status from [dbo].[ConnectorStatus] where ChargeBoxId=@ChargeBoxId and ConnectorId=@ConnectorId; ";
|
|
|
|
|
|
- int _statusfromdb = conn.Query<Int32>(connectorStrSql, parameters).FirstOrDefault();
|
|
|
+ int _statusfromdb = conn.QueryFirstOrDefault<Int32>(connectorStrSql, parameters);//.FirstOrDefault();
|
|
|
|
|
|
_status = (_statusfromdb <= 0 && connectorId == 0) ? ChargePointStatus.Available : (_statusfromdb <= 0 ? ((ChargePointStatus?)null) : ((ChargePointStatus)_statusfromdb));
|
|
|
|
|
@@ -274,10 +421,10 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
if (string.IsNullOrEmpty(chargeBoxId)) return online;
|
|
|
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
|
|
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select online from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
|
|
|
online = conn.ExecuteScalar<bool>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout);
|
|
@@ -294,11 +441,11 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
if (string.IsNullOrEmpty(chargeBoxId)) return existed;
|
|
|
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
- parameters.Add("@CustomerId", customerId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@CustomerId", customerId, DbType.String, ParameterDirection.Input, 36);
|
|
|
|
|
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select Count(*) from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and CustomerId=@CustomerId and IsDelete=0; ";
|
|
|
existed = conn.ExecuteScalar<bool>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout);
|
|
@@ -318,10 +465,10 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
try
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
|
|
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string date = DateTime.UtcNow.ToString("yyMMdd");
|
|
|
string strSql = string.Empty;
|
|
@@ -332,7 +479,7 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- parameters.Add(string.IsNullOrEmpty(sessionId) ? "@StartIdTag" : "@Id", string.IsNullOrEmpty(sessionId) ? idTag : sessionId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add(string.IsNullOrEmpty(sessionId) ? "@StartIdTag" : "@Id", string.IsNullOrEmpty(sessionId) ? idTag : sessionId, DbType.String, ParameterDirection.Input, 20);
|
|
|
|
|
|
strSql = "Select Id, ConnectorId, StartTime,Fee, StartIdTag from TransactionRecord " +
|
|
|
"where StopTime = '1991-01-01 00:00:00.000' and ChargeBoxId = @ChargeBoxId and StartTime in (select max(StartTime) from [TransactionRecord] where " + (string.IsNullOrEmpty(sessionId) ? "StartIdTag=@StartIdTag" : "Id=@Id") + " and StopTime = '1991-01-01 00:00:00.000' and ChargeBoxId = @ChargeBoxId group by ConnectorId )";
|
|
@@ -356,9 +503,9 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
for (int j = 0; j < transactionDatas.Count; j++)
|
|
|
{
|
|
|
var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
- parameters.Add("@TransactionId", transactionDatas[j].Id, DbType.String, ParameterDirection.Input);
|
|
|
- parameters.Add("@MeasurandId", requiredMeasurands[i], DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@TransactionId", transactionDatas[j].Id, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@MeasurandId", requiredMeasurands[i], DbType.Int32, ParameterDirection.Input);
|
|
|
|
|
|
try
|
|
|
{
|
|
@@ -368,10 +515,10 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
while (retry < 2)
|
|
|
{
|
|
|
retry++;
|
|
|
- using (SqlConnection conn = new SqlConnection(meterConnectionString))
|
|
|
+ using (SqlConnection conn = meterValueDbConnectionFactory.Create())
|
|
|
{
|
|
|
string strSql = "Select Top(1) * from [dbo].[ConnectorMeterValueRecord" + date + "] where ChargeBoxId=@ChargeBoxId and TransactionId=@TransactionId and MeasurandId=@MeasurandId order by CreatedOn desc;";
|
|
|
- meterModel = conn.Query<ConnectorMeterValueModel>(strSql, parameters, null, true, EVCBConfiguration.DB_DefaultConnectionTimeout).FirstOrDefault();
|
|
|
+ meterModel = conn.QueryFirstOrDefault<ConnectorMeterValueModel>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout);//.FirstOrDefault();
|
|
|
if (meterModel == null)
|
|
|
{
|
|
|
date = transactionDatas[j].StartTime.ToString("yyMMdd");
|
|
@@ -424,7 +571,7 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
}
|
|
|
|
|
|
|
|
|
- public List<SessionDetail> GetSessionDetail(string chargeBoxId, string sessionId, string idTag, DateTime startTime, DateTime stopTime)
|
|
|
+ public List<SessionDetail> GetSessionDetail(string chargeBoxId, int sessionId, string idTag, DateTime startTime, DateTime stopTime)
|
|
|
{
|
|
|
List<SessionDetail> detail = new List<SessionDetail>();
|
|
|
List<TransactionRecordModel> transactionModel = null;
|
|
@@ -444,30 +591,30 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
parameters.Add("@stopTime", stopTime, DbType.DateTime, ParameterDirection.Input);
|
|
|
}
|
|
|
|
|
|
- using (SqlConnection conn = new SqlConnection(mainConnectionString))
|
|
|
+ using (SqlConnection conn = mainDbConneciotnFactory.Create())
|
|
|
{
|
|
|
string strSql = string.Empty;
|
|
|
- if (!string.IsNullOrEmpty(sessionId) && string.IsNullOrEmpty(idTag))
|
|
|
+ if (sessionId >= 0 && string.IsNullOrEmpty(idTag))
|
|
|
{
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
- parameters.Add("@TransactionId", sessionId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
+ parameters.Add("@TransactionId", sessionId, DbType.Int32, ParameterDirection.Input);
|
|
|
strSql = "Select Top(1) * from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and Id=@TransactionId and StopTime!='1991-01-01 00:00:00.000' " + (restrictedRange ? " and StartTime >=@startTime and StartTime <=@stopTime" : "") + " Order by Id desc ;";
|
|
|
}
|
|
|
- else if (!string.IsNullOrEmpty(idTag) && string.IsNullOrEmpty(sessionId))
|
|
|
+ else if (!string.IsNullOrEmpty(idTag) && sessionId < 0)
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(chargeBoxId))
|
|
|
{
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
}
|
|
|
|
|
|
- parameters.Add("@StartIdTag", idTag, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StartIdTag", idTag, DbType.String, ParameterDirection.Input, 20);
|
|
|
strSql = "Select * from [dbo].[TransactionRecord] where " + (!string.IsNullOrEmpty(chargeBoxId) ? " ChargeBoxId=@ChargeBoxId and " : "") + " StartIdTag=@StartIdTag and StopTime!='1991-01-01 00:00:00.000' " + (restrictedRange ? " and StartTime >=@startTime and StartTime <=@stopTime" : "") + " Order by Id desc ;";
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
- parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
|
|
|
|
|
|
strSql = "Select * from [dbo].[TransactionRecord] where ChargeBoxId=@ChargeBoxId and StopTime!='1991-01-01 00:00:00.000' " + (restrictedRange ? " and StartTime >=@startTime and StartTime <=@stopTime" : "") + " Order by Id desc ;";
|
|
|
|
|
@@ -576,4 +723,11 @@ namespace EVCB_OCPP.WEBAPI.Services
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ file class LastUpdatedTimeDto
|
|
|
+ {
|
|
|
+ public DateTime LastUpdatedTime { get; set;}
|
|
|
+
|
|
|
+ public string Id;
|
|
|
+ }
|
|
|
}
|