using AppData; using Newtonsoft.Json; using NKC_SDK; using System.IO; using System.Web.Http; namespace NKC_WF.Controllers { public class BunkController : ApiController { /// /// oggetto static/singleton per fare chiamate sul datalayer /// protected DataLayer DLMan = new DataLayer(); /// /// COdice macchina (HARD CODED) /// protected string machine = "WRK001"; /// /// Restituisce il FIRST BUNK da lavorare /// GET: api/Bunk /// /// [HttpGet] public ProdBunk Get() { ProdBunk answ = null; try { answ = ComLib.prodGetFirstBunk(); } catch { } return answ; } /// /// Ottengo NEXT BUNK /// GET: api/Bunk/2?showNext=true /// /// /// /// [HttpGet] public ProdBunk Get(int id, bool showNext) { ProdBunk answ = null; try { if (showNext) { answ = ComLib.prodGetNextBunk(id); } else { answ = ComLib.prodGetBunk(id); } } catch { } return answ; } /************************************ * METODI PUT * * per abilitare è necessario agire sulla conf di IIS: * * - modificare il file applicationHost.config che si trova in C:\Windows\System32\inetsrv\config * - disinstallare webDav oppure commentare le righe * * * * - aggiungere PUT/DELETE a handler: * * * **************************************/ /// /// Effettua la chiamata di update /// /// /// // PUT: api/Bunk/5 [HttpPut] public void Put(ProdBunk currBunk) { // NB. decodifico direttamente come oggetto, vedere qui: // https://weblog.west-wind.com/posts/2013/dec/13/accepting-raw-request-body-content-with-aspnet-web-api // https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers // se non nullo... if (currBunk != null) { foreach (var item in currBunk.SheetList) { DLMan.taSHL.updateDate(item.SheetId, item.Printing.DtStart, item.Printing.DtEnd, item.Machining.DtStart, item.Machining.DtEnd, item.Unloading.DtStart, item.Unloading.DtEnd, (int)item.Status); // verifico SE SIA AVVENUTO CON SUCCESSO lo step di lavorazione... if (item.Machining.Success) { // SE machining completato --> status a LAVORATO! if (item.Machining.DtEnd != null) { DLMan.taIL.updateSheetStatus(item.SheetId, 1, "PROD"); } } } } // INVALIDO eventuale valore BUNK in REDIS... ComLib.resetRedisBunkData(machine); } /// /// Processa una chiamata POST per l'invio in blocco status BUNK /// POST: api/Bunk /// /// [HttpPost] public string Post() { string answ = ""; // questa classe è derivata da Controller.Response... x cui recupero lo stream in altro modo... string content = ""; System.Web.HttpContext.Current.Request.InputStream.Position = 0; using (var reader = new StreamReader(System.Web.HttpContext.Current.Request.InputStream, System.Text.Encoding.UTF8, true, 4096, true)) { content = reader.ReadToEnd(); } //Rest System.Web.HttpContext.Current.Request.InputStream.Position = 0; // procedo a deserializzare in blocco l'oggetto... try { // deserializzo. ProdBunk currBunk = JsonConvert.DeserializeObject(content); // se non nullo... if (currBunk != null) { foreach (var item in currBunk.SheetList) { // calcolo stsatus dalle date valide... DLMan.taSHL.updateDate(item.SheetId, item.Printing.DtStart, item.Printing.DtEnd, item.Machining.DtStart, item.Machining.DtEnd, item.Unloading.DtStart, item.Unloading.DtEnd, (int)item.Status); // SE machining completato --> status a LAVORATO! if (item.Machining.DtEnd != null) { DLMan.taIL.updateSheetStatus(item.SheetId, 1, "PROD"); } } // segnalo avanzamento su redis x pagina unload ComLib.advaceSheetRevByBunk(currBunk.BunkId); } answ = "OK"; } catch { answ = "NO"; } return answ; } } }