Files
cms_thermo_active/Thermo.Active/Controllers/WebApi/UnderTheHoodController.cs
T
2021-03-23 12:09:10 +01:00

408 lines
14 KiB
C#

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.DatabaseModels;
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 : aBaseApiController // ApiController
{
#if false
/// <summary>
/// Oggetto adapter condiviso da WebAPI
/// </summary>
protected static NcAdapter ncAdapter = new NcAdapter();
#endif
[ResponseType(typeof(DTOCycleLog))]
[Route("CycleLogRefresh"), HttpGet]
public IHttpActionResult RefreshCycleLog()
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"ncAdapter Not connected! | RefreshCycleLog | {libraryError.exception}");
}
libraryError = ncAdapter.RefreshCycleLog(out int numRec);
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"RefreshCycleLog error | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
return Ok(numRec);
}
[ResponseType(typeof(DTOCycleLog))]
[Route("CycleLog"), HttpGet]
public IHttpActionResult GetCycleLog(int from, int to)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
DTOCycleLog currCycleLog = new DTOCycleLog();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"ncAdapter Not connected! | GetCycleLog | {libraryError.exception}");
}
// recupero dati paginati
libraryError = ncAdapter.GetCycleLog(from, to, out currCycleLog);
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"GetCycleLog error | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
return Ok(currCycleLog);
}
[ResponseType(typeof(bool))]
[Route("GetAxesAdvMode"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult GetAxesAdvMode()
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ForceChAO | {libraryError.exception}");
return InternalServerError();
}
// chiamo scrittura
bool advMode = false;
libraryError = ncAdapter.GetAxesAdvMode(out advMode);
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"GetCycleLog error | {libraryError.exception}");
return BadRequest(libraryError.localizationKey);
}
// ritorno solo fatto!
return Ok(advMode);
}
[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<IoItemDigi> 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<int, bool> parametersList = new Dictionary<int, bool>();
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<IoItemAnal> 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<int, int> parametersList = new Dictionary<int, int>();
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<int> 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();
}
}
/// <summary>
/// Reset "force bit" on selected channels
/// </summary>
/// <param name="parametersList">List of channels index, zero based</param>
/// <returns></returns>
[Route("io_reset_ch_ao"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult ResetChAO(List<int> 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("SendAxisCommand"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult SendAxisCommand(int AxisId, Model.DTOModels.ThAxes.AxisCommand currCommand, double TargetPos)
{
if (AxisId > 0)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ForceChAO | {libraryError.exception}");
return InternalServerError();
}
// chiamo scrittura
ncAdapter.SendAxisCommand(AxisId, currCommand, TargetPos);
// ritorno solo fatto!
return Ok();
}
else
{
ThermoActiveLogger.LogError($"SendAxisCommand axis 0");
return BadRequest();
}
}
[Route("SetAxesAdvMode"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult SetAxesAdvMode(bool value)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.IsError())
{
ThermoActiveLogger.LogError($"NC Not connected! | ForceChAO | {libraryError.exception}");
return InternalServerError();
}
// chiamo scrittura
ncAdapter.SetAxesAdvMode(value);
// 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...
var expires = RedisController.SetFastIoSample(value);
// ritorno solo fatto!
return Ok(expires);
}
[ResponseType(typeof(List<HistorySheetModel>))]
[Route("SheetHistory"), HttpGet]
public IHttpActionResult GetSheetHistory()
{
using (HistorySheetsController hsc = new HistorySheetsController())
{
List<HistorySheetModel> models = hsc.GetData();
return Ok(models);
}
}
[ResponseType(typeof(List<HistorySheetModel>))]
[Route("SheetHistoryFiltered"), HttpGet]
public IHttpActionResult GetSheetHistoryFiltered(int start, int number)
{
using (HistorySheetsController hsc = new HistorySheetsController())
{
List<HistorySheetModel> models = hsc.GetData(start, number);
return Ok(models);
}
}
[ResponseType(typeof(int))]
[Route("SheetHistoryCount"), HttpGet]
public IHttpActionResult GetSheetHistoryCount()
{
using (HistorySheetsController hsc = new HistorySheetsController())
{
return Ok(hsc.count());
}
}
}
}