using AppData;
using Newtonsoft.Json;
using NKC_SDK;
using System;
using System.IO;
using System.Web.Http;
namespace NKC_WF.Controllers
{
public class BunkController : ApiController
{
#region Protected Fields
///
/// COdice macchina (HARD CODED)
///
protected string CodPost = "WRK001";
///
/// oggetto static/singleton per fare chiamate sul datalayer
///
protected DataLayer DLMan = new DataLayer();
#endregion Protected Fields
#region Public Methods
///
/// Restituisce il FIRST BUNK da lavorare
/// GET: api/Bunk
///
///
[HttpGet]
public ProdBunk Get()
{
ProdBunk answ = null;
try
{
answ = ComLib.prodGetFirstBunk(CodPost);
}
catch (Exception exc)
{
Log.Instance.Error($"EXCEPTION api/Bunk | get{Environment.NewLine}{exc}");
}
return answ;
}
///
/// Ottengo NEXT BUNK
/// GET: api/Bunk/2?showNext=true
///
///
///
///
[HttpGet]
public ProdBunk Get(int id, bool showNext, string machine)
{
ProdBunk answ = null;
CodPost = machine;
try
{
if (showNext)
{
answ = ComLib.prodGetNextBunk(id, CodPost);
}
else
{
answ = ComLib.prodGetBunk(id, CodPost);
}
}
catch (Exception exc)
{
Log.Instance.Error($"EXCEPTION api/Bunk | get(int {id}, bool {showNext}, string {machine}) {Environment.NewLine}{exc}");
}
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:
*
*
*
**************************************/
///
/// 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)
{
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(Exception exc)
{
Log.Instance.Error($"EXCEPTION api/Bunk | Post(){Environment.NewLine}{exc}");
answ = "NO";
}
return answ;
}
///
/// 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);
// 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(CodPost);
}
#endregion Public Methods
}
}