ASCU_ALL/ApiServer/Controllers/DiscretController.cs
2024-09-22 04:27:05 +05:00

139 lines
3.9 KiB
C#

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 DiscretController : ControllerBase
{
private Logger log = LogManager.GetCurrentClassLogger();
[HttpPost, Route("getdiscrets")]
public DiscretGetDscretsClient GetDiscrets([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DiscretGetiscretsApi>(
value.ToString());
log.Info("Ask discrets: " +
getResult.Vdp.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " "+
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_discrets = new List<Structures.Discret>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0,0,0
);
while (flagDate <= getResult.End)
{
var discrets = WorkWithFiles.GetDiscrets(flagDate, getResult.Vdp);
var first_discrets = new Dictionary<int, Structures.Discret>();
for (var i = 0; i < discrets.Length; i++)
{
if (discrets[i].start < getResult.Start)
{
if (first_discrets.ContainsKey(discrets[i].Index))
first_discrets[discrets[i].Index] = discrets[i];
else
first_discrets.Add(discrets[i].Index, discrets[i]);
first_discrets[discrets[i].Index].start = getResult.Start;
continue;
}
if (discrets[i].start > getResult.End)
continue;
var last_id = list_discrets.FindLastIndex(x => x.Index == discrets[i].Index);
if (first_discrets.ContainsKey(discrets[i].Index) &&
last_id == -1 &&
discrets[i].start != getResult.Start)
{
list_discrets.Add(first_discrets[discrets[i].Index]);
first_discrets.Remove(discrets[i].Index);
}
if (last_id != -1 &&
list_discrets[last_id].State == discrets[i].State)
continue;
list_discrets.Add(discrets[i]);
}
flagDate = flagDate.AddDays(1);
}
return new DiscretGetDscretsClient()
{
StateParams = Configuration.GetStateStructArray(),
DiscretParams = Configuration.GetDiscretParams(getResult.Vdp.ToString("D2")),
Discrets = list_discrets.ToArray()
};
}
catch (Exception e)
{
log.Warn(e);
return new DiscretGetDscretsClient();
}
}
[HttpPost, Route("getdiscretsmatrix")]
public DiscretGetDscretsMatrixClient GetDiscretsMatrix([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DiscretGetiscretsApi>(
value.ToString());
log.Info("Ask discrets: " +
getResult.Vdp.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " " +
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_discrets = new List<Structures.DiscretMatrix>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0, 0, 0
);
while (flagDate <= getResult.End)
{
var discrets = WorkWithFiles.GetDiscretsMatrix(flagDate, getResult.Vdp);
for (var i = 0; i < discrets.Length; i++)
{
if (discrets[i].start < getResult.Start)
continue;
if (discrets[i].start > getResult.End)
continue;
list_discrets.Add(discrets[i]);
}
flagDate = flagDate.AddDays(1);
}
log.Info("Send discret");
return new DiscretGetDscretsMatrixClient()
{
StateParams = Configuration.GetStateStructArray(),
DiscretParams = Configuration.GetDiscretParams(getResult.Vdp.ToString("D2")),
Discrets = list_discrets.ToArray()
};
}
catch (Exception e)
{
log.Warn(e);
return new DiscretGetDscretsMatrixClient();
}
}
}
}