302 lines
10 KiB
C#
302 lines
10 KiB
C#
namespace LaaProduction
|
|
{
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Mail;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
[ClassInterface(ClassInterfaceType.AutoDual)]
|
|
public partial class LaaProductionAPI
|
|
{
|
|
private const string Bearer = nameof(Bearer);
|
|
private const string RoutePrefix = "/LaaProductionWeb/API";
|
|
|
|
private readonly HttpClient httpClient;
|
|
|
|
private string bearer;
|
|
|
|
public LaaProductionAPI()
|
|
{
|
|
this.httpClient = new HttpClient()
|
|
{
|
|
BaseAddress = new Uri("http://sla12iis01.emea.sensus.net")
|
|
// BaseAddress = new Uri("http://localhost:52822")
|
|
};
|
|
}
|
|
|
|
public string Login(string assembly, int majorV, int minorV, int buildV, string username, string password)
|
|
{
|
|
var data = new LoginModel
|
|
{
|
|
Assembly = assembly,
|
|
MajorV = majorV,
|
|
MinorV = minorV,
|
|
BuildV = buildV,
|
|
Username = username,
|
|
Password = password.ComputeBase64Hash()
|
|
};
|
|
|
|
if (!data.TryValidate(out string errors))
|
|
{
|
|
return errors;
|
|
}
|
|
|
|
var error = default(Error);
|
|
|
|
try
|
|
{
|
|
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri($"{RoutePrefix}/Login/Local", UriKind.Relative)))
|
|
{
|
|
httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
|
|
|
|
var httpResponseMessage = this.httpClient
|
|
.SendAsync(httpRequestMessage)
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
if (httpResponseMessage?.Content != null)
|
|
{
|
|
using (httpResponseMessage)
|
|
{
|
|
var responseContent = httpResponseMessage
|
|
.Content
|
|
.ReadAsStringAsync()
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
try
|
|
{
|
|
if (!httpResponseMessage.IsSuccessStatusCode)
|
|
{
|
|
error = JsonConvert.DeserializeObject<Error>(responseContent);
|
|
}
|
|
else
|
|
{
|
|
this.bearer = JsonConvert.DeserializeObject<string>(responseContent);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
error = new Error
|
|
{
|
|
Message = e.Message,
|
|
MessageDetail = e.StackTrace
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return $"{e.Message}{Environment.NewLine}{e.StackTrace}";
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(this.bearer))
|
|
{
|
|
return "Invalid login attempt at '~/LaaProductionWeb/API/Login/Local'!";
|
|
}
|
|
else if (error != null)
|
|
{
|
|
return error.ToString();
|
|
}
|
|
|
|
|
|
return default(string);
|
|
}
|
|
|
|
public EquipmentsResult GetPendingInspections()
|
|
{
|
|
var result = new EquipmentsResult();
|
|
|
|
try
|
|
{
|
|
var uri = new Uri($"{RoutePrefix}/Personalization/Software/PendingInspections", UriKind.Relative);
|
|
|
|
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
|
|
{
|
|
httpRequestMessage
|
|
.Headers
|
|
.Authorization = new AuthenticationHeaderValue(Bearer, this.bearer);
|
|
|
|
var httpResponseMessage = this.httpClient
|
|
.SendAsync(httpRequestMessage)
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
if (httpResponseMessage?.Content != null)
|
|
{
|
|
using (httpResponseMessage)
|
|
{
|
|
var responseContent = httpResponseMessage
|
|
.Content
|
|
.ReadAsStringAsync()
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
try
|
|
{
|
|
if (!httpResponseMessage.IsSuccessStatusCode)
|
|
{
|
|
result.Error = JsonConvert.DeserializeObject<Error>(responseContent);
|
|
}
|
|
else
|
|
{
|
|
var pendingInspections = JsonConvert.DeserializeObject<IEnumerable<Equipment>>(responseContent);
|
|
|
|
result.AddRange(pendingInspections);
|
|
|
|
foreach (var equipment in pendingInspections)
|
|
{
|
|
var daysInSchedule = this.GetDaysInSchedule(equipment);
|
|
|
|
if (equipment.Erforderlich && daysInSchedule.Contains(equipment.DaysLeft))
|
|
{
|
|
this.SendPruefmittelPruefungReminderEmail(equipment);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
httpResponseMessage.Dispose();
|
|
httpRequestMessage.Dispose();
|
|
|
|
result.Error = new Error
|
|
{
|
|
Message = e.Message,
|
|
MessageDetail = e.StackTrace
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
result.Error = new Error
|
|
{
|
|
Message = e.Message,
|
|
MessageDetail = e.StackTrace
|
|
};
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Error TryRequestQAP(int auftragNr, int positionNr)
|
|
{
|
|
try
|
|
{
|
|
var uri = new Uri($"{RoutePrefix}/Production/VFM/QAPReady/{auftragNr}/{positionNr}", UriKind.Relative);
|
|
|
|
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
|
|
{
|
|
httpRequestMessage
|
|
.Headers
|
|
.Authorization = new AuthenticationHeaderValue(Bearer, this.bearer);
|
|
|
|
var httpResponseMessage = this.httpClient
|
|
.SendAsync(httpRequestMessage)
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
using (httpResponseMessage)
|
|
{
|
|
if (httpResponseMessage.IsSuccessStatusCode == false)
|
|
{
|
|
var responseContent = httpResponseMessage
|
|
.Content
|
|
.ReadAsStringAsync()
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
return JsonConvert.DeserializeObject<Error>(responseContent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return new Error
|
|
{
|
|
Message = e.Message,
|
|
MessageDetail = e.StackTrace
|
|
};
|
|
}
|
|
|
|
return default(Error);
|
|
}
|
|
|
|
private IEnumerable<int> GetDaysInSchedule(Equipment equipment)
|
|
{
|
|
var matches = Regex.Matches($"{equipment?.Schedule}", "\\d+", RegexOptions.Multiline);
|
|
var daysInSchedule = new List<int>();
|
|
|
|
foreach (Match match in matches)
|
|
{
|
|
if (match.Success && int.TryParse(match.Value, out var day))
|
|
{
|
|
daysInSchedule.Add(day);
|
|
}
|
|
}
|
|
|
|
return daysInSchedule;
|
|
}
|
|
|
|
private void SendPruefmittelPruefungReminderEmail(Equipment equipment)
|
|
{
|
|
var separator = new[] { ',', ';', ' ' };
|
|
var options = StringSplitOptions.RemoveEmptyEntries;
|
|
var sender = equipment.EmailFrom;
|
|
|
|
foreach (var x in separator)
|
|
{
|
|
sender = sender.Trim(x);
|
|
}
|
|
|
|
var message = new MailMessage
|
|
{
|
|
From = new MailAddress(sender),
|
|
Subject = equipment.Betreff,
|
|
Body = equipment.Nachricht,
|
|
};
|
|
|
|
var emailsTo = equipment.EmailAn.Split(separator, options);
|
|
|
|
foreach (var email in emailsTo)
|
|
{
|
|
message.To.Add(email);
|
|
}
|
|
|
|
var emailsCC = equipment.InCcAn.Split(separator, options);
|
|
|
|
foreach (var email in emailsCC)
|
|
{
|
|
message.CC.Add(email);
|
|
}
|
|
|
|
using (var smtpClient = new SmtpClient("smtp.xylem.com", 587))
|
|
{
|
|
smtpClient.Timeout = 5000;
|
|
smtpClient.UseDefaultCredentials = true;
|
|
|
|
smtpClient.Send(message);
|
|
}
|
|
}
|
|
}
|
|
}
|