Send Data

This commit is contained in:
2021-08-02 23:01:18 +05:00
parent 25f8be5f24
commit 6ba90bf7e5
118 changed files with 1794 additions and 33 deletions

33
ClientCollector/Data.cs Normal file
View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataClient.Struct;
namespace ApiServer.ApiStruct
{
public class DataCheckApi
{
public bool Status { get; set; }
public DateTime DateAndTime { get; set; }
public string Name { get; set; }
}
public class DataCheckClient
{
public bool Status { get; set; }
public bool Exist { get; set; }
public ulong DataSize { get; set; }
}
public class DataCreateApi
{
public bool Status { get; set; }
public DateTime DateAndTime { get; set; }
public string Name { get; set; }
public byte[] Struct { get; set; }
}
public class DataCreateClient
{
public bool Status { get; set; }
}
}

View File

@@ -14,6 +14,7 @@ using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
namespace ClientCollector
{
@@ -26,27 +27,36 @@ namespace ClientCollector
LogConf();
log.Info("Start Client Collector.");
Task taskPasp = null;
Task taskData = null;
while (true)
{
if (taskPasp == null || taskPasp.IsCompleted)
{
log.Info("Start Pasport Task.");
taskPasp = Task.Run(WorkPasport);
taskPasp = WorkPasport();
}
if (taskData == null || taskData.IsCompleted)
{
log.Info("Start Data Task.");
taskData = WorkData();
}
log.Info("Wait tasks.");
Task.WaitAny(new Task[] { taskPasp });
Task.WaitAny(new Task[] { taskPasp, taskData });
}
}
static async void WorkPasport()
static async Task WorkPasport()
{
await Task.Delay(1000);
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);
currDate = new DateTime(2001, 02, 01);
NETClient netClient = new NETClient(IpSTP, PortSTP);
var flagcycle = false;
while (currDate.Value < DateTime.Now.AddDays(-1))
{
try
@@ -56,14 +66,17 @@ namespace ClientCollector
currDate.Value.Month.ToString("D2") + '/' +
currDate.Value.Day.ToString("D2");
log.Info("Get pasports from: " + currDir);
while (!netClient.Connected())
if (!netClient.Connect())
while (!netClient.Connected() || flagcycle)
{
flagcycle = false;
if (!netClient.ReConnect())
{
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)
@@ -75,20 +88,25 @@ namespace ClientCollector
try
{
log.Info("Get pasport: " + paspDir);
while (!netClient.Connected())
if (!netClient.Connect())
while (!netClient.Connected() || flagcycle)
{
flagcycle = false;
if (!netClient.ReConnect())
{
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.Close();
netClient = new NETClient(IpSTP, PortSTP);
flagcycle = true;
await Task.Delay(10000);
continue;
}
@@ -102,6 +120,8 @@ namespace ClientCollector
catch (Exception e)
{
log.Warn(e.Message);
netClient.Close();
flagcycle = true;
await Task.Delay(5000);
}
}
@@ -110,11 +130,14 @@ namespace ClientCollector
log.Info("End day: " + currDir);
SaveCurrData(nameCurrDate, currDate.Value);
currDate = currDate.Value.AddDays(1);
await Task.Delay(60000);
await Task.Delay(5000);
}
catch (Exception e)
{
log.Warn(e.Message);
flagcycle = true;
netClient.Close();
netClient = new NETClient(IpSTP, PortSTP);
await Task.Delay(1000 * 60 * 5);
}
}
@@ -123,28 +146,166 @@ namespace ClientCollector
log.Info("Wait next day.");
await Task.Delay(1000 * 60 * 60);
} while (!(currDate.Value < DateTime.Now.AddDays(-1)));
currDate.Value.AddMonths(-1);
currDate.Value.AddDays(-15);
SaveCurrData(nameCurrDate, currDate.Value);
}
static async Task WorkData()
{
await Task.Delay(1000);
var IpSTP = "10.10.45.152";
var PortSTP = 1070;
NETClient netClient = new NETClient(IpSTP, PortSTP);
var mainDir = Path.Combine(Directory.GetCurrentDirectory(), "archive");
var nameCurrDate = "data";
var currDate = GetCurrData(nameCurrDate);
if (!currDate.HasValue)
currDate = new DateTime(2001, 02, 01);
var flagcycle = false;
while (currDate.Value < DateTime.Now.AddDays(-1))
{
try
{
while(DateTime.Now.Hour < 12)
{
log.Info("Await 12:00.");
await Task.Delay(15000 * 60);
}
log.Info("Connect to archive.");
await CreateConnectionNFS(mainDir);
var subDir = Path.Combine(mainDir, "data",
currDate.Value.Year.ToString("D4"),
currDate.Value.Month.ToString("D2"),
currDate.Value.Day.ToString("D2"));
log.Info("Check dir: " + subDir);
if (Directory.Exists(subDir))
{
var listFiles = Directory.GetFiles(subDir);
foreach(var fileDir in listFiles)
{
log.Info("Get file: " + fileDir);
var fileStruct = File.ReadAllBytes(fileDir);
log.Info("Send file to API: " + fileDir);
while (!SendData(currDate.Value, Path.GetFileName(fileDir), fileStruct))
{
log.Warn("Can't send data to API.");
await Task.Delay(10000);
}
}
}
log.Info("End day: " + subDir);
SaveCurrData(nameCurrDate, currDate.Value);
currDate = currDate.Value.AddDays(1);
await Task.Delay(5000);
}
catch (Exception e)
{
log.Warn(e.Message);
}
}
do
{
log.Info("Wait next day.");
await Task.Delay(1000 * 60 * 60);
} while (!(currDate.Value < DateTime.Now.AddDays(-1)));
currDate.Value.AddDays(-15);
SaveCurrData(nameCurrDate, currDate.Value);
}
static async Task CreateConnectionNFS(string dir)
{
var dirCreated = false;
while (!dirCreated)
{
try
{
dirCreated = Directory.Exists(Path.Combine(dir));
if (!dirCreated) Directory.CreateDirectory(dir);
}
catch (Exception e)
{
log.Warn(e.Message);
log.Warn("Can't create directory");
await Task.Delay(60000);
}
}
var connected = false;
while (!connected)
{
try
{
var procD = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/usr/bin/umount",
Arguments = dir,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
}
};
var procC = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/usr/bin/mount",
Arguments = "-t nfs 10.10.45.236:/archiv " + dir,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
},
};
if (!procC.Start())
{
log.Warn("Can't start mount command.");
await Task.Delay(60000);
continue;
}
var answer = procC.StandardError.ReadToEnd() + procC.StandardOutput.ReadToEnd();
connected = string.IsNullOrEmpty(answer);
if (connected)
{
log.Info("Connect Success");
continue;
}
else log.Info(answer);
if (!procD.Start())
{
log.Warn("Can't start umount command.");
await Task.Delay(60000);
continue;
}
answer = procC.StandardError.ReadToEnd() + procC.StandardOutput.ReadToEnd();
log.Info(string.IsNullOrEmpty(answer) ? "NFS disconnected." : answer);
}
catch (Exception e)
{
log.Warn(e.Message);
log.Warn("Can't connect to NFS.");
await Task.Delay(60000);
}
}
}
static 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;
}
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
{
{
@@ -222,7 +383,83 @@ namespace ClientCollector
return false;
}
}
static bool SendData(DateTime dateAndTime, string fileName, byte[] fileStruct)
{
try
{
{
log.Info("Check data on remote API server.");
var req = JsonConvert.SerializeObject(
new DataCheckApi()
{
Status = true,
DateAndTime = dateAndTime,
Name = fileName
});
var reqArr = Encoding.UTF8.GetBytes(req);
HttpWebRequest clientReq = GetRequest("https://vsmpo.mbucb.ru/api/data/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<DataCheckClient>(response);
if (!rep.Status) return false;
if (rep.Exist && rep.DataSize == (ulong)fileStruct.Length) return true;
if (!rep.Exist)
log.Info("API: Data not exist.");
if (rep.Exist && rep.DataSize != (ulong)fileStruct.Length)
log.Info("API: Wrong size data.");
}
{
log.Info("Send data to remote API server.");
var req = JsonConvert.SerializeObject(
new DataCreateApi()
{
Status = true,
DateAndTime = dateAndTime,
Name = fileName,
Struct = fileStruct
});
var reqArr = Encoding.UTF8.GetBytes(req);
HttpWebRequest clientReq = GetRequest("https://vsmpo.mbucb.ru/api/data/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<DataCreateClient>(response);
if (!rep.Status) return false;
}
return true;
}
catch (Exception e)
{
log.Warn(e.Message);
return false;
}
}
static void LogConf()
{
var conf = new LoggingConfiguration();

View File

@@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2021-08-01T17:53:02.2382201Z;True|2021-08-01T22:48:16.9658882+05:00;True|2021-08-01T22:40:58.1479436+05:00;True|2021-08-01T22:28:58.9840017+05:00;True|2021-08-01T20:16:20.5210616+05:00;True|2021-08-01T19:27:43.9900600+05:00;True|2021-08-01T19:18:48.8000969+05:00;True|2021-08-01T19:15:19.9257002+05:00;True|2021-08-01T19:08:17.1315589+05:00;True|2021-07-31T20:35:08.9408458+05:00;True|2021-07-31T20:20:29.0886405+05:00;</History>
<History>True|2021-08-02T17:58:49.1867297Z;True|2021-08-02T22:56:02.6749724+05:00;True|2021-08-02T22:50:24.0842490+05:00;True|2021-08-02T22:46:50.7307549+05:00;True|2021-08-02T22:46:32.2783734+05:00;True|2021-08-02T22:37:45.7957317+05:00;True|2021-08-02T22:34:37.2905589+05:00;True|2021-08-02T22:31:30.3451541+05:00;True|2021-08-02T12:26:02.5496847+05:00;True|2021-08-01T23:12:48.3492831+05:00;True|2021-08-01T22:53:02.2382201+05:00;True|2021-08-01T22:48:16.9658882+05:00;True|2021-08-01T22:40:58.1479436+05:00;True|2021-08-01T22:28:58.9840017+05:00;True|2021-08-01T20:16:20.5210616+05:00;True|2021-08-01T19:27:43.9900600+05:00;True|2021-08-01T19:18:48.8000969+05:00;True|2021-08-01T19:15:19.9257002+05:00;True|2021-08-01T19:08:17.1315589+05:00;True|2021-07-31T20:35:08.9408458+05:00;True|2021-07-31T20:20:29.0886405+05:00;</History>
</PropertyGroup>
</Project>

View File

@@ -1 +1 @@
5e9419778287ec5b4cc0bdffcb19831a1be63478
ab788a0c45ec070b599653ff0cff4d13aa37f97d