150 lines
3.9 KiB
C#
150 lines
3.9 KiB
C#
using Common;
|
|
using Config.Entities;
|
|
using log4net;
|
|
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
using TBF.Resources;
|
|
using TBF.Rig.Generic;
|
|
|
|
namespace TBF.Rig.RegisterReaders.FrequencyMeterFromUniCB
|
|
{
|
|
public class RRProcParams : ProcedureParamsBase, IParamsProvider, IProcedureParams
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(RRProcParams));
|
|
|
|
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(RRProcParams) })[0];
|
|
public override XmlSerializer GetSerializer() { return Serializer; }
|
|
|
|
public double PulsesPerLtr; /// [l^-1]
|
|
public int Filter;
|
|
|
|
public override void InitializeAll()
|
|
{
|
|
PulsesPerLtr = 1.0;
|
|
Filter = 0;
|
|
}
|
|
|
|
string[] paramNames = new string[]
|
|
{
|
|
Strings.PulsesPerLtr,
|
|
Strings.Filter,
|
|
};
|
|
public override string ParamName(int i) { return paramNames[i]; }
|
|
public override int ParamsCount() { return paramNames.Length; }
|
|
|
|
public override string ToString(int i)
|
|
{
|
|
switch (i)
|
|
{
|
|
case 0: return PulsesPerLtr.ToString();
|
|
case 1: return Filter.ToString();
|
|
default: return string.Empty;
|
|
}
|
|
}
|
|
|
|
/// Retrieves parameters from UI controls
|
|
CfgUpdateFlags IParamsProvider.UpdateParam(int i, string strValue)
|
|
{
|
|
switch (i)
|
|
{
|
|
case 0: PulsesPerLtr = Utils.ParseUDouble(strValue); return CfgUpdateFlags.None;
|
|
case 1: Filter = int.Parse(strValue); return CfgUpdateFlags.None;
|
|
default: return CfgUpdateFlags.None;
|
|
}
|
|
}
|
|
|
|
/// Verifies whether strings in UI controls represent valid parameters
|
|
bool IParamsProvider.ValidateParam(int i, string strValue, out string message)
|
|
{
|
|
message = string.Empty;
|
|
|
|
double dummy;
|
|
int idummy;
|
|
switch (i)
|
|
{
|
|
case 0:
|
|
if (Utils.TryParseUDouble(strValue, out dummy)) return true;
|
|
break;
|
|
|
|
case 1:
|
|
if (int.TryParse(strValue, out idummy) && (idummy >= 0)) return true;
|
|
break;
|
|
|
|
default:
|
|
message = "Invalid index";
|
|
return false;
|
|
}
|
|
|
|
message = ParamName(i) + " is invalid";
|
|
return false;
|
|
}
|
|
|
|
void CopyContentTo(RRProcParams prms)
|
|
{
|
|
prms.PulsesPerLtr = this.PulsesPerLtr;
|
|
prms.Filter = this.Filter;
|
|
}
|
|
|
|
IParamsProvider IParamsProvider.Clone()
|
|
{
|
|
RRProcParams pars = new RRProcParams();
|
|
CopyContentTo(pars);
|
|
return pars;
|
|
}
|
|
|
|
public override bool UpdateFromDbEntity(ComponentProcedure dbEntity)
|
|
{
|
|
if (dbEntity == null) return false;
|
|
try
|
|
{
|
|
RRProcParams tmp = Serializer.Deserialize(new StringReader(dbEntity.Parameters)) as RRProcParams;
|
|
|
|
procedureParamsEntity = dbEntity;
|
|
componentName = dbEntity.CmpntName;
|
|
procedure = dbEntity.Procedure;
|
|
|
|
if (tmp != null)
|
|
{
|
|
tmp.CopyContentTo(this);
|
|
return true;
|
|
}
|
|
else return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.DebugFormat(
|
|
"Error during RRProcParams deserialization. CmpntName='{0}', Procedure='{1}', Parameters='{2}', Exception: {3}",
|
|
dbEntity?.CmpntName,
|
|
dbEntity?.Procedure,
|
|
dbEntity?.Parameters,
|
|
ex
|
|
);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public RRProcParams()
|
|
{
|
|
}
|
|
|
|
public RRProcParams(bool initialize)
|
|
{
|
|
if (initialize) InitializeAll();
|
|
}
|
|
|
|
public RRProcParams(ComponentProcedure procedureParamsEntity, string componentName, Procedure procedure)
|
|
{
|
|
this.procedureParamsEntity = procedureParamsEntity;
|
|
this.componentName = componentName;
|
|
this.procedure = procedure;
|
|
}
|
|
}
|
|
}
|