206 lines
7.8 KiB
C#
206 lines
7.8 KiB
C#
using Core;
|
|
using LiMan.Transfer;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using RestSharp;
|
|
using RestSharp.Serializers.NewtonsoftJson;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using static Core.Enum;
|
|
|
|
namespace MyApp // Note: actual namespace depends on the project name.
|
|
{
|
|
internal class Program
|
|
{
|
|
protected static string dataFolder = "";
|
|
protected static string archFolder = "";
|
|
protected static string reqName = "request.json";
|
|
protected static int numDayKeep = 30;
|
|
protected static string apiUrl = "https://liman.egalware.com/ELM.Api/";
|
|
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
public static Logger lg = LogManager.GetCurrentClassLogger();
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
string myLine = "--------------------------------------";
|
|
logInfo(myLine);
|
|
logInfo("LiMan data transfer");
|
|
logInfo(myLine);
|
|
|
|
//verifico di avere parametro x directory con contenuto...
|
|
if (args == null || args.Count() < 2)
|
|
{
|
|
logError("Warning: Missing args !!!");
|
|
logError("");
|
|
logError("Syntax: LiMan.Transfer.exe data_folder archive_folder");
|
|
logError("data_folder: path to container for request.json file + *.zip file(s) to upload");
|
|
logError("archive_folder: where to put trasferred data");
|
|
logError("");
|
|
}
|
|
else
|
|
{
|
|
// verifico primo parametro
|
|
dataFolder = args[0];
|
|
archFolder = args[1];
|
|
if (!Directory.Exists(dataFolder))
|
|
{
|
|
logError("Error! data directory not found. Exiting");
|
|
}
|
|
else
|
|
{
|
|
// quella archivio se non ci fosse la creo
|
|
if (!Directory.Exists(archFolder))
|
|
{
|
|
Directory.CreateDirectory(archFolder);
|
|
}
|
|
|
|
// cerco il file di conf x invio
|
|
string fileName = Path.Combine(dataFolder, reqName);
|
|
if (!File.Exists(fileName))
|
|
{
|
|
logError("Missing conf file!");
|
|
}
|
|
else
|
|
{
|
|
// deserializzo conf
|
|
logInfo($"Found file {fileName}");
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool fatto = callTicketAndUpload(fileName);
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
if (fatto)
|
|
{
|
|
logInfo($"Upload done, {ts.TotalSeconds:0.000} sec");
|
|
}
|
|
else
|
|
{
|
|
logError($"Error in upload, {ts.TotalSeconds:0.000} sec");
|
|
}
|
|
|
|
cleanupArchive();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void cleanupArchive()
|
|
{
|
|
logInfo("Start Archive cleanup");
|
|
DateTime oggi = DateTime.Today;
|
|
var candidates = Directory.GetDirectories(archFolder);
|
|
foreach (var folder in candidates)
|
|
{
|
|
var currDir = new DirectoryInfo(folder);
|
|
if (currDir.CreationTime < oggi.AddDays(-numDayKeep))
|
|
{
|
|
Directory.Delete(folder, true);
|
|
logInfo($"Folder deleted: {folder}");
|
|
}
|
|
}
|
|
logInfo("Archive cleanup done!");
|
|
}
|
|
|
|
private static bool callTicketAndUpload(string fileName)
|
|
{
|
|
bool fatto = false;
|
|
try
|
|
{
|
|
// client chiamate rest
|
|
var client = new RestClient(apiUrl);
|
|
client.UseNewtonsoftJson();
|
|
|
|
SupportRequest? newSuppReq = new SupportRequest();
|
|
string rawData = "";
|
|
if (File.Exists(fileName))
|
|
{
|
|
rawData = File.ReadAllText(fileName);
|
|
}
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
// fare: composizione richiesta da parametri chiave
|
|
newSuppReq = JsonConvert.DeserializeObject<SupportRequest>(rawData);
|
|
}
|
|
else
|
|
{
|
|
string hostName = utils.machineName;
|
|
string listIP = string.Join(", ", utils.machineIp);
|
|
// genero il ticket
|
|
newSuppReq = new SupportRequest()
|
|
{
|
|
CodApp = "Uploader",
|
|
CodImp = "",
|
|
CodInst = "EgalWare",
|
|
ContactEmail = "info@egalware.com",
|
|
ContactName = "Utente Generico",
|
|
ContactPhone = "035-460560",
|
|
MasterKey = "4AIc8fMEXcSyDIMl1Ro05O/1xar7nrVHXAQzrh/fmxfvlczA13tQwXAqida6hTqV",
|
|
ReqBody = $"File Upload - MISSING license file | machine: {utils.machineName} | IP: {listIP}",
|
|
Tipo = TipologiaTicket.FileUpload,
|
|
idxSubLic = 0
|
|
};
|
|
}
|
|
if (newSuppReq == null)
|
|
{
|
|
logError("Error: support request conf is null! cannot proceed");
|
|
}
|
|
else
|
|
{
|
|
var ticketReq = new RestRequest("/api/ticket/sendReq", DataFormat.Json).AddJsonBody(newSuppReq);
|
|
|
|
|
|
var pUpd = Task.Run(async () =>
|
|
{
|
|
var ticketResp = await client.PostAsync<TicketDTO>(ticketReq);
|
|
logInfo($"Received ticket {ticketResp.idxTicket}");
|
|
// preparo richiesta x upload file
|
|
var fileUploadReq = new RestRequest("/api/filesave/single");
|
|
fileUploadReq.AddParameter("ticketId", ticketResp.idxTicket);
|
|
// cerco OGNI file zip nella folder indicata...
|
|
var fileList = Directory.GetFiles(dataFolder, "*.zip");
|
|
foreach (var file in fileList)
|
|
{
|
|
fileUploadReq.AddFile("file", file);
|
|
// ... infine INVIA file zip che li contiene...
|
|
var fileUploadResp = await client.PostAsync<UploadResult>(fileUploadReq);
|
|
logInfo($"Upload {file} completed!");
|
|
fileUploadReq.Files.Clear();
|
|
}
|
|
|
|
//var fileUploadResp = client.Post(fileUploadReq);
|
|
logInfo($"Uploaded {fileList.Count()} files");
|
|
|
|
// sposto folder in area archivio...
|
|
string destFolder = Path.Combine(archFolder, $"{DateTime.Now:yyyyMMdd_HHmmss}");
|
|
Directory.Move(dataFolder, destFolder);
|
|
logInfo($"Requested folder archived in {destFolder}");
|
|
|
|
fatto = true;
|
|
});
|
|
pUpd.Wait();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logError($"Eccezione in fase gestione REST services{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
return fatto;
|
|
}
|
|
|
|
protected static void logInfo(string message)
|
|
{
|
|
lg.Info(message);
|
|
Console.WriteLine(message);
|
|
}
|
|
protected static void logError(string message)
|
|
{
|
|
lg.Error(message);
|
|
Console.WriteLine($"!!! {message}");
|
|
}
|
|
}
|
|
} |