Added file method for template/current/load/save

This commit is contained in:
Samuele Locatelli
2020-06-08 15:44:59 +02:00
parent f48ee73ede
commit 698fd47177
8 changed files with 247 additions and 126 deletions
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@

+90 -32
View File
@@ -794,8 +794,9 @@ namespace Thermo.Active.Config
/// <summary>
/// Try to load live data from json persistence file
/// </summary>
public static void ReadLiveData()
public static bool ReadLiveData()
{
bool answ = false;
if (File.Exists(LIVE_RECIPE_PATH))
{
// load all text data
@@ -807,46 +808,39 @@ namespace Thermo.Active.Config
}
catch
{ }
answ = true;
}
else
{
// setup new object
RecipeLiveData = new LiveData()
// reload from template...
var rawData = File.ReadAllText(RECIPE_TEMPLATE_PATH);
try
{
RecipeName = "current.json",
ChannelSetpoints = new Dictionary<int, double>(),
RecipeParameters = new Dictionary<string, double>()
};
// deserialize to object
RecipeLiveData = JsonConvert.DeserializeObject<LiveData>(rawData);
}
catch
{ }
answ = true;
}
}
/// <summary>
/// Try to write live data to json persistence file
/// </summary>
public static void WriteLiveData()
{
try
{
// serialize
string rawData = JsonConvert.SerializeObject(RecipeLiveData);
// save live!
File.WriteAllText(LIVE_RECIPE_PATH, rawData);
}
catch
{ }
// rendo se fatto
return answ;
}
/// <summary>
/// Try to load selected recipe to live data (memory and json persistence file)
/// </summary>
public static void LoadRecipe(string filePath)
public static bool LoadRecipe(string filePath)
{
bool answ = false;
// check filePath...
if (!filePath.Contains(RECIPE_DIRECTORY) && filePath != RECIPE_TEMPLATE_PATH)
{
// aggiungo base path!
filePath = RECIPE_DIRECTORY + filePath;
}
if (File.Exists(filePath))
{
// check filePath...
if (!filePath.Contains(RECIPE_DIRECTORY))
{
// aggiungo base path!
filePath = RECIPE_DIRECTORY + filePath;
}
answ = true;
// load all text data
var rawData = File.ReadAllText(filePath);
@@ -858,22 +852,84 @@ namespace Thermo.Active.Config
catch
{ }
// update current live data!
WriteLiveData();
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!
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 void SaveRecipe(string filePath)
public static bool SaveRecipe(string filePath)
{
bool answ = false;
try
{
answ = true;
// fix name!
RecipeLiveData.RecipeName= Path.GetFileName(filePath);
// serialize
string rawData = JsonConvert.SerializeObject(RecipeLiveData);
// save live!
File.WriteAllText(LIVE_RECIPE_PATH, rawData);
// check filePath...
if (!filePath.Contains(RECIPE_DIRECTORY))
if (!filePath.Contains(RECIPE_DIRECTORY) && filePath != RECIPE_TEMPLATE_PATH)
{
// aggiungo base path!
filePath = RECIPE_DIRECTORY + filePath;
@@ -883,6 +939,8 @@ namespace Thermo.Active.Config
}
catch
{ }
// rendo se fatto
return answ;
}
#if false
@@ -59,6 +59,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerConfig.cs" />
<Compile Include="ServerConfigController.cs" />
<Content Include="Recipes\.placeholder.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Report\Programs.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
@@ -179,10 +182,11 @@
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Recipes\" />
</ItemGroup>
<ItemGroup>
<Content Include="Config\Recipe\template.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+2 -1
View File
@@ -211,6 +211,8 @@ namespace Thermo.Active.Model
#endif
public const string CONFIG_DIRECTORY = "Config\\";
public const string RECIPE_DIRECTORY = "Recipes\\";
public const string RECIPE_TEMPLATE_PATH = CONFIG_DIRECTORY + RECIPE_DIRECTORY + "template.json";
public const string LIVE_RECIPE_PATH = TEMP_FOLDER + RECIPE_DIRECTORY + "current.json";
public const string RESOURCE_DIRECTORY = @"Thermo.Active.Config.Config.";
public const string SERVER_CONFIG_SCHEMA_PATH = RESOURCE_DIRECTORY + @"serverConfigValidator.xsd";
public const string SERVER_CONFIG_PATH = CONFIG_DIRECTORY + "serverConfig.xml";
@@ -253,7 +255,6 @@ namespace Thermo.Active.Model
public const string MAIN_PROGRAM_CONFIG_PATH = CONFIG_DIRECTORY + "customMainProgram.txt";
public const string LIVE_RECIPE_PATH = RECIPE_DIRECTORY + "current.json";
public static string LANGUAGE_PACK_DIRECTORY = BASE_PATH + "\\languages\\";
public static string LANGUAGE_SCHEMA_PATH = BASE_PATH + "\\LanguageValidator.xsd";
@@ -8,32 +8,39 @@ namespace Thermo.Active.Model.DTOModels.Recipe
{
public class DTOWarmers
{
public int Id { get; set; } = 0;
public double SetpointHMI { get; set; } = 0;
public double SetpointPLC { get; set; } = 0;
//public RPRange Range { get; set; }
//public RPStatus Status { get; set; }
public string UnitMeasure { get; set; }
public double ValueAct { get; set; }
public int IdChannel { get; set; } = 0;
public int IdReflector { get; set; } = 0;
public int SetpointRecipe { get; set; } = 0;
public int SetpointTermocam { get; set; } = 0;
public int SetpointPLC { get; set; } = 0;
public int ChannelStatus { get; set; } = 0;
public double ActualCurrent { get; set; } = 0;
public int ActualPerc { get; set; } = 0;
public int MaxPower { get; set; } = 0;
public override bool Equals(object obj)
{
if (!(obj is DTOWarmers item))
return false;
if (Id!= item.Id)
if (IdChannel != item.IdChannel)
return false;
if (SetpointHMI != item.SetpointHMI)
if (IdReflector != item.IdReflector)
return false;
if (SetpointRecipe != item.SetpointRecipe)
return false;
if (SetpointTermocam != item.SetpointTermocam)
return false;
if (SetpointPLC != item.SetpointPLC)
return false;
//if (!Range.Equals(item.Range))
// return false;
//if (!Status.Equals(item.Status))
// return false;
if (UnitMeasure != item.UnitMeasure)
if (ChannelStatus != item.ChannelStatus)
return false;
if (ValueAct != item.ValueAct)
if (ActualCurrent != item.ActualCurrent)
return false;
if (ActualPerc != item.ActualPerc)
return false;
if (MaxPower != item.MaxPower)
return false;
return true;
@@ -45,53 +52,4 @@ namespace Thermo.Active.Model.DTOModels.Recipe
}
}
#if false
public struct RPRange
{
public double Min { get; set; }
public double Max { get; set; }
public override bool Equals(object obj)
{
if (!(obj is RPRange item))
return false;
if (Min != item.Min)
return false;
if (Max != item.Max)
return false;
return true;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public struct RPStatus
{
public bool Visible { get; set; }
public bool Enabled { get; set; }
public bool HasError { get; set; }
public override bool Equals(object obj)
{
if (!(obj is RPStatus item))
return false;
if (Visible != item.Visible)
return false;
if (Enabled != item.Enabled)
return false;
if (HasError != item.HasError)
return false;
return true;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
#endif
}
+24 -10
View File
@@ -1719,26 +1719,40 @@ namespace Thermo.Active.NC
}
/// <summary>
/// Legge tutti i parametri della ricetta
/// GEt all warmers data by channel
/// </summary>
/// <param name="currWarmers">Oggetto elenco 1024 ch riscaldi</param>
/// <param name="currWarmers">List of <= 1024 warmers channels</param>
/// <returns></returns>
public CmsError ReadWarmers(out Dictionary<int, DTOWarmers> currWarmers)
{
CmsError cmsError = NO_ERROR;
currWarmers = new Dictionary<int, DTOWarmers>();
DTOWarmers currVal = new DTOWarmers();
// FIXME TODO
#if false
DTORecipeParam currParam = new DTORecipeParam();
RPRange currRange = new RPRange();
RPStatus currStatus = new RPStatus();
// read and return config
if (NcConfig.NcVendor == NC_VENDOR.S7NET)
{
foreach (var item in RiskChannelConfig)
{
currVal = new DTOWarmers()
{
IdChannel = item.IdChannel,
IdReflector = item.IdReflector,
SetpointRecipe = 80, // leggere da NUOVA area PLC?
SetpointTermocam = 0, // per ora a zero
SetpointPLC = 0, // leggere da PLC!
ChannelStatus = 0, // leggere da PLC
ActualCurrent = 0, // leggere da PLC
ActualPerc = 0, // leggere da PLC
MaxPower = item.MaxPower
};
currWarmers.Add(item.IdChannel, currVal);
}
}
// gestione errore
cmsError = ReadRecipeData(false, true, out currModules);
if (cmsError.IsError())
return cmsError;
#endif
return cmsError;
// restituisco cod errore se trovato
return cmsError;
@@ -127,10 +127,7 @@ namespace Thermo.Active.Controllers.WebApi
currParams.Add(item.Key, item.Value.SetpointHMI);
}
// ora salvo ANCHE i dati live...
RecipeLiveData.RecipeParameters = currParams;
// e salvo su disco
ServerConfigController.WriteLiveData();
saveCurrentRecipeParams(currParams);
// ritorno solo fatto!
return Ok();
@@ -168,9 +165,56 @@ namespace Thermo.Active.Controllers.WebApi
}
// ora salvo ANCHE i dati live...
RecipeLiveData.RecipeParameters = currParams;
// e salvo su disco
ServerConfigController.WriteLiveData();
saveCurrentRecipeParams(currParams);
// ritorno solo fatto!
return Ok();
}
}
/// <summary>
/// load recipe from file and send to PLC
/// </summary>
/// <returns></returns>
[Route("load"), HttpPut]
public IHttpActionResult Load(string newName)
{
using (NcFileAdapter ncAdapter = new NcFileAdapter())
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// chiamo metodo di lettura...
//CmsError cmsError = ServerConfigController.LoadRecipe(newName);
bool fatto = ServerConfigController.LoadRecipe(newName);
if (!fatto)
return NotFound();
// ritorno solo fatto!
return Ok();
}
}
/// <summary>
/// Load tempalte recipe from file and save as new current...
/// </summary>
/// <returns></returns>
[Route("new"), HttpPut]
public IHttpActionResult NewRecipe()
{
using (NcFileAdapter ncAdapter = new NcFileAdapter())
{
// Try connection
CmsError libraryError = ncAdapter.Connect();
if (libraryError.errorCode != 0)
return NotFound();
// chiamo metodo di lettura...
bool fatto = ServerConfigController.LoadTemplate();
if (!fatto)
return NotFound();
// ritorno solo fatto!
return Ok();
@@ -203,15 +247,12 @@ namespace Thermo.Active.Controllers.WebApi
}
// ora salvo ANCHE i dati live...
RecipeLiveData.RecipeParameters = currParams;
// e salvo su disco
ServerConfigController.WriteLiveData();
saveCurrentRecipeParams(currParams);
// ritorno solo fatto!
return Ok();
}
}
/// <summary>
/// Save current recipe from PLC with new name
/// </summary>
@@ -247,11 +288,11 @@ namespace Thermo.Active.Controllers.WebApi
}
}
/// <summary>
/// load recipe from file and send to PLC
/// Save current recipe from PLC to Template
/// </summary>
/// <returns></returns>
[Route("load"), HttpPut]
public IHttpActionResult Load(string newName)
[Route("saveTemplate"), HttpPut]
public IHttpActionResult SaveTemplate()
{
using (NcFileAdapter ncAdapter = new NcFileAdapter())
{
@@ -260,13 +301,56 @@ namespace Thermo.Active.Controllers.WebApi
if (libraryError.errorCode != 0)
return NotFound();
// chiamo metodo di lettura...
ServerConfigController.LoadRecipe(newName);
// recupero i dati LIVE dei parametri HMI della ricetta...
CmsError cmsError = ncAdapter.ReadFullRecipe(out Dictionary<string, DTORecipeParam> currRecipe);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// recupero i dati LIVE dei carichi load dei cahnnels di riscaldo...
cmsError = ncAdapter.ReadWarmers(out Dictionary<int, DTOWarmers> currWarmers);
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
// uso i valopri HMI...
var currParams = new Dictionary<string, double>();
foreach (var item in currRecipe)
{
currParams.Add(item.Key, item.Value.SetpointHMI);
}
// ora salvo nei dati live...
RecipeLiveData.RecipeParameters = currParams;
// carico i dati dei riscaldi...
var currChSet = new Dictionary<int, double>();
foreach (var item in currWarmers)
{
currChSet.Add(item.Key, item.Value.SetpointRecipe);
}
RecipeLiveData.ChannelSetpoints = currChSet;
// e salvo su disco
ServerConfigController.SaveRecipeTemplate();
// ritorno solo fatto!
return Ok();
}
}
/// <summary>
/// Do actual recipe parameters save
/// </summary>
/// <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();
}
}
}