234 lines
7.7 KiB
C#
234 lines
7.7 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using MP.IOC.Data;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using System.Reflection;
|
|
|
|
namespace MP.IOC.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class IOBController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
public IOBController(IConfiguration configuration, MpDataService DataService)
|
|
{
|
|
_configuration = configuration;
|
|
DService = DataService;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary> SALVA x macchina KVP parametro/valore:
|
|
/// GET: api/IOB/addOptPar/SIMUL_03?pName=PZREQ&pValue=1000
|
|
/// </summary> <param name="id"></param> <param name="pName"></param> <param name="pValue"></param>
|
|
[HttpGet("addOptPar/{id}")]
|
|
public string AddOptPar(string id, string pName, string pValue)
|
|
{
|
|
string answ = "";
|
|
DService.ScriviKeepAlive(id, DateTime.Now);
|
|
try
|
|
{
|
|
DService.AddOptPar4Machine(id, pName, pValue);
|
|
answ = GetOptPar(id);
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// GET: IOB/
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public IActionResult Alive()
|
|
{
|
|
return Ok("OK"); // Restituisce Status 200
|
|
}
|
|
|
|
/// <summary>
|
|
/// GET: IOB/enabled/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("enabled/{id}")]
|
|
public IActionResult Enabled(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
if (DService.IobInsEnab(id))
|
|
{
|
|
return Ok("OK");
|
|
}
|
|
else
|
|
{
|
|
//return StatusCode(503, "NO");
|
|
return UnprocessableEntity("NO");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera COUNTER x macchina:
|
|
/// GET: IOB/getCounter/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns>Il conteggio pezzi o un errore strutturato</returns>
|
|
[HttpGet("getCounter/{id}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetCounter(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
try
|
|
{
|
|
var pzCount = await DService.pzCounter(id);
|
|
return Ok(pzCount);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Errore durante il recupero del counter per la macchina {MachineId}", id);
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "Errore interno | GetCounter");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera COUNTER x macchina dal CONTEGGIO dei TCRecorded:
|
|
///
|
|
/// GET: IOB/getCounterTCRec/5
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns>Il conteggio pezzi o un errore strutturato</returns>
|
|
[HttpGet("getCounterTCRec/{id}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetCounterTCRec(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
try
|
|
{
|
|
var pzCount = await DService.pzCounterTC(id);
|
|
return Ok(pzCount);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Errore durante il recupero del counter TC per la macchina {MachineId}", id);
|
|
//return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "Errore interno | GetCounterTCRec");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera ODL corrente x macchina:
|
|
/// GET: IOB/getCurrODL/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCurrODL/{id}")]
|
|
public async Task<IActionResult> GetCurrODL(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
try
|
|
{
|
|
var odl = await DService.GetCurrOdlAsync(id);
|
|
return Ok($"{odl}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Errore GetCurrODL | macchina {MachineId}", id);
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
//return StatusCode(StatusCodes.Status500InternalServerError, "Errore interno | GetCurrODL");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera TASK richiesto x macchina:
|
|
/// GET: IOB/getOptPar/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns>Json contenente 1..n task da eseguire</returns>
|
|
[HttpGet("getOptPar/{id}")]
|
|
public string GetOptPar(string id)
|
|
{
|
|
string answ = "";
|
|
// scrivo keep alive!!! (se necessario, altrimenti è in cache...)
|
|
DService.ScriviKeepAlive(id, DateTime.Now);
|
|
try
|
|
{
|
|
// leggo da REDIS eventuale elenco task x macchina...
|
|
Dictionary<string, string> valori = DService.mOptParMacchina(id);
|
|
answ = JsonConvert.SerializeObject(valori);
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera TASK richiesto x macchina: ///
|
|
/// GET: IOB/getTask2Exe/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns>Json contenente 1..n task da eseguire</returns>
|
|
[HttpGet("getTask2Exe/{id}")]
|
|
public async Task<IActionResult> GetTask2Exe(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
if (!string.IsNullOrEmpty(id))
|
|
{
|
|
id = id.Replace("|", "#");
|
|
}
|
|
string answ = "";
|
|
DService.ScriviKeepAlive(id, DateTime.Now);
|
|
// leggo da REDIS eventuale elenco task x macchina...
|
|
Dictionary<string, string> valori = await DService.GetTask2ExeMacchinaAsync(id);
|
|
answ = JsonConvert.SerializeObject(valori);
|
|
return Ok(answ);
|
|
}
|
|
|
|
[HttpGet("version")]
|
|
public IActionResult Version()
|
|
{
|
|
var version = Assembly
|
|
.GetExecutingAssembly()
|
|
.GetName()
|
|
.Version?
|
|
.ToString() ?? "unknown";
|
|
|
|
return Ok(version);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Dataservice x accesso DB
|
|
/// </summary>
|
|
private MpDataService DService { get; set; }
|
|
|
|
#endregion Private Properties
|
|
}
|
|
} |