using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using Newtonsoft; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; using NLog; using DataClient.Struct; using System.IO; using ApiServer.ApiStruct; namespace ApiServer.Controllers { [ApiController, Route("[controller]")] public class PasportController : ControllerBase { private Logger log = LogManager.GetCurrentClassLogger(); [HttpPost, Route("check")] public PasportCheckClient Check([FromBody] object value) { try { var getResult = JsonConvert.DeserializeObject(value.ToString()); if (!getResult.Status || string.IsNullOrEmpty(getResult.Name)) { log.Warn("Wrong answer."); return new PasportCheckClient(); } var paspName = getResult.DateAndTime.Hour.ToString("D2") + getResult.DateAndTime.Minute.ToString("D2") + getResult.DateAndTime.Second.ToString("D2") + "-" + getResult.Name; var paspDir = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.DateAndTime.Year.ToString("D4"), getResult.DateAndTime.Month.ToString("D2"), getResult.DateAndTime.Day.ToString("D2"), paspName); log.Info("Search pasport: " + paspDir); if (!System.IO.File.Exists(paspDir)) { log.Info("Psport not exist: " + paspDir); return new PasportCheckClient { Status = true, Exist = false, PaspSum = 0 }; } var pasport = System.IO.File.ReadAllBytes(paspDir); ulong paspResult = 0; foreach (var b in pasport) paspResult += b; log.Info("Send pasport size: " + paspResult + " | " + paspDir); return new PasportCheckClient { Status = true, Exist = true, PaspSum = paspResult }; } catch(Exception e) { log.Warn(e); return new PasportCheckClient(); } } [HttpPost, Route("create")] public PasportCreateClient Create([FromBody] object value) { try { var getResult = JsonConvert.DeserializeObject(value.ToString()); if (!getResult.Status || getResult.Pasp == null || !getResult.Pasp.HasData) { log.Warn("Wrong answer."); return new PasportCreateClient(); } var paspName = getResult.Pasp.dEnd.Value.Hour.ToString("D2") + getResult.Pasp.dEnd.Value.Minute.ToString("D2") + getResult.Pasp.dEnd.Value.Second.ToString("D2") + "-" + (string.IsNullOrEmpty(getResult.Pasp.nplav) ? getResult.Pasp.numVDP.Value.ToString("D2") : getResult.Pasp.nplav); var dir = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.Pasp.dEnd.Value.Year.ToString("D4"), getResult.Pasp.dEnd.Value.Month.ToString("D2"), getResult.Pasp.dEnd.Value.Day.ToString("D2")); var paspDir = Path.Combine(dir, paspName); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); System.IO.File.WriteAllBytes(paspDir, getResult.Pasp.PaspByte); log.Info("Save pasport: " + paspDir); return new PasportCreateClient { Status = true }; } catch (Exception e) { log.Warn(e); return new PasportCreateClient(); } } [HttpPost, Route("getlist")] public PasportGetListClient GetList([FromBody] object value) { try { var pasportNameList = new List(); var pasportTimeList = new List(); var getResult = JsonConvert.DeserializeObject( value.ToString(), new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd" }); log.Info("Ask pasport list: " + getResult.Date.ToString("yyyy-MM-dd")); var pasportDir = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.Date.Year.ToString("D4"), getResult.Date.Month.ToString("D2"), getResult.Date.Day.ToString("D2") ); if (!Directory.Exists(pasportDir)) throw new Exception("Directory not exist: " + pasportDir); var allPasportFiles = Directory.GetFiles(pasportDir); foreach (var fullPasportName in allPasportFiles) { var name = Path.GetFileNameWithoutExtension(fullPasportName); var pasportParts = name.Split('-'); var pasport = ""; for (var i = 1; i < pasportParts.Length; i++) { if (pasport.Length != 0) pasport += "-"; pasport += pasportParts[i]; } pasportTimeList.Add(pasportParts[0]); pasportNameList.Add(pasport); } log.Info("Send pasport list: " + pasportNameList); return new PasportGetListClient() { Name = pasportNameList.ToArray(), Time = pasportTimeList.ToArray() }; } catch (Exception e) { log.Warn(e); return new PasportGetListClient(); } } [HttpPost, Route("getpasport")] public PasportGetPasportClient GetPasport([FromBody] object value) { try { var getResult = JsonConvert.DeserializeObject( value.ToString(), new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd" }); log.Info("Ask pasport: " + getResult.Date.ToString("yyyy-MM-dd") + " " + getResult.Name); var pasportFile = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.Date.Year.ToString("D4"), getResult.Date.Month.ToString("D2"), getResult.Date.Day.ToString("D2"), getResult.Name ); if (!System.IO.File.Exists(pasportFile)) throw new Exception("File not exist: " + pasportFile); var pasport = new Pasport(System.IO.File.ReadAllBytes(pasportFile)); return new PasportGetPasportClient() { HasData = pasport.HasData, NumVDP = pasport.numVDP, DStart = pasport.dStart.HasValue ? pasport.dStart.Value.ToString("yyyy.MM.dd HH:mm:ss") : "", DEnd = pasport.dEnd.HasValue ? pasport.dEnd.Value.ToString("yyyy.MM.dd HH:mm:ss") : "", HasPasport = pasport.hasPasport, Kod_npl = pasport.kod_npl, Nplav = pasport.nplav, Rm = pasport.rm, Splav = pasport.splav, IS = pasport.iS, Notd = pasport.notd, Vessl = pasport.vessl, Diam = pasport.diam, Prpl = pasport.prpl, Tin = pasport.tin, Dzap = pasport.dzap, Dlog = pasport.dlog, Last = pasport.last, Dlper = pasport.dlper, Nazn = pasport.nazn, Kompl = pasport.kompl, Izl = pasport.izl, Robm = pasport.robm, Rizol = pasport.rizol, Dkr = pasport.dkr, Nkon = pasport.nkon, Pos = pasport.pos, Ukaz = pasport.ukaz, Zakaz = pasport.zakaz, Kat = pasport.kat, Pril = pasport.pril, Rezerved = pasport.rezerved, }; } catch (Exception e) { log.Warn(e); return new PasportGetPasportClient() { HasData = false, HasPasport = false }; } } } }