using System;
using System.IO;
using System.Xml.Serialization;
using TBF.BenchControl.Generic;
namespace TBF.BenchControl.Elde.RegulValve
{
public class RegulValveCfg : ComponentCfgBase, IChildComponentCfg
{
///
/// Stored parameters
///
public int Position; /// 0..7
public int DacValueClosed; /// 0..1023
public int DacValueOpen; /// 0..1023
public int PidCoef; /// 1..100
public int StableTimeMs; /// 200..1500 ms
public bool StoredPositionReuse; /// true = store and re-use previous regulation valve positions
///
/// Stabilization time when setting the flow: 0=200ms, step 50ms, max. 1.5 sec.
///
public int StableTime { get { return (StableTimeMs - 200) / 50; } }
/// Parameterless constructor
public RegulValveCfg()
{
StableTimeMs = 500;
}
///
/// Regulation valve configuration parameters
///
/// Regulation valve name
/// Position on the control board 0..7
/// 0..1023
/// 0..1023
/// 1..100
public RegulValveCfg(IComponentFactory factory, string name, string parentName, int position, int dacValueClosed, int dacValueOpen, int pidCoef, int stableTimeMs, bool storedPositionReuse)
: base(factory, name, parentName)
{
if (position < 0 || position > 7) throw new ArgumentOutOfRangeException("position");
if (dacValueClosed < 0 || dacValueClosed > 1023) throw new ArgumentOutOfRangeException("dacValueOpen");
if (dacValueOpen < 0 || dacValueOpen > 1023) throw new ArgumentOutOfRangeException("dacValueOpen");
if (pidCoef < 1 || pidCoef > 100) throw new ArgumentOutOfRangeException("pidCoef");
this.Position = position;
this.DacValueClosed = dacValueClosed;
this.DacValueOpen = dacValueOpen;
this.PidCoef = pidCoef;
this.StableTimeMs = stableTimeMs;
this.StoredPositionReuse = storedPositionReuse;
}
public IComponentCfg Clone()
{
return new RegulValveCfg(Factory, Name, ParentName, Position, DacValueClosed, DacValueOpen, PidCoef, StableTimeMs, StoredPositionReuse);
}
public string ToString(int i)
{
return string.Format("position={0}, DAC-closed={1}, DAC-open={2}, PID={3}, posReuse={4}",
Position, DacValueClosed, DacValueOpen, PidCoef, StoredPositionReuse);
}
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(Entities.Component component, IComponentFactory factory)
{
using (var reader = new StringReader(component.Parameters))
{
RegulValveCfg config = (RegulValveCfg)(new XmlSerializer(typeof(RegulValveCfg))).Deserialize(reader);
config.InitCfgBaseFromEntity(component, factory);
return config;
}
}
}
}