diff --git a/Thermo.Active.Config/ServerConfig.cs b/Thermo.Active.Config/ServerConfig.cs index 732926fd..9f479ce2 100644 --- a/Thermo.Active.Config/ServerConfig.cs +++ b/Thermo.Active.Config/ServerConfig.cs @@ -62,7 +62,7 @@ namespace Thermo.Active.Config public static List RiskChannelConfig; public static List RiskBoardConfig; - public static LiveData RecipeLiveData; + public static LiveData RecipeLiveData = new LiveData(); } } \ No newline at end of file diff --git a/Thermo.Active.Config/ServerConfigController.cs b/Thermo.Active.Config/ServerConfigController.cs index f3172c2b..d106b195 100644 --- a/Thermo.Active.Config/ServerConfigController.cs +++ b/Thermo.Active.Config/ServerConfigController.cs @@ -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 diff --git a/Thermo.Active.Core/ThreadsFunctions.cs b/Thermo.Active.Core/ThreadsFunctions.cs index 2731f119..1bbf73df 100644 --- a/Thermo.Active.Core/ThreadsFunctions.cs +++ b/Thermo.Active.Core/ThreadsFunctions.cs @@ -87,6 +87,128 @@ public static class ThreadsFunctions } } + /// + /// Manage status/command words for actions + /// + 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(); + } + } + + /// + /// Manage action for conf request + /// + 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(); + } + } + /// + /// Manage action for conf request + /// + 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(); diff --git a/Thermo.Active.Core/ThreadsHandler.cs b/Thermo.Active.Core/ThreadsHandler.cs index 5b7b22b3..8f9c137a 100644 --- a/Thermo.Active.Core/ThreadsHandler.cs +++ b/Thermo.Active.Core/ThreadsHandler.cs @@ -11,6 +11,9 @@ namespace Thermo.Active.Core private static List ThreadsFunctionsList = new List { ThreadsFunctions.ManageWatchdog, + ThreadsFunctions.ManageStatusCommand, + ThreadsFunctions.ManageConfRequest, + ThreadsFunctions.ManageProdUpdate, ThreadsFunctions.ReadAlarms, ThreadsFunctions.ReadPowerOnData, ThreadsFunctions.StatThread, diff --git a/Thermo.Active.NC/NcAdapter.cs b/Thermo.Active.NC/NcAdapter.cs index 529de3b2..3692288b 100644 --- a/Thermo.Active.NC/NcAdapter.cs +++ b/Thermo.Active.NC/NcAdapter.cs @@ -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 statusCmd = new List(); + /// + /// Check bit on word + /// + /// + /// + /// + 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(); + 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 prevRecipe); + if (cmsError.IsError()) + return cmsError; + // save parameters to PLC!!! + Dictionary updtRecipe = new Dictionary(); + 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; } + /// + /// Get area for material from parameters list + /// + /// dictionary X/Y and double value + /// + public CmsError GetWarmMaterialArea(out Dictionary currAreaPerc) + { + CmsError cmsError = NO_ERROR; + currAreaPerc = new Dictionary(); + DTORecipeParam currParam = new DTORecipeParam(); + var currRecipe = new Dictionary(); + + // 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; + } /// /// Write all warmers load for recipe diff --git a/Thermo.Active/Controllers/WebApi/RecipeController.cs b/Thermo.Active/Controllers/WebApi/RecipeController.cs index bed83865..eca92022 100644 --- a/Thermo.Active/Controllers/WebApi/RecipeController.cs +++ b/Thermo.Active/Controllers/WebApi/RecipeController.cs @@ -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 prevRecipe); - if (cmsError.IsError()) - return BadRequest(cmsError.localizationKey); - - Dictionary updtRecipe = new Dictionary(); - - 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 prevRecipe); + if (cmsError.IsError()) + return BadRequest(cmsError.localizationKey); + + Dictionary updtRecipe = new Dictionary(); + + 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 currRecipe); - if (cmsError.IsError()) - return BadRequest(cmsError.localizationKey); - - // rileggo la ricetta - var currParams = new Dictionary(); - 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 currRecipe); + if (cmsError.IsError()) + return BadRequest(cmsError.localizationKey); + + // rileggo la ricetta + var currParams = new Dictionary(); + 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 currRecipe); - if (cmsError.IsError()) - return BadRequest(cmsError.localizationKey); - - var currParams = new Dictionary(); - 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 currRecipe); + if (cmsError.IsError()) + return BadRequest(cmsError.localizationKey); + + var currParams = new Dictionary(); + 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 /// private static void saveCurrentRecipeParams(Dictionary 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() diff --git a/Thermo.Active/Controllers/WebApi/WarmersController.cs b/Thermo.Active/Controllers/WebApi/WarmersController.cs index 4e989bfc..8294ed1e 100644 --- a/Thermo.Active/Controllers/WebApi/WarmersController.cs +++ b/Thermo.Active/Controllers/WebApi/WarmersController.cs @@ -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 currAreaPerc); + if (cmsError.IsError()) + return BadRequest(cmsError.localizationKey); + + return Ok(currAreaPerc); + } + } [Route("update"), HttpPut] public IHttpActionResult WriteSetpoints(Dictionary channelsLoad) diff --git a/Thermo.Active/Properties/AssemblyInfo.cs b/Thermo.Active/Properties/AssemblyInfo.cs index 9425cccd..32016308 100644 --- a/Thermo.Active/Properties/AssemblyInfo.cs +++ b/Thermo.Active/Properties/AssemblyInfo.cs @@ -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")]