PostService
This commit is contained in:
21
Server/Services/FileService.cs
Normal file
21
Server/Services/FileService.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Barcode.Host.Server.Services;
|
||||
|
||||
public class FileService : IFileService
|
||||
{
|
||||
|
||||
void IFileService.Write(string equipmentName, string fileShare, Calendar? calendar, Notification notification)
|
||||
{
|
||||
DateTime dateTime = DateTime.Now;
|
||||
calendar ??= new CultureInfo("en-US").Calendar;
|
||||
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
||||
string directory = Path.Combine(fileShare, weekOfYear, dateTime.ToString("yyyy-MM-dd_HH"));
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
File.WriteAllText(Path.Combine(directory, $"{equipmentName}.csv"), notification.LastScanServiceResultValue);
|
||||
}
|
||||
|
||||
}
|
34
Server/Services/PostService.cs
Normal file
34
Server/Services/PostService.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using System.Text;
|
||||
|
||||
namespace Barcode.Host.Server.Services;
|
||||
|
||||
public class PostService : IPostService
|
||||
{
|
||||
|
||||
private static Task<HttpResponseMessage> PostAsync(string postTo, HttpClient httpClient, Notification notification)
|
||||
{
|
||||
Task<HttpResponseMessage> result;
|
||||
string json = System.Text.Json.JsonSerializer.Serialize(notification);
|
||||
StringContent stringContent = new(json, Encoding.UTF8, "application/json");
|
||||
result = httpClient.PostAsync(postTo, stringContent);
|
||||
return result;
|
||||
}
|
||||
|
||||
Task<HttpResponseMessage> IPostService.PostAsync(string postTo, HttpClient httpClient, Notification notification)
|
||||
{
|
||||
Task<HttpResponseMessage> result = PostAsync(postTo, httpClient, notification);
|
||||
return result;
|
||||
}
|
||||
|
||||
HttpResponseMessage IPostService.Post(string postTo, HttpClient httpClient, Notification notification)
|
||||
{
|
||||
HttpResponseMessage result;
|
||||
Task<HttpResponseMessage> httpResponseMessage = PostAsync(postTo, httpClient, notification);
|
||||
httpResponseMessage.Wait();
|
||||
result = httpResponseMessage.Result;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user