tbf/TBF/Rig/DataContainer/BenchInfo/ComponentCfg.cs

398 lines
16 KiB
C#

///
/// Copyright (c) 2018-2023 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Common;
using TBF.Resources;
using TBF.Rig.Generic;
namespace TBF.Rig.DataContainer.BenchInfo
{
/// <summary>
/// Holds information identifying the test bench - serializable configuration.
/// </summary>
public class ComponentCfg : ComponentCfgBase, Generic.IComponentCfg, IParamsProvider
{
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(ComponentCfg) })[0];
public override XmlSerializer GetSerializer() { return Serializer; }
public IComponentCfgCtrl GetControl(IList<Config.Entities.Component> cmpntEntities) { return new Configs.ParamsProvider.ComponentCfgCtrl(this, null); }
///
/// Serialized parameters
///
public string TestBenchName; /// 0
public int TestBenchId; /// 1
public string Address1; /// 2
public string Address2; /// 3
public string Address3; /// 4
public string Address4; /// 5
public string Address5; /// 6
public string ApprovalId; /// 7
public string ApprovalDate; /// 8
public string ApprovedBy; /// 9
public int WaterMetersCount; /// 10
public int LinesCount; /// 11
public int CompoundMetersCount; /// 12
public Unit VolumeUnit; /// 13
public Unit FlowUnit; /// 14
public bool IsFromToInPct; /// 15
public Unit MassUnit; /// 16
public Unit TempUnit; /// 17
public Unit PressUnit; /// 18
public Unit LenghtUnit; /// 19
public ProcedureSelection Source1; /// 20
public ProcedureSelection Source2; /// 21
public ProcedureSelection Source3; /// 22
public ProcedureSelection Source4; /// 23
public Unit ElectricUnit; /// 24
/// Private parameterless constructor invoked by all other (public) constructors
ComponentCfg() {}
public ComponentCfg(string name, IComponentFactory factory)
: this()
{
Name = name;
Factory = factory;
ParentName = string.Empty;
InitializeAll();
}
public string ComponentName { get { return Name; } }
public void InitializeAll()
{
TestBenchName = "1";
TestBenchId = 1;
Address1 = string.Empty;
Address2 = string.Empty;
Address3 = string.Empty;
Address4 = string.Empty;
Address5 = string.Empty;
ApprovalId = string.Empty;
ApprovalDate = string.Empty;
ApprovedBy = string.Empty;
WaterMetersCount = 20;
LinesCount = 2;
CompoundMetersCount = 1;
VolumeUnit = Unit.l;
FlowUnit = Unit.m3ph;
IsFromToInPct = false;
MassUnit = Unit.kg;
TempUnit = Unit.C;
PressUnit = Unit.bar;
LenghtUnit = Unit.mm;
Source1 = ProcedureSelection.FromLocalDB;
Source2 = ProcedureSelection.None;
Source3 = ProcedureSelection.None;
Source4 = ProcedureSelection.None;
}
string[] paramNames = new string[]
{
"Test bench name", /// 0
"Test bench ID", /// 1
"Address 1", /// 2
"Address 2", /// 3
"Address 3", /// 4
"Address 4", /// 5
"Address 5", /// 6
"Approval", /// 7
"Approval date", /// 8
"Approved by", /// 9
"Water meters count", /// 10
"Lines count", /// 11
"Compound meters count", /// 12
"Preffered volume unit", /// 13
"Preffered flow unit", /// 14
"Qfrom and Qto preferably in %", /// 15
"Preffered mass unit", /// 16
"Preffered temperature unit", /// 17
"Preffered pressure unit", /// 18
"Preffered lenght unit", /// 19
"Procedure selection source 1", /// 20
"Procedure selection source 2", /// 21
"Procedure selection source 3", /// 22
"Procedure selection source 4", /// 23
};
public string ParamName(int i) { return paramNames[i]; }
public int ParamsCount() { return paramNames.Length; }
public ICollection<string> ParamValues(int i)
{
var list = new List<string>();
switch (i)
{
case 10:
return new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "10", "12", "14", "16", "18", "20" };
case 11:
return new string[] { "1", "2", "3", "4" };
case 12:
return new string[] { "0", "1", "2", "3" };
case 13:
/// Volume unit
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Volume))
list.Add(unit.ToDescription());
return list;
case 14:
/// Flow unit
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Flow))
list.Add(unit.ToDescription());
return list;
case 15:
return new string[] { Strings.yes, Strings.no };
case 16:
/// Mass unit
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Mass))
list.Add(unit.ToDescription());
return list;
case 17:
/// Temperature unit
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Temperature))
list.Add(unit.ToDescription());
return list;
case 18:
/// Pressure unit
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Pressure))
list.Add(unit.ToDescription());
return list;
case 19:
/// Length unit
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Length))
list.Add(unit.ToDescription());
return list;
case 20:
case 21:
case 22:
case 23:
for (ProcedureSelection src = 0; src < ProcedureSelection.Count; src++)
list.Add(src.ToDescription());
return list;
default:
return null;
}
}
public string ToString(int i)
{
switch (i)
{
case 0: return TestBenchName;
case 1: return TestBenchId.ToString();
case 2: return Address1;
case 3: return Address2;
case 4: return Address3;
case 5: return Address4;
case 6: return Address5;
case 7: return ApprovalId;
case 8: return ApprovalDate;
case 9: return ApprovedBy;
case 10: return WaterMetersCount.ToString();
case 11: return LinesCount.ToString();
case 12: return CompoundMetersCount.ToString();
case 13: return VolumeUnit.ToDescription();
case 14: return FlowUnit.ToDescription();
case 15: return IsFromToInPct ? Strings.yes : Strings.no ;
case 16: return MassUnit.ToDescription();
case 17: return TempUnit.ToDescription();
case 18: return PressUnit.ToDescription();
case 19: return LenghtUnit.ToDescription();
case 20: return Source1.ToDescription();
case 21: return Source2.ToDescription();
case 22: return Source3.ToDescription();
case 23: return Source4.ToDescription();
default:
return string.Format("{0}: Bench={1}, ID={2}, Mtrs={3}, Lines={4}, Compund mtrs={5}, S1={6}, S2={7}, S3={8}, S4={9}",
Name, TestBenchName, TestBenchId, WaterMetersCount, LinesCount, CompoundMetersCount, Source1, Source2, Source3, Source4);
}
}
public CfgUpdateFlags UpdateParam(int i, string str)
{
switch (i)
{
case 0: TestBenchName = str; return CfgUpdateFlags.RestartRqrd;
case 1: TestBenchId = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 2: Address1 = str; return CfgUpdateFlags.RestartRqrd;
case 3: Address2 = str; return CfgUpdateFlags.RestartRqrd;
case 4: Address3 = str; return CfgUpdateFlags.RestartRqrd;
case 5: Address4 = str; return CfgUpdateFlags.RestartRqrd;
case 6: Address5 = str; return CfgUpdateFlags.RestartRqrd;
case 7: ApprovalId = str; return CfgUpdateFlags.RestartRqrd;
case 8: ApprovalDate = str; return CfgUpdateFlags.RestartRqrd;
case 9: ApprovedBy = str; return CfgUpdateFlags.RestartRqrd;
case 10: WaterMetersCount = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 11: LinesCount = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 12: CompoundMetersCount = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 13:
VolumeUnit = Units.FromDescription(str);
return CfgUpdateFlags.RestartRqrd;
case 14:
FlowUnit = Units.FromDescription(str);
return CfgUpdateFlags.RestartRqrd;
case 15:
IsFromToInPct = (str == Strings.yes);
return CfgUpdateFlags.RestartRqrd;
case 16:
MassUnit = Units.FromDescription(str);
return CfgUpdateFlags.RestartRqrd;
case 17:
TempUnit = Units.FromDescription(str);
return CfgUpdateFlags.RestartRqrd;
case 18:
PressUnit = Units.FromDescription(str);
return CfgUpdateFlags.RestartRqrd;
case 19:
LenghtUnit = Units.FromDescription(str);
return CfgUpdateFlags.RestartRqrd;
case 20:
for (ProcedureSelection source = 0; source < ProcedureSelection.Count; source++)
{
if (str == source.ToDescription()) { Source1 = source; return CfgUpdateFlags.RestartRqrd; }
}
break;
case 21:
for (ProcedureSelection source = 0; source < ProcedureSelection.Count; source++)
{
if (str == source.ToDescription()) { Source2 = source; return CfgUpdateFlags.RestartRqrd; }
}
break;
case 22:
for (ProcedureSelection source = 0; source < ProcedureSelection.Count; source++)
{
if (str == source.ToDescription()) { Source3 = source; return CfgUpdateFlags.RestartRqrd; }
}
break;
case 23:
for (ProcedureSelection source = 0; source < ProcedureSelection.Count; source++)
{
if (str == source.ToDescription()) { Source4 = source; return CfgUpdateFlags.RestartRqrd; }
}
break;
default: return CfgUpdateFlags.None;
}
return CfgUpdateFlags.Error;
}
public bool ValidateParam(int i, string str, out string message)
{
message = string.Empty;
int idummy;
switch (i)
{
case 0:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return true;
case 1:
if (int.TryParse(str, out idummy) && (idummy >= 0)) return true;
break;
case 10:
if (int.TryParse(str, out idummy) && idummy >= 1 && idummy <= 20) return true;
break;
case 11:
if (int.TryParse(str, out idummy) && idummy >= 1 && idummy <= 4) return true;
break;
case 12:
if (int.TryParse(str, out idummy) && idummy >= 0 && idummy <= 3) return true;
break;
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
if (ParamValues(i).Contains(str)) return true;
break;
case 20:
case 21:
case 22:
case 23:
for (ProcedureSelection source = 0; source < ProcedureSelection.Count; source++)
{
if (str == source.ToDescription()) return true;
}
break;
default:
message = "Invalid index";
return false;
}
message = ParamName(i) + " is invalid";
return false;
}
void CopyContentTo(ComponentCfg prms)
{
prms.TestBenchName = this.TestBenchName;
prms.TestBenchId = this.TestBenchId;
prms.Address1 = this.Address1;
prms.Address2 = this.Address2;
prms.Address3 = this.Address3;
prms.Address4 = this.Address4;
prms.Address5 = this.Address5;
prms.ApprovalId = this.ApprovalId;
prms.ApprovalDate = this.ApprovalDate;
prms.ApprovedBy = this.ApprovedBy;
prms.WaterMetersCount = this.WaterMetersCount;
prms.LinesCount = this.LinesCount;
prms.CompoundMetersCount = this.CompoundMetersCount;
prms.VolumeUnit = this.VolumeUnit;
prms.FlowUnit = this.FlowUnit;
prms.IsFromToInPct = this.IsFromToInPct;
prms.MassUnit = this.MassUnit;
prms.TempUnit = this.TempUnit;
prms.PressUnit = this.PressUnit;
prms.LenghtUnit = this.LenghtUnit;
prms.Source1 = this.Source1;
prms.Source2 = this.Source2;
prms.Source3 = this.Source3;
prms.Source4 = this.Source4;
}
public IParamsProvider Clone()
{
ComponentCfg pars = new ComponentCfg();
CopyContentTo(pars);
return pars;
}
public bool UpdateEmbeddedDbEntity()
{
return true; /// =OK, do nothing
}
}
}