using System;
using System.IO;
using System.Messaging;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;
using NeoEnergiaRequester.NeoEnergiaScrapper;
using NeoEnergiaRequester.NeoEnergiaScrapper.DTO;
using NeoEnergiaRequester.NeoEnergiaScrapper.Performace;
namespace NeoEnergiaRequester
{
internal class Program
{
private static void Main(string[] args)
{
var needLogin = true;
var scrapper = new Scrapper();
var performace = new PerformaceHandler();
var contrats = new []
{
}
while (true)
{
var perfFile = new StreamWriter(ConfigSettings.PERF_LOG_FILE, true);
performace.StartCounting();
var begin = DateTime.Now;
var receiveQueue = new MessageQueue(ConfigSettings.INPUT_QUEUE)
{
Formatter = new XmlMessageFormatter(new []{typeof(StartInfo)})
};
var info = new StartInfo { Empresa = "CELPE", NumeroContrato = "2643852023" };//receiveQueue.Receive().Body as StartInfo;
Console.Write("Processando..." + info.NumeroContrato);
if (begin.AddMinutes(20) < DateTime.Now) needLogin = true;
try
{
var billDto = scrapper.Scrap(info, needLogin);
perfFile.WriteLine("{0};{1};{2}",
info.NumeroContrato, performace.StopCounting(), "SUCCESS");
Console.Write(" Sucesso!");
Console.Write("\n");
var sendQueue = new MessageQueue(ConfigSettings.OUTPUT_QUEUE);
sendQueue.Send(billDto);
}
catch (AggregateException ex)
{
HandleException(info, ex, performace.StopCounting());
perfFile.WriteLine("{0};{1};{2}",
info.NumeroContrato, performace.StopCounting(), "FAIL");
Console.Write(" Falha!");
Console.Write("\n");
}
catch (Exception ex)
{
HandleException(info, ex, performace.StopCounting());
perfFile.WriteLine("{0};{1};{2}",
info.NumeroContrato, performace.StopCounting(), "FAIL");
Console.Write(" Falha!");
Console.Write("\n");
}
finally
{
needLogin = false;
perfFile.Close();
}
}
}
private static void HandleException(StartInfo info, Exception ex, TimeSpan elapsedTime)
{
var sendQueue = new MessageQueue(ConfigSettings.FAIL_QUEUE);
sendQueue.Send(info);
using (var stream = new StreamWriter(ConfigSettings.FAIL_LOG_FILE, true))
{
stream.WriteLine("Contract: {0}\nTime of Error: {1}\nException: {2}",
info.NumeroContrato, DateTime.Now,
ex.InnerException == null
? ex.StackTrace
: ex.InnerException.Message);
}
if (ex.InnerException == null || ex.InnerException.GetType() != typeof(HttpRequestException)) return;
}
private static string GenerateString(BillDto bill)
{
var sbuilder = new StringBuilder();
foreach (var property in bill.GetType().GetProperties())
{
sbuilder.Append(property.Name);
sbuilder.Append(" : ");
sbuilder.Append(property.GetValue(bill, null));
sbuilder.Append(Environment.NewLine);
}
return sbuilder.ToString();
}
}
}