diff --git a/Thermo.Active.Config/ServerConfig.cs b/Thermo.Active.Config/ServerConfig.cs index ca673d75..089ef523 100644 --- a/Thermo.Active.Config/ServerConfig.cs +++ b/Thermo.Active.Config/ServerConfig.cs @@ -62,7 +62,6 @@ namespace Thermo.Active.Config public static List RiskChannelConfig; public static List RiskBoardConfig; - 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 7b5efcb9..bf1d76ce 100644 --- a/Thermo.Active.Config/ServerConfigController.cs +++ b/Thermo.Active.Config/ServerConfigController.cs @@ -57,28 +57,6 @@ namespace Thermo.Active.Config ExceptionManager.ManageError(ERROR_LEVEL.FATAL, message, true); } } - public static void ReadLastRecipe() - { - try - { - ReadLiveData(); - } - catch (XmlException ex) - { - ExceptionManager.ManageError(ERROR_LEVEL.FATAL, - "Error while reading file: " + ex.SourceUri + - "\n Error: " + ex.Message, - true - ); - } - catch (Exception ex) - { - var message = ex.Message; - if (ex.InnerException != null) - message += "\n" + ex.InnerException.Message; - ExceptionManager.ManageError(ERROR_LEVEL.FATAL, message, true); - } - } private static XDocument GetXmlHandlerWithValidator(string configSchemaFilePath, string configFilePath, bool isFullPath = false) { @@ -846,180 +824,6 @@ namespace Thermo.Active.Config .Select(x => x.Value) .ToList(); } - /// - /// Try to load live data from json persistence file - /// - public static bool ReadLiveData() - { - bool answ = false; - if (File.Exists(LIVE_RECIPE_PATH)) - { - // load all text data - var rawData = File.ReadAllText(LIVE_RECIPE_PATH); - try - { - // deserialize to object - RecipeLiveData = JsonConvert.DeserializeObject(rawData); - } - catch - { } - answ = true; - } - else - { - // reload from template... - var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH); - try - { - // deserialize to object - RecipeLiveData = JsonConvert.DeserializeObject(rawData); - } - catch - { } - // salva current - SaveRecipeCurrent(); - answ = true; - } - // rendo se fatto - return answ; - } - /// - /// Try to load selected recipe to live data (memory and json persistence file) - /// - public static bool LoadRecipe(string filePath) - { - bool answ = false; - - // check file extension - string fileName = Path.GetFileName(filePath); - if (!fileName.EndsWith(".json")) - { - fileName += ".json"; - filePath += ".json"; - } - - // check filePath... - if (!filePath.Contains(RECIPE_DIRECTORY) && filePath != RECIPE_TEMPLATE_PATH) - { - // aggiungo base path! - filePath = RECIPE_DIRECTORY + filePath; - } - if (File.Exists(filePath)) - { - answ = true; - - // load all text data - var rawData = File.ReadAllText(filePath); - try - { - // deserialize to object - RecipeLiveData = JsonConvert.DeserializeObject(rawData); - } - catch - { } - // update current live data! - SaveRecipeCurrent(); - } - // rendo se fatto - return answ; - } - /// - /// Try to load template recipe - /// - public static bool LoadTemplate() - { - bool answ = false; - // check filePath... - if (File.Exists(RECIPE_TEMPLATE_PATH)) - { - answ = true; - - // load all text data - var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH); - try - { - // deserialize to object - RecipeLiveData = JsonConvert.DeserializeObject(rawData); - // update NAME - RecipeLiveData.RecipeName = $"{DateTime.Now:yyyyMMss_HHmmss}.json"; - } - catch - { } - // update current live data! - SaveRecipeCurrent(); - } - // rendo se fatto - return answ; - } - /// - /// Try to write live data to json persistence file - /// - public static bool SaveRecipeCurrent() - { - bool answ = false; - try - { - answ = true; - // serialize - string rawData = JsonConvert.SerializeObject(RecipeLiveData); - // save live! - var dir = Path.GetDirectoryName(LIVE_RECIPE_PATH); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - File.WriteAllText(LIVE_RECIPE_PATH, rawData); - } - catch - { } - // rendo se fatto - return answ; - } - /// - /// Try to save live recipe as NEW template - /// - public static bool SaveRecipeTemplate() - { - - RecipeLiveData.RecipeName = "template.json"; - return SaveRecipe(RECIPE_TEMPLATE_PATH); - } - /// - /// Try to save live recipe to selected filePath - /// - public static bool SaveRecipe(string filePath) - { - bool answ = false; - try - { - answ = true; - string fileName = Path.GetFileName(filePath); - if (!fileName.EndsWith(".json")) - { - fileName += ".json"; - filePath += ".json"; - } - // fix name! - RecipeLiveData.RecipeName = fileName; - // serialize - string rawData = JsonConvert.SerializeObject(RecipeLiveData); - // save live! - File.WriteAllText(LIVE_RECIPE_PATH, rawData); - // check filePath... - if (!filePath.Contains(RECIPE_DIRECTORY) && filePath != RECIPE_TEMPLATE_PATH) - { - // aggiungo base path! - filePath = RECIPE_DIRECTORY + filePath; - } - // save! - File.WriteAllText(filePath, rawData); - } - catch - { } - // rendo se fatto - return answ; - } - public static string CalculateHash(string filename) { using (var sha = SHA1.Create()) diff --git a/Thermo.Active.Config/Thermo.Active.Config.csproj b/Thermo.Active.Config/Thermo.Active.Config.csproj index bfee6bae..84b24ca5 100644 --- a/Thermo.Active.Config/Thermo.Active.Config.csproj +++ b/Thermo.Active.Config/Thermo.Active.Config.csproj @@ -55,7 +55,6 @@ Designer PreserveNewest - diff --git a/Thermo.Active.Config/LiveData.cs b/Thermo.Active.Model/DTOModels/ThRecipe/LiveData.cs similarity index 76% rename from Thermo.Active.Config/LiveData.cs rename to Thermo.Active.Model/DTOModels/ThRecipe/LiveData.cs index ea8d785f..d223d693 100644 --- a/Thermo.Active.Config/LiveData.cs +++ b/Thermo.Active.Model/DTOModels/ThRecipe/LiveData.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Thermo.Active.Config +namespace Thermo.Active.Model.DTOModels.ThRecipe { /// /// Live data for Thermo Active @@ -19,6 +19,9 @@ namespace Thermo.Active.Config /// Dictionary of all channels and relative setpoints /// public Dictionary ChannelSetpoints; - + /// + /// Recipe Overview + /// + public Dictionary RecipeOverview; } } diff --git a/Thermo.Active.Model/Thermo.Active.Model.csproj b/Thermo.Active.Model/Thermo.Active.Model.csproj index 1d04ca14..169d38a2 100644 --- a/Thermo.Active.Model/Thermo.Active.Model.csproj +++ b/Thermo.Active.Model/Thermo.Active.Model.csproj @@ -108,6 +108,7 @@ + diff --git a/Thermo.Active.NC/NcAdapter.cs b/Thermo.Active.NC/NcAdapter.cs index e50428c4..ba2e6f68 100644 --- a/Thermo.Active.NC/NcAdapter.cs +++ b/Thermo.Active.NC/NcAdapter.cs @@ -23,10 +23,12 @@ namespace Thermo.Active.NC { public class NcAdapter : IDisposable { - + /// + /// NC object + /// public NcThermo numericalControl; /// - /// Lock per connessione PLC + /// Lock semaphore for PLC connection step /// private readonly static object connectLock = new object(); @@ -279,12 +281,12 @@ namespace Thermo.Active.NC public CmsError ManageConfRequest() { CmsError libraryError = NO_ERROR; - if (RecipeLiveData != null) + if (NcFileAdapter.RecipeLiveData != null) { // controllo SE HO richieste di configurazione if (ThermoReqConfWarmerStr) { - if (RecipeLiveData.ChannelSetpoints != null) + if (NcFileAdapter.RecipeLiveData.ChannelSetpoints != null) { // se si in questo caso scrivo configurazione... WriteRecipeWarmConfig(); @@ -294,7 +296,7 @@ namespace Thermo.Active.NC } if (ThermoReqConfRecipeStr) { - if (RecipeLiveData.RecipeParameters != null) + if (NcFileAdapter.RecipeLiveData.RecipeParameters != null) { // copy data to PLC libraryError = ReadFullRecipe(out Dictionary prevRecipe); @@ -302,7 +304,7 @@ namespace Thermo.Active.NC return libraryError; // save parameters to PLC!!! Dictionary updtRecipe = new Dictionary(); - foreach (var item in RecipeLiveData.RecipeParameters) + foreach (var item in NcFileAdapter.RecipeLiveData.RecipeParameters) { if (prevRecipe.ContainsKey(item.Key)) { diff --git a/Thermo.Active.NC/NcFileAdapter.cs b/Thermo.Active.NC/NcFileAdapter.cs index 513b9de8..3474cba1 100644 --- a/Thermo.Active.NC/NcFileAdapter.cs +++ b/Thermo.Active.NC/NcFileAdapter.cs @@ -11,11 +11,20 @@ using static CMS_CORE_Library.Models.DataStructures; using static Thermo.Active.Config.ServerConfig; using static Thermo.Active.Database.Controllers.QueueController; using static Thermo.Active.Model.Constants; +using Newtonsoft.Json; +using System.Xml; +using Thermo.Active.Model.DTOModels.ThRecipe; namespace Thermo.Active.NC { public class NcFileAdapter : NcAdapter { + + /// + /// Recipe Live data + /// + public static LiveData RecipeLiveData = new LiveData(); + /// /// Read file from local devices ad give back string content /// @@ -582,5 +591,209 @@ namespace Thermo.Active.NC return numericalControl.FILES_WCleanUploadFolder(); } + + #region recipe file persistence + + + public static void ReadLastRecipe() + { + try + { + ReadLiveData(); + } + catch (XmlException ex) + { + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, + "Error while reading file: " + ex.SourceUri + + "\n Error: " + ex.Message, + true + ); + } + catch (Exception ex) + { + var message = ex.Message; + if (ex.InnerException != null) + message += "\n" + ex.InnerException.Message; + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, message, true); + } + } + /// + /// Try to load live data from json persistence file + /// + public static bool ReadLiveData() + { + bool answ = false; + if (File.Exists(LIVE_RECIPE_PATH)) + { + // load all text data + var rawData = File.ReadAllText(LIVE_RECIPE_PATH); + try + { + // deserialize to object + RecipeLiveData = JsonConvert.DeserializeObject(rawData); + } + catch + { } + answ = true; + } + else + { + // reload from template... + var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH); + try + { + // deserialize to object + RecipeLiveData = JsonConvert.DeserializeObject(rawData); + } + catch + { } + // salva current + SaveRecipeCurrent(); + answ = true; + } + // rendo se fatto + return answ; + } + /// + /// Try to load selected recipe to live data (memory and json persistence file) + /// + public static bool LoadRecipe(string filePath) + { + bool answ = false; + + // check file extension + string fileName = Path.GetFileName(filePath); + if (!fileName.EndsWith(".json")) + { + fileName += ".json"; + filePath += ".json"; + } + + // check filePath... + if (!filePath.Contains(RECIPE_DIRECTORY) && filePath != RECIPE_TEMPLATE_PATH) + { + // aggiungo base path! + filePath = RECIPE_DIRECTORY + filePath; + } + if (File.Exists(filePath)) + { + answ = true; + + // load all text data + var rawData = File.ReadAllText(filePath); + try + { + // deserialize to object + RecipeLiveData = JsonConvert.DeserializeObject(rawData); + } + catch + { } + // update current live data! + SaveRecipeCurrent(); + } + // rendo se fatto + return answ; + } + /// + /// Try to load template recipe + /// + public static bool LoadTemplate() + { + bool answ = false; + // check filePath... + if (File.Exists(RECIPE_TEMPLATE_PATH)) + { + answ = true; + + // load all text data + var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH); + try + { + // deserialize to object + RecipeLiveData = JsonConvert.DeserializeObject(rawData); + // update NAME + RecipeLiveData.RecipeName = $"{DateTime.Now:yyyyMMss_HHmmss}.json"; + } + catch + { } + // update current live data! + SaveRecipeCurrent(); + } + // rendo se fatto + return answ; + } + /// + /// Try to write live data to json persistence file + /// + public static bool SaveRecipeCurrent() + { + bool answ = false; + try + { + answ = true; + // serialize + string rawData = JsonConvert.SerializeObject(RecipeLiveData); + // save live! + var dir = Path.GetDirectoryName(LIVE_RECIPE_PATH); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + File.WriteAllText(LIVE_RECIPE_PATH, rawData); + } + catch + { } + // rendo se fatto + return answ; + } + /// + /// Try to save live recipe as NEW template + /// + public static bool SaveRecipeTemplate() + { + + RecipeLiveData.RecipeName = "template.json"; + return SaveRecipe(RECIPE_TEMPLATE_PATH); + } + /// + /// Try to save live recipe to selected filePath + /// + public static bool SaveRecipe(string filePath) + { + bool answ = false; + try + { + answ = true; + string fileName = Path.GetFileName(filePath); + if (!fileName.EndsWith(".json")) + { + fileName += ".json"; + filePath += ".json"; + } + // fix name! + RecipeLiveData.RecipeName = fileName; + // serialize + string rawData = JsonConvert.SerializeObject(RecipeLiveData); + // save live! + File.WriteAllText(LIVE_RECIPE_PATH, rawData); + // check filePath... + if (!filePath.Contains(RECIPE_DIRECTORY) && filePath != RECIPE_TEMPLATE_PATH) + { + // aggiungo base path! + filePath = RECIPE_DIRECTORY + filePath; + } + // save! + File.WriteAllText(filePath, rawData); + } + catch + { } + // rendo se fatto + return answ; + } + + + #endregion + + } } diff --git a/Thermo.Active.NC/Thermo.Active.NC.csproj b/Thermo.Active.NC/Thermo.Active.NC.csproj index 58647d15..cf63ac30 100644 --- a/Thermo.Active.NC/Thermo.Active.NC.csproj +++ b/Thermo.Active.NC/Thermo.Active.NC.csproj @@ -33,6 +33,9 @@ + + ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll + @@ -64,6 +67,7 @@ Always + diff --git a/Thermo.Active.NC/packages.config b/Thermo.Active.NC/packages.config new file mode 100644 index 00000000..7c080311 --- /dev/null +++ b/Thermo.Active.NC/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Thermo.Active/Controllers/WebApi/RecipeController.cs b/Thermo.Active/Controllers/WebApi/RecipeController.cs index 7d9a7126..c5081cea 100644 --- a/Thermo.Active/Controllers/WebApi/RecipeController.cs +++ b/Thermo.Active/Controllers/WebApi/RecipeController.cs @@ -65,7 +65,7 @@ namespace Thermo.Active.Controllers.WebApi { if (parametersList != null) { - if (RecipeLiveData != null) + if (NcFileAdapter.RecipeLiveData != null) { // Try connection CmsError libraryError = ncAdapter.Connect(); @@ -127,7 +127,7 @@ namespace Thermo.Active.Controllers.WebApi [Route("confirm"), HttpPut] public IHttpActionResult ConfirmEdit() { - if (RecipeLiveData != null) + if (NcFileAdapter.RecipeLiveData != null) { // Try connection CmsError libraryError = ncAdapter.Connect(); @@ -179,7 +179,7 @@ namespace Thermo.Active.Controllers.WebApi [Route("cancel"), HttpPut] public IHttpActionResult CancelEdit() { - if (RecipeLiveData != null) + if (NcFileAdapter.RecipeLiveData != null) { // Try connection CmsError libraryError = ncAdapter.Connect(); @@ -234,7 +234,7 @@ namespace Thermo.Active.Controllers.WebApi { // chiamo metodo di lettura... - bool fatto = ServerConfigController.LoadRecipe(newName); + bool fatto = NcFileAdapter.LoadRecipe(newName); if (!fatto) { ThermoActiveLogger.LogError($"LoadRecipe error"); @@ -260,7 +260,7 @@ namespace Thermo.Active.Controllers.WebApi public IHttpActionResult NewRecipe() { // chiamo metodo di lettura... - bool fatto = ServerConfigController.LoadTemplate(); + bool fatto = NcFileAdapter.LoadTemplate(); if (!fatto) { @@ -324,9 +324,9 @@ namespace Thermo.Active.Controllers.WebApi } // ora salvo ANCHE i dati live... - RecipeLiveData.RecipeParameters = currParams; + NcFileAdapter.RecipeLiveData.RecipeParameters = currParams; // e salvo su disco - ServerConfigController.SaveRecipe(newName); + NcFileAdapter.SaveRecipe(newName); // ritorno solo fatto! return Ok(); @@ -363,7 +363,7 @@ namespace Thermo.Active.Controllers.WebApi } // ora salvo nei dati live... - RecipeLiveData.RecipeParameters = currParams; + NcFileAdapter.RecipeLiveData.RecipeParameters = currParams; // carico i dati dei riscaldi... var currChSet = new Dictionary(); @@ -371,11 +371,11 @@ namespace Thermo.Active.Controllers.WebApi { currChSet.Add(item.Key, item.Value.SetpointHMI); } - RecipeLiveData.ChannelSetpoints = currChSet; + NcFileAdapter.RecipeLiveData.ChannelSetpoints = currChSet; // e salvo su disco - ServerConfigController.SaveRecipeTemplate(); + NcFileAdapter.SaveRecipeTemplate(); // ritorno solo fatto! return Ok(); @@ -391,9 +391,9 @@ namespace Thermo.Active.Controllers.WebApi try { // ora salvo ANCHE i dati live... - RecipeLiveData.RecipeParameters = currParams; + NcFileAdapter.RecipeLiveData.RecipeParameters = currParams; // e salvo su disco - ServerConfigController.SaveRecipeCurrent(); + NcFileAdapter.SaveRecipeCurrent(); } catch (Exception exc) { @@ -424,7 +424,7 @@ namespace Thermo.Active.Controllers.WebApi // save parameters to PLC!!! Dictionary updtRecipe = new Dictionary(); - foreach (var item in RecipeLiveData.RecipeParameters) + foreach (var item in NcFileAdapter.RecipeLiveData.RecipeParameters) { if (prevRecipe.ContainsKey(item.Key)) { @@ -452,7 +452,7 @@ namespace Thermo.Active.Controllers.WebApi // process ch load setup... Dictionary newRisk = new Dictionary(); - foreach (var item in RecipeLiveData.ChannelSetpoints) + foreach (var item in NcFileAdapter.RecipeLiveData.ChannelSetpoints) { newRisk.Add(item.Key, item.Value); } diff --git a/Thermo.Active/Controllers/WebApi/WarmersController.cs b/Thermo.Active/Controllers/WebApi/WarmersController.cs index 309faebf..f864cf38 100644 --- a/Thermo.Active/Controllers/WebApi/WarmersController.cs +++ b/Thermo.Active/Controllers/WebApi/WarmersController.cs @@ -233,9 +233,9 @@ namespace Thermo.Active.Controllers.WebApi try { // ora salvo ANCHE i dati live... - RecipeLiveData.ChannelSetpoints = chSetpoints; + NcFileAdapter.RecipeLiveData.ChannelSetpoints = chSetpoints; // e salvo su disco - ServerConfigController.SaveRecipeCurrent(); + NcFileAdapter.SaveRecipeCurrent(); } catch (Exception exc) { diff --git a/Thermo.Active/Properties/AssemblyInfo.cs b/Thermo.Active/Properties/AssemblyInfo.cs index 7002e76e..5e80ead3 100644 --- a/Thermo.Active/Properties/AssemblyInfo.cs +++ b/Thermo.Active/Properties/AssemblyInfo.cs @@ -30,4 +30,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.9.9")] +[assembly: AssemblyVersion("0.9.10")] diff --git a/Thermo.Active/program.cs b/Thermo.Active/program.cs index ceb0a981..ecb0b188 100644 --- a/Thermo.Active/program.cs +++ b/Thermo.Active/program.cs @@ -85,7 +85,7 @@ namespace Thermo.Active opt.Urls.Add("http://" + ServerStartupConfig.ServerAddress.ToString() + ":" + ServerStartupConfig.ServerPort.ToString()); // read and save last CURRENT RECIPE data... - ServerConfigController.ReadLastRecipe(); + NcFileAdapter.ReadLastRecipe(); RecipeController.WriteCurrentRecipeToPlc(); //starts threads