/// /// Copyright (c) 2018 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; using Config.Entities; using TBF.BenchControl.Generic; namespace TBF.BenchControl.Modbus.UltrasoundLevelMeter { public class LevelMeterCfg : ComponentCfgBase, Generic.IChildComponentCfg, Config.Entities.IParamsProvider { public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(LevelMeterCfg) })[0]; public override XmlSerializer GetSerializer() { return Serializer; } public IComponentCfgCtrl GetControl(IList cmpntEntities) { IList parents = new List(); // TODO: //if (StateMachine.Components != null) //{ // foreach (var cmpnt in StateMachine.Components) // { // if (TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName) is TBF.BenchControl.Modbus.Common.Factory) // { // parents.Add(cmpnt.Name); // } // } //} parents.Add("Modbus"); return new Configs.ParamsProvider.ComponentCfgCtrl(this, parents); } /// /// Serialized parameters /// public byte ModbusAddress; /// 1..247 /// Private parameterless constructor invoked by all other (public) constructors LevelMeterCfg() {} public LevelMeterCfg(IComponentFactory factory) : this() { Factory = factory; Name = "ULM"; ParentName = "Modbus"; InitializeAll(); } public string ComponentName { get { return Name; } } public void InitializeAll() { ModbusAddress = 1; } string[] paramNames = new string[] { "Modbus address", }; public string ParamName(int i) { return paramNames[i]; } public int ParamsCount() { return paramNames.Length; } public ICollection ParamValues(int i) { return null; } public string ToString(int i) { if (i == -1) { return string.Format("ModbusAddr={0}", ModbusAddress); } switch (i) { case 0: return ModbusAddress.ToString(); default: return string.Empty; } } public CfgUpdateFlags UpdateParam(int i, string strValue) { switch (i) { case 0: byte addr = byte.Parse(strValue); if (ModbusAddress != addr) { ModbusAddress = addr; return CfgUpdateFlags.RestartRqrd; } else { return CfgUpdateFlags.None; } default: return CfgUpdateFlags.None; } } public bool ValidateParam(int i, string strValue, out string message) { message = string.Empty; int dummy; switch (i) { case 0: if (int.TryParse(strValue, out dummy) && dummy >= 1 && dummy <= 247) return true; message = ParamName(i) + " is invalid. Address range is 1 .. 247"; return false; default: message = "Invalid index"; return false; } message = ParamName(i) + " is invalid"; return false; } void CopyContentTo(LevelMeterCfg prms) { prms.ParentName = this.ParentName; prms.ModbusAddress = this.ModbusAddress; } public Config.Entities.IParamsProvider Clone() { LevelMeterCfg pars = new LevelMeterCfg(); CopyContentTo(pars); return pars; } public bool UpdateEmbeddedDbEntity() { return true; /// =OK, do nothing } } }