tbf/TestBenchFramework/BenchControl/ComponentCfgBase.cs

175 lines
4.7 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using TBF.BenchControl.Generic;
namespace TBF.BenchControl
{
public class ComponentCfgBase
{
[XmlIgnore]
public string Name
{
get { return name; }
set { name = value; }
}
string name;
[XmlIgnore]
public Generic.IComponentFactory Factory
{
get { return factory; }
set { factory = value; }
}
Generic.IComponentFactory factory;
[XmlIgnore]
public string ParentName
{
get { return parentName; }
set { parentName = value; }
}
string parentName;
[XmlIgnore]
public int ItemNr
{
get { return itemNr; }
set { itemNr = value; }
}
int itemNr;
[XmlIgnore]
public Entities.DebugMode DebugLevel
{
get { return debugLevel; }
set { debugLevel = value; }
}
Entities.DebugMode debugLevel;
[XmlIgnore]
public Entities.LogLevel LogLevel
{
get { return logLevel; }
set { logLevel = value; }
}
Entities.LogLevel logLevel;
[XmlIgnore]
public IList<Entities.MeasurementCorrection> Corrections
{
get { return corrections; }
set { corrections = value; }
}
IList<Entities.MeasurementCorrection> corrections;
protected ComponentCfgBase()
{
itemNr = 0;
debugLevel = Entities.DebugMode.Normal;
logLevel = Entities.LogLevel.Off;
}
protected ComponentCfgBase(Generic.IComponentFactory factory)
: this()
{
this.factory = factory;
}
protected ComponentCfgBase(Generic.IComponentFactory factory, string name)
: this(factory)
{
this.name = name;
}
protected ComponentCfgBase(Generic.IComponentFactory factory, string name, string parentName)
: this(factory, name)
{
this.parentName = parentName;
}
public TBF.Entities.Component CreateDbEntity()
{
using (var writer = new StringWriter())
{
(new XmlSerializer(GetType())).Serialize(writer, this);
return Entities.Component.CreateFromCfg(Name, factory.ClassName, ParentName, ItemNr, DebugLevel, LogLevel, writer.ToString());
}
}
public static IComponentCfg CreateFromDbEntity(Type type, Entities.Component cmpntEntity, IComponentFactory factory)
{
using (var reader = new StringReader(cmpntEntity.Parameters))
{
IComponentCfg config = (IComponentCfg)(new XmlSerializer(type)).Deserialize(reader);
config.Name = cmpntEntity.Name;
config.ParentName = cmpntEntity.ParentName;
config.ItemNr = cmpntEntity.ItemNr;
config.DebugLevel = cmpntEntity.Mode;
config.LogLevel = cmpntEntity.Logging;
config.Corrections = cmpntEntity.Corrections;
config.Factory = factory;
return config;
}
}
/// <summary>
/// Default implementation returning null - no test aprameters.
/// In case there are any test parameters, this function override should return
/// a reference to a class that implements IParamsProvider and ITestParams.
/// </summary>
/// <returns>Test parameters obtained by the last UpdateTestParams(test)</returns>
public virtual IParamsProvider GetTestParams()
{
return null;
}
/// <summary>
/// Default implementation returning null - no procedure parameters.
/// In case there are any procedure parameters, this function override should return
/// a reference to a class that implements IParamsProvider and IProcedureParams.
/// </summary>
/// <returns>Procedure parameters obtained by the last UpdateProcedureParams(procedure)</returns>
public virtual IParamsProvider GetProcedureParams()
{
return null;
}
public void UpdateTestParams(Entities.Test test)
{
ITestParams tstPrms = (GetTestParams() as ITestParams);
if (tstPrms != null)
{
tstPrms.UpdateTestParams(Name, test);
}
}
public void UpdateProcedureParams(Entities.Procedure procedure)
{
IProcedureParams procPrms = (GetProcedureParams() as IProcedureParams);
if (procPrms != null)
{
procPrms.UpdateProcedureParams(Name, procedure);
}
}
public virtual IParamsProvider CreateTestParams(Entities.Test test)
{
return null;
}
public virtual IParamsProvider CreateProcedureParams(Entities.Procedure procedure)
{
return null;
}
}
}