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 /// SALVA x macchina KVP parametro/valore: /// GET: api/IOB/addOptPar/SIMUL_03?pName=PZREQ&pValue=1000 /// [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; } /// /// GET: IOB/ /// /// [HttpGet] public IActionResult Alive() { return Ok("OK"); // Restituisce Status 200 } /// /// GET: IOB/enabled/SIMUL_03 /// /// /// [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 } /// /// Recupera COUNTER x macchina: /// GET: IOB/getCounter/SIMUL_03 /// /// /// Il conteggio pezzi o un errore strutturato [HttpGet("getCounter/{id}")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task 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"); } } /// /// Recupera COUNTER x macchina dal CONTEGGIO dei TCRecorded: /// /// GET: IOB/getCounterTCRec/5 /// /// /// Il conteggio pezzi o un errore strutturato [HttpGet("getCounterTCRec/{id}")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task 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 /// /// Recupera COUNTER x macchina: /// /// GET: IOB/getCounter/5 /// /// /// [HttpGet("getCounter/{id}")] public async Task 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; } /// /// Recupera COUNTER x macchina dal CONTEGGIO dei TCRecorded: /// /// GET: IOB/getCounterTCRec/5 /// /// /// [HttpGet("getCounterTCRec/{id}")] public async Task 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 /// /// Recupera TASK richiesto x macchina: /// /// GET: IOB/getOptPar/SIMUL_03 /// /// /// Json contenente 1..n task da eseguire [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 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 /// /// Dataservice x accesso DB /// private MpDataService DService { get; set; } #endregion Private Properties } }