Add project files.
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
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;
|
||||
|
||||
namespace SafeBagDE70.API
|
||||
{
|
||||
public static class APIModels
|
||||
{
|
||||
public static string URL = "https://203.171.20.94:9090/Safebag/";
|
||||
public static async void SendHealthCheck(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(URL + "HealthCheck?Id=" + ID);
|
||||
|
||||
if (response.EnsureSuccessStatusCode().StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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(URL + "UpdateCountingData", content);
|
||||
|
||||
if (response.EnsureSuccessStatusCode().StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
bSuccess = true;
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
return bSuccess;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user