ASCU_ALL/ClientCollector/Program.cs

312 lines
8.4 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;
2021-08-01 19:48:09 +05:00
using System.Collections.Generic;
2021-07-31 22:27:59 +05:00
namespace ClientCollector
{
class Program
{
static Logger log = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
LogConf();
log.Info("Start Client Collector.");
2021-08-01 19:48:09 +05:00
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()
{
2021-08-01 22:56:52 +05:00
var IpSTP = "10.10.45.152";
var PortSTP = 1070;
2021-08-01 19:48:09 +05:00
var nameCurrDate = "pasport";
var currDate = GetCurrData(nameCurrDate);
if (!currDate.HasValue)
currDate = new DateTime(2001, 01, 01);
2021-08-01 22:56:52 +05:00
NETClient netClient = new NETClient(IpSTP, PortSTP);
2021-08-01 19:48:09 +05:00
while (currDate.Value < DateTime.Now.AddDays(-1))
2021-07-31 22:27:59 +05:00
{
try
{
2021-08-01 19:48:09 +05:00
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())
2021-07-31 22:27:59 +05:00
{
2021-08-01 19:48:09 +05:00
netClient.Close();
2021-08-01 22:56:52 +05:00
netClient = new NETClient(IpSTP, PortSTP);
2021-08-01 19:48:09 +05:00
log.Warn("Can't connect to STP.");
await Task.Delay(10000);
2021-07-31 22:27:59 +05:00
}
2021-08-01 19:48:09 +05:00
var currPasports = netClient.Full_Dir_Browse(currDir);
netClient.Close();
if (currPasports == null)
currPasports = Array.Empty<string>();
foreach (var paspDir in currPasports)
2021-07-31 22:27:59 +05:00
{
2021-08-01 19:48:09 +05:00
do
2021-07-31 22:27:59 +05:00
{
2021-08-01 19:48:09 +05:00
try
2021-07-31 22:27:59 +05:00
{
2021-08-01 22:56:52 +05:00
log.Info("Get pasport: " + paspDir);
2021-08-01 19:48:09 +05:00
while (!netClient.Connected())
if (!netClient.Connect())
2021-07-31 22:27:59 +05:00
{
log.Warn("Can't connect to STP.");
2021-08-01 19:48:09 +05:00
netClient.Close();
2021-08-01 22:56:52 +05:00
netClient = new NETClient(IpSTP, PortSTP);
2021-08-01 19:48:09 +05:00
await Task.Delay(10000);
2021-07-31 22:27:59 +05:00
}
2021-08-01 19:48:09 +05:00
var pasp = netClient.Full_Pasp_Download(paspDir);
netClient.Close();
if (pasp == null || !pasp.HasData)
{
log.Warn("Can't get pasport.");
2021-08-01 22:56:52 +05:00
netClient = new NETClient(IpSTP, PortSTP);
2021-08-01 19:48:09 +05:00
await Task.Delay(10000);
continue;
}
2021-08-01 22:56:52 +05:00
while (!SendPasport(pasp))
2021-08-01 19:48:09 +05:00
{
log.Warn("Can't send pasp to API.");
await Task.Delay(10000);
}
break;
}
catch (Exception e)
{
2021-08-01 22:56:52 +05:00
log.Warn(e.Message);
2021-08-01 19:48:09 +05:00
await Task.Delay(5000);
2021-07-31 22:27:59 +05:00
}
}
2021-08-01 22:56:52 +05:00
while (true);
2021-07-31 22:27:59 +05:00
}
2021-08-01 22:56:52 +05:00
log.Info("End day: " + currDir);
2021-08-01 19:48:09 +05:00
SaveCurrData(nameCurrDate, currDate.Value);
currDate = currDate.Value.AddDays(1);
2021-08-01 22:56:52 +05:00
await Task.Delay(60000);
2021-07-31 22:27:59 +05:00
}
catch (Exception e)
{
2021-08-01 22:56:52 +05:00
log.Warn(e.Message);
2021-08-01 19:48:09 +05:00
await Task.Delay(1000 * 60 * 5);
2021-07-31 22:27:59 +05:00
}
}
2021-08-01 19:48:09 +05:00
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);
2021-07-31 22:27:59 +05:00
}
static bool SendPasport(Pasport pasp)
{
2021-08-01 19:48:09 +05:00
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;
}
2021-07-31 22:27:59 +05:00
try
{
{
log.Info("Check pasport on remote API server.");
var req = JsonConvert.SerializeObject(
2021-08-01 19:48:09 +05:00
new PasportCheckApi()
2021-07-31 22:27:59 +05:00
{
2021-08-01 19:48:09 +05:00
Status = true,
2021-07-31 22:27:59 +05:00
DateAndTime = pasp.dEnd.Value,
2021-08-01 19:48:09 +05:00
Name = string.IsNullOrEmpty(pasp.nplav) ? pasp.numVDP.Value.ToString("D2") : pasp.nplav
2021-07-31 22:27:59 +05:00
});
var reqArr = Encoding.UTF8.GetBytes(req);
2021-08-01 19:48:09 +05:00
HttpWebRequest clientReq = GetRequest("https://vsmpo.mbucb.ru/api/pasport/check");
2021-07-31 22:27:59 +05:00
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();
}
}
2021-08-01 19:48:09 +05:00
var rep = JsonConvert.DeserializeObject<PasportCheckClient>(response);
2021-07-31 22:27:59 +05:00
if (!rep.Status) return false;
2021-08-01 19:48:09 +05:00
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.");
2021-07-31 22:27:59 +05:00
}
{
log.Info("Send pasport to remote API server.");
var req = JsonConvert.SerializeObject(
2021-08-01 19:48:09 +05:00
new PasportCreateApi()
2021-07-31 22:27:59 +05:00
{
2021-08-01 19:48:09 +05:00
Status = true,
2021-07-31 22:27:59 +05:00
Pasp = pasp
});
var reqArr = Encoding.UTF8.GetBytes(req);
2021-08-01 19:48:09 +05:00
HttpWebRequest clientReq = GetRequest("https://vsmpo.mbucb.ru/api/pasport/create");
2021-07-31 22:27:59 +05:00
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();
}
}
2021-08-01 19:48:09 +05:00
var rep = JsonConvert.DeserializeObject<PasportCreateClient>(response);
2021-07-31 22:27:59 +05:00
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;
}
2021-08-01 19:48:09 +05:00
static bool SaveCurrData(string name, DateTime currDate)
{
try
{
var fileForSave = Path.Combine(Directory.GetCurrentDirectory(), "currentState");
var currStrings = Array.Empty<string>();
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)
{
2021-08-01 22:56:52 +05:00
log.Warn(e.Message);
2021-08-01 19:48:09 +05:00
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)
{
2021-08-01 22:56:52 +05:00
log.Warn(e.Message);
2021-08-01 19:48:09 +05:00
return null;
}
}
2021-07-31 22:27:59 +05:00
}
}