Refactoring methods for File persistance

This commit is contained in:
Samuele Locatelli
2020-06-24 18:45:22 +02:00
parent ab2abc7c2e
commit 1dd83f4e17
13 changed files with 253 additions and 224 deletions
-1
View File
@@ -62,7 +62,6 @@ namespace Thermo.Active.Config
public static List<RiskChannelModel> RiskChannelConfig;
public static List<RiskBoardModel> RiskBoardConfig;
public static LiveData RecipeLiveData = new LiveData();
}
}
@@ -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();
}
/// <summary>
/// Try to load live data from json persistence file
/// </summary>
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<LiveData>(rawData);
}
catch
{ }
answ = true;
}
else
{
// reload from template...
var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH);
try
{
// deserialize to object
RecipeLiveData = JsonConvert.DeserializeObject<LiveData>(rawData);
}
catch
{ }
// salva current
SaveRecipeCurrent();
answ = true;
}
// rendo se fatto
return answ;
}
/// <summary>
/// Try to load selected recipe to live data (memory and json persistence file)
/// </summary>
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<LiveData>(rawData);
}
catch
{ }
// update current live data!
SaveRecipeCurrent();
}
// rendo se fatto
return answ;
}
/// <summary>
/// Try to load template recipe
/// </summary>
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<LiveData>(rawData);
// update NAME
RecipeLiveData.RecipeName = $"{DateTime.Now:yyyyMMss_HHmmss}.json";
}
catch
{ }
// update current live data!
SaveRecipeCurrent();
}
// rendo se fatto
return answ;
}
/// <summary>
/// Try to write live data to json persistence file
/// </summary>
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;
}
/// <summary>
/// Try to save live recipe as NEW template
/// </summary>
public static bool SaveRecipeTemplate()
{
RecipeLiveData.RecipeName = "template.json";
return SaveRecipe(RECIPE_TEMPLATE_PATH);
}
/// <summary>
/// Try to save live recipe to selected filePath
/// </summary>
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())
@@ -55,7 +55,6 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="LiveData.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerConfig.cs" />
<Compile Include="ServerConfigController.cs" />
@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Thermo.Active.Config
namespace Thermo.Active.Model.DTOModels.ThRecipe
{
/// <summary>
/// Live data for Thermo Active
@@ -19,6 +19,9 @@ namespace Thermo.Active.Config
/// Dictionary of all channels and relative setpoints
/// </summary>
public Dictionary<int, int> ChannelSetpoints;
/// <summary>
/// Recipe Overview
/// </summary>
public Dictionary<string, RecipeCatStatus> RecipeOverview;
}
}
@@ -108,6 +108,7 @@
<Compile Include="DTOModels\ThModules\DTOModule.cs" />
<Compile Include="DTOModels\ThModules\DTOModuleConfigModel.cs" />
<Compile Include="DTOModels\ThModules\DTOModulesBlock.cs" />
<Compile Include="DTOModels\ThRecipe\LiveData.cs" />
<Compile Include="DTOModels\ThWarmers\DTOWarmers.cs" />
<Compile Include="DTOModels\ThRecipe\DTORecipeParam.cs" />
<Compile Include="DTOModels\ThRecipe\DTORecipeConfigModel .cs" />
+8 -6
View File
@@ -23,10 +23,12 @@ namespace Thermo.Active.NC
{
public class NcAdapter : IDisposable
{
/// <summary>
/// NC object
/// </summary>
public NcThermo numericalControl;
/// <summary>
/// Lock per connessione PLC
/// Lock semaphore for PLC connection step
/// </summary>
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<string, DTORecipeParam> prevRecipe);
@@ -302,7 +304,7 @@ namespace Thermo.Active.NC
return libraryError;
// save parameters to PLC!!!
Dictionary<string, DTORecipeParam> updtRecipe = new Dictionary<string, DTORecipeParam>();
foreach (var item in RecipeLiveData.RecipeParameters)
foreach (var item in NcFileAdapter.RecipeLiveData.RecipeParameters)
{
if (prevRecipe.ContainsKey(item.Key))
{
+213
View File
@@ -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
{
/// <summary>
/// Recipe Live data
/// </summary>
public static LiveData RecipeLiveData = new LiveData();
/// <summary>
/// Read file from local devices ad give back string content
/// </summary>
@@ -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);
}
}
/// <summary>
/// Try to load live data from json persistence file
/// </summary>
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<LiveData>(rawData);
}
catch
{ }
answ = true;
}
else
{
// reload from template...
var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH);
try
{
// deserialize to object
RecipeLiveData = JsonConvert.DeserializeObject<LiveData>(rawData);
}
catch
{ }
// salva current
SaveRecipeCurrent();
answ = true;
}
// rendo se fatto
return answ;
}
/// <summary>
/// Try to load selected recipe to live data (memory and json persistence file)
/// </summary>
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<LiveData>(rawData);
}
catch
{ }
// update current live data!
SaveRecipeCurrent();
}
// rendo se fatto
return answ;
}
/// <summary>
/// Try to load template recipe
/// </summary>
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<LiveData>(rawData);
// update NAME
RecipeLiveData.RecipeName = $"{DateTime.Now:yyyyMMss_HHmmss}.json";
}
catch
{ }
// update current live data!
SaveRecipeCurrent();
}
// rendo se fatto
return answ;
}
/// <summary>
/// Try to write live data to json persistence file
/// </summary>
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;
}
/// <summary>
/// Try to save live recipe as NEW template
/// </summary>
public static bool SaveRecipeTemplate()
{
RecipeLiveData.RecipeName = "template.json";
return SaveRecipe(RECIPE_TEMPLATE_PATH);
}
/// <summary>
/// Try to save live recipe to selected filePath
/// </summary>
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
}
}
+4
View File
@@ -33,6 +33,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -64,6 +67,7 @@
<None Include="Language\ERR_Spanish.INI">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\cms_core_library\CMS_CORE_Library\CMS_CORE_Library.csproj">
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net462" />
</packages>
@@ -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<int, int>();
@@ -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<string, DTORecipeParam> updtRecipe = new Dictionary<string, DTORecipeParam>();
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<int, int> newRisk = new Dictionary<int, int>();
foreach (var item in RecipeLiveData.ChannelSetpoints)
foreach (var item in NcFileAdapter.RecipeLiveData.ChannelSetpoints)
{
newRisk.Add(item.Key, item.Value);
}
@@ -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)
{
+1 -1
View File
@@ -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")]
+1 -1
View File
@@ -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