Files
NKC/NKC_WF/Controllers/SheetController.cs
T
2020-07-15 16:34:46 +02:00

187 lines
8.0 KiB
C#

using AppData;
using Newtonsoft.Json;
using NKC_SDK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace NKC_WF.Controllers
{
public class SheetController : ApiController
{
/// <summary>
/// Restituisce un array di sheet da lavorare (quindi NON ancora scaricati, anche di + BUNK)
/// GET: api/Sheet
/// </summary>
/// <returns></returns>
[HttpGet]
public SheetWorkList Get()
{
// fisso su machcina 1
string machineName = "WRK001";
SheetWorkList answ = null;
try
{
answ = ComLib.prodGetSheetWorkList(machineName);
}
catch
{ }
return answ;
}
/// <summary>
/// Ottengo elenco specifico dato cod MACCHINA
/// GET: api/Sheet/WRK001
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public SheetWorkList Get(string id)
{
SheetWorkList answ = null;
try
{
answ = ComLib.prodGetSheetWorkList(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
* <!-- <add name="WebDAVModule" /> -->
* <!-- <add name="WebDAVModule" image="%windir%\System32\inetsrv\webdav.dll" /> -->
* <!-- <add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" /> -->
* - aggiungere PUT/DELETE a handler:
* <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
* <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
*
**************************************/
/// <summary>
/// Effettua la chiamata di update x SINGOLO foglio
/// </summary>
/// <param name="sheetUpdated">Oggetto con Elenco fogli da aggiornare</param>
// PUT: api/Sheet/5
[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)
{
DataLayer.man.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);
// verifico SE SIA AVVENUTO CON SUCCESSO lo step di lavorazione...
if (currSheet.Machining.Success)
{
// SE machining completato --> status a LAVORATO!
if (currSheet.Machining.DtEnd != null)
{
// hard coded su multiax
DataLayer.man.taIL.updateSheetStatus(currSheet.SheetId, 1, "WRK001");
}
}
// segnalo avanzamento su redis x pagina unload
ComLib.advaceSheetRevByBunk(currSheet.BunkId);
// calcolo Batch
BunkId = currSheet.BunkId;
}
}
var tabBunks = DataLayer.man.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();
}
}
/// <summary>
/// Processa una chiamata POST per l'invio in blocco status BUNK
/// POST: api/Bunk
/// </summary>
/// <returns></returns>
[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
{
int BunkId = 0;
// deserializzo.
SheetWorkList sheetUpdated = JsonConvert.DeserializeObject<SheetWorkList>(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)
{
DataLayer.man.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
DataLayer.man.taIL.updateSheetStatus(currSheet.SheetId, 1, "WRK001");
}
// segnalo avanzamento su redis x pagina unload
ComLib.advaceSheetRevByBunk(currSheet.BunkId);
BunkId = currSheet.BunkId;
answ = "OK";
}
}
}
// ricalcolo stato BUNK
ComLib.updateBatchPosition(BunkId);
}
}
catch (Exception exc)
{
answ = "NO";
}
return answ;
}
}
}