using CMS_CORE_Library.Models; using Thermo.Active.Model.DTOModels; using Thermo.Active.NC; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using System.Web.Http; using static CMS_CORE_Library.Models.DataStructures; using static Thermo.Active.Config.ServerConfig; using static Thermo.Active.Model.Constants; using Thermo.Active.Model.DTOModels.Recipe; using Thermo.Active.Config; using Thermo.Active.Utils; namespace Thermo.Active.Controllers.WebApi { [RoutePrefix("api/prod")] public class ProdController : ApiController { /// /// Request mode SETUP /// /// [Route("mode/manual"), HttpPut] public IHttpActionResult RequestManual() { using (NcAdapter ncAdapter = new NcAdapter()) { // Try connection CmsError libraryError = ncAdapter.Connect(); if (libraryError.errorCode != 0) { ThermoActiveLogger.LogError($"NC Not connected! | RequestManual | {libraryError.exception}"); return InternalServerError(); } // scrivo sul PLC il comando strobe richiesta AUTO! CmsError cmsError = ncAdapter.StrobeMode(Model.DTOModels.ThermoProd.Mode.MANUAL); if (cmsError.IsError()) { ThermoActiveLogger.LogError($"RequestManual error | {cmsError.exception}"); return BadRequest(cmsError.localizationKey); } // ritorno solo fatto! return Ok(); } } /// /// Request mode AUTO /// /// [Route("mode/auto"), HttpPut] public IHttpActionResult RequestAuto() { using (NcAdapter ncAdapter = new NcAdapter()) { // Try connection CmsError libraryError = ncAdapter.Connect(); if (libraryError.errorCode != 0) { ThermoActiveLogger.LogError($"NC Not connected! | RequestAuto | {libraryError.exception}"); return InternalServerError(); } // scrivo sul PLC il comando strobe richiesta AUTO! CmsError cmsError = ncAdapter.StrobeMode(Model.DTOModels.ThermoProd.Mode.AUTO); if (cmsError.IsError()) { ThermoActiveLogger.LogError($"RequestAuto error | {cmsError.exception}"); return BadRequest(cmsError.localizationKey); } // ritorno solo fatto! return Ok(); } } /// /// Request mode SETUP /// /// [Route("mode/setup"), HttpPut] public IHttpActionResult RequestSetup() { using (NcAdapter ncAdapter = new NcAdapter()) { // Try connection CmsError libraryError = ncAdapter.Connect(); if (libraryError.errorCode != 0) { ThermoActiveLogger.LogError($"NC Not connected! | RequestSetup | {libraryError.exception}"); return InternalServerError(); } // scrivo sul PLC il comando strobe richiesta AUTO! CmsError cmsError = ncAdapter.StrobeMode(Model.DTOModels.ThermoProd.Mode.SETUP); if (cmsError.IsError()) { ThermoActiveLogger.LogError($"RequestSetup error | {cmsError.exception}"); return BadRequest(cmsError.localizationKey); } // ritorno solo fatto! return Ok(); } } /// /// Request start production /// /// [Route("prod/start"), HttpPut] public IHttpActionResult StartProd(int requestQty, bool newWorkOrder) { using (NcAdapter ncAdapter = new NcAdapter()) { // Try connection CmsError libraryError = ncAdapter.Connect(); if (libraryError.errorCode != 0) { ThermoActiveLogger.LogError($"NC Not connected! | StartProd | {libraryError.exception}"); return InternalServerError(); } // scrivo sul PLC il comando strobe richiesta AUTO! CmsError cmsError = ncAdapter.UpdateProdInfoData((short)requestQty, newWorkOrder); if (cmsError.IsError()) { ThermoActiveLogger.LogError($"StartProd error | {cmsError.exception}"); return BadRequest(cmsError.localizationKey); } // ritorno solo fatto! return Ok(); } } /// /// Request production current data /// /// [Route("prod/get"), HttpPut] public IHttpActionResult GetProd() { using (NcAdapter ncAdapter = new NcAdapter()) { // Try connection CmsError libraryError = ncAdapter.Connect(); if (libraryError.errorCode != 0) { ThermoActiveLogger.LogError($"NC Not connected! | StartProd | {libraryError.exception}"); return InternalServerError(); } // scrivo sul PLC il comando strobe richiesta AUTO! CmsError cmsError = ncAdapter.ReadProdInfoData(out ThermoModels.ProdInfoModel prodInfoDat); if (cmsError.IsError()) { ThermoActiveLogger.LogError($"GetProd error | {cmsError.exception}"); return BadRequest(cmsError.localizationKey); } // ritorno solo fatto! return Ok(prodInfoDat); } } /// /// Request production data from indexStart record for numRecord items (DESCENDING) /// /// [Route("prod/history"), HttpPut] public IHttpActionResult GetHistory(int indexStart, int numRecord) { using (NcAdapter ncAdapter = new NcAdapter()) { // Try connection CmsError libraryError = ncAdapter.Connect(); if (libraryError.errorCode != 0) { ThermoActiveLogger.LogError($"NC Not connected! | StartProd | {libraryError.exception}"); return InternalServerError(); } // scrivo sul PLC il comando strobe richiesta AUTO! CmsError cmsError = ncAdapter.GetHistProdInfoData(out List prodInfoDataList, indexStart, numRecord); if (cmsError.IsError()) { ThermoActiveLogger.LogError($"GetHistory error | {cmsError.exception}"); return BadRequest(cmsError.localizationKey); } // ritorno solo fatto! return Ok(prodInfoDataList); } } } }