///
/// Copyright (c) 2018-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Common;
using Config;
using Config.Entities;
using TBF.Rig.Generic;
using TBF.Resources;
namespace TBF.Rig.DataContainers.BenchInfo.iPerl
{
///
/// Holds information identifying the test bench - serializable configuration.
///
public class ComponentCfg : ComponentCfgBase, Generic.IComponentCfg, IParamsProvider, TBF.Rig.GenericDevices.ICalibInfoCfg
{
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(ComponentCfg) })[0];
public override XmlSerializer GetSerializer() { return Serializer; }
public IComponentCfgCtrl GetControl(IList cmpntEntities) { return new Configs.ParamsProvider.ComponentCfgCtrl(this, null); }
///
/// Serialized parameters
///
public string TestBenchName;
public int TestBenchId;
public string Address1;
public string Address2;
public string Address3;
public string Address4;
public string Address5;
public string ApprovalId;
public string ApprovalDate;
public string ApprovedBy;
public RemoteDBUse RemoteDBUse;
public Unit VolumeUnit;
public Unit FlowUnit;
public Unit MassUnit;
public Unit TempUnit;
public Unit PressUnit;
public Unit LenghtUnit;
public Side Side;
public int MaxTestIndex; /// (MaxPruefindex % 100) value when to reject water meters completely if they are NOK
/// Calibration info serialized parameters displayed in Metrology tab page
public string CalibCertificateNr { get; set; }
public DateTime CalibDate { get; set; }
public DateTime CalibValidDate { get; set; }
/// Private parameterless constructor invoked by all other (public) constructors
ComponentCfg() {}
public ComponentCfg(string name, IComponentFactory factory)
: this()
{
Factory = factory;
Name = name;
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;
RemoteDBUse = RemoteDBUse.LocalDBOnly;
VolumeUnit = Unit.l;
FlowUnit = Unit.m3ph;
MassUnit = Unit.kg;
TempUnit = Unit.C;
PressUnit = Unit.bar;
LenghtUnit = Unit.mm;
Side = Side.Left;
MaxTestIndex = 4;
CalibCertificateNr = string.Empty;
CalibDate = TBF.UI.Constants.MinDate;
CalibValidDate = TBF.UI.Constants.MaxDate;
}
string[] paramNames = new string[]
{
"Test bench name",
"Test bench ID",
"Address 1",
"Address 2",
"Address 3",
"Address 4",
"Address 5",
"Approval",
"Approval date",
"Approved by",
"Remote DB use",
"Preffered volume unit",
"Preffered flow unit",
"Preffered mass unit",
"Preffered temperature unit",
"Preffered pressure unit",
"Preffered lenght unit",
Strings.Side,
"Max.test index",
};
public string ParamName(int i) { return paramNames[i]; }
public int ParamsCount() { return paramNames.Length; }
public ICollection ParamValues(int i)
{
var list = new List();
switch (i)
{
case 10:
for (RemoteDBUse rdbu = 0; rdbu < RemoteDBUse.Count; rdbu++)
list.Add(rdbu.ToDescription());
return list;
case 11:
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Volume))
list.Add(unit.ToDescription());
return list;
case 12:
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Flow))
list.Add(unit.ToDescription());
return list;
case 13:
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Mass))
list.Add(unit.ToDescription());
return list;
case 14:
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Temperature))
list.Add(unit.ToDescription());
return list;
case 15:
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Pressure))
list.Add(unit.ToDescription());
return list;
case 16:
for (Unit unit = 0; unit < Unit.Count; unit++)
if (Units.IsQuantity(unit, Quantity.Length))
list.Add(unit.ToDescription());
return list;
case 17:
for (Side side = 0; side < Side.Count; side++)
list.Add(side.ToDescription());
return list;
default:
return null;
}
}
public string ToString(int i)
{
if (i == -1)
{
return string.Format("{0}: Bench name={1}, Bench Id={2}, Procedures={3}", Name, TestBenchName, TestBenchId, RemoteDBUse.ToDescription());
}
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 RemoteDBUse.ToDescription();
case 11: return VolumeUnit.ToDescription();
case 12: return FlowUnit.ToDescription();
case 13: return MassUnit.ToDescription();
case 14: return TempUnit.ToDescription();
case 15: return PressUnit.ToDescription();
case 16: return LenghtUnit.ToDescription();
case 17: return Side.ToDescription();
case 18: return MaxTestIndex.ToString();
default: return string.Empty;
}
}
public CfgUpdateFlags UpdateParam(int i, string strValue)
{
switch (i)
{
case 0: TestBenchName = strValue; return CfgUpdateFlags.RestartRqrd;
case 1: TestBenchId = int.Parse(strValue); return CfgUpdateFlags.RestartRqrd;
case 2: Address1 = strValue; return CfgUpdateFlags.RestartRqrd;
case 3: Address2 = strValue; return CfgUpdateFlags.RestartRqrd;
case 4: Address3 = strValue; return CfgUpdateFlags.RestartRqrd;
case 5: Address4 = strValue; return CfgUpdateFlags.RestartRqrd;
case 6: Address5 = strValue; return CfgUpdateFlags.RestartRqrd;
case 7: ApprovalId = strValue; return CfgUpdateFlags.RestartRqrd;
case 8: ApprovalDate = strValue; return CfgUpdateFlags.RestartRqrd;
case 9: ApprovedBy = strValue; return CfgUpdateFlags.RestartRqrd;
case 10:
for (RemoteDBUse rdbu = 0; rdbu < RemoteDBUse.Count; rdbu++)
{
if (strValue == rdbu.ToDescription())
{
if (RemoteDBUse != rdbu)
{
RemoteDBUse = rdbu;
return CfgUpdateFlags.RestartRqrd;
}
else
{
return CfgUpdateFlags.None;
}
}
}
return CfgUpdateFlags.Error;
case 11:
VolumeUnit = Units.FromDescription(strValue);
return CfgUpdateFlags.RestartRqrd;
case 12:
FlowUnit = Units.FromDescription(strValue);
return CfgUpdateFlags.RestartRqrd;
case 13:
MassUnit = Units.FromDescription(strValue);
return CfgUpdateFlags.RestartRqrd;
case 14:
TempUnit = Units.FromDescription(strValue);
return CfgUpdateFlags.RestartRqrd;
case 15:
PressUnit = Units.FromDescription(strValue);
return CfgUpdateFlags.RestartRqrd;
case 16:
LenghtUnit = Units.FromDescription(strValue);
return CfgUpdateFlags.RestartRqrd;
case 17:
for (Side side = 0; side < Side.Count; side++)
{
if (strValue == side.ToDescription())
{
if (Side != side)
{
Side = side;
return CfgUpdateFlags.RestartRqrd;
}
else
{
return CfgUpdateFlags.None;
}
}
}
return CfgUpdateFlags.Error;
case 18: MaxTestIndex = int.Parse(strValue); return CfgUpdateFlags.RestartRqrd;
default: return CfgUpdateFlags.None;
}
}
public bool ValidateParam(int i, string strValue, 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:
case 18:
if (int.TryParse(strValue, out idummy) && (idummy >= 0)) return true;
break;
case 10:
for (RemoteDBUse rdbu = 0; rdbu < RemoteDBUse.Count; rdbu++)
{
if (strValue == rdbu.ToDescription()) return true;
}
break;
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
if (ParamValues(i).Contains(strValue)) return true;
break;
case 17:
for (Side side = 0; side < Side.Count; side++)
{
if (strValue == side.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.RemoteDBUse = this.RemoteDBUse;
prms.VolumeUnit = this.VolumeUnit;
prms.FlowUnit = this.FlowUnit;
prms.MassUnit = this.MassUnit;
prms.TempUnit = this.TempUnit;
prms.PressUnit = this.PressUnit;
prms.LenghtUnit = this.LenghtUnit;
prms.Side = this.Side;
prms.MaxTestIndex = this.MaxTestIndex;
}
public IParamsProvider Clone()
{
ComponentCfg pars = new ComponentCfg();
CopyContentTo(pars);
return pars;
}
public bool UpdateEmbeddedDbEntity()
{
return true; /// =OK, do nothing
}
}
}