Browse Source

1. 調整呼叫API 功能
2.檢查是否在線的時間放寬 => 120秒

Jessica Tseng 4 years ago
parent
commit
25cc2fec87

+ 1 - 0
EVCB_OCPP.TaskScheduler/EVCB_OCPP.TaskScheduler.csproj

@@ -120,6 +120,7 @@
     <Compile Include="Jobs\StartTransacionReportJob.cs" />
     <Compile Include="Jobs\StopTransacionReportJob.cs" />
     <Compile Include="Models\ComandExecution.cs" />
+    <Compile Include="Models\CustomerConnectionDto.cs" />
     <Compile Include="Models\EVSECurrentStatus.cs" />
     <Compile Include="Models\EVSEOnlineRecord.cs" />
     <Compile Include="Models\Internal_ExecutionCode.cs" />

+ 1 - 1
EVCB_OCPP.TaskScheduler/Jobs/CheckEVSEOnlineJob.cs

@@ -391,7 +391,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
         private bool IsOnlineNow(EVSECurrentStatus currentEVSE)
         {
             bool isOnline = false;
-            var checkTime = DateTime.Now.AddSeconds(-50);
+            var checkTime = DateTime.Now.AddSeconds(-120);
 
             if (currentEVSE.HeartbeatUpdatedOn > checkTime)
             {

+ 17 - 0
EVCB_OCPP.TaskScheduler/Models/CustomerConnectionDto.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EVCB_OCPP.TaskScheduler.Models
+{
+   public class CustomerConnectionDto
+    {
+        public string ApiKey { set; get; }
+
+        public string ApiUrl { set; get; }
+
+
+    }
+}

+ 4 - 4
EVCB_OCPP.TaskScheduler/OuterHttpClient.cs

@@ -15,7 +15,7 @@ namespace EVCB_OCPP.TaskScheduler
     {
         private HttpClientService httpClient = new HttpClientService();
 
-        async public Task<HttpResult> Post(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
+        async public Task<HttpResult> Post(string url, Dictionary<string, string> headers, string requestBody, string saltkey)
         {
             HttpResult result = new HttpResult() { Success = false };
 
@@ -87,7 +87,7 @@ namespace EVCB_OCPP.TaskScheduler
             return result;
         }
 
-        async public Task<HttpResult> Put(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
+        async public Task<HttpResult> Put(string url, Dictionary<string, string> headers, string requestBody, string saltkey)
         {
             HttpResult result = new HttpResult() { Success = false };
 
@@ -110,9 +110,9 @@ namespace EVCB_OCPP.TaskScheduler
             return result;
         }
 
-        private string PreAction(string url, ref Dictionary<string, string> headers, object requestBody, string saltkey)
+        private string PreAction(string url, ref Dictionary<string, string> headers, string requestBody, string saltkey)
         {
-            var _body = requestBody == null ? "" : JsonConvert.SerializeObject(requestBody, DefaultSetting.JSONSERIALIZER_FORMAT);
+            var _body = requestBody == null ? "" : requestBody;
             headers.Add("Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
             string signature = GetSignature(GetUnencodeText(url, _body, headers["Timestamp"], headers["PartnerId"], saltkey));
             headers.Add("Signature", signature);

+ 12 - 7
EVCB_OCPP.TaskScheduler/Services/CommonCustomerService.cs

@@ -16,7 +16,8 @@ namespace EVCB_OCPP.TaskScheduler.Services
         private NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger();
         private Guid customerId = Guid.Empty;
         private string customerName = string.Empty;
-        private string partnerAPIRoot = string.Empty;
+        private string _partnerAPIRoot = string.Empty;
+        private string _saltkey = string.Empty;
         private CancellationToken _ct;
         private DatabaseService _dbService = new DatabaseService();
         private ParallelOptions po = new ParallelOptions();
@@ -30,6 +31,10 @@ namespace EVCB_OCPP.TaskScheduler.Services
             this.customerId = customerId;
             customerName = _dbService.GetCustomerName(this.customerId);
             _dbService.GetCustomerName(this.customerId);
+            var connectionInfo = _dbService.GetAPIConnectionInfo(customerId);
+            _saltkey = connectionInfo.ApiKey;
+            _partnerAPIRoot = connectionInfo.ApiUrl;
+
         }
 
 
@@ -112,11 +117,11 @@ namespace EVCB_OCPP.TaskScheduler.Services
                                 StartTime = r.StartTime.ToString(DefaultSetting.UTC_DATETIMEFORMAT)
                             };
 
-                            var response = await httpClient.Post(partnerAPIRoot + "start_session", new Dictionary<string, string>()
+                            var response = await httpClient.Post(_partnerAPIRoot + "start_session", new Dictionary<string, string>()
                             {
                                 { "PartnerId",customerId.ToString()}
 
-                            }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _dbService.GetAPIKey(customerId));
+                            }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _saltkey);
 
 
                             lock (responseLock)
@@ -229,11 +234,11 @@ namespace EVCB_OCPP.TaskScheduler.Services
 
                             };
 
-                            var response = await httpClient.Post(partnerAPIRoot + "completed_session", new Dictionary<string, string>()
+                            var response = await httpClient.Post(_partnerAPIRoot + "completed_session", new Dictionary<string, string>()
                             {
                                 { "PartnerId",customerId.ToString()}
 
-                            }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _dbService.GetAPIKey(customerId));
+                            }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _saltkey);
 
 
                             lock (responseLock)
@@ -355,11 +360,11 @@ namespace EVCB_OCPP.TaskScheduler.Services
                                 Message = r.GetExecution().Detail,
                             };
 
-                            var response = await httpClient.Post(partnerAPIRoot + "commands/results", new Dictionary<string, string>()
+                            var response = await httpClient.Post(_partnerAPIRoot + "commands/results", new Dictionary<string, string>()
                             {
                                 { "PartnerId",customerId.ToString()}
 
-                            }, JsonConvert.SerializeObject(request,DefaultSetting.JSONSERIALIZER_FORMAT), _dbService.GetAPIKey(customerId));
+                            }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _saltkey);
 
 
                             lock (responseLock)

+ 5 - 4
EVCB_OCPP.TaskScheduler/Services/DatabaseService.cs

@@ -71,17 +71,18 @@ namespace EVCB_OCPP.TaskScheduler.Services
 
         }
 
-        internal string GetAPIKey(Guid partnerId)
+        internal CustomerConnectionDto GetAPIConnectionInfo(Guid partnerId)
         {
+            CustomerConnectionDto result = new CustomerConnectionDto();
             string key = string.Empty;
             var parameters = new DynamicParameters();
             parameters.Add("@Id", partnerId, DbType.Guid, ParameterDirection.Input);
             using (SqlConnection conn = new SqlConnection(mainDBConnectString))
             {
-                string strSql = "Select ApiKey from [dbo].[Customer] where Id=@Id; ";
-                key = conn.ExecuteScalarAsync<string>(strSql, parameters, null, DefaultSetting.DB_DefaultConnectionTimeout).Result;
+                string strSql = "Select ApiKey, ApiUrl from [dbo].[Customer] where Id=@Id; ";
+                result = conn.Query<CustomerConnectionDto>(strSql, parameters).FirstOrDefault();
             }
-            return key;
+            return result;
 
         }