246 lines
9.1 KiB
C#
246 lines
9.1 KiB
C#
using CMS_CORE_Library.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Web.Http;
|
|
using Thermo.Active.Config;
|
|
using Thermo.Active.Model.DTOModels.ThWarmers;
|
|
using Thermo.Active.NC;
|
|
using Thermo.Active.Utils;
|
|
using static Thermo.Active.Config.ServerConfig;
|
|
|
|
namespace Thermo.Active.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/warmers")]
|
|
public class WarmersController : ApiController
|
|
{
|
|
/// <summary>
|
|
/// Oggetto adapter condiviso da WebAPI
|
|
/// </summary>
|
|
protected static NcAdapter ncAdapter = new NcAdapter();
|
|
|
|
[Route("channels"), HttpGet]
|
|
public IHttpActionResult GetCurrentWarmersChannels()
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | GetCurrentWarmersChannels | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
libraryError = ncAdapter.ReadWarmers(out Dictionary<int, DTOWarmers> currWarmers);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetRecipeOverview error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
return Ok(currWarmers);
|
|
}
|
|
[Route("resistances"), HttpGet]
|
|
public IHttpActionResult GetCurrentWarmersResistances()
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | GetCurrentWarmersResistances | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
libraryError = ncAdapter.GetWarmersResistances(out Dictionary<int, Model.ConfigModels.RiskResistModel> currWarmers);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetCurrentWarmersResistances error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
return Ok(currWarmers);
|
|
}
|
|
|
|
[Route("area"), HttpGet]
|
|
public IHttpActionResult GetCurrentAreaPerc()
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | GetCurrentAreaPerc | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// recupera aree % X / Y
|
|
libraryError = ncAdapter.GetWarmMaterialArea(out Dictionary<string, double> currAreaPerc);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetCurrentAreaPerc error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
return Ok(currAreaPerc);
|
|
}
|
|
|
|
[Route("update"), HttpPut]
|
|
public IHttpActionResult WriteSetpoints(Dictionary<int, int> channelsLoad)
|
|
{
|
|
// scrive su CHp da ricetta oppure da override x parametri utente...
|
|
if (channelsLoad != null)
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | WriteSetpoints | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
// scrivo sul PLC
|
|
libraryError = ncAdapter.WriteRecipeWarmChSetpHMI(channelsLoad);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"WriteSetpoints error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
else
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Confirm recipe modification (parameters: HMI --> PLC)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("confirm"), HttpPut]
|
|
public IHttpActionResult ConfirmEdit()
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | Warmers ConfirmEdit | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// scrivo sul PLC il comando conferma!
|
|
libraryError = ncAdapter.ConfirmRecipeData(true);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"Warmers ConfirmEdit error | ConfirmRecipeData | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// recupero i dati LIVE dei parametri HMI dei riscaldi...
|
|
libraryError = ncAdapter.ReadWarmers(out Dictionary<int, DTOWarmers> currWarmers);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"Warmers ConfirmEdit error | ReadWarmers | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// rileggo la ricetta
|
|
var currParams = new Dictionary<int, int>();
|
|
foreach (var item in currWarmers)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointHMI);
|
|
}
|
|
|
|
saveCurrentRecipeWarmers(currParams);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cancel recipe modification (parameters: PLC --> HMI)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("cancel"), HttpPut]
|
|
public IHttpActionResult CancelEdit()
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | Recipe CancelEdit | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// scrivo sul PLC il comando annula!
|
|
libraryError = ncAdapter.ConfirmRecipeData(false);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"Warmers CancelEdit error | ConfirmRecipeData | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// recupero i dati LIVE dei parametri HMI dei riscaldi...
|
|
libraryError = ncAdapter.ReadWarmers(out Dictionary<int, DTOWarmers> currWarmers);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"Warmers ConfirmEdit error | ReadWarmers | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// rileggo la ricetta
|
|
var currParams = new Dictionary<int, int>();
|
|
foreach (var item in currWarmers)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointHMI);
|
|
}
|
|
|
|
saveCurrentRecipeWarmers(currParams);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
|
|
[Route("setConfig"), HttpPut]
|
|
public IHttpActionResult SetConfig()
|
|
{
|
|
// scrive configurazioni RISK
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | SetConfig | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// scrivo sul PLC
|
|
libraryError = ncAdapter.WriteRecipeWarmConfig();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"Warmers SetConfig error | WriteRecipeWarmConfig | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Do actual recipe warmers setpoints FileSave
|
|
/// </summary>
|
|
/// <param name="chSetpoints"></param>
|
|
private static void saveCurrentRecipeWarmers(Dictionary<int, int> chSetpoints)
|
|
{
|
|
try
|
|
{
|
|
// ora salvo ANCHE i dati live...
|
|
NcFileAdapter.RecipeLiveData.ChannelSetpoints = chSetpoints;
|
|
// e salvo su disco
|
|
NcFileAdapter.SaveRecipeCurrent();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
ThermoActiveLogger.LogError($"Warmers | SaveCurrentRecipeParams exception | {exc}");
|
|
}
|
|
}
|
|
}
|
|
} |