252 lines
7.9 KiB
C#
252 lines
7.9 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");
|
|
}
|
|
#if false
|
|
if (string.IsNullOrEmpty(id)) return Ok("KO");
|
|
|
|
try
|
|
{
|
|
var result = DService.IobInsEnab(id) ? "OK" : "NO";
|
|
// Logga solo l'esito se necessario
|
|
return Ok(result);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in enabled {id}: {exc.Message}");
|
|
return Ok("NO");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/// <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");
|
|
|
|
// attenzione! poiché nell'URL il carattere "#" viene filtrato ci aspettiamo il
|
|
// carattere "|" che poi trasformiamo ora 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, "Errore interno | GetCounterTCRec");
|
|
}
|
|
}
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Recupera COUNTER x macchina:
|
|
///
|
|
/// GET: IOB/getCounter/5
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCounter/{id}")]
|
|
public async Task<string> getCounter(string id)
|
|
{
|
|
string answ = "";
|
|
try
|
|
{
|
|
var pzCount = await _tabDService.pzCounter(id);
|
|
answ = $"{pzCount}";
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in counter TC (get){Environment.NewLine}{exc}");
|
|
answ = "NO";
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Recupera COUNTER x macchina dal CONTEGGIO dei TCRecorded:
|
|
///
|
|
/// GET: IOB/getCounterTCRec/5
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCounterTCRec/{id}")]
|
|
public async Task<string> getCounterTCRec(string id)
|
|
{
|
|
// attenzione! poiché nell'URL il carattere "#" viene filtrato ci aspettiamo il
|
|
// carattere "|" che poi trasformiamo ora in "#"
|
|
id = id.Replace("|", "#");
|
|
string answ = "";
|
|
try
|
|
{
|
|
var pzCountTC = await _tabDService.pzCounterTC(id);
|
|
answ = $"{pzCountTC}";
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in counter TC (get){Environment.NewLine}{exc}");
|
|
answ = "NO";
|
|
}
|
|
return answ;
|
|
}
|
|
#endif
|
|
|
|
/// <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;
|
|
}
|
|
|
|
[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
|
|
}
|
|
} |