525 lines
20 KiB
C#
525 lines
20 KiB
C#
using CMS_CORE_Library.Models;
|
|
using Thermo.Active.Model.DTOModels;
|
|
using Thermo.Active.NC;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using static CMS_CORE_Library.Models.DataStructures;
|
|
using static Thermo.Active.Config.ServerConfig;
|
|
using static Thermo.Active.Model.Constants;
|
|
using Thermo.Active.Model.DTOModels.ThRecipe;
|
|
using Thermo.Active.Model.DTOModels.ThWarmers;
|
|
using Thermo.Active.Config;
|
|
using Thermo.Active.Utils;
|
|
|
|
namespace Thermo.Active.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/recipe")]
|
|
public class RecipeController : ApiController
|
|
{
|
|
[Route("overview"), HttpGet]
|
|
public IHttpActionResult GetOverview()
|
|
{
|
|
using (NcAdapter ncAdapter = new NcAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | GetOverview | {libraryError.exception}");
|
|
return InternalServerError();
|
|
}
|
|
|
|
libraryError = ncAdapter.GetRecipeOverview(out Dictionary<string, RecipeCatStatus> currOverview);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetRecipeOverview error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
return Ok(currOverview);
|
|
}
|
|
}
|
|
[Route("current"), HttpGet]
|
|
public IHttpActionResult GetCurrentParameters()
|
|
{
|
|
using (NcAdapter ncAdapter = new NcAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | GetCurrentParameters | {libraryError.exception}");
|
|
return InternalServerError();
|
|
}
|
|
|
|
libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetRecipeOverview error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
return Ok(currRecipe);
|
|
}
|
|
}
|
|
|
|
[Route("update"), HttpPut]
|
|
public IHttpActionResult WriteParameters(Dictionary<string, double> parametersList)
|
|
{
|
|
if (parametersList != null)
|
|
{
|
|
if (RecipeLiveData != null)
|
|
{
|
|
using (NcAdapter ncAdapter = new NcAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | WriteParameters | {libraryError.exception}");
|
|
return InternalServerError();
|
|
}
|
|
|
|
// read recipe!
|
|
libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> prevRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetRecipeOverview error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
Dictionary<string, DTORecipeParam> updtRecipe = new Dictionary<string, DTORecipeParam>();
|
|
foreach (var item in parametersList)
|
|
{
|
|
if (prevRecipe.ContainsKey(item.Key))
|
|
{
|
|
// aggiorno il valore HMI nel parametro
|
|
var currParam = prevRecipe[item.Key];
|
|
currParam.SetpointHMI = item.Value;
|
|
// salvo (1 parametro, potrei fare N...)
|
|
updtRecipe.Add(item.Key, currParam);
|
|
}
|
|
else
|
|
{
|
|
ThermoActiveLogger.LogError($"WriteParameters error | key not found: {item.Key}");
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
// scrivo sul PLC
|
|
ncAdapter.WriteRecipeParams(updtRecipe);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// non pronto!
|
|
ThermoActiveLogger.LogError($"RecipeLiveData null | WriteParameters");
|
|
return InternalServerError();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ThermoActiveLogger.LogError($"RecipeLiveData null | Empty Parameters");
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Confirm recipe modification (parameters: HMI --> PLC)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("confirm"), HttpPut]
|
|
public IHttpActionResult ConfirmEdit()
|
|
{
|
|
if (RecipeLiveData != null)
|
|
{
|
|
using (NcAdapter ncAdapter = new NcAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | Recipe ConfirmEdit | {libraryError.exception}");
|
|
return InternalServerError();
|
|
}
|
|
|
|
// scrivo sul PLC il comando conferma!
|
|
libraryError = ncAdapter.ConfirmRecipeData(true);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ConfirmEdit error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// recupero i dati LIVE dei parametri HMI della ricetta...
|
|
libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ConfirmEdit error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// rileggo la ricetta
|
|
var currParams = new Dictionary<string, double>();
|
|
foreach (var item in currRecipe)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointHMI);
|
|
}
|
|
|
|
SaveCurrentRecipeParams(currParams);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// non pronto!
|
|
ThermoActiveLogger.LogError($"RecipeLiveData null | Empty");
|
|
return InternalServerError();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cancel recipe modification (parameters: PLC --> HMI)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("cancel"), HttpPut]
|
|
public IHttpActionResult CancelEdit()
|
|
{
|
|
if (RecipeLiveData != null)
|
|
{
|
|
using (NcAdapter ncAdapter = new NcAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | Recipe CancelEdit | {libraryError.exception}");
|
|
return InternalServerError();
|
|
}
|
|
|
|
// scrivo sul PLC il comando annula!
|
|
libraryError = ncAdapter.ConfirmRecipeData(false);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ConfirmEdit error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// recupero i dati LIVE dei parametri HMI della ricetta...
|
|
libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ConfirmEdit error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
var currParams = new Dictionary<string, double>();
|
|
foreach (var item in currRecipe)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointPLC);
|
|
}
|
|
|
|
// ora salvo ANCHE i dati live...
|
|
SaveCurrentRecipeParams(currParams);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// non pronto!
|
|
ThermoActiveLogger.LogError($"RecipeLiveData null | Empty");
|
|
return InternalServerError();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// load recipe from file and send to PLC
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("load"), HttpPut]
|
|
public IHttpActionResult Load(string newName)
|
|
{
|
|
using (NcFileAdapter ncAdapter = new NcFileAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | Load | {libraryError.exception}");
|
|
return InternalServerError();
|
|
}
|
|
|
|
// chiamo metodo di lettura...
|
|
bool fatto = ServerConfigController.LoadRecipe(newName);
|
|
if (!fatto)
|
|
{
|
|
ThermoActiveLogger.LogError($"LoadRecipe error");
|
|
return NotFound();
|
|
}
|
|
|
|
libraryError = WriteCurrentRecipeToPlc();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ConfirmEdit error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load tempalte recipe from file and save as new current...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("new"), HttpPut]
|
|
public IHttpActionResult NewRecipe()
|
|
{
|
|
using (NcFileAdapter ncAdapter = new NcFileAdapter())
|
|
{
|
|
// chiamo metodo di lettura...
|
|
bool fatto = ServerConfigController.LoadTemplate();
|
|
|
|
if (!fatto)
|
|
{
|
|
ThermoActiveLogger.LogError($"LoadRecipe error");
|
|
return NotFound();
|
|
}
|
|
|
|
CmsError libraryError = WriteCurrentRecipeToPlc();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ConfirmEdit error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
}
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save current recipe from PLC to default
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("save"), HttpPut]
|
|
public IHttpActionResult Save()
|
|
{
|
|
using (NcFileAdapter ncAdapter = new NcFileAdapter())
|
|
{
|
|
|
|
// recupero i dati LIVE dei parametri HMI della ricetta...
|
|
CmsError libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
|
|
var currParams = new Dictionary<string, double>();
|
|
foreach (var item in currRecipe)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointPLC);
|
|
}
|
|
|
|
// ora salvo ANCHE i dati live...
|
|
SaveCurrentRecipeParams(currParams);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Save current recipe from PLC with new name
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("saveAs"), HttpPut]
|
|
public IHttpActionResult SaveAs(string newName)
|
|
{
|
|
using (NcFileAdapter ncAdapter = new NcFileAdapter())
|
|
{
|
|
// recupero i dati LIVE dei parametri HMI della ricetta...
|
|
CmsError libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"SaveAs error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
var currParams = new Dictionary<string, double>();
|
|
foreach (var item in currRecipe)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointPLC);
|
|
}
|
|
|
|
// ora salvo ANCHE i dati live...
|
|
RecipeLiveData.RecipeParameters = currParams;
|
|
// e salvo su disco
|
|
ServerConfigController.SaveRecipe(newName);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Save current recipe from PLC to Template
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("saveTemplate"), HttpPut]
|
|
public IHttpActionResult SaveTemplate()
|
|
{
|
|
using (NcFileAdapter ncAdapter = new NcFileAdapter())
|
|
{
|
|
// recupero i dati LIVE dei parametri HMI della ricetta...
|
|
CmsError libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"SaveTemplate error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
|
|
// recupero i dati LIVE dei carichi load dei cahnnels di riscaldo...
|
|
libraryError = ncAdapter.ReadWarmers(out Dictionary<int, DTOWarmers> currWarmers);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ReadWarmers error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// uso i valopri HMI...
|
|
var currParams = new Dictionary<string, double>();
|
|
foreach (var item in currRecipe)
|
|
{
|
|
currParams.Add(item.Key, item.Value.SetpointHMI);
|
|
}
|
|
|
|
// ora salvo nei dati live...
|
|
RecipeLiveData.RecipeParameters = currParams;
|
|
|
|
// carico i dati dei riscaldi...
|
|
var currChSet = new Dictionary<int, int>();
|
|
foreach (var item in currWarmers)
|
|
{
|
|
currChSet.Add(item.Key, item.Value.SetpointHMI);
|
|
}
|
|
RecipeLiveData.ChannelSetpoints = currChSet;
|
|
|
|
|
|
// e salvo su disco
|
|
ServerConfigController.SaveRecipeTemplate();
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Do actual recipe parameters FileSave
|
|
/// </summary>
|
|
/// <param name="currParams"></param>
|
|
private static void SaveCurrentRecipeParams(Dictionary<string, double> currParams)
|
|
{
|
|
try
|
|
{
|
|
// ora salvo ANCHE i dati live...
|
|
RecipeLiveData.RecipeParameters = currParams;
|
|
// e salvo su disco
|
|
ServerConfigController.SaveRecipeCurrent();
|
|
}
|
|
catch(Exception exc)
|
|
{
|
|
ThermoActiveLogger.LogError($"Recipe | SaveCurrentRecipeParams exception | {exc}");
|
|
}
|
|
}
|
|
|
|
public static CmsError WriteCurrentRecipeToPlc()
|
|
{
|
|
CmsError checkError = NO_ERROR;
|
|
|
|
using (NcAdapter ncAdapter = new NcAdapter())
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | Load | {libraryError.exception}");
|
|
return libraryError;
|
|
}
|
|
|
|
|
|
// copy data to PLC
|
|
checkError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> prevRecipe);
|
|
if (checkError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"WriteCurrentRecipeToPlc | ReadFullRecipe error | {checkError.exception}");
|
|
return checkError;
|
|
}
|
|
|
|
// save parameters to PLC!!!
|
|
Dictionary<string, DTORecipeParam> updtRecipe = new Dictionary<string, DTORecipeParam>();
|
|
foreach (var item in RecipeLiveData.RecipeParameters)
|
|
{
|
|
if (prevRecipe.ContainsKey(item.Key))
|
|
{
|
|
// aggiorno il valore HMI nel parametro
|
|
var currParam = prevRecipe[item.Key];
|
|
currParam.SetpointHMI = item.Value;
|
|
// salvo (1 parametro, potrei fare N...)
|
|
updtRecipe.Add(item.Key, currParam);
|
|
}
|
|
else
|
|
{
|
|
ThermoActiveLogger.LogError($"prevRecipe not found | key: {item.Key}");
|
|
return NOT_FOUND_ERROR;
|
|
}
|
|
}
|
|
|
|
// write to PLC
|
|
checkError = ncAdapter.WriteRecipeParams(updtRecipe);
|
|
if (checkError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"WriteCurrentRecipeToPlc | WriteRecipeParams error | {checkError.exception}");
|
|
return checkError;
|
|
}
|
|
|
|
|
|
// process ch load setup...
|
|
Dictionary<int, int> newRisk = new Dictionary<int, int>();
|
|
foreach (var item in RecipeLiveData.ChannelSetpoints)
|
|
{
|
|
newRisk.Add(item.Key, item.Value);
|
|
}
|
|
|
|
// write to PLC
|
|
checkError = ncAdapter.WriteRecipeWarmChSetpHMI(newRisk);
|
|
if (checkError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"WriteCurrentRecipeToPlc | WriteRecipeWarmChSetpHMI error | {checkError.exception}");
|
|
return checkError;
|
|
}
|
|
|
|
// enable recipe! scrivo sul PLC il comando conferma!
|
|
libraryError = ncAdapter.ConfirmRecipeData(true);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ReadWarmers error | {libraryError.exception}");
|
|
return checkError;
|
|
}
|
|
}
|
|
return checkError;
|
|
}
|
|
}
|
|
} |