658 lines
26 KiB
C#
658 lines
26 KiB
C#
using CMS_CORE_Library.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using System.Windows.Media.Animation;
|
|
using Thermo.Active.Config;
|
|
using Thermo.Active.Database.Controllers;
|
|
using Thermo.Active.Model.DTOModels.ThRecipe;
|
|
using Thermo.Active.Model.DTOModels.ThWarmers;
|
|
using Thermo.Active.NC;
|
|
using Thermo.Active.Utils;
|
|
using static CMS_CORE_Library.Models.DataStructures;
|
|
using static Thermo.Active.Config.ServerConfig;
|
|
using static Thermo.Active.Model.Constants;
|
|
|
|
namespace Thermo.Active.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/recipe")]
|
|
public class RecipeController : ApiController
|
|
{
|
|
/// <summary>
|
|
/// Oggetto adapter condiviso da WebAPI
|
|
/// </summary>
|
|
protected static NcAdapter ncAdapter = new NcAdapter();
|
|
|
|
[Route("overview"), HttpGet]
|
|
public IHttpActionResult GetOverview()
|
|
{
|
|
// // Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"ncAdapter Not connected! | GetOverview | {libraryError.exception}");
|
|
}
|
|
|
|
libraryError = ncAdapter.GetRecipeOverview(out Dictionary<RecipeSection, RecipeCatStatus> currOverview);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"GetRecipeOverview error | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
return Ok(currOverview);
|
|
}
|
|
[Route("current"), HttpGet]
|
|
public IHttpActionResult GetCurrentParameters()
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"NC Not connected! | GetCurrentParameters | {libraryError.exception}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
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]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult WriteParameters(Dictionary<string, double> parametersList)
|
|
{
|
|
if (parametersList != null)
|
|
{
|
|
if (NcFileAdapter.RecipeLiveData != null)
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
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];
|
|
// se il parametro richiesto è DIVERSO dal precedente su HMI...
|
|
if (currParam.SetpointHMI != item.Value)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
if (updtRecipe.Count > 0)
|
|
{
|
|
// 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>
|
|
/// <param name="section">section confirmed (string as in overview)</param>
|
|
/// <returns></returns>
|
|
[Route("confirm"), HttpPut]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult ConfirmEdit(RecipeSection section)
|
|
{
|
|
if (NcFileAdapter.RecipeLiveData != null)
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
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.errorCode} | {libraryError.exception} | {libraryError.localizationKey}");
|
|
return BadRequest(libraryError.localizationKey);
|
|
}
|
|
|
|
// SE HO una section != null/empty --> salvo come modificata...
|
|
try
|
|
{
|
|
NcFileAdapter.upsRecipeOverview(section, RecipeCatStatus.ChangedOk);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
ThermoActiveLogger.LogError($"Error on set recipe overview | section: {section}{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
// 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]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult CancelEdit()
|
|
{
|
|
if (NcFileAdapter.RecipeLiveData != null)
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.IsError())
|
|
{
|
|
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]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult Load(string newName)
|
|
{
|
|
|
|
// chiamo metodo di lettura...
|
|
bool fatto = NcFileAdapter.LoadRecipe(newName);
|
|
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>
|
|
/// Load tempalte recipe from file and save as new current...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("new"), HttpPut]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult NewRecipe()
|
|
{
|
|
// chiamo metodo di lettura...
|
|
bool fatto = NcFileAdapter.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]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult Save()
|
|
{
|
|
// recupero dati utente loggato
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.FirstOrDefault(c => c.Type == USER_ID_KEY);
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
// salvo utente!
|
|
using (UsersController usersController = new UsersController())
|
|
{
|
|
var userData = usersController.GetUserInfo(Convert.ToInt32(userId.Value));
|
|
NcFileAdapter.RecipeLiveData.UserSave = userData.Id.ToString();
|
|
}
|
|
|
|
// 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]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult SaveAs(string newName)
|
|
{
|
|
// recupero dati utente loggato
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.FirstOrDefault(c => c.Type == USER_ID_KEY);
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
// salvo utente!
|
|
using (UsersController usersController = new UsersController())
|
|
{
|
|
var userData = usersController.GetUserInfo(Convert.ToInt32(userId.Value));
|
|
NcFileAdapter.RecipeLiveData.UserSave = userData.Id.ToString();
|
|
}
|
|
|
|
// 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...
|
|
NcFileAdapter.RecipeLiveData.RecipeParameters = currParams;
|
|
// e salvo su disco
|
|
NcFileAdapter.SaveRecipe(newName, NcFileAdapter.RecipeLiveData);
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
/// <summary>
|
|
/// Save current recipe from PLC to Template
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("saveTemplate"), HttpPut]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult SaveTemplate()
|
|
{
|
|
// 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...
|
|
NcFileAdapter.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);
|
|
}
|
|
NcFileAdapter.RecipeLiveData.ChannelSetpoints = currChSet;
|
|
|
|
|
|
// e salvo su disco
|
|
NcFileAdapter.SaveRecipeTemplate();
|
|
|
|
// ritorno solo fatto!
|
|
return Ok();
|
|
}
|
|
|
|
[Route("uploadImage"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public async Task<IHttpActionResult> UpdloadImage()
|
|
{
|
|
bool imageUploaded = false;
|
|
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
}
|
|
|
|
List<FormItem> formItems = await MultipartHandler.ManageMultiPartFileAsync(Request);
|
|
|
|
foreach (FormItem item in formItems)
|
|
{
|
|
if (item.IsAFile)
|
|
{
|
|
if (item.ParameterName == "image" || item.MediaType.StartsWith("image/"))
|
|
{
|
|
string filePath = item.FileName;
|
|
imageUploaded = NcFileAdapter.SaveRecipeImage(item.FileName, item.Data);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("updateNote"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public IHttpActionResult UpdateNote(string recipeNotes)
|
|
{
|
|
// salvo note...
|
|
if (!string.IsNullOrEmpty(recipeNotes))
|
|
{
|
|
NcFileAdapter.RecipeLiveData.recipeNotes = recipeNotes.Trim();
|
|
}
|
|
// e salvo su disco
|
|
NcFileAdapter.SaveRecipe(NcFileAdapter.RecipeLiveData.RecipeName, NcFileAdapter.RecipeLiveData);
|
|
return Ok();
|
|
}
|
|
|
|
[Route("saveAll"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.RECIPE_MANAGER, Action = ACTIONS.READ)]
|
|
public async Task<IHttpActionResult> SaveAll(string newName, string recipeNotes, double estimTimeSec)
|
|
{
|
|
// recupero dati utente loggato
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.FirstOrDefault(c => c.Type == USER_ID_KEY);
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
// salvo utente!
|
|
using (UsersController usersController = new UsersController())
|
|
{
|
|
var userData = usersController.GetUserInfo(Convert.ToInt32(userId.Value));
|
|
NcFileAdapter.RecipeLiveData.UserSave = userData.Id.ToString();
|
|
}
|
|
|
|
// salvo note...
|
|
if (!string.IsNullOrEmpty(recipeNotes))
|
|
{
|
|
NcFileAdapter.RecipeLiveData.recipeNotes = recipeNotes.Trim();
|
|
}
|
|
|
|
bool imageUploaded = false;
|
|
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
}
|
|
|
|
List<FormItem> formItems = await MultipartHandler.ManageMultiPartFileAsync(Request);
|
|
|
|
foreach (FormItem item in formItems)
|
|
{
|
|
if (item.IsAFile)
|
|
{
|
|
if (item.ParameterName == "image" || item.MediaType.StartsWith("image/"))
|
|
{
|
|
string filePath = item.FileName;
|
|
imageUploaded = NcFileAdapter.SaveRecipeImage(item.FileName, item.Data);
|
|
}
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(newName))
|
|
{
|
|
// recupero i dati LIVE dei parametri HMI della ricetta...
|
|
CmsError libraryError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
|
|
if (libraryError.IsError())
|
|
{
|
|
ThermoActiveLogger.LogError($"SaveAll 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);
|
|
}
|
|
// salvo parametri
|
|
NcFileAdapter.RecipeLiveData.RecipeParameters = currParams;
|
|
|
|
// ora salvo il dato del TC stimato se presente
|
|
if (estimTimeSec > 0)
|
|
{
|
|
NcFileAdapter.RecipeLiveData.TC_last = estimTimeSec;
|
|
}
|
|
// e salvo su disco
|
|
NcFileAdapter.SaveRecipe(newName, NcFileAdapter.RecipeLiveData);
|
|
}
|
|
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...
|
|
NcFileAdapter.RecipeLiveData.RecipeParameters = currParams;
|
|
// e salvo su disco
|
|
NcFileAdapter.SaveRecipeCurrent();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
ThermoActiveLogger.LogError($"Recipe | SaveCurrentRecipeParams exception | {exc}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// write current recipe to PLC
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static CmsError WriteCurrentRecipeToPlc()
|
|
{
|
|
CmsError checkError = NO_ERROR;
|
|
|
|
// 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 NcFileAdapter.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 NcFileAdapter.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;
|
|
}
|
|
}
|
|
} |