sendevent
This commit is contained in:
+126
-3
@@ -14,6 +14,7 @@ using System.Threading.Tasks;
|
||||
using Serilog;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using SafeBagDE70.Model;
|
||||
|
||||
namespace SafeBagDE70.API
|
||||
{
|
||||
@@ -41,19 +42,36 @@ namespace SafeBagDE70.API
|
||||
Log.Fatal(ex, "SendHealthCheck");
|
||||
}
|
||||
}
|
||||
public static async Task<bool> SendHealthCheck(DeviceStatus? deviceStatus=null)
|
||||
public static void SendHealthCheck(DeviceStatus? deviceStatus = null)
|
||||
{
|
||||
TaskSendHealthCheck(deviceStatus).ContinueWith(t => {
|
||||
if (t.IsCompletedSuccessfully)
|
||||
{
|
||||
Log.Information("Send HealthCheck Success");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Information("Send HealthCheck Fail");
|
||||
}
|
||||
});
|
||||
}
|
||||
public static async Task<bool> TaskSendHealthCheck(DeviceStatus? deviceStatus=null)
|
||||
{
|
||||
bool bSuccess = false;
|
||||
try
|
||||
{
|
||||
if(deviceStatus != null)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(deviceStatus);
|
||||
var _deviceStatus = JsonConvert.DeserializeObject<DeviceStatus>(json);
|
||||
_deviceStatus.DeviceTime=DateTime.Now;
|
||||
|
||||
HttpClientHandler clientHandler = new HttpClientHandler();
|
||||
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
|
||||
|
||||
// Pass the handler to httpclient(from you are calling api)
|
||||
HttpClient httpClient = new HttpClient(clientHandler);
|
||||
string strJsonObj = JsonConvert.SerializeObject(deviceStatus);
|
||||
string strJsonObj = JsonConvert.SerializeObject(_deviceStatus);
|
||||
var content = new StringContent(strJsonObj, Encoding.UTF8, "application/json");
|
||||
|
||||
using HttpResponseMessage response = await httpClient.PostAsync(AppData.BaseURL + "HealthCheck_v2", content);
|
||||
@@ -72,6 +90,111 @@ namespace SafeBagDE70.API
|
||||
}
|
||||
return bSuccess;
|
||||
|
||||
}
|
||||
public static void SendEvent(DeviceStatus? deviceStatus = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (deviceStatus == null) return;
|
||||
DeviceEvent deviceEvent = InsertEventToDB(deviceStatus);
|
||||
deviceStatus.DeviceTime = DateTime.Now;
|
||||
deviceStatus.TransactionCode = AppData.GetTransactionCode();
|
||||
TaskSendEvent(deviceStatus).ContinueWith(t =>
|
||||
{
|
||||
if (t.IsCompletedSuccessfully)
|
||||
{
|
||||
Log.Information("Send Event Success");
|
||||
deviceEvent.IsSent = t.Result;
|
||||
UpdateEventToDB(deviceEvent);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Information("Send Event Fail");
|
||||
}
|
||||
});
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "SendEvent");
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateEventToDB(DeviceEvent deviceEvent)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var db = new CDMDbContext())
|
||||
{
|
||||
db.DeviceEvents.Update(deviceEvent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Fail to Update Event to DB");
|
||||
}
|
||||
}
|
||||
|
||||
private static DeviceEvent InsertEventToDB(DeviceStatus? deviceStatus)
|
||||
{
|
||||
var data = JsonConvert.SerializeObject(deviceStatus);
|
||||
var deviceEvent = new DeviceEvent
|
||||
{
|
||||
Data = data,
|
||||
IsSent = false,
|
||||
DeviceTime = DateTime.Now
|
||||
};
|
||||
try
|
||||
{
|
||||
using (var db = new CDMDbContext())
|
||||
{
|
||||
db.DeviceEvents.Add(deviceEvent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Fail to Save Event to DB");
|
||||
}
|
||||
|
||||
return deviceEvent;
|
||||
}
|
||||
|
||||
public static async Task<bool> TaskSendEvent(DeviceStatus? deviceStatus = null)
|
||||
{
|
||||
bool bSuccess = false;
|
||||
try
|
||||
{
|
||||
if (deviceStatus != null)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(deviceStatus);
|
||||
var _deviceStatus = JsonConvert.DeserializeObject<DeviceStatus>(json);
|
||||
_deviceStatus.DeviceTime = DateTime.Now;
|
||||
|
||||
HttpClientHandler clientHandler = new HttpClientHandler();
|
||||
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
|
||||
|
||||
// Pass the handler to httpclient(from you are calling api)
|
||||
HttpClient httpClient = new HttpClient(clientHandler);
|
||||
string strJsonObj = JsonConvert.SerializeObject(_deviceStatus);
|
||||
var content = new StringContent(strJsonObj, Encoding.UTF8, "application/json");
|
||||
|
||||
using HttpResponseMessage response = await httpClient.PostAsync(AppData.BaseURL + "DeviceEvents", content);
|
||||
|
||||
if (response.EnsureSuccessStatusCode().StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
bSuccess = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "SendHealthCheck");
|
||||
}
|
||||
return bSuccess;
|
||||
|
||||
}
|
||||
public static async Task<bool> SendCountingData(CountingData2Send data2Send)
|
||||
{
|
||||
@@ -104,7 +227,7 @@ namespace SafeBagDE70.API
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = AppData.BaseURL + "GetAeonMallCustomers?tenantId="+tenantId;
|
||||
var url = AppData.BaseURL + "GetAeonMallCustomers?tenantId="+tenantId;
|
||||
HttpClientHandler clientHandler = new HttpClientHandler();
|
||||
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user