Merge branch 'feature/add/risk' into develop

This commit is contained in:
Samuele Locatelli
2020-06-12 17:18:56 +02:00
8 changed files with 463 additions and 86 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ namespace Thermo.Active.Config
public static List<RiskChannelModel> RiskChannelConfig;
public static List<RiskBoardModel> RiskBoardConfig;
public static LiveData RecipeLiveData;
public static LiveData RecipeLiveData = new LiveData();
}
}
@@ -856,6 +856,8 @@ namespace Thermo.Active.Config
}
catch
{ }
// write template to current data...
File.WriteAllText(LIVE_RECIPE_PATH, rawData);
answ = true;
}
// rendo se fatto
+122
View File
@@ -87,6 +87,128 @@ public static class ThreadsFunctions
}
}
/// <summary>
/// Manage status/command words for actions
/// </summary>
public static void ManageStatusCommand()
{
NcAdapter ncAdapter = new NcAdapter();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
// Check if client is connected
if (ncAdapter.numericalControl.NC_IsConnected())
{
// Manage status command
libraryError = ncAdapter.ManageStatusCommand();
}
else
RestoreConnection();
sw.Stop();
//Update thread timer
UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds);
// Wait
Thread.Sleep(CalcSleepTime(250, (int)sw.ElapsedMilliseconds));
}
}
catch (ThreadAbortException ex)
{
ncAdapter.Dispose();
}
}
/// <summary>
/// Manage action for conf request
/// </summary>
public static void ManageConfRequest()
{
NcAdapter ncAdapter = new NcAdapter();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
// Check if client is connected
if (ncAdapter.numericalControl.NC_IsConnected())
{
// Manage status command
libraryError = ncAdapter.ManageConfRequest();
}
else
RestoreConnection();
sw.Stop();
//Update thread timer
UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds);
// Wait
Thread.Sleep(CalcSleepTime(2000, (int)sw.ElapsedMilliseconds));
}
}
catch (ThreadAbortException ex)
{
ncAdapter.Dispose();
}
}
/// <summary>
/// Manage action for conf request
/// </summary>
public static void ManageProdUpdate()
{
NcAdapter ncAdapter = new NcAdapter();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
// Check if client is connected
if (ncAdapter.numericalControl.NC_IsConnected())
{
// Manage status command
libraryError = ncAdapter.ManageProdUpdate();
}
else
RestoreConnection();
sw.Stop();
//Update thread timer
UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds);
// Wait
Thread.Sleep(CalcSleepTime(1000, (int)sw.ElapsedMilliseconds));
}
}
catch (ThreadAbortException ex)
{
ncAdapter.Dispose();
}
}
public static void ReadAlarms()
{
NcAdapter ncAdapter = new NcAdapter();
+3
View File
@@ -11,6 +11,9 @@ namespace Thermo.Active.Core
private static List<Action> ThreadsFunctionsList = new List<Action>
{
ThreadsFunctions.ManageWatchdog,
ThreadsFunctions.ManageStatusCommand,
ThreadsFunctions.ManageConfRequest,
ThreadsFunctions.ManageProdUpdate,
ThreadsFunctions.ReadAlarms,
ThreadsFunctions.ReadPowerOnData,
ThreadsFunctions.StatThread,
+203
View File
@@ -28,6 +28,7 @@ namespace Thermo.Active.NC
{
public class NcAdapter : IDisposable
{
public NcThermo numericalControl;
public NcAdapter() =>
@@ -66,6 +67,90 @@ namespace Thermo.Active.NC
return null;
}
#region StatusCommand words
// variabili delle richieste da PLC: tutte nella status command
private static List<ushort> statusCmd = new List<ushort>();
/// <summary>
/// Check bit on word
/// </summary>
/// <param name="value"></param>
/// <param name="numBit"></param>
/// <returns></returns>
protected static bool checkBitOnWord(ushort value, int numBit)
{
bool answ = false;
try
{
ushort pow = (ushort)(1 << numBit);
answ = (value & pow) == pow;
}
catch
{ }
return answ;
}
private static bool ThermoReqConfWarmerStr
{
get
{
bool answ = false;
if (statusCmd.Count > 0)
{
answ = checkBitOnWord(statusCmd[0], 7);
}
return answ;
}
}
//private static bool ThermoReqConfWarmerAck
//{
// get
// {
// return checkBitOnWord(statusCmd[1], 7);
// }
//}
private static bool ThermoReqConfRecipeStr
{
get
{
bool answ = false;
if (statusCmd.Count > 0)
{
answ = checkBitOnWord(statusCmd[0], 8);
}
return answ;
}
}
//private static bool ThermoReqConfRecipeAck
//{
// get
// {
// return checkBitOnWord(statusCmd[1], 8);
// }
//}
private static bool ThermoProdUpdatedStr
{
get
{
bool answ = false;
if (statusCmd.Count > 0)
{
answ = checkBitOnWord(statusCmd[0], 3);
}
return answ;
}
}
//private static bool ThermoProdUpdatedAck
//{
// get
// {
// return checkBitOnWord(statusCmd[1], 3);
// }
//}
#endregion
#region Read Data
#region Axes
@@ -168,6 +253,84 @@ namespace Thermo.Active.NC
return numericalControl.PLC_RWManageWatchdog();
}
public CmsError ManageStatusCommand()
{
CmsError cmsError = NO_ERROR;
// recupero i dati
//statusCmd = new List<ushort>();
cmsError = numericalControl.PLC_RStatusCommand(ref statusCmd);
if (cmsError.IsError())
return cmsError;
// restituisco errore
return cmsError;
}
public CmsError ManageConfRequest()
{
CmsError cmsError = NO_ERROR;
// controllo SE HO richieste di configurazione
if (ThermoReqConfWarmerStr)
{
// se si in questo caso scrivo configurazione...
WriteRecipeWarmConfig();
// Ack !
cmsError = numericalControl.PLC_WAckConfRiskRequest();
}
if (ThermoReqConfRecipeStr)
{
// copy data to PLC
cmsError = ReadFullRecipe(out Dictionary<string, DTORecipeParam> prevRecipe);
if (cmsError.IsError())
return cmsError;
// 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
{
return NOT_FOUND_ERROR;
}
}
// se si in questo caso scrivo configurazione attuale...
WriteRecipeParams(updtRecipe);
// Ack !
cmsError = numericalControl.PLC_WAckConfRecipeRequest();
}
return cmsError;
}
public CmsError ManageProdUpdate()
{
CmsError cmsError = NO_ERROR;
// controllo SE HO segnalazioni produzione
if (ThermoProdUpdatedStr)
{
// se si in questo caso leggo il record produzione...
/// e quindi lo gestisco (salvataggio su DB)
// do il bit di ack
cmsError = numericalControl.PLC_WAckProdUpdate();
}
else
{
// se è zero e il mio ack fosse 1 --> resetto il mio!
}
return cmsError;
}
public CmsError GetNcGenericData(out DTONcGenericDataModel genericData)
{
genericData = new DTONcGenericDataModel(MachineConfig.Model);
@@ -1957,6 +2120,46 @@ namespace Thermo.Active.NC
// restituisco cod errore se trovato
return cmsError;
}
/// <summary>
/// Get area for material from parameters list
/// </summary>
/// <param name="currAreaPerc">dictionary X/Y and double value</param>
/// <returns></returns>
public CmsError GetWarmMaterialArea(out Dictionary<string, double> currAreaPerc)
{
CmsError cmsError = NO_ERROR;
currAreaPerc = new Dictionary<string, double>();
DTORecipeParam currParam = new DTORecipeParam();
var currRecipe = new Dictionary<string, DTORecipeParam>();
// read recipe
cmsError = ReadRecipeData(false, true, out currRecipe);
if (cmsError.IsError())
return cmsError;
// use 2 fixed data for calculus: general_sizes_sheet_dim_x / general_sizes_sheet_dim_y
currAreaPerc.Add("X", getDimRatio(currRecipe["general_sizes_sheet_dim_x"]));
currAreaPerc.Add("Y", getDimRatio(currRecipe["general_sizes_sheet_dim_y"]));
// restituisco cod errore se trovato
return cmsError;
}
protected double getDimRatio(DTORecipeParam singlePar)
{
double ratio = 1;
if (singlePar!=null)
{
try
{
if (singlePar.Range.Max > 0)
ratio = singlePar.SetpointPLC / singlePar.Range.Max;
}
catch
{ }
}
return ratio;
}
/// <summary>
/// Write all warmers load for recipe
@@ -60,41 +60,49 @@ namespace Thermo.Active.Controllers.WebApi
{
if (parametersList != null)
{
using (NcAdapter ncAdapter = new NcAdapter())
if (RecipeLiveData != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// read recipe!
CmsError cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> prevRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
Dictionary<string, DTORecipeParam> updtRecipe = new Dictionary<string, DTORecipeParam>();
foreach (var item in parametersList)
using (NcAdapter ncAdapter = new NcAdapter())
{
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
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// read recipe!
CmsError cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> prevRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.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
{
return NotFound();
}
}
// scrivo sul PLC
ncAdapter.WriteRecipeParams(updtRecipe);
// ritorno solo fatto!
return Ok();
}
// scrivo sul PLC
ncAdapter.WriteRecipeParams(updtRecipe);
// ritorno solo fatto!
return Ok();
}
else
{
// non pronto!
return InternalServerError();
}
}
else
@@ -110,34 +118,42 @@ namespace Thermo.Active.Controllers.WebApi
[Route("confirm"), HttpPut]
public IHttpActionResult ConfirmEdit()
{
using (NcAdapter ncAdapter = new NcAdapter())
if (RecipeLiveData != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// scrivo sul PLC il comando conferma!
CmsError cmsError = ncAdapter.ConfirmRecipeData(true);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// recupero i dati LIVE dei parametri HMI della ricetta...
cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// rileggo la ricetta
var currParams = new Dictionary<string, double>();
foreach (var item in currRecipe)
using (NcAdapter ncAdapter = new NcAdapter())
{
currParams.Add(item.Key, item.Value.SetpointHMI);
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// scrivo sul PLC il comando conferma!
CmsError cmsError = ncAdapter.ConfirmRecipeData(true);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// recupero i dati LIVE dei parametri HMI della ricetta...
cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.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();
}
saveCurrentRecipeParams(currParams);
// ritorno solo fatto!
return Ok();
}
else
{
// non pronto!
return InternalServerError();
}
}
@@ -148,34 +164,42 @@ namespace Thermo.Active.Controllers.WebApi
[Route("cancel"), HttpPut]
public IHttpActionResult CancelEdit()
{
using (NcAdapter ncAdapter = new NcAdapter())
if (RecipeLiveData != null)
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// scrivo sul PLC il comando annula!
CmsError cmsError = ncAdapter.ConfirmRecipeData(false);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// recupero i dati LIVE dei parametri HMI della ricetta...
cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
var currParams = new Dictionary<string, double>();
foreach (var item in currRecipe)
using (NcAdapter ncAdapter = new NcAdapter())
{
currParams.Add(item.Key, item.Value.SetpointPLC);
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// scrivo sul PLC il comando annula!
CmsError cmsError = ncAdapter.ConfirmRecipeData(false);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// recupero i dati LIVE dei parametri HMI della ricetta...
cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.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();
}
// ora salvo ANCHE i dati live...
saveCurrentRecipeParams(currParams);
// ritorno solo fatto!
return Ok();
}
else
{
// non pronto!
return InternalServerError();
}
}
@@ -344,10 +368,15 @@ namespace Thermo.Active.Controllers.WebApi
/// <param name="currParams"></param>
private static void saveCurrentRecipeParams(Dictionary<string, double> currParams)
{
// ora salvo ANCHE i dati live...
RecipeLiveData.RecipeParameters = currParams;
// e salvo su disco
ServerConfigController.SaveRecipeCurrent();
try
{
// ora salvo ANCHE i dati live...
RecipeLiveData.RecipeParameters = currParams;
// e salvo su disco
ServerConfigController.SaveRecipeCurrent();
}
catch
{ }
}
protected static CmsError writeCurrentRecipeToPlc()
@@ -55,6 +55,24 @@ namespace Thermo.Active.Controllers.WebApi
}
}
[Route("area"), HttpGet]
public IHttpActionResult GetCurrentAreaPerc()
{
using (NcAdapter ncAdapter = new NcAdapter())
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// recupera aree % X / Y
CmsError cmsError = ncAdapter.GetWarmMaterialArea(out Dictionary<string,double> currAreaPerc);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
return Ok(currAreaPerc);
}
}
[Route("update"), HttpPut]
public IHttpActionResult WriteSetpoints(Dictionary<int, int> channelsLoad)
+1 -1
View File
@@ -31,4 +31,4 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.6.2")]
[assembly: AssemblyVersion("0.6.3")]