ASCU_ALL/ClientCollector/Program.cs

253 lines
6.9 KiB
C#
Raw Normal View History

2021-07-31 22:27:59 +05:00
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Net;
using System.Net.Http;
using DataClient;
using DataClient.Struct;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using ApiServer.ApiStruct;
using Newtonsoft.Json;
using System.Text;
using System.IO;
namespace ClientCollector
{
class Program
{
static Logger log = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
LogConf();
log.Info("Start Client Collector.");
while (true)
{
NETClient nc = new NETClient("10.10.45.152", 1070);
try
{
log.Info("Get Years.");
var dir_1lvl = nc.Full_Dir_Browse();
foreach (var dir_1lvl_val in dir_1lvl)
{
if (dir_1lvl_val == "current") continue;
log.Info("Get Month of " + dir_1lvl_val + ".");
var dir_2lvl = nc.Full_Dir_Browse(dir_1lvl_val);
foreach (var dir_2lvl_val in dir_2lvl)
{
log.Info("Get Days of " + dir_2lvl_val + ".");
var dir_3lvl = nc.Full_Dir_Browse(dir_2lvl_val);
foreach (var dir_3lvl_val in dir_3lvl)
{
log.Info("Get Pasps of " + dir_3lvl_val + ".");
var pasp_list = nc.Full_Dir_Browse(dir_3lvl_val);
foreach (var pasp_dir in pasp_list)
{
int count = 0;
do
{
try
{
log.Info("Get Pasp " + pasp_dir + ". Try " + (count + 1));
var pasp = nc.Full_Pasp_Download(pasp_dir);
if (pasp.HasData && SendPasport(pasp))
{
log.Info("Send OK.");
break;
}
log.Warn("Can't send " + pasp_dir + ".");
Task.Delay(1000 * 60 * 5);
count = 0;
}
catch (Exception e) { log.Warn(e.Message); }
if (nc.Connect())
{
nc.Close();
count++;
}
else
{
log.Warn("Can't connect to STP.");
Task.Delay(1000 * 60 * 5);
count = 0;
}
} while (count < 5);
}
}
}
}
while (true)
{
log.Info("Wait next day.");
var currDate = DateTime.Now;
while (currDate.ToString("yyyy.MM.dd") == DateTime.Now.ToString("yyyy.MM.dd"))
Task.Delay(1000 * 60 * 60);
try
{
var dir_3lvl_val =
currDate.Year.ToString("D4") + "/" +
currDate.Month.ToString("D2") + "/" +
currDate.Day.ToString("D2");
log.Info("Get Pasps of " + dir_3lvl_val + ".");
var pasp_list = nc.Full_Dir_Browse(dir_3lvl_val);
foreach (var pasp_dir in pasp_list)
{
int count = 0;
do
{
try
{
log.Info("Get Pasp " + pasp_dir + ". Try " + (count + 1));
var pasp = nc.Full_Pasp_Download(pasp_dir);
if (pasp.HasData && SendPasport(pasp))
{
log.Info("Send OK.");
break;
}
log.Warn("Can't send " + pasp_dir + ".");
Task.Delay(1000 * 60 * 5);
count = 0;
}
catch (Exception e) { log.Warn(e.Message); }
if (nc.Connect())
{
nc.Close();
count++;
}
else
{
log.Warn("Can't connect to STP.");
Task.Delay(1000 * 60 * 5);
count = 0;
}
} while (count < 5);
}
currDate = DateTime.Now;
}
catch (Exception e)
{
log.Warn(e);
Task.Delay(1000 * 60 * 10);
}
}
}
catch (Exception e)
{
log.Warn(e);
Task.Delay(1000 * 60 * 10);
}
}
}
static bool SendPasport(Pasport pasp)
{
try
{
{
log.Info("Check pasport on remote API server.");
var req = JsonConvert.SerializeObject(
new PasportCheckRep()
{
HasData = true,
DateAndTime = pasp.dEnd.Value,
Name = string.IsNullOrEmpty(pasp.nplav) ? pasp.numVDP.Value.ToString() : pasp.nplav
});
var reqArr = Encoding.UTF8.GetBytes(req);
HttpWebRequest clientReq = (HttpWebRequest)WebRequest.Create("https://vsmpo.mbucb.ru/api/pasport/check");
clientReq.Method = "POST";
clientReq.ContentType = "application/json; charset=utf-8";
clientReq.Proxy = new WebProxy("194.226.128.245", 3128)
{
BypassProxyOnLocal = false,
Credentials = new NetworkCredential("user4", "user4")
};
clientReq.ContentLength = reqArr.Length;
using (var stream = clientReq.GetRequestStream())
{
stream.Write(reqArr, 0, reqArr.Length);
}
string response = "";
using (var clientRes = (HttpWebResponse)clientReq.GetResponse())
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var readStream = new StreamReader(clientRes.GetResponseStream(), Encoding.UTF8))
{
response = readStream.ReadToEnd();
readStream.Close();
clientRes.Close();
}
}
var rep = JsonConvert.DeserializeObject<PasportCheckReq>(response);
if (!rep.Status) return false;
if (rep.Exist && rep.Pasp.PaspByte.SequenceEqual(pasp.PaspByte)) return true;
}
{
log.Info("Send pasport to remote API server.");
var req = JsonConvert.SerializeObject(
new PasportCreateRep()
{
HasData = true,
DateAndTime = pasp.dEnd.Value,
Name = string.IsNullOrEmpty(pasp.nplav) ? pasp.numVDP.Value.ToString() : pasp.nplav,
Pasp = pasp
});
var reqArr = Encoding.UTF8.GetBytes(req);
HttpWebRequest clientReq = (HttpWebRequest)WebRequest.Create("https://vsmpo.mbucb.ru/api/pasport/create");
clientReq.Method = "POST";
clientReq.ContentType = "application/json; charset=utf-8";
clientReq.Proxy = new WebProxy("194.226.128.245", 3128)
{
BypassProxyOnLocal = false,
Credentials = new NetworkCredential("user4", "user4")
};
clientReq.ContentLength = reqArr.Length;
using (var stream = clientReq.GetRequestStream())
{
stream.Write(reqArr, 0, reqArr.Length);
}
string response = "";
using (var clientRes = (HttpWebResponse)clientReq.GetResponse())
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var readStream = new StreamReader(clientRes.GetResponseStream(), Encoding.UTF8))
{
response = readStream.ReadToEnd();
readStream.Close();
clientRes.Close();
}
}
var rep = JsonConvert.DeserializeObject<PasportCreateReq>(response);
if (!rep.Status) return false;
}
return true;
}
catch (Exception e)
{
log.Warn(e.Message);
return false;
}
}
static void LogConf()
{
var conf = new LoggingConfiguration();
var logcon = new ConsoleTarget()
{
Name = "logcon",
Layout = @"${time}|${level:uppercase=true}|${logger}|${message}${when:when=length('${exception}')>0:Inner=|}${exception:format=ToString,StackTrace}"
};
conf.AddRule(LogLevel.Trace, LogLevel.Fatal, logcon);
LogManager.Configuration = conf;
}
}
}