123 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
namespace Adaptation.Shared.Metrology;
public partial class WS
{
#nullable enable
public static (string, Results) SendData(string url, long sequence, string directory, object payload, int timeoutSeconds = 120)
{
Results? wsResults = null;
string resultsJson = string.Empty;
try
{
string json = JsonSerializer.Serialize(payload, payload.GetType());
if (string.IsNullOrEmpty(url) || !url.Contains(":") || !url.Contains("."))
throw new Exception("Invalid URL");
using (HttpClient httpClient = new())
{
httpClient.Timeout = new TimeSpan(0, 0, 0, timeoutSeconds, 0);
HttpRequestMessage httpRequestMessage = new()
{
RequestUri = new Uri(url),
Method = HttpMethod.Post,
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
HttpResponseMessage httpResponseMessage = httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead).Result;
resultsJson = httpResponseMessage.Content.ReadAsStringAsync().Result;
wsResults = JsonSerializer.Deserialize(resultsJson, ResultsSourceGenerationContext.Default.Results);
if (wsResults is null)
throw new NullReferenceException(nameof(wsResults));
string checkDirectory = Path.Combine(directory, $"-{wsResults.HeaderId}");
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
File.WriteAllText(Path.Combine(checkDirectory, $"{sequence}.json"), json);
}
if (wsResults.Success is null || !wsResults.Success.Value)
wsResults.Errors?.Add(wsResults.ToString());
}
catch (Exception e)
{ wsResults ??= Results.Get(resultsJson, e); }
return new(resultsJson, wsResults);
}
public static void AttachFile(string url, Attachment attachment, int timeoutSeconds = 60)
{
using HttpClient httpClient = new();
string json = JsonSerializer.Serialize(attachment);
httpClient.Timeout = new TimeSpan(0, 0, 0, timeoutSeconds, 0);
StringContent httpContent = new(json, Encoding.UTF8, "application/json");
HttpResponseMessage httpResponseMessage = httpClient.PostAsync($"{url}/attachment", httpContent).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
string resultBody = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception($"Attachment failed: {resultBody}");
}
}
public static void AttachFiles(string url, List<Attachment>? headerAttachments = null, List<Attachment>? dataAttachments = null)
{
string directory;
try
{
string? directoryName;
if (headerAttachments is not null)
{
foreach (Attachment attachment in headerAttachments)
{
directoryName = Path.GetDirectoryName(attachment.HeaderIdDirectory);
if (string.IsNullOrEmpty(directoryName))
continue;
directory = Path.Combine(directoryName, attachment.AttachmentId) ?? throw new Exception();
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
File.Copy(attachment.SourceFileName, Path.Combine(directory, attachment.DestinationFileName), overwrite: true);
}
}
if (dataAttachments is not null)
{
foreach (Attachment attachment in dataAttachments)
{
directoryName = Path.GetDirectoryName(attachment.HeaderIdDirectory.Replace("Header", "Data"));
if (string.IsNullOrEmpty(directoryName))
continue;
directory = Path.Combine(directoryName, attachment.AttachmentId) ?? throw new Exception();
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
File.Copy(attachment.SourceFileName, Path.Combine(directory, attachment.DestinationFileName), overwrite: true);
}
}
if (headerAttachments is not null)
{
foreach (Attachment attachment in headerAttachments)
AttachFile(url, attachment);
}
if (dataAttachments is not null)
{
foreach (Attachment attachment in dataAttachments)
AttachFile(url, attachment);
}
//MessageBox.Show(r.ToString());
}
catch (Exception e)
{
Exception? exception = e;
StringBuilder stringBuilder = new();
while (exception is not null)
{
_ = stringBuilder.AppendLine(exception.Message);
exception = exception.InnerException;
}
//MessageBox.Show(msgs.ToString(), "Exception", //MessageBoxButtons.OK, //MessageBoxIcon.Error);
throw new Exception(stringBuilder.ToString());
}
}
}