using AppData; using Newtonsoft.Json; using NKC_SDK; using SteamWare; using System; using System.IO; using System.Web.Http; namespace NKC_WF.Controllers { public class SheetController : ApiController { #region Public Constructors public SheetController() { dfWrkStart = memLayer.ML.CRB("dateFix_WrkStart"); dfWrkEnd = memLayer.ML.CRB("dateFix_WrkEnd"); dfUnlStart = memLayer.ML.CRB("dateFix_UnlStart"); dfUnlEnd = memLayer.ML.CRB("dateFix_UnlEnd"); } #endregion Public Constructors #region Public Methods /// /// Restituisce un array di sheet da lavorare (quindi NON ancora scaricati, anche di + BUNK) /// GET: api/Sheet /// /// [HttpGet] public SheetWorkList Get() { // fisso su macchina 0 (NON esistente) string machineName = "NE00"; SheetWorkList answ = null; try { answ = ComLib.prodGetSheetWorkList(machineName); } catch (Exception exc) { Log.Instance.Error($"EXCEPTION api/Sheet | get{Environment.NewLine}{exc}"); } return answ; } /// /// Ottengo elenco specifico dato cod MACCHINA (es NE01) /// GET: api/Sheet/NE01 /// /// /// [HttpGet] public SheetWorkList Get(string id) { SheetWorkList answ = null; try { answ = ComLib.prodGetSheetWorkList(id); } catch (Exception exc) { Log.Instance.Error($"EXCEPTION api/Sheet | get({id}){Environment.NewLine}{exc}"); } Log.Instance.Trace($"api/Sheet | get({id}) | answ: {answ}"); return answ; } /// /// Processa una chiamata POST per l'invio in blocco status BUNK /// POST: api/Bunk /// /// [HttpPost] public string Post() { int BunkId = 0; int BatchId = 0; 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. SheetWorkList sheetUpdated = JsonConvert.DeserializeObject(content); if (sheetUpdated != null) { // 2020.01.16 salvo su mongoDb la risposta... ComLib.man.saveProdAnsw(sheetUpdated); if (sheetUpdated.SheetList != null) { // salvo macchina machine = sheetUpdated.Machine; foreach (var currSheet in sheetUpdated.SheetList) { // se non nullo... if (currSheet != null) { // verifico il fixSheet ProdSheetExt fixSheet = checkFixSheet(currSheet); DLMan.taSHL.updateDate(fixSheet.SheetId, fixSheet.Printing.DtStart, fixSheet.Printing.DtEnd, fixSheet.Machining.DtStart, fixSheet.Machining.DtEnd, fixSheet.Unloading.DtStart, fixSheet.Unloading.DtEnd, (int)fixSheet.Status); // SE machining completato --> status a LAVORATO x item! if (fixSheet.Machining.DtEnd != null) { // hard coded su multiax DLMan.taIL.updateSheetStatus(fixSheet.SheetId, 1, sheetUpdated.Machine); } // segnalo avanzamento su redis x pagina unload ComLib.advaceSheetRevByBunk(fixSheet.BunkId); BunkId = fixSheet.BunkId; answ = "OK"; } } } var tabBunks = DLMan.taSTL.getByKey(BunkId); if (tabBunks.Count > 0) { BatchId = tabBunks[0].BatchID; } // ricalcolo stato BUNK ComLib.updateBatchPosition(BatchId); // INVALIDO eventuale valore BUNK in REDIS... ComLib.resetRedisBunkData(machine); } } catch { answ = "NO"; } Log.Instance.Trace($"api/Sheet | post() | machine: {machine} | BatchId: {BatchId} | answ: {answ}"); return answ; } /// Effettua la chiamata di update x SINGOLO foglio /// /// /// *********************************** 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: /// /// *************************************/ Oggetto con /// Elenco fogli da aggiornare /// PUT: api/Sheet/machine_ID [HttpPut] public void Put(SheetWorkList sheetUpdated) { // 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 int BunkId = 0; int BatchId = 0; if (sheetUpdated != null) { // 2020.01.16 salvo su mongoDb la risposta... ComLib.man.saveProdAnsw(sheetUpdated); if (sheetUpdated.SheetList != null) { foreach (var currSheet in sheetUpdated.SheetList) { // se non nullo... if (currSheet != null) { DLMan.taSHL.updateDate(currSheet.SheetId, currSheet.Printing.DtStart, currSheet.Printing.DtEnd, currSheet.Machining.DtStart, currSheet.Machining.DtEnd, currSheet.Unloading.DtStart, currSheet.Unloading.DtEnd, (int)currSheet.Status); // SE machining completato --> status a LAVORATO! if (currSheet.Machining.DtEnd != null) { // hard coded su multiax DLMan.taIL.updateSheetStatus(currSheet.SheetId, 1, sheetUpdated.Machine); } // segnalo avanzamento su redis x pagina unload ComLib.advaceSheetRevByBunk(currSheet.BunkId); // calcolo Batch BunkId = currSheet.BunkId; } } var tabBunks = DLMan.taSTL.getByKey(BunkId); if (tabBunks.Count > 0) { BatchId = tabBunks[0].BatchID; } } // ricalcolo stato BUNK ComLib.updateBatchPosition(BatchId); // INVALIDO eventuale valore BUNK in REDIS... ComLib.resetRedisBunkData(sheetUpdated.Machine); } } #endregion Public Methods #region Protected Fields /// /// oggetto static/singleton per fare chiamate sul datalayer /// protected DataLayer DLMan = new DataLayer(); /// /// COdice macchina (HARD CODED) /// protected string machine = "WRK001"; #endregion Protected Fields #region Private Fields /// /// Forza fix date al momento ricezione UnlEnd valido /// private bool dfUnlEnd = false; /// /// Forza fix date al momento ricezione UnlStart valido /// private bool dfUnlStart = false; /// /// Forza fix date al momento ricezione WrkEnd valido /// private bool dfWrkEnd = false; /// /// Forza fix date al momento ricezione WrkStart valido /// private bool dfWrkStart = false; #endregion Private Fields #region Private Methods /// /// Verifica sheet loggando le eventuali anomalie sulle date e sistemando quelle mancanti in /// base a setup /// /// Sheet da verificare ed eventualmente sistemare /// private ProdSheetExt checkFixSheet(ProdSheetExt CurrSheet) { var fixSheet = CurrSheet; // controllo SOLO SE se non sia ne in attesa ne completato... if (!fixSheet.Waiting || !fixSheet.Done) { // calcolo SE sia in errore secondo i 4 punti di controllo work/unload if (fixSheet.Unloading.OkE) { // loggo LogSheetError(fixSheet); if (dfUnlEnd) { // sistemo... fixSheet.Unloading.DtStart = fixSheet.Unloading.OkS ? fixSheet.Unloading.DtStart : fixSheet.Unloading.DtEnd; fixSheet.Machining.DtEnd = fixSheet.Machining.OkE ? fixSheet.Machining.DtEnd : fixSheet.Unloading.DtEnd; fixSheet.Machining.DtStart = fixSheet.Machining.OkS ? fixSheet.Machining.DtStart : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtEnd = fixSheet.Printing.OkE ? fixSheet.Printing.DtEnd : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtStart = fixSheet.Printing.OkS ? fixSheet.Printing.DtStart : fixSheet.Unloading.DtEnd; } } else if (fixSheet.Unloading.OkS && (!fixSheet.Printing.Done || !fixSheet.Machining.Done)) { // loggo LogSheetError(fixSheet); if (dfUnlStart) { // sistemo... fixSheet.Machining.DtEnd = fixSheet.Machining.OkE ? fixSheet.Machining.DtEnd : fixSheet.Unloading.DtEnd; fixSheet.Machining.DtStart = fixSheet.Machining.OkS ? fixSheet.Machining.DtStart : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtEnd = fixSheet.Printing.OkE ? fixSheet.Printing.DtEnd : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtStart = fixSheet.Printing.OkS ? fixSheet.Printing.DtStart : fixSheet.Unloading.DtEnd; } } else if (fixSheet.Machining.OkE && (!fixSheet.Printing.Done || !fixSheet.Machining.Done)) { // loggo LogSheetError(fixSheet); if (dfWrkEnd) { // sistemo... fixSheet.Machining.DtStart = fixSheet.Machining.OkS ? fixSheet.Machining.DtStart : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtEnd = fixSheet.Printing.OkE ? fixSheet.Printing.DtEnd : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtStart = fixSheet.Printing.OkS ? fixSheet.Printing.DtStart : fixSheet.Unloading.DtEnd; } } else if (fixSheet.Machining.OkS && !fixSheet.Printing.Done) { // loggo LogSheetError(fixSheet); if (dfWrkStart) { // sistemo... fixSheet.Printing.DtEnd = fixSheet.Printing.OkE ? fixSheet.Printing.DtEnd : fixSheet.Unloading.DtEnd; fixSheet.Printing.DtStart = fixSheet.Printing.OkS ? fixSheet.Printing.DtStart : fixSheet.Unloading.DtEnd; } } } return fixSheet; } private void LogSheetError(ProdSheetExt cSheet) { // loggo Log.Instance.Error($"Sheet Date error | SheetId: {cSheet.SheetId} | Print: {prtDate(cSheet.Printing)} | Work: {prtDate(cSheet.Machining)} | Unload: {prtDate(cSheet.Unloading)}"); } private string prtDate(WorkData wrkRec) { string dtSta = wrkRec.DtStart == null ? "NULL" : $"{wrkRec.DtStart:yyyy.MM.dd HH:mm:ss}"; string dtEnd = wrkRec.DtEnd == null ? "NULL" : $"{wrkRec.DtEnd:yyyy.MM.dd HH:mm:ss}"; return $"{dtSta} ... {dtEnd}"; } #endregion Private Methods } }