e57f6a1326
- req/run/done state - modifica api
127 lines
5.0 KiB
C#
127 lines
5.0 KiB
C#
using Core.DTO;
|
|
using LiMan.APi.Data;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NLog;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiMan.APi.Controllers
|
|
{
|
|
[Route("api/apptask")]
|
|
[ApiController]
|
|
public class AppTaskController : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Init generico
|
|
/// </summary>
|
|
/// <param name="DataService"></param>
|
|
public AppTaskController(ApiDataService DataService)
|
|
{
|
|
dataService = DataService;
|
|
Log.Info("Avviata classe TaskController");
|
|
}
|
|
|
|
#if false
|
|
[HttpGet]
|
|
public ActionResult<string> Get([FromHeader] string codOne = "pippo", [FromQuery] bool showAll = false)
|
|
{
|
|
string answ = $"CodOne: {codOne} | showAll: {showAll}";
|
|
return answ;
|
|
}
|
|
[HttpGet("alt-get")]
|
|
public async Task<string> GetAlt([FromHeader] string codOne = "pippo", [FromQuery] bool showAll = false)
|
|
{
|
|
await Task.Delay(1);
|
|
string answ = $"CodOne: {codOne} | showAll: {showAll}";
|
|
return answ;
|
|
}
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Richiede elenco di task da eseguire x il dev richiedente
|
|
/// </summary>
|
|
/// <param name="dev">dev richiedente</param>
|
|
/// <param name="AppKey">Cod Auth applicativo</param>
|
|
/// <param name="CodImp">CodImp del dev</param>
|
|
[HttpGet("pend/{dev}")]
|
|
public async Task<Dictionary<string, string>> Task2ExePending(string dev, [FromHeader] string AppKey, [FromHeader] string CodImp)
|
|
//public async Task<ActionResult<Dictionary<string, string>>> Task2ExeRequest(string dev, [FromHeader] string AppKey, [FromHeader] string CodImp)
|
|
{
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
// verifica validità richiesta...
|
|
if (!string.IsNullOrEmpty(AppKey) && !string.IsNullOrEmpty(CodImp))
|
|
{
|
|
// FixMe ToDo !!! effettuare verifica valori chiavi/imp/device...
|
|
|
|
// recupero da REDIS le richieste pending x la macchina...
|
|
result = dataService.TaskListGet(CodImp);
|
|
// registro infine chiamata
|
|
await dataService.recordCall(dev, CodImp, $"POST:api/apptask/pending/ | {dev} | {CodImp} | {AppKey}");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registra in esecuzione itask richiesti al device
|
|
/// </summary>
|
|
/// <param name="dev">dev richiedente</param>
|
|
/// <param name="CurrRequest">Obj AuthDataDTO con chiavi (master o app)</param>
|
|
[HttpPost("running/{dev}")]
|
|
public async Task<string> Task2ExeRunning(string dev, [FromBody] TaskResultDTO CurrRequest)
|
|
{
|
|
string result = "NA";
|
|
// verifica validità richiesta...
|
|
if (CurrRequest.IsValid)
|
|
{
|
|
// FixMe ToDo !!! effettuare verifica valori chiavi/imp/device...
|
|
|
|
// recupero da REDIS le richieste pending x la macchina...
|
|
int numDone = dataService.TaskRunning(CurrRequest.CodImp, CurrRequest.DataPayload);
|
|
|
|
result = $"saved {numDone}/{CurrRequest.DataPayload.Count}";
|
|
// salva in redis e toglie dai task da eseguire la richiesta relativa...
|
|
|
|
// registro infine chiamata
|
|
await dataService.recordCall(CurrRequest.CodImp, CurrRequest.CodImp, $"POST:api/apptask/running/ | {CurrRequest.MastKey} | {CurrRequest.CodImp}");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registra risultato esecuzione task richiesti al device
|
|
/// </summary>
|
|
/// <param name="dev">dev richiedente</param>
|
|
/// <param name="CurrRequest">Obj AuthDataDTO con chiavi (master o app)</param>
|
|
[HttpPost("done/{dev}")]
|
|
public async Task<string> Task2ExeResult(string dev, [FromBody] TaskResultDTO CurrRequest)
|
|
{
|
|
string result = "NA";
|
|
// verifica validità richiesta...
|
|
if (CurrRequest.IsValid)
|
|
{
|
|
// FixMe ToDo !!! effettuare verifica valori chiavi/imp/device...
|
|
|
|
// recupero da REDIS le richieste pending x la macchina...
|
|
int numDone = dataService.TaskDoneAdd(CurrRequest.CodImp, CurrRequest.DataPayload);
|
|
|
|
result = $"saved {numDone}/{CurrRequest.DataPayload.Count}";
|
|
// salva in redis e toglie dai task da eseguire la richiesta relativa...
|
|
|
|
// registro infine chiamata
|
|
await dataService.recordCall(CurrRequest.CodImp, CurrRequest.CodImp, $"POST:api/apptask/done/ | {CurrRequest.MastKey} | {CurrRequest.CodImp}");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Classe per logging
|
|
/// </summary>
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
/// <summary>
|
|
/// Dataservice x accesso DB
|
|
/// </summary>
|
|
protected ApiDataService dataService { get; set; } = null!;
|
|
}
|
|
}
|