Преглед на файлове

1. fix GetEVSEsbyChargeBoxId null reference
2. add customer fileter when get evses by chargeboxid

Robert преди 1 година
родител
ревизия
fe79ce3384

+ 17 - 9
EVCB_OCPP.WEBAPI/Controllers/Version1/CPOController.cs

@@ -79,7 +79,15 @@ namespace EVCB_OCPP.WEBAPI.Controllers.Version1
         [Route("information")]
         [Produces(typeof(CPOOuterResponse))]
         [HttpGet]
-        public IActionResult BasicInformationofEVSEs(int? Limit = -1, int StationId = -1, string ChargeBoxId = "", DateTime? DateFrom = null, DateTime? DateTo = null, int? Offset = 0)
+        public async Task<IActionResult> BasicInformationofEVSEs(
+            [FromServices] ChargePointService _CPService,
+            [FromServices] ChargingStationService _stationService,
+            int? Limit = -1,
+            int StationId = -1,
+            string ChargeBoxId = "",
+            DateTime? DateFrom = null,
+            DateTime? DateTo = null,
+            int? Offset = 0)
         {
 
             var result = new CPOOuterResponse();
@@ -87,8 +95,8 @@ namespace EVCB_OCPP.WEBAPI.Controllers.Version1
             int statusCode = StatusCodes.Status500InternalServerError;
             try
             {
-                ChargePointService _CPService = serviceProvider.GetRequiredService<ChargePointService>();// new ChargePointService();
-                var tt = _CPService.GetLastUpdatedTimebyMachineId("0da4f4a6-a952-46f0-b2f3-696385a9a56a");
+                //ChargePointService _CPService = serviceProvider.GetRequiredService<ChargePointService>();// new ChargePointService();
+                //var tt = _CPService.GetLastUpdatedTimebyMachineId("0da4f4a6-a952-46f0-b2f3-696385a9a56a");
                 if (!IsCustomerIdAvaliable())
                 {
                     //return Request.CreateResponse(statusCode, result);
@@ -98,11 +106,11 @@ namespace EVCB_OCPP.WEBAPI.Controllers.Version1
                 var _customerId = GetCustomerId();
 
                 var _innerResponse = new { EVSEs = new List<EVSE>() };
-                ChargingStationService _stationService = serviceProvider.GetRequiredService<ChargingStationService>();// new ChargingStationService();
+                //ChargingStationService _stationService = serviceProvider.GetRequiredService<ChargingStationService>();// new ChargingStationService();
 
                 if (StationId > -1)
                 {
-                    if (_stationService.ContainsStation(_customerId, StationId))
+                    if (await _stationService.ContainsStation(_customerId, StationId))
                     {
                         if (DateTo.HasValue)
                         {
@@ -117,15 +125,15 @@ namespace EVCB_OCPP.WEBAPI.Controllers.Version1
                                 return StatusCode(statusCode, result);
                             }
                         }
-                        _innerResponse = new { EVSEs = _stationService.GetEVSEsbyStationId(StationId, DateFrom, DateTo, Offset.Value, Limit == -1 ? 1000 : Limit.Value) };
-
+                        //_innerResponse = new { EVSEs = _stationService.GetEVSEsbyStationId(StationId, DateFrom, DateTo, Offset.Value, Limit == -1 ? 1000 : Limit.Value) };
+                        _innerResponse.EVSEs.AddRange(await _stationService.GetEVSEsbyStationIdAsync(StationId, DateFrom, DateTo, Offset.Value, Limit == -1 ? 1000 : Limit.Value));
 
                     }
                 }
                 else
                 {
                     _innerResponse = new { EVSEs = new List<EVSE>() };
-                    _innerResponse.EVSEs.Add(_CPService.GetEVSEsbyChargeBoxId(ChargeBoxId, DateFrom, DateTo));
+                    _innerResponse.EVSEs.Add(await _CPService.GetEVSEsbyChargeBoxId(_customerId, ChargeBoxId, DateFrom, DateTo));
 
 
                 }
@@ -738,7 +746,7 @@ namespace EVCB_OCPP.WEBAPI.Controllers.Version1
 
                 var _innerResult = await _client.Delete(
                     string.Format(urlformat, Request.Scheme, Request.Host.ToUriComponent()),
-                    string.Format("/api/v1/ocpp16/chargingprofile?ChargeBoxId={2}{3}", ChargeBoxId, ChargeProfileId == -1 ? "" : "&Id=" + ChargeProfileId),
+                    string.Format("/api/v1/ocpp16/chargingprofile?ChargeBoxId={0}{1}", ChargeBoxId, ChargeProfileId == -1 ? "" : "&Id=" + ChargeProfileId),
                     new Dictionary<string, string>()
                     {
                         { "PartnerId",_CustomerId}

+ 16 - 9
EVCB_OCPP.WEBAPI/Services/ChargePointService.cs

@@ -112,15 +112,19 @@ namespace EVCB_OCPP.WEBAPI.Services
             return tmp.ToDictionary(x => x.Id, x => x.LastUpdatedTime);
         }
 
-        public string GetMachineIdbyChargeBoxId(string chargeBoxId)
+        public async Task<string> GetMachineIdbyChargeBoxId(string customerId, string chargeBoxId)
         {
             string machineId = string.Empty;
             using (SqlConnection conn = mainDbConneciotnFactory.Create())
             {
                 var parameters = new DynamicParameters();
                 parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
-                string strSql = "Select Id from [dbo].[Machine] where ChargeBoxId=@ChargeBoxId and IsDelete=0; ";
-                machineId = conn.QueryFirstOrDefault<string>(strSql, parameters);
+                parameters.Add("@CustomerId", customerId, DbType.AnsiString, ParameterDirection.Input, 36);
+                string strSql = """
+                    Select Id from [dbo].[Machine]
+                    where CustomerId=@CustomerId and ChargeBoxId=@ChargeBoxId and IsDelete=0; 
+                    """;
+                machineId = await conn.QueryFirstOrDefaultAsync<string>(strSql, parameters);
             }
 
             return machineId;
@@ -141,16 +145,19 @@ namespace EVCB_OCPP.WEBAPI.Services
             return version;
         }
 
-        public EVSE GetEVSEsbyChargeBoxId(string chargeboxid, DateTime? dateFrom, DateTime? dateTo)
+        public async Task<EVSE> GetEVSEsbyChargeBoxId(string customerId, string chargeboxid, DateTime? dateFrom, DateTime? dateTo)
         {
-            string machineId = GetMachineIdbyChargeBoxId(chargeboxid);
-            ChargePointService _CPService = serviceProvider.GetRequiredService<ChargePointService>();//new ChargePointService();
+            string machineId = await GetMachineIdbyChargeBoxId(customerId, chargeboxid);
 
-            var _machineUpdateOn = _CPService.GetLastUpdatedTimebyMachineId(machineId);
+            if (string.IsNullOrEmpty(machineId))
+            {
+                return null;
+            }
 
-            var _machine = _CPService.GetBasicInfobyId(machineId);
-            _machine.LastUpdated = _machineUpdateOn;
+            var _machineUpdateOn = GetLastUpdatedTimebyMachineId(machineId);
 
+            var _machine = GetBasicInfobyId(machineId);
+            _machine.LastUpdated = _machineUpdateOn;
 
             return _machine;
         }

+ 3 - 3
EVCB_OCPP.WEBAPI/Services/ChargingStationService.cs

@@ -113,17 +113,17 @@ namespace EVCB_OCPP.WEBAPI.Services
             return _stations;
         }
 
-        public bool ContainsStation(string customerId, int stationId)
+        public async Task<bool> ContainsStation(string customerId, int stationId)
         {
             bool isContains = false;
 
             var parameters = new DynamicParameters();
             parameters.Add("@CustomerId", customerId, DbType.AnsiString, ParameterDirection.Input, 36);
             parameters.Add("@Id", stationId, DbType.Int32, ParameterDirection.Input);
-            using (SqlConnection conn = webConnectionFactory.Create())
+            using (SqlConnection conn = await webConnectionFactory.CreateAsync())
             {
                 string strSql = "Select count(*) from [dbo].[Station] where CustomerId=@CustomerId and Id=@Id; ";
-                isContains = conn.ExecuteScalar<Int32>(strSql, parameters) > 0 ? true : false;
+                isContains = await conn.ExecuteScalarAsync<Int32>(strSql, parameters) > 0 ? true : false;
             }
 
             return isContains;