Refactoring methods for File persistance
This commit is contained in:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user