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; using System.Collections.Generic; namespace ClientCollector { class Program { static Logger log = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { LogConf(); log.Info("Start Client Collector."); Task taskPasp = null; while (true) { if (taskPasp == null || taskPasp.IsCompleted) { log.Info("Start Pasport Task."); taskPasp = Task.Run(WorkPasport); } log.Info("Wait tasks."); Task.WaitAny(new Task[] { taskPasp }); } } static async void WorkPasport() { var IpSTP = "10.10.45.152"; var PortSTP = 1070; var nameCurrDate = "pasport"; var currDate = GetCurrData(nameCurrDate); if (!currDate.HasValue) currDate = new DateTime(2001, 01, 01); NETClient netClient = new NETClient(IpSTP, PortSTP); while (currDate.Value < DateTime.Now.AddDays(-1)) { try { var currDir = currDate.Value.Year.ToString("D4") + '/' + currDate.Value.Month.ToString("D2") + '/' + currDate.Value.Day.ToString("D2"); log.Info("Get pasports from: " + currDir); while (!netClient.Connected()) if (!netClient.Connect()) { netClient.Close(); netClient = new NETClient(IpSTP, PortSTP); log.Warn("Can't connect to STP."); await Task.Delay(10000); } var currPasports = netClient.Full_Dir_Browse(currDir); netClient.Close(); if (currPasports == null) currPasports = Array.Empty(); foreach (var paspDir in currPasports) { do { try { log.Info("Get pasport: " + paspDir); while (!netClient.Connected()) if (!netClient.Connect()) { log.Warn("Can't connect to STP."); netClient.Close(); netClient = new NETClient(IpSTP, PortSTP); await Task.Delay(10000); } var pasp = netClient.Full_Pasp_Download(paspDir); netClient.Close(); if (pasp == null || !pasp.HasData) { log.Warn("Can't get pasport."); netClient = new NETClient(IpSTP, PortSTP); await Task.Delay(10000); continue; } while (!SendPasport(pasp)) { log.Warn("Can't send pasp to API."); await Task.Delay(10000); } break; } catch (Exception e) { log.Warn(e.Message); await Task.Delay(5000); } } while (true); } log.Info("End day: " + currDir); SaveCurrData(nameCurrDate, currDate.Value); currDate = currDate.Value.AddDays(1); await Task.Delay(60000); } catch (Exception e) { log.Warn(e.Message); await Task.Delay(1000 * 60 * 5); } } do { log.Info("Wait next day."); await Task.Delay(1000 * 60 * 60); } while (!(currDate.Value < DateTime.Now.AddDays(-1))); currDate.Value.AddMonths(-1); SaveCurrData(nameCurrDate, currDate.Value); } static bool SendPasport(Pasport pasp) { HttpWebRequest GetRequest(string path, bool useProxy = true) { HttpWebRequest clientReq = (HttpWebRequest)WebRequest.Create(path); clientReq.Method = "POST"; clientReq.ContentType = "application/json; charset=utf-8"; if (useProxy) { clientReq.Proxy = new WebProxy("194.226.128.245", 3128) { BypassProxyOnLocal = false, Credentials = new NetworkCredential("user4", "user4") }; } return clientReq; } try { { log.Info("Check pasport on remote API server."); var req = JsonConvert.SerializeObject( new PasportCheckApi() { Status = true, DateAndTime = pasp.dEnd.Value, Name = string.IsNullOrEmpty(pasp.nplav) ? pasp.numVDP.Value.ToString("D2") : pasp.nplav }); var reqArr = Encoding.UTF8.GetBytes(req); HttpWebRequest clientReq = GetRequest("https://vsmpo.mbucb.ru/api/pasport/check"); clientReq.ContentLength = reqArr.Length; using (var stream = clientReq.GetRequestStream()) { stream.Write(reqArr, 0, reqArr.Length); } string response = ""; using (var clientRes = (HttpWebResponse)clientReq.GetResponse()) { using (var readStream = new StreamReader(clientRes.GetResponseStream(), Encoding.UTF8)) { response = readStream.ReadToEnd(); readStream.Close(); clientRes.Close(); } } var rep = JsonConvert.DeserializeObject(response); if (!rep.Status) return false; ulong paspSum = 0; var paspArr = pasp.PaspByte; foreach (var b in paspArr) paspSum += b; if (rep.Exist && rep.PaspSum == paspSum) return true; if (!rep.Exist) log.Info("API: Pasport not exist."); if (rep.Exist && rep.PaspSum != paspSum) log.Info("API: Wrong size pasport."); } { log.Info("Send pasport to remote API server."); var req = JsonConvert.SerializeObject( new PasportCreateApi() { Status = true, Pasp = pasp }); var reqArr = Encoding.UTF8.GetBytes(req); HttpWebRequest clientReq = GetRequest("https://vsmpo.mbucb.ru/api/pasport/create"); clientReq.ContentLength = reqArr.Length; using (var stream = clientReq.GetRequestStream()) { stream.Write(reqArr, 0, reqArr.Length); } string response = ""; using (var clientRes = (HttpWebResponse)clientReq.GetResponse()) { using (var readStream = new StreamReader(clientRes.GetResponseStream(), Encoding.UTF8)) { response = readStream.ReadToEnd(); readStream.Close(); clientRes.Close(); } } var rep = JsonConvert.DeserializeObject(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; } static bool SaveCurrData(string name, DateTime currDate) { try { var fileForSave = Path.Combine(Directory.GetCurrentDirectory(), "currentState"); var currStrings = Array.Empty(); if (File.Exists(fileForSave)) currStrings = File.ReadAllLines(fileForSave, Encoding.UTF8); var newString = name + ':' + currDate.Year.ToString("D4") + currDate.Month.ToString("D2") + currDate.Day.ToString("D2"); for (var i = 0; i < currStrings.Length; i++) { var splitString = currStrings[i].Split(':'); if (splitString.Length != 2) continue; if (splitString[0] != name) continue; currStrings[i] = newString; newString = null; break; } if (!string.IsNullOrEmpty(newString)) { Array.Resize(ref currStrings, currStrings.Length + 1); currStrings[currStrings.Length - 1] = newString; } File.WriteAllLines(fileForSave, currStrings, Encoding.UTF8); return true; } catch (Exception e) { log.Warn(e.Message); return false; } } static DateTime? GetCurrData(string name) { try { var file = Path.Combine(Directory.GetCurrentDirectory(), "currentState"); if (!File.Exists(file)) { log.Info("State file not exist."); return null; } var listStrings = File.ReadAllLines(file, Encoding.UTF8); for(var i = 0; i < listStrings.Length; i++) { var splitString = listStrings[i].Split(':'); if (splitString.Length != 2) continue; if (splitString[0] != name) continue; if ( !int.TryParse(splitString[1].Substring(0, 4), out int year) || !int.TryParse(splitString[1].Substring(4, 2), out int month) || !int.TryParse(splitString[1].Substring(6, 2), out int day)) { log.Warn("Wron format state string."); return null; } return new DateTime(year, month, day); } log.Info("State not exist."); return null; } catch (Exception e) { log.Warn(e.Message); return null; } } } }