Files
DE70/API/APIModels.cs
T
2024-09-06 10:15:39 +07:00

187 lines
7.0 KiB
C#

using System.IO;
using System.Net;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Text;
using System.Net.NetworkInformation;
using System.Linq;
using System;
using System.Windows.Documents;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Serilog;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace SafeBagDE70.API
{
public static class APIModels
{
public static async void SendHealthCheck_old(string? ID=null)
{
try
{
if (ID == null) ID = GetMACAddress();
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);
using HttpResponseMessage response = await httpClient.GetAsync(AppData.BaseURL + "HealthCheck?Id=" + ID);
if (response.EnsureSuccessStatusCode().StatusCode == HttpStatusCode.OK)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
}
}
catch(Exception ex)
{
Log.Fatal(ex, "SendHealthCheck");
}
}
public static async Task<bool> SendHealthCheck(DeviceStatus? deviceStatus=null)
{
bool bSuccess = false;
try
{
if(deviceStatus != null)
{
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 + "HealthCheck_v2", 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)
{
bool bSuccess=false;
try{
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(data2Send);
var content = new StringContent(strJsonObj, Encoding.UTF8, "application/json");
using HttpResponseMessage response = await httpClient.PostAsync(AppData.BaseURL + "UpdateCountingData", content);
if (response.EnsureSuccessStatusCode().StatusCode == HttpStatusCode.OK)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
bSuccess = true;
}
}
catch(Exception ex)
{
}
return bSuccess;
}
public static async Task<List<AeonMallCustomer>> GetAeonMallCustomers(int tenantId)
{
try
{
var url = AppData.BaseURL + "GetAeonMallCustomers?tenantId="+tenantId;
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
// Pass the handler to httpclient(from you are calling api)
HttpClient client = new HttpClient(clientHandler);
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
var tenants = JsonConvert.DeserializeObject<List<AeonMallCustomer>>(jsonResponse);
return tenants;
}
else
{
Log.Error($"Error: {response.StatusCode}");
return null;
}
}
catch (Exception ex)
{
Log.Fatal($"Exception: {ex.Message}");
return null;
}
}
private static string GetMACAddress()
{
string? MACAdress = null;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
MACAdress = AddressBytesToString(nic.GetPhysicalAddress().GetAddressBytes());
break;
}
}
if (MACAdress == null) MACAdress = "N/A";
return MACAdress;
}
private static string? AddressBytesToString(byte[] addressBytes)
{
if (addressBytes != null)
{
var str = string.Join(":", (from b in addressBytes
select b.ToString("X2")).ToArray());
return str;
}
else
return null;
}
}
public class AeonMallCustomer
{
public long Id { get; set; }
public DateTime CreationTime { get; set; }
public long? CreatorUserId { get; set; }
public long? DeleterUserId { get; set; }
public DateTime? DeletionTime { get; set; }
public bool IsDeleted { get; set; }
public DateTime? LastModificationTime { get; set; }
public long? LastModifierUserId { get; set; }
public int? TenantId { get; set; }
public string? Cards { get; set; }
public string? TenantCode { get; set; }
public string? TenantName { get; set; }
public string? TenantShortName { get; set; }
public string? OpenningDate { get; set; }
public string? Note { get; set; }
public string? Type { get; set; }
public string? TimeTag { get; set; }
}
}