using CMS_CORE_Library.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
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.Web.Http.Description;
using System.Windows.Media.Animation;
using TeamDev.SDK.MVVM;
using Thermo.Active.Config;
using Thermo.Active.Database.Controllers;
using Thermo.Active.Model.DTOModels;
using Thermo.Active.Model.DTOModels.ThIO;
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/underthehood")]
public class UnderTheHoodController : ApiController
{
///
/// Oggetto adapter condiviso da WebAPI
///
protected static NcAdapter ncAdapter = new NcAdapter();
[ResponseType(typeof(DTOCycleLog))]
[Route("cyclelog"), HttpGet]
public IHttpActionResult GetCycleLog()
{
// // Try connection
CmsError libraryError = ncAdapter.Connect();
DTOCycleLog currCycleLog = new DTOCycleLog();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"ncAdapter Not connected! | GetCycleLog | {libraryError.exception}");
}
libraryError = ncAdapter.GetCycleLog(out Dictionary cycleLog);
foreach (var item in cycleLog)
{
currCycleLog.events.Add(new CycleEvent() { dtEvent=item.Key, code=item.Value });
}
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"GetCycleLog error | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
return Ok(currCycleLog);
}
[ResponseType(typeof(DTOChannelsIO))]
[Route("channels_io"), HttpGet]
public IHttpActionResult GetChannelsIO()
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | GetChannelsIO | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
// leggo dati gauges
libraryError = ncAdapter.ReadFullIO(out DTOChannelsIO ChannelsIO);
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"GetChannelsIO error | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
// ritorno!
return Ok(ChannelsIO);
}
[ResponseType(typeof(DTOChannelsIOVal))]
[Route("channels_io_val"), HttpGet]
public IHttpActionResult GetChannelsIoVal()
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | GetChannelsIoVal | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
// leggo dati gauges
libraryError = ncAdapter.ReadValIO(out DTOChannelsIOVal ChannelsIOVal);
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"GetChannelsIoVal error | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
// ritorno!
return Ok(ChannelsIOVal);
}
[Route("io_force_ch_do"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult ForceChDO(List updVal)
{
if (updVal != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ForceChDO | {libraryError.exception}");
return InternalServerError();
}
if (updVal.Count > 0)
{
// converto
Dictionary parametersList = new Dictionary();
foreach (var item in updVal)
{
parametersList.Add(item.Id, item.value);
}
// scrivo sul PLC con i parametri specificati
ncAdapter.Write_IO_DO_ToPLC(parametersList);
}
// ritorno solo fatto!
return Ok();
}
else
{
ThermoActiveLogger.LogError($"ForceChDO updatedVal null | Empty Parameters");
return BadRequest();
}
}
[Route("io_force_ch_ao"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult ForceChAO(List updVal)
{
if (updVal != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ForceChAO | {libraryError.exception}");
return InternalServerError();
}
if (updVal.Count > 0)
{
Dictionary parametersList = new Dictionary();
foreach (var item in updVal)
{
parametersList.Add(item.Id, item.value);
}
// scrivo sul PLC con i parametri specificati
ncAdapter.Write_IO_AO_ToPLC(parametersList);
}
// ritorno solo fatto!
return Ok();
}
else
{
ThermoActiveLogger.LogError($"ForceChAO parametersList null | Empty Parameters");
return BadRequest();
}
}
[Route("io_reset_ch_do"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult ResetChDO(List parametersList)
{
if (parametersList != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ResetChDO | {libraryError.exception}");
return InternalServerError();
}
if (parametersList.Count > 0)
{
// scrivo sul PLC con i parametri specificati x ritardo/raggruppamento
ncAdapter.WriteReset_IO_DO_ToPLC(parametersList);
}
// ritorno solo fatto!
return Ok();
}
else
{
ThermoActiveLogger.LogError($"ResetChDO parametersList null | Empty Parameters");
return BadRequest();
}
}
///
/// Reset "force bit" on selected channels
///
/// List of channels index, zero based
///
[Route("io_reset_ch_ao"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult ResetChAO(List parametersList)
{
if (parametersList != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ResetChAO | {libraryError.exception}");
return InternalServerError();
}
if (parametersList.Count > 0)
{
// scrivo sul PLC con i parametri specificati x ritardo/raggruppamento
ncAdapter.WriteReset_IO_AO_ToPLC(parametersList);
}
// ritorno solo fatto!
return Ok();
}
else
{
ThermoActiveLogger.LogError($"ResetChAO parametersList null | Empty Parameters");
return BadRequest();
}
}
[Route("io_reset_all_ch"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult ResetAllCh()
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ResetAllCh | {libraryError.exception}");
return InternalServerError();
}
// scrivo sul PLC con i parametri specificati x ritardo/raggruppamento
ncAdapter.WriteReset_IO_ALL_ToPLC();//parametersList);
// ritorno solo fatto!
return Ok();
}
[Route("io_sample_fast"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult SetFastSample(bool value)
{
// imposta (su redis) campionamento RAPIDO/lento x IO...
RedisController.FastIoSample = value;
// ritorno solo fatto!
return Ok();
}
}
}