using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.BL.Dispensing; using Tango.BL.Entities; using Tango.BL.Enumerations; namespace Tango.BL { public class LiquidVolume : ExtendedObject { public event Action VolumeChanged; private BrushStop _brushStop; [JsonIgnore] public BrushStop BrushStop { get { return _brushStop; } set { _brushStop = value; RaisePropertyChangedAuto(); } } private Configuration _configuration; [JsonIgnore] public Configuration Configuration { get { return _configuration; } set { _configuration = value; RaisePropertyChangedAuto(); } } private IdsPack _idsPack; [JsonIgnore] public IdsPack IdsPack { get { return _idsPack; } set { _idsPack = value; RaisePropertyChangedAuto(); } } private Rml _rml; [JsonIgnore] public Rml RML { get { return _rml; } set { _rml = value; RaisePropertyChangedAuto(); } } private ProcessParametersTable _processParametersTable; [JsonIgnore] public ProcessParametersTable ProcessParametersTable { get { return _processParametersTable; } set { _processParametersTable = value; RaisePropertyChangedAuto(); } } private double _volume; public double Volume { get { return _volume; } set { _volume = Math.Max(0, Math.Round(value, 4)); RaisePropertyChangedAuto(); OnVolumeChanged(); } } [JsonIgnore] public LiquidTypes LiquidType { get { return (LiquidTypes)IdsPack.LiquidType.Code; } } [JsonIgnore] public DispenserStepDivisions DispenserStepDivision { get { return (DispenserStepDivisions)BrushStop.GetDispensingDivision(IdsPack.PackIndex); } set { BrushStop.SetDispensingDivision(IdsPack.PackIndex, (int)value); RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(PulsePerSecond)); RaisePropertyChanged(nameof(NanoliterPerStep)); RaisePropertyChanged(nameof(PulsePerSecondFull)); } } public void Invalidate() { if (BrushStop.LiquidVolumes != null) { InvalidateSolo(); foreach (var liquidVolume in BrushStop.LiquidVolumes.ToList().Where(x => x != this)) { liquidVolume.InvalidateSolo(); } } } private void InvalidateSolo() { if (BrushStop.LiquidVolumes != null) { _volume = DispensingCalcService.CoerceVolume(this); BrushStop.SetVolume(IdsPack.PackIndex, Volume); RaisePropertyChanged(nameof(LiquidMaxNanoliterPerCentimeter)); RaisePropertyChanged(nameof(NanoliterPerSecond)); RaisePropertyChanged(nameof(PulsePerSecond)); RaisePropertyChanged(nameof(PulsePerSecondFull)); RaisePropertyChanged(nameof(NanoliterPerCentimeter)); RaisePropertyChanged(nameof(Volume)); RaisePropertyChanged(nameof(NanoliterPerStep)); } } public LiquidVolume() //For XML Serialization.. { } public LiquidVolume(Configuration configuration, IdsPack idsPack, Rml rml, ProcessParametersTable processParametersTable, BrushStop brushStop) { ProcessParametersTable = processParametersTable; BrushStop = brushStop; IdsPack = idsPack; Configuration = configuration; RML = rml; Volume = BrushStop.GetVolume(idsPack.PackIndex); } private void OnVolumeChanged() { Invalidate(); VolumeChanged?.Invoke(); } [JsonIgnore] public double LiquidMaxNanoliterPerCentimeter { get { if (Configuration != null && RML != null) { List factors = Configuration.GetSupportedIdsPacks(RML).Select(x => x.LiquidType).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == RML.Guid).ToList(); var factor = factors.FirstOrDefault(x => x.LiquidType != null && x.LiquidType.Type == LiquidType); if (factor != null) { return factor.MaxNlPerCm; } else { return RML.DefaultLiquidFactor; } //int index = BrushStop.LiquidVolumes.IndexOf(this); //if (index > factors.Count - 1 || index < 0) //{ // return RML.DefaultLiquidFactor; //} //else //{ // return factors[index].MaxNlPerCm; //} } else { return 0d; } } } [JsonIgnore] public double NanoliterPerSecond { get { return DispensingCalcService.CalculateNanoliterPerSecond(this); } } [JsonIgnore] public double NanoliterPerCentimeter { get { return DispensingCalcService.CalculateNanoliterPerCentimeter(this); } } [JsonIgnore] public double PulsePerSecond { get { return DispensingCalcService.CalculatePulsePerSecond(this); } } [JsonIgnore] public double PulsePerSecondFull { get { return DispensingCalcService.CalculatePulsePerSecondFull(this); } } [JsonIgnore] public double NanoliterPerStep { get { return (IdsPack.Dispenser.NlPerPulse / ((double)(DispenserStepDivision != DispenserStepDivisions.Auto ? DispenserStepDivision : DispenserStepDivisions.D8) / 8d)); } } public LiquidVolume Clone(BrushStop stop) { LiquidVolume cloned = new LiquidVolume(Configuration, IdsPack, RML, ProcessParametersTable, stop); cloned.DispenserStepDivision = DispenserStepDivision; cloned.Volume = Volume; return cloned; } public override string ToString() { return $"{LiquidType}, {Volume}"; } } }