476 lines
17 KiB
C#
476 lines
17 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using MP.Core.DTO;
|
|
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>
|
|
/// GET elenco parametri correnti x IOB
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCurrObjItems/{id}")]
|
|
public async Task<IActionResult> GetCurrObjItems(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
|
|
var rawData = await DService.MachineParamListAsync(id);
|
|
List<ObjItemDTO> actValues = new List<ObjItemDTO>();
|
|
// ordino!
|
|
actValues = rawData
|
|
.OrderBy(x => x.displOrdinal)
|
|
.ThenBy(x => x.description)
|
|
.ToList();
|
|
return Ok(actValues);
|
|
}
|
|
|
|
/// <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 la quantità pezzi dell'odl correntemente in lavorazione sulla macchina...
|
|
/// GET: IOB/getCurrOdlQtaReq/SIMUL_01
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCurrOdlQtaReq/{id}")]
|
|
public async Task<IActionResult> GetCurrOdlQtaReq(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
|
|
int answ = 0;
|
|
// chiamo metodo redis/db...
|
|
try
|
|
{
|
|
var odlData = await DService.OdlCurrByMaccAsync(id);
|
|
if (odlData != null && odlData.DataInizio.HasValue)
|
|
{
|
|
answ = odlData.NumPezzi;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in recupero GetCurrOdlQtaReq{Environment.NewLine}{exc}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
}
|
|
return Ok(answ);
|
|
}
|
|
|
|
/// <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>
|
|
/// Processa una chiamata POST per l'invio di una Dictionary HashList da salvare in redis
|
|
/// POST: IOB/saveMachineIobConf/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id">ID dell'IOB</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[HttpPost("saveMachineIobConf/{id}")]
|
|
public async Task<IActionResult> SaveMachineIobConf(string id, [FromBody] Dictionary<string, string> currDict)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
string answ = "NA";
|
|
|
|
try
|
|
{
|
|
bool fatto = await DService.SaveMachineIobConf(id, currDict);
|
|
answ = fatto ? "OK" : "KO";
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in saveMachineIobConf{Environment.NewLine}{exc}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
}
|
|
return Ok(answ);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processa una chiamata POST per l'invio di una List Json di UNO O PIU' oggetti objItem
|
|
/// POST: IOB/upsertObjItems/SIMUL_03
|
|
/// </summary>
|
|
/// <param name="id">ID dell'IOB</param>
|
|
/// <returns></returns>
|
|
[HttpPost("upsertObjItems/{id}")]
|
|
public async Task<IActionResult> UpsertObjItems(string id, [FromBody] List<ObjItemDTO> innovazioni)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
|
|
|
// Multi: gestione carattere "|" trasformato in "#"
|
|
id = id.Replace("|", "#");
|
|
string answ = "NA";
|
|
|
|
try
|
|
{
|
|
// se != null --> salvo!
|
|
if (innovazioni != null)
|
|
{
|
|
// salvo
|
|
bool fatto = await DService.MachineParamUpsertAsync(id, innovazioni);
|
|
answ = "OK";
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in upsertObjItems{Environment.NewLine}{exc}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "NO");
|
|
}
|
|
return Ok(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica versione corrente REST API
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[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
|
|
}
|
|
} |