354 lines
12 KiB
C#
354 lines
12 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 async Task<IActionResult> AddOptPar(string id, string pName, string pValue)
|
|
{
|
|
DService.ScriviKeepAlive(id, DateTime.Now);
|
|
try
|
|
{
|
|
DService.AddOptPar4Machine(id, pName, pValue);
|
|
return await GetOptPar(id);
|
|
}
|
|
catch
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "Errore interno | AddOptPar");
|
|
}
|
|
}
|
|
|
|
/// <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}")]
|
|
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}")]
|
|
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.pzCounterTcAsync(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>
|
|
/// Restituisce data-ora inizio dell'odl correntemente in lavorazione sulla macchina...
|
|
/// es: http://url_site/MP/IO/IOB/getCurrOdlStart/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCurrOdlStart/{id}")]
|
|
public async Task<IActionResult> GetCurrOdlStart(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
|
|
DateTime answ = new DateTime(DateTime.Now.Year - 1, 12, 31);
|
|
// chiamo metodo redis/db...
|
|
try
|
|
{
|
|
var odlData = await DService.OdlCurrByMaccAsync(id);
|
|
if (odlData != null && odlData.DataInizio.HasValue)
|
|
{
|
|
answ = odlData.DataInizio.Value;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in recupero getCurrOdlStart{Environment.NewLine}{exc}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
}
|
|
return Ok(answ.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
}
|
|
|
|
/// <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 async Task<IActionResult> GetOptPar(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
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 Ok(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 "#"
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Metodo di dichiarazione "improprio" tramite get call totalmetne definita da URL
|
|
/// GET: IOB/input/SIMUL_03?valore=3&dtEve=20181206180600000&dtCurr=20181206180600000&cnt=999
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="valore"></param>
|
|
/// <param name="dtEve"></param>
|
|
/// <param name="dtCurr"></param>
|
|
/// <param name="cnt"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("input/{id}")]
|
|
public async Task<IActionResult> Input(string id, string valore, string dtEve, string dtCurr, string cnt)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
string answ = "";
|
|
if (cnt == null)
|
|
{
|
|
cnt = "0";
|
|
}
|
|
|
|
DateTime dataOraEvento = DateTime.Now;
|
|
try
|
|
{
|
|
answ = DService.processInput(id, valore, dtEve, dtCurr, cnt);
|
|
return Ok(answ);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in processInput{Environment.NewLine}{exc}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SALVA in blocco un incremento pezzi x macchina restituendo il valore appena inviato o,
|
|
/// se mancasse chaive redis, del valore da DB
|
|
///
|
|
/// GET: IOB/savePzCountInc/5?qty=10
|
|
/// </summary>
|
|
/// <param name="id">codice macchina</param>
|
|
/// <param name="qty">num peziz da salvare in blocco</param>
|
|
/// <returns></returns>
|
|
[HttpGet("savePzCountInc/{id}")]
|
|
public async Task<IActionResult> SavePzCountInc(string id, string qty)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
|
|
string answ = "";
|
|
DateTime dataOraEvento = DateTime.Now;
|
|
// salvo SEMPRE log x questo tipo di dati!
|
|
Log.Info($"Salvataggio incremento contapezzi | id: {id} | pezzi: {qty}");
|
|
try
|
|
{
|
|
answ = await DService.saveCaricoPezzi(id, qty);
|
|
return Ok(answ);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in savePzCountInc{Environment.NewLine}{exc}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SALVA Counter x macchina restituendo il valore appena inviato o, se mancasse chiave
|
|
/// redis, del valore da DB
|
|
///
|
|
/// GET: IOB/setCounter/5?counter=10
|
|
/// </summary>
|
|
/// <param name="id">cod macchina</param>
|
|
/// <param name="counter">contapezzi da salvare</param>
|
|
/// <returns></returns>
|
|
[HttpGet("setCounter/{id}")]
|
|
public async Task<IActionResult> SetCounter(string id, string counter)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
|
|
string answ = "-1";
|
|
DateTime dataOraEvento = DateTime.Now;
|
|
Log.Debug($"Salvataggio counter | id: {id} | pzCount: {counter}");
|
|
|
|
answ = DService.saveCounter(id, counter);
|
|
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
|
|
}
|
|
} |