namespace InspectionUI.ViewModels { using InspectionUI.Models; using OmniPlus; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; public class MetersPoolVM : ItemsGridVM { private readonly OmniContext omniContext; private readonly int connectionType; private CancellationTokenSource cancellationTokenSource; private ReferenceMeterVM referenceMeter; public MetersPoolVM(int connectionType) : base() { this.connectionType = connectionType; this.omniContext = OmniContext.Create(Appsettings.OmniDB, Appsettings.CurrentDirectory); } public Boolean HasPruefpunkte { get; private set; } internal IList LoadTypes() { return this.omniContext.LoadTypes(); } internal IList LoadSizes() { return this.omniContext.LoadSizes(); } public void CancelInspection() { this.cancellationTokenSource?.Cancel(); this.referenceMeter?.Stop(); this.Dispose(); } public override void Dispose() { this.Clear(); this.omniContext.Dispose(); } public void LoadMetersAsync() { if (!this.HasPruefpunkte) { return; } this.referenceMeter = new ReferenceMeterVM(OmniContext.Current); this.Add(this.referenceMeter); this.omniContext.LoadMeters(Appsettings.COMPorts, Appsettings.PSNR); foreach (var meter in this.omniContext.Meters) { this.Add(new MeterVM(meter)); } this.NotifyPropertyChanged(); this.cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = this.cancellationTokenSource.Token; var tasks = new List(); var metersVM = this .Select(x => x is MeterVM m ? m : null) .Where(x => x != null) .ToArray(); foreach (var vm in metersVM) { tasks.Add(vm.ConnectAsync(this.connectionType, cancellationToken)); } this.WaitAll(tasks); foreach (var vm in metersVM) { tasks.Add(vm.IdentifySelfAsync(cancellationToken)); } this.WaitAll(tasks); this.NotifyPropertyChanged(); } public void RunInspection(bool keepCorrection = false) { this.cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = this.cancellationTokenSource.Token; var tasks = new List(); var metersVM = this .Select(x => x is MeterVM m ? m : null) .Where(x => x != null) .ToArray(); foreach (var vm in metersVM) { tasks.Add(vm.LoadTestRunAsync(cancellationToken)); } this.WaitAll(tasks); foreach (var vm in metersVM) { tasks.Add(vm.InitializeInspectionAsync(keepCorrection, cancellationToken)); } this.WaitAll(tasks); var pruefpunkte = this.omniContext .Auftrag .Pruefpunkte; foreach (var pp in pruefpunkte) { if (cancellationToken.IsCancellationRequested) { break; } this.referenceMeter.Start(pp); if (cancellationToken.IsCancellationRequested) { break; } foreach (var vm in metersVM) { tasks.Add(vm.StartCalibrationAsync(pp, cancellationToken)); } this.WaitAll(tasks); if (cancellationToken.IsCancellationRequested) { break; } var refm3s = this.referenceMeter.GetFlow(); if (cancellationToken.IsCancellationRequested) { break; } foreach (var vm in metersVM) { tasks.Add(vm.StopCalibrationAsync(pp, refm3s, cancellationToken)); } this.WaitAll(tasks); if (cancellationToken.IsCancellationRequested) { break; } } this.referenceMeter.Stop(); foreach (var vm in metersVM) { tasks.Add(vm.FinishInspectionAsync(cancellationToken)); } this.WaitAll(tasks); } internal void LoadAuftrag(string size, string type) { this.Dispose(); this.NotifyPropertyChanged(); this.omniContext.LoadAuftrag(size, type); this.HasPruefpunkte = this.omniContext.Pruefpunkte.Any(); if (this.HasPruefpunkte) { this.UpdateItemsGridHeaders(); } } internal void LoadAuftrag(string suchNr) { this.Dispose(); this.NotifyPropertyChanged(); this.omniContext.LoadAuftrag(suchNr); this.HasPruefpunkte = this.omniContext.Pruefpunkte.Any(); if (this.HasPruefpunkte) { this.UpdateItemsGridHeaders(); } } private void UpdateItemsGridHeaders() { this.DispatcherInvoke(() => { this.Add(new HeadersRowVM()); this.Add(new DefaultsHeaderRow()); this.Add(new DefaultsRowVM()); }); } } }