laatzen/Common/Hardware/WaterMeter/MechanicalMeter/FM2014/FM2014Config/FM2014Config.cs

146 lines
5.3 KiB
C#

/*********************************************************************************************************************/
/*! @file FM2014Config.cs
* @brief <i>FM2014 configuration file support</i>
*
* @author Thomas Wiedebusch
* @date 2025-Nov-13
*
* @details <i>Store and read all configuration which should survive a restart to keep those alive.</i>
*
* @copyright © SENSUS GmbH 20025..2026. All rights reserved.
*********************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Xylem.Common.CommonCore.Consts;
namespace Sensus.Common.Hardware.WaterMeter.MechanicalMeter.FM2014.FM2014Config
{
/// <summary>
/// Configuration of the FM2014 on local machine:
/// - Setup communication with serial com-port.
/// </summary>
/// <remarks date="2025-Nov-13" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public class FM2014Config
{
#region Properties
/// <summary>
/// Comport for the FM2014
/// </summary>
public String SerialPort { get; set; }
/// <summary>
/// Address of the FM2014
/// </summary>
public Int32? Address { get; set; }
/// <summary>
/// Address of the FM2014
/// </summary>
public Int32? TolerancePercent { get; set; }
/// <summary>
/// Address of the FM2014
/// </summary>
public List<Int32?> IndividualTolerancePercent { get; set; }
/// <summary>
/// Serial baudrate for FM2014 serial bus
/// </summary>
public Int32 Baudrate { get; set; } = 1200;
/// <summary>
/// Serial baudrate for FM2014 serial bus
/// </summary>
public List<Boolean?> SlotIsSelected { get; set; }
/// <summary>
/// Backup REF pulses for calibration measurement to simplify manual setup
/// </summary>
public UInt16 RefPulsesRequired { get; set; } = 500;
/// <summary>
/// Backup DUT pulses for calibration measurement to simplify manual setup
/// </summary>
public UInt16 DutPulsesRequired { get; set; } = 50;
/// <summary>
/// Backup double pulses detection deadtime for calibration measurement to simplify manual setup
/// </summary>
public UInt16 DoublePulseDeadtime { get; set; }
/// <summary>
/// Backup tolerance maximal value for calibration measurement threshold check to simplify manual setup
/// </summary>
public Double DutToRefToleranceMax { get; set; } = 0.8;
/// <summary>
/// Backup tolerance maximal value for calibration measurement threshold check to simplify manual setup
/// </summary>
public Double DutToRefToleranceMin { get; set; } = -0.5;
#endregion
#region methods
/// <summary>
/// Load FM2014 configuration file from AppRoaming or working directory
/// </summary>
/// <remarks date="2025-Nov-13" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2026-Feb-05" author="Thomas Wiedebusch">
/// - Support multiple FM2014 with shared port but slot selected and individual tolerance.
/// </remarks>
public Boolean ReadFM2014Config()
{
// Check for AppRoaming
var configFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
nameof(FM2014), ProgramConfig.FM2014ConfigFileName);
if (!File.Exists(configFile))
return false;
using (var tr = new StreamReader(configFile))
{
var fm2014Setup = JsonConvert.DeserializeObject<FM2014Config>(tr.ReadToEnd());
SerialPort = fm2014Setup.SerialPort;
Address = fm2014Setup.Address;
TolerancePercent = fm2014Setup.TolerancePercent;
Baudrate = fm2014Setup.Baudrate;
IndividualTolerancePercent = fm2014Setup.IndividualTolerancePercent;
SlotIsSelected = fm2014Setup.SlotIsSelected;
RefPulsesRequired = fm2014Setup.RefPulsesRequired;
DutPulsesRequired = fm2014Setup.DutPulsesRequired;
DutToRefToleranceMax = fm2014Setup.DutToRefToleranceMax;
DutToRefToleranceMin = fm2014Setup.DutToRefToleranceMin;
DoublePulseDeadtime = fm2014Setup.DoublePulseDeadtime;
}
return true;
}
/// <summary>
/// Write file to AppRoaming
/// </summary>
/// <remarks date="2025-Nov-13" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public void Update()
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
nameof(FM2014));
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var configFile = Path.Combine(path, ProgramConfig.FM2014ConfigFileName);
File.WriteAllText(configFile, JsonConvert.SerializeObject(this));
}
#endregion
}
}