FlexitimeTracker/CardReaderService/CardReaderService/DataCenterHelper.cs
chris.watts90@outlook.com 4c99dfba71 Fix install/uninstall files to use correct path for installation on rpi.
Fix CardReaderService.conf for supervisorctl to ensure correct pathing as per install directories.
Correct MessageLogger to ensure it appends log messages, previously overwrote the logs.
Add manual Reset event in Service1.cs to ensure that we exit when we stop.
Add configurationmanager/config property, to allow DataCenterHelper to pull updated configurations at runtime.
Add TimeStamp to CardDataPost object
#103
2019-09-11 17:28:34 +01:00

48 lines
1.6 KiB
C#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Logger;
using Newtonsoft.Json;
namespace CardReaderService
{
static class DataCenterHelper
{
static DataCenterHelper()
{
}
public static void Post(CardDataPost postObject, string url)
{
var cfgMgr = new ConfigurationManager();
//using ConfigurationManager means we can change the value at runtime without having to restart the service.
var endpointConfig = cfgMgr.GetConfiguration("DataCenterServiceEndpoint")?.Value?? "http://localhost:8800";
MessageLogger.Log("Targeting Endpoint: " + endpointConfig);
using (var client = new HttpClient())
{
var jsonObject = JsonConvert.SerializeObject(postObject);
var content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
try
{
MessageLogger.Log("Writing uid: "+ postObject.CardUId + " to the server.");
var fullUrl = endpointConfig + url;
MessageLogger.Log("Writing to URL: " + fullUrl);
var response = client.PostAsync(fullUrl, content).Result;
MessageLogger.Log("Successfully written to server");
}
catch (Exception)
{
MessageLogger.Log("Failed to send log to server.");
}
}
}
public static Task PostAsync(CardDataPost postObject, string url)
{
return Task.Run(() => Post(postObject, url));
}
}
}