using AppData;
using Newtonsoft.Json;
using NKC_SDK;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Http;
namespace NKC_WF.Controllers
{
///
/// Classe controller per gestione eventi stats
/// Riceve una lista di 1..n eventi con
/// * CodMacchina
/// * DataOra
/// * Codice (Evento/stato) - codice 00 = KeepAlive, imposto dataora/drift
/// * Valore/Descrizione
///
/// e la salva su DB per successiva elaborazione
///
public class MachineStatController : ApiController
{
#region Protected Fields
///
/// oggetto static/singleton per fare chiamate sul datalayer
///
protected DataLayer DLMan = new DataLayer();
///
/// Codice macchina (HARD CODED)
///
protected string machine = "WRK000";
#endregion Protected Fields
#region Public Methods
///
/// Restituisce un array di stati macchina correnti
/// GET: api/MachineStat
///
///
[HttpGet]
public List Get()
{
List answ = new List();
try
{
answ = ComLib.prodMachStateDataGet();
}
catch
{ }
// se vuoto metto 1 finto...
if (answ.Count == 0)
{
var demoItem = new MachineStatData();
demoItem.Records.Add(new MachineStatRecord());
answ.Add(demoItem);
}
return answ;
}
///
/// Fornisce il record dello stato macchina corrente
/// GET: api/MachineStat/WRK001
///
///
///
[HttpGet]
public MachineStatData Get(string id)
{
MachineStatData answ = new MachineStatData();
try
{
var listState = ComLib.prodMachStateDataGet();
if (listState != null && listState.Count > 0)
{
answ = listState.Where(x => x.Machine == id).FirstOrDefault();
}
}
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:
*
*
*
**************************************/
#if false
///
/// Processa una chiamata POST per l'invio in blocco
/// 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)
{
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 x item!
if (currSheet.Machining.DtEnd != null)
{
// hard coded su multiax
DLMan.taIL.updateSheetStatus(currSheet.SheetId, 1, "WRK001");
}
// segnalo avanzamento su redis x pagina unload
ComLib.advaceSheetRevByBunk(currSheet.BunkId);
BunkId = currSheet.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";
}
return answ;
}
#endif
///
/// Effettua la chiamata di update x un set di dati delle macchine
///
/// Oggetto con macchina + Elenco record da aggiornare
// PUT: api/Sheet/5
[HttpPut]
public void Put(MachineStatData updatedInfo)
{
// 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
if (updatedInfo != null)
{
// mi limito a chiamnare procedura di update...
ComLib.prodMachStateDataInsert(updatedInfo);
}
}
#endregion Public Methods
}
}